diff options
-rw-r--r-- | qt-ui/modeldelegates.cpp | 33 | ||||
-rw-r--r-- | qt-ui/modeldelegates.h | 2 | ||||
-rw-r--r-- | qt-ui/models.cpp | 24 | ||||
-rw-r--r-- | qt-ui/models.h | 7 |
4 files changed, 50 insertions, 16 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) { } diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h index baf7d50c3..eb78d12b6 100644 --- a/qt-ui/modeldelegates.h +++ b/qt-ui/modeldelegates.h @@ -18,6 +18,8 @@ class TankInfoDelegate : public QStyledItemDelegate{ 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; }; #endif diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 7ccbe1e49..029a788bf 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -398,10 +398,25 @@ void WeightModel::setDive(dive* d) endInsertRows(); } -void TankInfoModel::add(const QString& description) +TankInfoModel* TankInfoModel::instance() { - // When the user `creates` a new one on the combobox. - // for now, empty till dirk cleans the GTK code. + static TankInfoModel *self = new TankInfoModel(); + return self; +} + +bool TankInfoModel::insertRows(int row, int count, const QModelIndex& parent) +{ + beginInsertRows(parent, rowCount(), rowCount()); + rows += count; + endInsertRows(); + return true; +} + +bool TankInfoModel::setData(const QModelIndex& index, const QVariant& value, int role) +{ + struct tank_info *info = &tank_info[index.row()]; + QByteArray name = value.toByteArray(); + info->name = strdup(name.data()); } void TankInfoModel::clear() @@ -430,7 +445,7 @@ QVariant TankInfoModel::data(const QModelIndex& index, int role) const p.mbar = psi_to_mbar(info->psi); ml = wet_volume(info->cuft, p); } - if (role == Qt::DisplayRole) { + if (role == Qt::DisplayRole || role == Qt::EditRole) { switch(index.column()) { case BAR: ret = bar; @@ -478,7 +493,6 @@ TankInfoModel::TankInfoModel() : QAbstractTableModel(), rows(-1) { struct tank_info *info = tank_info; for (info = tank_info; info->name; info++, rows++); - if (rows > -1) { beginInsertRows(QModelIndex(), 0, rows); endInsertRows(); diff --git a/qt-ui/models.h b/qt-ui/models.h index 5fa97eb56..038f400a7 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -9,6 +9,7 @@ #include <QAbstractTableModel> #include <QCoreApplication> +#include <QStringList> #include "../dive.h" #include "../divelist.h" @@ -18,6 +19,8 @@ class TankInfoModel : public QAbstractTableModel { Q_OBJECT public: + static TankInfoModel* instance(); + enum Column { DESCRIPTION, ML, BAR}; TankInfoModel(); @@ -25,8 +28,8 @@ public: /*reimp*/ int columnCount(const QModelIndex& parent = QModelIndex()) const; /*reimp*/ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; /*reimp*/ int rowCount(const QModelIndex& parent = QModelIndex()) const; - - void add(const QString& description); + /*reimp*/ bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()); + /*reimp*/ bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole); void clear(); void update(); private: |