summaryrefslogtreecommitdiffstats
path: root/qt-models
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-06-26 17:21:03 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-07-18 06:01:07 -0700
commita5e7f4253ac98ad18354973fda7049e9daaea8eb (patch)
tree16b95a18d0a1af296e4645d6b7a204b5b325ff01 /qt-models
parentefdb3503eadd7e47cb64b1c252e50488d2e9d0fe (diff)
downloadsubsurface-a5e7f4253ac98ad18354973fda7049e9daaea8eb.tar.gz
Core: dynamically resize weight table
Replace the fixed-size weightsystem table by a dynamically relocated table. Reuse the table-macros used in other parts of the code. The table stores weightsystem entries, not pointers to weightsystems. Thus, ownership of the description string is taken when adding a weightsystem. An extra function adds a cloned weightsystem at the end of the table. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models')
-rw-r--r--qt-models/weightmodel.cpp42
1 files changed, 16 insertions, 26 deletions
diff --git a/qt-models/weightmodel.cpp b/qt-models/weightmodel.cpp
index d82b8a7e8..3295e29ae 100644
--- a/qt-models/weightmodel.cpp
+++ b/qt-models/weightmodel.cpp
@@ -18,14 +18,14 @@ WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
weightsystem_t *WeightModel::weightSystemAt(const QModelIndex &index)
{
- return &displayed_dive.weightsystem[index.row()];
+ return &displayed_dive.weightsystems.weightsystems[index.row()];
}
void WeightModel::remove(const QModelIndex &index)
{
if (index.column() != REMOVE)
return;
- beginRemoveRows(QModelIndex(), index.row(), index.row()); // yah, know, ugly.
+ beginRemoveRows(QModelIndex(), index.row(), index.row());
rows--;
remove_weightsystem(&displayed_dive, index.row());
changed = true;
@@ -34,18 +34,17 @@ void WeightModel::remove(const QModelIndex &index)
void WeightModel::clear()
{
- if (rows > 0) {
- beginRemoveRows(QModelIndex(), 0, rows - 1);
- endRemoveRows();
- }
+ beginResetModel();
+ rows = 0;
+ endResetModel();
}
QVariant WeightModel::data(const QModelIndex &index, int role) const
{
- if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS)
+ if (!index.isValid() || index.row() >= displayed_dive.weightsystems.nr)
return QVariant();
- weightsystem_t *ws = &displayed_dive.weightsystem[index.row()];
+ const weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
switch (role) {
case Qt::FontRole:
@@ -79,7 +78,7 @@ QVariant WeightModel::data(const QModelIndex &index, int role) const
// so we only implement the two columns we care about
void WeightModel::passInData(const QModelIndex &index, const QVariant &value)
{
- weightsystem_t *ws = &displayed_dive.weightsystem[index.row()];
+ weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
if (index.column() == WEIGHT) {
if (ws->weight.grams != value.toInt()) {
ws->weight.grams = value.toInt();
@@ -92,7 +91,7 @@ void WeightModel::passInData(const QModelIndex &index, const QVariant &value)
bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
QString vString = value.toString();
- weightsystem_t *ws = &displayed_dive.weightsystem[index.row()];
+ weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
switch (index.column()) {
case TYPE:
if (!value.isNull()) {
@@ -142,11 +141,10 @@ int WeightModel::rowCount(const QModelIndex&) const
void WeightModel::add()
{
- if (rows >= MAX_WEIGHTSYSTEMS)
- return;
-
int row = rows;
+ weightsystem_t ws { {0}, "" };
beginInsertRows(QModelIndex(), row, row);
+ add_cloned_weightsystem(&displayed_dive.weightsystems, ws);
rows++;
changed = true;
endInsertRows();
@@ -154,17 +152,9 @@ void WeightModel::add()
void WeightModel::updateDive()
{
- clear();
- rows = 0;
- for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
- if (!weightsystem_none(&displayed_dive.weightsystem[i])) {
- rows = i + 1;
- }
- }
- if (rows > 0) {
- beginInsertRows(QModelIndex(), 0, rows - 1);
- endInsertRows();
- }
+ beginResetModel();
+ rows = displayed_dive.weightsystems.nr;
+ endResetModel();
}
void WeightModel::weightsystemsReset(const QVector<dive *> &dives)
@@ -174,8 +164,8 @@ void WeightModel::weightsystemsReset(const QVector<dive *> &dives)
if (!current_dive || std::find(dives.begin(), dives.end(), current_dive) == dives.end())
return;
- // Copy the cylinders from the current dive to the displayed dive.
- copy_weights(current_dive, &displayed_dive);
+ // Copy the weights from the current dive to the displayed dive.
+ copy_weights(&current_dive->weightsystems, &displayed_dive.weightsystems);
// And update the model..
updateDive();