diff options
author | Tomaz Canabrava <tcanabrava@kde.org> | 2013-06-16 10:15:19 -0300 |
---|---|---|
committer | Tomaz Canabrava <tcanabrava@kde.org> | 2013-06-16 10:15:19 -0300 |
commit | 10ea572f5af471e959e34441e9a23fac58f4b928 (patch) | |
tree | 2226206a0963fa6a74c3f53b98856d5dd4d8943d | |
parent | 84d4a2dae069a2e728cea8d7a4506921b558603f (diff) | |
download | subsurface-10ea572f5af471e959e34441e9a23fac58f4b928.tar.gz |
Create a 'ComboBoxDelegate' to concentrate the comboboxness of delegates.
This patch creates a ComboBoxDelegate where the other specific delegates
should inherit from. this adds a little code cleanup for the current
version, and will help as soon as more delegates got added to the code.
Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
-rw-r--r-- | qt-ui/modeldelegates.cpp | 51 | ||||
-rw-r--r-- | qt-ui/modeldelegates.h | 16 |
2 files changed, 26 insertions, 41 deletions
diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index c90a1e8e1..d201337b4 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -17,7 +17,6 @@ StarWidgetsDelegate::StarWidgetsDelegate(QWidget* parent): QStyledItemDelegate(parent), parentWidget(parent) { - } void StarWidgetsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const @@ -51,19 +50,11 @@ QSize StarWidgetsDelegate::sizeHint(const QStyleOptionViewItem& option, const QM return QSize(IMG_SIZE * TOTALSTARS + SPACING * (TOTALSTARS-1), IMG_SIZE); } -QWidget* TankInfoDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const +ComboBoxDelegate::ComboBoxDelegate(QAbstractItemModel *model, QObject* parent): QStyledItemDelegate(parent), model(model) { - QComboBox *comboDelegate = new QComboBox(parent); - TankInfoModel *model = TankInfoModel::instance(); - comboDelegate->setModel(model); - comboDelegate->setEditable(true); - comboDelegate->setAutoCompletion(true); - comboDelegate->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive); - comboDelegate->completer()->setCompletionMode(QCompleter::PopupCompletion); - return comboDelegate; } -void TankInfoDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const +void ComboBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { QComboBox *c = qobject_cast<QComboBox*>(editor); QString data = index.model()->data(index, Qt::DisplayRole).toString(); @@ -74,6 +65,17 @@ void TankInfoDelegate::setEditorData(QWidget* editor, const QModelIndex& index) c->setEditText(data); } +QWidget* ComboBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + QComboBox *comboDelegate = new QComboBox(parent); + comboDelegate->setModel(model); + comboDelegate->setEditable(true); + comboDelegate->setAutoCompletion(true); + comboDelegate->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive); + comboDelegate->completer()->setCompletionMode(QCompleter::PopupCompletion); + return comboDelegate; +} + void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const { QComboBox *c = qobject_cast<QComboBox*>(editor); @@ -97,31 +99,8 @@ void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, mymodel->passInData(model->index(thisindex.row(), CylindersModel::SIZE), tankSize); } -TankInfoDelegate::TankInfoDelegate(QObject* parent): QStyledItemDelegate(parent) -{ -} - -QWidget* WSInfoDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const +TankInfoDelegate::TankInfoDelegate(QObject* parent): ComboBoxDelegate(TankInfoModel::instance(), parent) { - QComboBox *comboDelegate = new QComboBox(parent); - WSInfoModel *model = WSInfoModel::instance(); - comboDelegate->setModel(model); - comboDelegate->setEditable(true); - comboDelegate->setAutoCompletion(true); - comboDelegate->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive); - comboDelegate->completer()->setCompletionMode(QCompleter::PopupCompletion); - return comboDelegate; -} - -void WSInfoDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const -{ - QComboBox *c = qobject_cast<QComboBox*>(editor); - QString data = index.model()->data(index, Qt::DisplayRole).toString(); - int i = c->findText(data); - if (i != -1) - c->setCurrentIndex(i); - else - c->setEditText(data); } void WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const @@ -145,6 +124,6 @@ void WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, co mymodel->passInData(model->index(thisindex.row(), WeightModel::WEIGHT), grams); } -WSInfoDelegate::WSInfoDelegate(QObject* parent): QStyledItemDelegate(parent) +WSInfoDelegate::WSInfoDelegate(QObject* parent): ComboBoxDelegate(WSInfoModel::instance(), parent) { } diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h index 79fbe297b..00436d874 100644 --- a/qt-ui/modeldelegates.h +++ b/qt-ui/modeldelegates.h @@ -13,21 +13,27 @@ private: QWidget *parentWidget; }; -class TankInfoDelegate : public QStyledItemDelegate{ +class ComboBoxDelegate : public QStyledItemDelegate{ Q_OBJECT public: - explicit TankInfoDelegate(QObject* parent = 0); + explicit ComboBoxDelegate(QAbstractItemModel *model, QObject* parent = 0); virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const; virtual void setEditorData(QWidget* editor, const QModelIndex& index) const; +protected: + QAbstractItemModel *model; +}; + +class TankInfoDelegate : public ComboBoxDelegate{ + Q_OBJECT +public: + explicit TankInfoDelegate(QObject* parent = 0); virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; }; -class WSInfoDelegate : public QStyledItemDelegate{ +class WSInfoDelegate : public ComboBoxDelegate{ Q_OBJECT public: explicit WSInfoDelegate(QObject* parent = 0); - virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const; - virtual void setEditorData(QWidget* editor, const QModelIndex& index) const; virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; }; |