From 10ea572f5af471e959e34441e9a23fac58f4b928 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Sun, 16 Jun 2013 10:15:19 -0300 Subject: [PATCH] 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 --- qt-ui/modeldelegates.cpp | 51 ++++++++++++---------------------------- qt-ui/modeldelegates.h | 18 +++++++++----- 2 files changed, 27 insertions(+), 42 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(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(editor); @@ -97,33 +99,10 @@ void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, mymodel->passInData(model->index(thisindex.row(), CylindersModel::SIZE), tankSize); } -TankInfoDelegate::TankInfoDelegate(QObject* parent): QStyledItemDelegate(parent) +TankInfoDelegate::TankInfoDelegate(QObject* parent): ComboBoxDelegate(TankInfoModel::instance(), parent) { } -QWidget* WSInfoDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const -{ - 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(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 { QComboBox *c = qobject_cast(editor); @@ -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 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 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; }; -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; };