mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Add weightsystem delegate to enable editing of weightsystem
This is very much analogous to the way cylinders are implemented. That means that just like with cylinders, if the user enters a new type and hits 'tab' before hitting 'enter', Subsurface will crash. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
ecbcd4db47
commit
01a3bd2cc6
5 changed files with 218 additions and 9 deletions
|
@ -83,7 +83,8 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
|
||||||
ui->cylinders->horizontalHeader()->setResizeMode (CylindersModel::REMOVE , QHeaderView::Fixed);
|
ui->cylinders->horizontalHeader()->setResizeMode (CylindersModel::REMOVE , QHeaderView::Fixed);
|
||||||
ui->cylinders->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate());
|
ui->cylinders->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate());
|
||||||
ui->weights->setColumnWidth(WeightModel::REMOVE, 24);
|
ui->weights->setColumnWidth(WeightModel::REMOVE, 24);
|
||||||
ui->cylinders->horizontalHeader()->setResizeMode (WeightModel::REMOVE , QHeaderView::Fixed);
|
ui->weights->horizontalHeader()->setResizeMode (WeightModel::REMOVE , QHeaderView::Fixed);
|
||||||
|
ui->weights->setItemDelegateForColumn(WeightModel::TYPE, new WSInfoDelegate());
|
||||||
}
|
}
|
||||||
|
|
||||||
// We need to manually position the 'plus' on cylinder and weight.
|
// We need to manually position the 'plus' on cylinder and weight.
|
||||||
|
|
|
@ -92,3 +92,43 @@ void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
|
||||||
TankInfoDelegate::TankInfoDelegate(QObject* parent): QStyledItemDelegate(parent)
|
TankInfoDelegate::TankInfoDelegate(QObject* parent): QStyledItemDelegate(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<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 WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
|
||||||
|
{
|
||||||
|
QComboBox *c = static_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);
|
||||||
|
}
|
||||||
|
|
||||||
|
WSInfoDelegate::WSInfoDelegate(QObject* parent): QStyledItemDelegate(parent)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
|
@ -22,4 +22,13 @@ public:
|
||||||
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const;
|
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class WSInfoDelegate : public QStyledItemDelegate{
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
148
qt-ui/models.cpp
148
qt-ui/models.cpp
|
@ -287,6 +287,10 @@ void CylindersModel::remove(const QModelIndex& index)
|
||||||
endRemoveRows();
|
endRemoveRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WeightModel::WeightModel(QObject* parent): QAbstractTableModel(parent), current(0), rows(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void WeightModel::remove(const QModelIndex& index)
|
void WeightModel::remove(const QModelIndex& index)
|
||||||
{
|
{
|
||||||
if (index.column() != REMOVE) {
|
if (index.column() != REMOVE) {
|
||||||
|
@ -318,7 +322,7 @@ QVariant WeightModel::data(const QModelIndex& index, int role) const
|
||||||
if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS)
|
if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
weightsystem_t *ws = ¤t_dive->weightsystem[index.row()];
|
weightsystem_t *ws = ¤t->weightsystem[index.row()];
|
||||||
|
|
||||||
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
||||||
switch(index.column()) {
|
switch(index.column()) {
|
||||||
|
@ -336,19 +340,43 @@ QVariant WeightModel::data(const QModelIndex& index, int role) const
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is our magic 'pass data in' function that allows the delegate to get
|
||||||
|
// the data here without silly unit conversions;
|
||||||
|
// so we only implement the two columns we care about
|
||||||
|
void WeightModel::passInData(const QModelIndex& index, const QVariant& value)
|
||||||
|
{
|
||||||
|
weightsystem_t *ws = ¤t->weightsystem[index.row()];
|
||||||
|
if (index.column() == WEIGHT) {
|
||||||
|
if (ws->weight.grams != value.toInt()) {
|
||||||
|
ws->weight.grams = value.toInt();
|
||||||
|
mark_divelist_changed(TRUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role)
|
bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role)
|
||||||
{
|
{
|
||||||
weightsystem_t *ws = ¤t_dive->weightsystem[index.row()];
|
weightsystem_t *ws = ¤t->weightsystem[index.row()];
|
||||||
switch(index.column()) {
|
switch(index.column()) {
|
||||||
case TYPE:{
|
case TYPE:
|
||||||
QByteArray desc = value.toByteArray();
|
if (!value.isNull()) {
|
||||||
ws->description = strdup(desc.data());
|
char *text= value.toByteArray().data();
|
||||||
|
if (!ws->description || strcmp(ws->description, text)) {
|
||||||
|
ws->description = strdup(text);
|
||||||
|
mark_divelist_changed(TRUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case WEIGHT:
|
case WEIGHT:
|
||||||
ws->weight.grams = value.toInt() *1000;
|
if (CHANGED(toDouble, "kg", "lbs")) {
|
||||||
|
if (prefs.units.weight == prefs.units.LBS)
|
||||||
|
ws->weight.grams = lbs_to_grams(value.toDouble());
|
||||||
|
else
|
||||||
|
ws->weight.grams = value.toDouble() * 1000.0;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
return QAbstractItemModel::setData(index, value, role);
|
||||||
}
|
}
|
||||||
|
|
||||||
Qt::ItemFlags WeightModel::flags(const QModelIndex& index) const
|
Qt::ItemFlags WeightModel::flags(const QModelIndex& index) const
|
||||||
|
@ -418,6 +446,111 @@ void WeightModel::setDive(dive* d)
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WSInfoModel* WSInfoModel::instance()
|
||||||
|
{
|
||||||
|
static WSInfoModel *self = new WSInfoModel();
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WSInfoModel::insertRows(int row, int count, const QModelIndex& parent)
|
||||||
|
{
|
||||||
|
beginInsertRows(parent, rowCount(), rowCount());
|
||||||
|
rows += count;
|
||||||
|
endInsertRows();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WSInfoModel::setData(const QModelIndex& index, const QVariant& value, int role)
|
||||||
|
{
|
||||||
|
struct ws_info *info = &ws_info[index.row()];
|
||||||
|
QByteArray name = value.toByteArray();
|
||||||
|
info->name = strdup(name.data());
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WSInfoModel::clear()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int WSInfoModel::columnCount(const QModelIndex& parent) const
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant WSInfoModel::data(const QModelIndex& index, int role) const
|
||||||
|
{
|
||||||
|
QVariant ret;
|
||||||
|
if (!index.isValid()) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
struct ws_info *info = &ws_info[index.row()];
|
||||||
|
|
||||||
|
int gr = info->grams;
|
||||||
|
|
||||||
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
||||||
|
switch(index.column()) {
|
||||||
|
case GR:
|
||||||
|
ret = gr;
|
||||||
|
break;
|
||||||
|
case DESCRIPTION:
|
||||||
|
ret = QString(info->name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant WSInfoModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||||
|
{
|
||||||
|
QVariant ret;
|
||||||
|
|
||||||
|
if (orientation != Qt::Horizontal)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
if (role == Qt::DisplayRole) {
|
||||||
|
switch(section) {
|
||||||
|
case GR:
|
||||||
|
ret = tr("kg");
|
||||||
|
break;
|
||||||
|
case DESCRIPTION:
|
||||||
|
ret = tr("Description");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int WSInfoModel::rowCount(const QModelIndex& parent) const
|
||||||
|
{
|
||||||
|
return rows+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
WSInfoModel::WSInfoModel() : QAbstractTableModel(), rows(-1)
|
||||||
|
{
|
||||||
|
struct ws_info *info = ws_info;
|
||||||
|
for (info = ws_info; info->name; info++, rows++);
|
||||||
|
|
||||||
|
if (rows > -1) {
|
||||||
|
beginInsertRows(QModelIndex(), 0, rows);
|
||||||
|
endInsertRows();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WSInfoModel::update()
|
||||||
|
{
|
||||||
|
if (rows > -1) {
|
||||||
|
beginRemoveRows(QModelIndex(), 0, rows);
|
||||||
|
endRemoveRows();
|
||||||
|
}
|
||||||
|
struct ws_info *info = ws_info;
|
||||||
|
for (info = ws_info; info->name; info++, rows++);
|
||||||
|
|
||||||
|
if (rows > -1) {
|
||||||
|
beginInsertRows(QModelIndex(), 0, rows);
|
||||||
|
endInsertRows();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TankInfoModel* TankInfoModel::instance()
|
TankInfoModel* TankInfoModel::instance()
|
||||||
{
|
{
|
||||||
static TankInfoModel *self = new TankInfoModel();
|
static TankInfoModel *self = new TankInfoModel();
|
||||||
|
@ -437,6 +570,7 @@ bool TankInfoModel::setData(const QModelIndex& index, const QVariant& value, int
|
||||||
struct tank_info *info = &tank_info[index.row()];
|
struct tank_info *info = &tank_info[index.row()];
|
||||||
QByteArray name = value.toByteArray();
|
QByteArray name = value.toByteArray();
|
||||||
info->name = strdup(name.data());
|
info->name = strdup(name.data());
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TankInfoModel::clear()
|
void TankInfoModel::clear()
|
||||||
|
|
|
@ -21,7 +21,7 @@ Q_OBJECT
|
||||||
public:
|
public:
|
||||||
static TankInfoModel* instance();
|
static TankInfoModel* instance();
|
||||||
|
|
||||||
enum Column { DESCRIPTION, ML, BAR};
|
enum Column {DESCRIPTION, ML, BAR};
|
||||||
TankInfoModel();
|
TankInfoModel();
|
||||||
|
|
||||||
/*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
/*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
||||||
|
@ -36,6 +36,28 @@ private:
|
||||||
int rows;
|
int rows;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Encapsulate ws_info */
|
||||||
|
class WSInfoModel : public QAbstractTableModel {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
static WSInfoModel* instance();
|
||||||
|
|
||||||
|
enum Column {DESCRIPTION, GR};
|
||||||
|
WSInfoModel();
|
||||||
|
|
||||||
|
/*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
||||||
|
/*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;
|
||||||
|
/*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:
|
||||||
|
int rows;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
/* Encapsulation of the Cylinder Model, that presents the
|
/* Encapsulation of the Cylinder Model, that presents the
|
||||||
* Current cylinders that are used on a dive. */
|
* Current cylinders that are used on a dive. */
|
||||||
class CylindersModel : public QAbstractTableModel {
|
class CylindersModel : public QAbstractTableModel {
|
||||||
|
@ -70,6 +92,8 @@ class WeightModel : public QAbstractTableModel {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
enum Column {REMOVE, TYPE, WEIGHT, COLUMNS};
|
enum Column {REMOVE, TYPE, WEIGHT, COLUMNS};
|
||||||
|
|
||||||
|
explicit WeightModel(QObject *parent = 0);
|
||||||
/*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
/*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
||||||
/*reimp*/ int columnCount(const QModelIndex& parent = QModelIndex()) const;
|
/*reimp*/ int columnCount(const QModelIndex& parent = QModelIndex()) const;
|
||||||
/*reimp*/ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
|
/*reimp*/ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
|
||||||
|
@ -77,6 +101,7 @@ public:
|
||||||
/*reimp*/ Qt::ItemFlags flags(const QModelIndex& index) const;
|
/*reimp*/ Qt::ItemFlags flags(const QModelIndex& index) const;
|
||||||
/*reimp*/ bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
|
/*reimp*/ bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
|
||||||
|
|
||||||
|
void passInData(const QModelIndex& index, const QVariant& value);
|
||||||
void add();
|
void add();
|
||||||
void clear();
|
void clear();
|
||||||
void update();
|
void update();
|
||||||
|
|
Loading…
Add table
Reference in a new issue