mirror of
https://github.com/saitohirga/WSJT-X.git
synced 2024-11-22 04:11:16 -05:00
New combo box with a list of checkable items
This commit is contained in:
parent
014812e6c6
commit
8516a4f538
@ -241,6 +241,7 @@ set (wsjt_qt_CXXSRCS
|
|||||||
logbook/Multiplier.cpp
|
logbook/Multiplier.cpp
|
||||||
Network/NetworkAccessManager.cpp
|
Network/NetworkAccessManager.cpp
|
||||||
widgets/LazyFillComboBox.cpp
|
widgets/LazyFillComboBox.cpp
|
||||||
|
widgets/CheckableItemComboBox.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set (wsjt_qtmm_CXXSRCS
|
set (wsjt_qtmm_CXXSRCS
|
||||||
|
93
widgets/CheckableItemComboBox.cpp
Normal file
93
widgets/CheckableItemComboBox.cpp
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
#include "CheckableItemComboBox.hpp"
|
||||||
|
|
||||||
|
#include <QStyledItemDelegate>
|
||||||
|
#include <QStandardItemModel>
|
||||||
|
#include <QStandardItem>
|
||||||
|
#include <QLineEdit>
|
||||||
|
#include <QEvent>
|
||||||
|
#include <QListView>
|
||||||
|
|
||||||
|
class CheckableItemComboBoxStyledItemDelegate
|
||||||
|
: public QStyledItemDelegate
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit CheckableItemComboBoxStyledItemDelegate (QObject * parent = nullptr)
|
||||||
|
: QStyledItemDelegate {parent}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void paint (QPainter * painter, QStyleOptionViewItem const& option, QModelIndex const& index) const override
|
||||||
|
{
|
||||||
|
QStyleOptionViewItem& mutable_option = const_cast<QStyleOptionViewItem&> (option);
|
||||||
|
mutable_option.showDecorationSelected = false;
|
||||||
|
QStyledItemDelegate::paint (painter, mutable_option, index);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
CheckableItemComboBox::CheckableItemComboBox (QWidget * parent)
|
||||||
|
: QComboBox {parent}
|
||||||
|
, model_ {new QStandardItemModel()}
|
||||||
|
{
|
||||||
|
setModel (model_.data ());
|
||||||
|
|
||||||
|
setEditable (true);
|
||||||
|
lineEdit ()->setReadOnly (true);
|
||||||
|
lineEdit ()->installEventFilter (this);
|
||||||
|
setItemDelegate (new CheckableItemComboBoxStyledItemDelegate {this});
|
||||||
|
|
||||||
|
connect (lineEdit(), &QLineEdit::selectionChanged, lineEdit(), &QLineEdit::deselect);
|
||||||
|
connect (static_cast<QListView *> (view ()), &QListView::pressed, this, &CheckableItemComboBox::item_pressed);
|
||||||
|
connect (model_.data (), &QStandardItemModel::dataChanged, this, &CheckableItemComboBox::model_data_changed);
|
||||||
|
}
|
||||||
|
|
||||||
|
QStandardItem * CheckableItemComboBox::addCheckItem (QString const& label, QVariant const& data
|
||||||
|
, Qt::CheckState checkState)
|
||||||
|
{
|
||||||
|
auto * item = new QStandardItem {label};
|
||||||
|
item->setCheckState (checkState);
|
||||||
|
item->setData (data);
|
||||||
|
item->setFlags (Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
|
||||||
|
model_->appendRow (item);
|
||||||
|
update_text ();
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CheckableItemComboBox::eventFilter (QObject * object, QEvent * event)
|
||||||
|
{
|
||||||
|
if (object == lineEdit() && event->type () == QEvent::MouseButtonPress)
|
||||||
|
{
|
||||||
|
showPopup();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CheckableItemComboBox::update_text()
|
||||||
|
{
|
||||||
|
QString text;
|
||||||
|
for (int i = 0; i < model_->rowCount (); ++i)
|
||||||
|
{
|
||||||
|
if (model_->item (i)->checkState () == Qt::Checked)
|
||||||
|
{
|
||||||
|
if (text.size ())
|
||||||
|
{
|
||||||
|
text+= ", ";
|
||||||
|
}
|
||||||
|
text += model_->item (i)->data ().toString ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lineEdit ()->setText (text);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CheckableItemComboBox::model_data_changed ()
|
||||||
|
{
|
||||||
|
update_text ();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CheckableItemComboBox::item_pressed (QModelIndex const& index)
|
||||||
|
{
|
||||||
|
QStandardItem * item = model_->itemFromIndex (index);
|
||||||
|
item->setCheckState (item->checkState () == Qt::Checked ? Qt::Unchecked : Qt::Checked);
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "widgets/moc_CheckableItemComboBox.cpp"
|
36
widgets/CheckableItemComboBox.hpp
Normal file
36
widgets/CheckableItemComboBox.hpp
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#ifndef CHECKABLE_ITEM_COMBO_BOX_HPP__
|
||||||
|
#define CHECKABLE_ITEM_COMBO_BOX_HPP__
|
||||||
|
|
||||||
|
#include <QComboBox>
|
||||||
|
#include <QScopedPointer>
|
||||||
|
|
||||||
|
class QStandardItemModel;
|
||||||
|
class QStandardItem;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief QComboBox with support of checkboxes
|
||||||
|
* http://stackoverflow.com/questions/8422760/combobox-of-checkboxes
|
||||||
|
*/
|
||||||
|
class CheckableItemComboBox
|
||||||
|
: public QComboBox
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit CheckableItemComboBox (QWidget * parent = nullptr);
|
||||||
|
QStandardItem * addCheckItem (QString const& label, QVariant const& data, Qt::CheckState checkState);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool eventFilter (QObject *, QEvent *) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void update_text();
|
||||||
|
|
||||||
|
Q_SLOT void model_data_changed ();
|
||||||
|
Q_SLOT void item_pressed (QModelIndex const&);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QScopedPointer<QStandardItemModel> model_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -10,7 +10,9 @@ SOURCES += \
|
|||||||
widgets/AbstractLogWindow.cpp \
|
widgets/AbstractLogWindow.cpp \
|
||||||
widgets/FrequencyLineEdit.cpp widgets/FrequencyDeltaLineEdit.cpp \
|
widgets/FrequencyLineEdit.cpp widgets/FrequencyDeltaLineEdit.cpp \
|
||||||
widgets/FoxLogWindow.cpp widgets/CabrilloLogWindow.cpp \
|
widgets/FoxLogWindow.cpp widgets/CabrilloLogWindow.cpp \
|
||||||
widgets/HelpTextWindow.cpp widgets/RestrictedSpinBox.cpp
|
widgets/HelpTextWindow.cpp widgets/RestrictedSpinBox.cpp \
|
||||||
|
widgets/LazyFillComboBox.cpp widgets/CheckableItemComboBox.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
widgets/mainwindow.h widgets/plotter.h \
|
widgets/mainwindow.h widgets/plotter.h \
|
||||||
widgets/about.h widgets/widegraph.h \
|
widgets/about.h widgets/widegraph.h \
|
||||||
@ -22,7 +24,8 @@ HEADERS += \
|
|||||||
widgets/ExportCabrillo.h widgets/AbstractLogWindow.hpp \
|
widgets/ExportCabrillo.h widgets/AbstractLogWindow.hpp \
|
||||||
widgets/FoxLogWindow.hpp widgets/CabrilloLogWindow.hpp \
|
widgets/FoxLogWindow.hpp widgets/CabrilloLogWindow.hpp \
|
||||||
widgets/DateTimeEdit.hpp widgets/HelpTextWindow.hpp \
|
widgets/DateTimeEdit.hpp widgets/HelpTextWindow.hpp \
|
||||||
widgets/RestrictedSpinBox.hpp
|
widgets/RestrictedSpinBox.hpp \
|
||||||
|
widgets/LazyFillComboBox.hpp widgets/CheckableItemComboBox.hpp
|
||||||
|
|
||||||
FORMS += \
|
FORMS += \
|
||||||
widgets/mainwindow.ui widgets/about.ui \
|
widgets/mainwindow.ui widgets/about.ui \
|
||||||
|
Loading…
Reference in New Issue
Block a user