summaryrefslogtreecommitdiffstats
path: root/qt-ui/modeldelegates.cpp
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tcanabrava@kde.org>2013-05-23 15:33:20 -0300
committerGravatar Tomaz Canabrava <tcanabrava@kde.org>2013-05-23 15:33:20 -0300
commitbabbfa9204b45d459227bdde0ff63469244b1c39 (patch)
tree888bcba51a30c37b608d9c55823fec0571876c82 /qt-ui/modeldelegates.cpp
parentc917a99eb2973701a79ba8bdb6002bb6ec0ad880 (diff)
downloadsubsurface-babbfa9204b45d459227bdde0ff63469244b1c39.tar.gz
Added support for Completing on the Cylinder Type delegate
I had to immprove the TankInfoModel with two new methods, insertRows and setData, because the delegate used this model to show what kind of Tanks we are offering. Since the user can enter a new type of Tank, it's important to add this tank to all lists using the delegates. I Also added two new methods on the delegate itself, to correctly shows the data, and set the data on the model. This also will help dirk with a working example on how to edit things while using a delegate. Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
Diffstat (limited to 'qt-ui/modeldelegates.cpp')
-rw-r--r--qt-ui/modeldelegates.cpp33
1 files changed, 24 insertions, 9 deletions
diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp
index ea27ed253..283cf17b0 100644
--- a/qt-ui/modeldelegates.cpp
+++ b/qt-ui/modeldelegates.cpp
@@ -10,6 +10,8 @@
#include <QStyle>
#include <QStyleOption>
#include <QComboBox>
+#include <QCompleter>
+#include <QLineEdit>
StarWidgetsDelegate::StarWidgetsDelegate(QWidget* parent):
QStyledItemDelegate(parent),
@@ -52,19 +54,32 @@ QSize StarWidgetsDelegate::sizeHint(const QStyleOptionViewItem& option, const QM
QWidget* TankInfoDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QComboBox *comboDelegate = new QComboBox(parent);
- TankInfoModel *model = new TankInfoModel;
- QString data = index.model()->data(index, Qt::DisplayRole).toString();
+ TankInfoModel *model = TankInfoModel::instance();
comboDelegate->setModel(model);
- int i;
- for (i = 0; i < model->rowCount(); i++) {
- if (model->data(model->index(i,0), Qt::DisplayRole).toString() == data)
- break;
- }
- if (i != model->rowCount())
- comboDelegate->setCurrentIndex(i);
+ 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
+{
+ 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 TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
+{
+ QComboBox *c = static_cast<QComboBox*>(editor);
+ model->setData(index, c->currentText(), Qt::EditRole);
+}
+
TankInfoDelegate::TankInfoDelegate(QObject* parent): QStyledItemDelegate(parent)
{
}