aboutsummaryrefslogtreecommitdiffstats
path: root/qt-ui/modeldelegates.cpp
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2013-05-23 22:56:12 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2013-05-23 23:26:17 -0700
commit90a24255b25106c2f4bbd91970c45c370297aefe (patch)
tree6803899142562f341817a4f23fd9f1a48bb61a4c /qt-ui/modeldelegates.cpp
parent01a3bd2cc6dcc035607cce61285ed9cc8d807b9b (diff)
downloadsubsurface-90a24255b25106c2f4bbd91970c45c370297aefe.tar.gz
Correctly add new weight / cylinder types, even when hitting 'tab'
We now detect if a weight / cylinder with this description exists and if not add it on the fly. We also remember the additional values (weight and size / workingpressure) for new entries and take the values for these fields into account when autocompleting. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui/modeldelegates.cpp')
-rw-r--r--qt-ui/modeldelegates.cpp54
1 files changed, 35 insertions, 19 deletions
diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp
index b80e30fcd..c90a1e8e1 100644
--- a/qt-ui/modeldelegates.cpp
+++ b/qt-ui/modeldelegates.cpp
@@ -74,19 +74,27 @@ void TankInfoDelegate::setEditorData(QWidget* editor, const QModelIndex& index)
c->setEditText(data);
}
-void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
+void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const
{
- QComboBox *c = static_cast<QComboBox*>(editor);
+ QComboBox *c = qobject_cast<QComboBox*>(editor);
CylindersModel *mymodel = qobject_cast<CylindersModel *>(model);
TankInfoModel *tanks = TankInfoModel::instance();
- QModelIndex tankIndex = tanks->match(tanks->index(0,0), Qt::DisplayRole, c->currentText()).first();
-
- int tankSize = tanks->data(tanks->index(tankIndex.row(), TankInfoModel::ML)).toInt();
- int tankPressure = tanks->data(tanks->index(tankIndex.row(), TankInfoModel::BAR)).toInt();
-
- mymodel->setData(index, c->currentText(), Qt::EditRole);
- mymodel->passInData(model->index(index.row(), CylindersModel::WORKINGPRESS), tankPressure);
- mymodel->passInData(model->index(index.row(), CylindersModel::SIZE), tankSize);
+ QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, c->currentText());
+ int row;
+ if (matches.isEmpty()) {
+ // we need to add this
+ tanks->insertRows(tanks->rowCount(), 1);
+ tanks->setData(tanks->index(tanks->rowCount() -1, 0), c->currentText());
+ row = tanks->rowCount() - 1;
+ } else {
+ row = matches.first().row();
+ }
+ int tankSize = tanks->data(tanks->index(row, TankInfoModel::ML)).toInt();
+ int tankPressure = tanks->data(tanks->index(row, TankInfoModel::BAR)).toInt();
+
+ mymodel->setData(model->index(thisindex.row(), CylindersModel::TYPE), c->currentText(), Qt::EditRole);
+ mymodel->passInData(model->index(thisindex.row(), CylindersModel::WORKINGPRESS), tankPressure);
+ mymodel->passInData(model->index(thisindex.row(), CylindersModel::SIZE), tankSize);
}
TankInfoDelegate::TankInfoDelegate(QObject* parent): QStyledItemDelegate(parent)
@@ -116,17 +124,25 @@ void WSInfoDelegate::setEditorData(QWidget* editor, const QModelIndex& index) co
c->setEditText(data);
}
-void WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
+void WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const
{
- QComboBox *c = static_cast<QComboBox*>(editor);
+ QComboBox *c = qobject_cast<QComboBox*>(editor);
WeightModel *mymodel = qobject_cast<WeightModel *>(model);
- WSInfoModel *ws = WSInfoModel::instance();
- QModelIndex wsIndex = ws->match(ws->index(0,0), Qt::DisplayRole, c->currentText()).first();
-
- int grams = ws->data(ws->index(wsIndex.row(), WSInfoModel::GR)).toInt();
-
- mymodel->setData(index, c->currentText(), Qt::EditRole);
- mymodel->passInData(model->index(index.row(), WeightModel::WEIGHT), grams);
+ WSInfoModel *wsim = WSInfoModel::instance();
+ QModelIndexList matches = wsim->match(wsim->index(0,0), Qt::DisplayRole, c->currentText());
+ int row;
+ if (matches.isEmpty()) {
+ // we need to add this puppy
+ wsim->insertRows(wsim->rowCount(), 1);
+ wsim->setData(wsim->index(wsim->rowCount() - 1, 0), c->currentText());
+ row = wsim->rowCount() - 1;
+ } else {
+ row = matches.first().row();
+ }
+ int grams = wsim->data(wsim->index(row, WSInfoModel::GR)).toInt();
+ QVariant v = QString(c->currentText());
+ mymodel->setData(model->index(thisindex.row(), WeightModel::TYPE), v, Qt::EditRole);
+ mymodel->passInData(model->index(thisindex.row(), WeightModel::WEIGHT), grams);
}
WSInfoDelegate::WSInfoDelegate(QObject* parent): QStyledItemDelegate(parent)