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>
This commit is contained in:
Berthold Stoeger 2019-06-26 17:21:03 +02:00 committed by Dirk Hohndel
parent efdb3503ea
commit a5e7f4253a
25 changed files with 182 additions and 154 deletions

View file

@ -654,7 +654,7 @@ PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dI
tags(nullptr)
{
memset(&cylinders[0], 0, sizeof(cylinders));
memset(&weightsystems[0], 0, sizeof(weightsystems));
clear_weightsystem_table(&weightsystems);
if (what.notes)
notes = data->notes;
if (what.divemaster)
@ -675,12 +675,8 @@ PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dI
for (int i = 0; i < MAX_CYLINDERS; ++i)
copy_cylinder(data->cylinder[i], cylinders[i]);
}
if (what.weights) {
for (int i = 0; i < MAX_WEIGHTSYSTEMS; ++i) {
weightsystems[i] = data->weightsystem[i];
weightsystems[i].description = copy_string(data->weightsystem[i].description);
}
}
if (what.weights)
copy_weights(&data->weightsystems, &weightsystems);
}
PasteState::~PasteState()
@ -688,8 +684,8 @@ PasteState::~PasteState()
taglist_free(tags);
for (cylinder_t &c: cylinders)
free((void *)c.type.description);
for (weightsystem_t &w: weightsystems)
free((void *)w.description);
clear_weightsystem_table(&weightsystems);
free(weightsystems.weightsystems);
}
void PasteState::swap(dive_components what)
@ -713,7 +709,7 @@ void PasteState::swap(dive_components what)
if (what.cylinders)
std::swap(cylinders, d->cylinder);
if (what.weights)
std::swap(weightsystems, d->weightsystem);
std::swap(weightsystems, d->weightsystems);
}
// ***** Paste *****

View file

@ -236,7 +236,7 @@ struct PasteState {
int visibility;
tag_entry *tags;
cylinder_t cylinders[MAX_CYLINDERS];
weightsystem_t weightsystems[MAX_WEIGHTSYSTEMS];
struct weightsystem_table weightsystems;
PasteState(dive *d, const dive *data, dive_components what); // Read data from dive data for dive d
~PasteState();

View file

@ -447,16 +447,12 @@ void DiveLogExportDialog::export_TeX(const char *filename, const bool selected_o
put_format(&buf, "\n%% Weighting information:\n");
qty_weight = 0;
total_weight = 0;
for (i = 0; i < MAX_WEIGHTSYSTEMS; i++){
if (dive->weightsystem[i].weight.grams){
put_format(&buf, "\\def\\%sweight%ctype{%s}\n", ssrf, 'a' + i, dive->weightsystem[i].description);
put_format(&buf, "\\def\\%sweight%camt{%.3f\\%sweightunit}\n", ssrf, 'a' + i, get_weight_units(dive->weightsystem[i].weight.grams, NULL, &unit), ssrf);
qty_weight += 1;
total_weight += get_weight_units(dive->weightsystem[i].weight.grams, NULL, &unit);
} else {
put_format(&buf, "\\def\\%sweight%ctype{}\n", ssrf, 'a' + i);
put_format(&buf, "\\def\\%sweight%camt{}\n", ssrf, 'a' + i);
}
for (i = 0; i < dive->weightsystems.nr; i++) {
weightsystem_t w = dive->weightsystems.weightsystems[i];
put_format(&buf, "\\def\\%sweight%ctype{%s}\n", ssrf, 'a' + i, w.description);
put_format(&buf, "\\def\\%sweight%camt{%.3f\\%sweightunit}\n", ssrf, 'a' + i, get_weight_units(w.weight.grams, NULL, &unit), ssrf);
qty_weight += 1;
total_weight += get_weight_units(w.weight.grams, NULL, &unit);
}
put_format(&buf, "\\def\\%sqtyweights{%d}\n", ssrf, qty_weight);
put_format(&buf, "\\def\\%stotalweight{%.2f\\%sweightunit}\n", ssrf, total_weight, ssrf);

View file

@ -518,9 +518,9 @@ void DiveComponentSelection::buttonClicked(QAbstractButton *button)
if (what->weights) {
int w;
text << tr("Weights:\n");
for (w = 0; w < MAX_WEIGHTSYSTEMS; w++) {
if (displayed_dive.weightsystem[w].weight.grams)
text << displayed_dive.weightsystem[w].description << displayed_dive.weightsystem[w].weight.grams / 1000 << "kg\n";
for (w = 0; w < displayed_dive.weightsystems.nr; w++) {
weightsystem_t ws = displayed_dive.weightsystems.weightsystems[w];
text << ws.description << ws.weight.grams / 1000 << "kg\n";
}
}
clipboard->setText(cliptext);

View file

@ -172,6 +172,10 @@ static QVector<dive *> getSelectedDivesCurrentLast()
return res;
}
// TODO: This is only a temporary function until undo of weightsystems is implemented.
// Therefore it is not worth putting it in a header.
extern bool weightsystems_equal(const dive *d1, const dive *d2);
void TabDiveEquipment::acceptChanges()
{
bool do_replot = false;
@ -232,17 +236,10 @@ void TabDiveEquipment::acceptChanges()
if (weightModel->changed) {
mark_divelist_changed(true);
MODIFY_DIVES(selectedDives,
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
if (mydive != cd && (same_string(mydive->weightsystem[i].description, cd->weightsystem[i].description))) {
mydive->weightsystem[i] = displayed_dive.weightsystem[i];
mydive->weightsystem[i].description = copy_string(displayed_dive.weightsystem[i].description);
}
}
if (weightsystems_equal(mydive, cd))
copy_weights(&displayed_dive.weightsystems, &mydive->weightsystems);
);
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
cd->weightsystem[i] = displayed_dive.weightsystem[i];
cd->weightsystem[i].description = copy_string(displayed_dive.weightsystem[i].description);
}
copy_weights(&displayed_dive.weightsystems, &cd->weightsystems);
}
if (do_replot)

View file

@ -669,6 +669,17 @@ void MainTab::acceptChanges()
editMode = NONE;
}
bool weightsystems_equal(const dive *d1, const dive *d2)
{
if (d1->weightsystems.nr != d2->weightsystems.nr)
return false;
for (int i = 0; i < d1->weightsystems.nr; ++i) {
if (!same_weightsystem(d1->weightsystems.weightsystems[0], d2->weightsystems.weightsystems[i]))
return false;
}
return true;
}
void MainTab::rejectChanges()
{
EditMode lastMode = editMode;
@ -676,7 +687,7 @@ void MainTab::rejectChanges()
if (lastMode != NONE && current_dive &&
(modified ||
memcmp(&current_dive->cylinder[0], &displayed_dive.cylinder[0], sizeof(cylinder_t) * MAX_CYLINDERS) ||
memcmp(&current_dive->weightsystem[0], &displayed_dive.weightsystem[0], sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS))) {
!weightsystems_equal(current_dive, &displayed_dive))) {
if (QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT(tr("Discard the changes?"),
tr("You are about to discard your changes.")),
QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Discard) != QMessageBox::Discard) {