mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-30 22:20:21 +00:00
Desktop: refactor WSInfoDelegate logic
The WSInfoDelegate (weight-system-info delegate) is used to display a combo box of known weightsystem-types and auto-fills the weight if the weightsystem-type is changed. This would overwrite the weight data of the displayed dive when the user hovers over the different entries. Moreover, it saves the original weight in case the user cancels the editing action. This is not viable when implementing undo of weightsystem changes, because hovering over entries should not produce individual undo commands. Instead, implement a special "temporary" row in the weightsystem model. On canceling of the edit actions, simply reload the weightsystem from the unmodified dive. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
ab99ca85f1
commit
029c9ccf02
4 changed files with 63 additions and 52 deletions
|
@ -85,10 +85,8 @@ const QSize& StarWidgetsDelegate::starSize() const
|
||||||
ComboBoxDelegate::ComboBoxDelegate(QAbstractItemModel *model, QObject *parent, bool allowEdit) : QStyledItemDelegate(parent), model(model)
|
ComboBoxDelegate::ComboBoxDelegate(QAbstractItemModel *model, QObject *parent, bool allowEdit) : QStyledItemDelegate(parent), model(model)
|
||||||
{
|
{
|
||||||
editable = allowEdit;
|
editable = allowEdit;
|
||||||
connect(this, SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)),
|
connect(this, &ComboBoxDelegate::closeEditor, this, &ComboBoxDelegate::editorClosed);
|
||||||
this, SLOT(revertModelData(QWidget *, QAbstractItemDelegate::EndEditHint)));
|
connect(this, &ComboBoxDelegate::closeEditor, this, &ComboBoxDelegate::fixTabBehavior);
|
||||||
connect(this, SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)),
|
|
||||||
this, SLOT(fixTabBehavior()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
||||||
|
@ -276,7 +274,7 @@ void TankInfoDelegate::reenableReplot(QWidget*, QAbstractItemDelegate::EndEditHi
|
||||||
// MainWindow::instance()->graphics->replot();
|
// MainWindow::instance()->graphics->replot();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TankInfoDelegate::revertModelData(QWidget*, QAbstractItemDelegate::EndEditHint hint)
|
void TankInfoDelegate::editorClosed(QWidget*, QAbstractItemDelegate::EndEditHint hint)
|
||||||
{
|
{
|
||||||
if (hint == QAbstractItemDelegate::NoHint ||
|
if (hint == QAbstractItemDelegate::NoHint ||
|
||||||
hint == QAbstractItemDelegate::RevertModelCache) {
|
hint == QAbstractItemDelegate::RevertModelCache) {
|
||||||
|
@ -326,19 +324,13 @@ void TankUseDelegate::setModelData(QWidget * editor, QAbstractItemModel * model,
|
||||||
model->setData(index, comboBox->currentIndex());
|
model->setData(index, comboBox->currentIndex());
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct RevertWeightData {
|
void WSInfoDelegate::editorClosed(QWidget*, QAbstractItemDelegate::EndEditHint hint)
|
||||||
QString type;
|
|
||||||
int weight;
|
|
||||||
} currWeight;
|
|
||||||
|
|
||||||
void WSInfoDelegate::revertModelData(QWidget*, QAbstractItemDelegate::EndEditHint hint)
|
|
||||||
{
|
{
|
||||||
if (hint == QAbstractItemDelegate::NoHint ||
|
WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
|
||||||
hint == QAbstractItemDelegate::RevertModelCache) {
|
if (hint == QAbstractItemDelegate::RevertModelCache)
|
||||||
WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
|
mymodel->clearTempWS();
|
||||||
mymodel->setData(IDX(WeightModel::TYPE), currWeight.type, Qt::EditRole);
|
else
|
||||||
mymodel->passInData(IDX(WeightModel::WEIGHT), currWeight.weight);
|
mymodel->commitTempWS();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WSInfoDelegate::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const
|
void WSInfoDelegate::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const
|
||||||
|
@ -356,28 +348,15 @@ void WSInfoDelegate::setModelData(QWidget*, QAbstractItemModel*, const QModelInd
|
||||||
row = matches.first().row();
|
row = matches.first().row();
|
||||||
}
|
}
|
||||||
int grams = wsim->data(wsim->index(row, WSInfoModel::GR)).toInt();
|
int grams = wsim->data(wsim->index(row, WSInfoModel::GR)).toInt();
|
||||||
QVariant v = QString(currCombo.activeText);
|
|
||||||
|
|
||||||
mymodel->setData(IDX(WeightModel::TYPE), v, Qt::EditRole);
|
mymodel->setTempWS(currCombo.currRow, weightsystem_t{ { grams }, copy_qstring(currCombo.activeText) });
|
||||||
mymodel->passInData(IDX(WeightModel::WEIGHT), grams);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WSInfoDelegate::WSInfoDelegate(QObject *parent) : ComboBoxDelegate(WSInfoModel::instance(), parent, true)
|
WSInfoDelegate::WSInfoDelegate(QObject *parent) : ComboBoxDelegate(WSInfoModel::instance(), parent, true)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
QWidget *WSInfoDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
void AirTypesDelegate::editorClosed(QWidget*, QAbstractItemDelegate::EndEditHint)
|
||||||
{
|
|
||||||
/* First, call the combobox-create editor, it will setup our globals. */
|
|
||||||
QWidget *editor = ComboBoxDelegate::createEditor(parent, option, index);
|
|
||||||
WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
|
|
||||||
weightsystem_t ws = mymodel->weightSystemAt(index);
|
|
||||||
currWeight.type = ws.description;
|
|
||||||
currWeight.weight = ws.weight.grams;
|
|
||||||
return editor;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AirTypesDelegate::revertModelData(QWidget*, QAbstractItemDelegate::EndEditHint)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -393,7 +372,7 @@ AirTypesDelegate::AirTypesDelegate(QObject *parent) : ComboBoxDelegate(GasSelect
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiveTypesDelegate::revertModelData(QWidget*, QAbstractItemDelegate::EndEditHint)
|
void DiveTypesDelegate::editorClosed(QWidget*, QAbstractItemDelegate::EndEditHint)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ slots:
|
||||||
//HACK: try to get rid of this in the future.
|
//HACK: try to get rid of this in the future.
|
||||||
void fakeActivation();
|
void fakeActivation();
|
||||||
void fixTabBehavior();
|
void fixTabBehavior();
|
||||||
virtual void revertModelData(QWidget *widget, QAbstractItemDelegate::EndEditHint hint) = 0;
|
virtual void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint) = 0;
|
||||||
private:
|
private:
|
||||||
bool editable;
|
bool editable;
|
||||||
protected:
|
protected:
|
||||||
|
@ -60,7 +60,7 @@ public:
|
||||||
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||||
public
|
public
|
||||||
slots:
|
slots:
|
||||||
void revertModelData(QWidget *widget, QAbstractItemDelegate::EndEditHint hint);
|
void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint);
|
||||||
void reenableReplot(QWidget *widget, QAbstractItemDelegate::EndEditHint hint);
|
void reenableReplot(QWidget *widget, QAbstractItemDelegate::EndEditHint hint);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -78,10 +78,9 @@ class WSInfoDelegate : public ComboBoxDelegate {
|
||||||
public:
|
public:
|
||||||
explicit WSInfoDelegate(QObject *parent = 0);
|
explicit WSInfoDelegate(QObject *parent = 0);
|
||||||
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
|
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
|
||||||
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
|
||||||
public
|
public
|
||||||
slots:
|
slots:
|
||||||
void revertModelData(QWidget *widget, QAbstractItemDelegate::EndEditHint hint);
|
void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint);
|
||||||
};
|
};
|
||||||
|
|
||||||
class AirTypesDelegate : public ComboBoxDelegate {
|
class AirTypesDelegate : public ComboBoxDelegate {
|
||||||
|
@ -91,7 +90,7 @@ public:
|
||||||
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
|
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
|
||||||
public
|
public
|
||||||
slots:
|
slots:
|
||||||
void revertModelData(QWidget *widget, QAbstractItemDelegate::EndEditHint hint);
|
void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint);
|
||||||
};
|
};
|
||||||
|
|
||||||
class DiveTypesDelegate : public ComboBoxDelegate {
|
class DiveTypesDelegate : public ComboBoxDelegate {
|
||||||
|
@ -101,7 +100,7 @@ public:
|
||||||
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
|
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
|
||||||
public
|
public
|
||||||
slots:
|
slots:
|
||||||
void revertModelData(QWidget *widget, QAbstractItemDelegate::EndEditHint hint);
|
void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint);
|
||||||
};
|
};
|
||||||
|
|
||||||
class SpinBoxDelegate : public QStyledItemDelegate {
|
class SpinBoxDelegate : public QStyledItemDelegate {
|
||||||
|
|
|
@ -9,7 +9,9 @@
|
||||||
|
|
||||||
WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
|
WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
|
||||||
changed(false),
|
changed(false),
|
||||||
d(nullptr)
|
d(nullptr),
|
||||||
|
tempRow(-1),
|
||||||
|
tempWS(empty_weightsystem)
|
||||||
{
|
{
|
||||||
//enum Column {REMOVE, TYPE, WEIGHT};
|
//enum Column {REMOVE, TYPE, WEIGHT};
|
||||||
setHeaderDataStrings(QStringList() << tr("") << tr("Type") << tr("Weight"));
|
setHeaderDataStrings(QStringList() << tr("") << tr("Type") << tr("Weight"));
|
||||||
|
@ -38,7 +40,7 @@ QVariant WeightModel::data(const QModelIndex &index, int role) const
|
||||||
if (!index.isValid() || index.row() >= d->weightsystems.nr)
|
if (!index.isValid() || index.row() >= d->weightsystems.nr)
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
|
||||||
const weightsystem_t ws = weightSystemAt(index);
|
weightsystem_t ws = index.row() == tempRow ? tempWS : weightSystemAt(index);
|
||||||
|
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::FontRole:
|
case Qt::FontRole:
|
||||||
|
@ -70,18 +72,44 @@ QVariant WeightModel::data(const QModelIndex &index, int role) const
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is our magic 'pass data in' function that allows the delegate to get
|
// Ownership of passed in weight system will be taken. Caller must not use it any longer.
|
||||||
// the data here without silly unit conversions;
|
void WeightModel::setTempWS(int row, weightsystem_t ws)
|
||||||
// so we only implement the two columns we care about
|
|
||||||
void WeightModel::passInData(const QModelIndex &index, const QVariant &value)
|
|
||||||
{
|
{
|
||||||
weightsystem_t *ws = &d->weightsystems.weightsystems[index.row()];
|
if (!d || row < 0 || row >= d->weightsystems.nr) // Sanity check: row must exist
|
||||||
if (index.column() == WEIGHT) {
|
return;
|
||||||
if (ws->weight.grams != value.toInt()) {
|
|
||||||
ws->weight.grams = value.toInt();
|
clearTempWS(); // Shouldn't be necessary, just in case: Reset old temporary row.
|
||||||
dataChanged(index, index);
|
free_weightsystem(tempWS);
|
||||||
}
|
|
||||||
|
// It is really hard to get the editor-close-hints and setModelData calls under
|
||||||
|
// control. Therefore, if the row is set to the already existing entry, don't
|
||||||
|
// enter temporary mode.
|
||||||
|
if (same_string(d->weightsystems.weightsystems[row].description, ws.description)) {
|
||||||
|
free_weightsystem(ws);
|
||||||
|
tempWS.description = nullptr;
|
||||||
|
} else {
|
||||||
|
tempRow = row;
|
||||||
|
tempWS = ws;
|
||||||
}
|
}
|
||||||
|
dataChanged(index(row, TYPE), index(row, WEIGHT));
|
||||||
|
}
|
||||||
|
|
||||||
|
void WeightModel::clearTempWS()
|
||||||
|
{
|
||||||
|
if (tempRow < 0)
|
||||||
|
return;
|
||||||
|
int oldRow = tempRow;
|
||||||
|
tempRow = -1;
|
||||||
|
dataChanged(index(oldRow, TYPE), index(oldRow, WEIGHT));
|
||||||
|
}
|
||||||
|
|
||||||
|
void WeightModel::commitTempWS()
|
||||||
|
{
|
||||||
|
if (tempRow < 0)
|
||||||
|
return;
|
||||||
|
tempRow = -1;
|
||||||
|
setData(index(tempRow, TYPE), QVariant::fromValue(QString(tempWS.description)), Qt::EditRole);
|
||||||
|
setData(index(tempRow, WEIGHT), QVariant::fromValue(get_weight_string(tempWS.weight, true)), Qt::EditRole);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||||
|
|
|
@ -21,8 +21,10 @@ public:
|
||||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
||||||
|
void setTempWS(int row, weightsystem_t ws);
|
||||||
|
void clearTempWS();
|
||||||
|
void commitTempWS();
|
||||||
|
|
||||||
void passInData(const QModelIndex &index, const QVariant &value);
|
|
||||||
void clear();
|
void clear();
|
||||||
void updateDive(dive *d);
|
void updateDive(dive *d);
|
||||||
weightsystem_t weightSystemAt(const QModelIndex &index) const;
|
weightsystem_t weightSystemAt(const QModelIndex &index) const;
|
||||||
|
@ -36,6 +38,9 @@ slots:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
dive *d;
|
dive *d;
|
||||||
|
// If we temporarily change a line because the user is selecting a weight type
|
||||||
|
int tempRow;
|
||||||
|
weightsystem_t tempWS;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue