From ebaa7d1fd08fec3371e6448532564ebef6df4f29 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Mon, 11 May 2015 10:38:50 -0700 Subject: Multi dive edit: treat buddy and divemaster as tag lists So just like tags do "differential editing". Figure out what (or in this case, who) was added / removed from the displayed dive and do the same for all other selected dives. This does seem a bit over top for divemaster, but this is more consistent and therefore should make more sense to the user - assuming we ever have one who runs into this. Signed-off-by: Dirk Hohndel --- qt-ui/maintab.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 4 deletions(-) (limited to 'qt-ui/maintab.cpp') diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index 2dd33544c..ebfa0537e 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -26,6 +26,7 @@ #include #include #include +#include MainTab::MainTab(QWidget *parent) : QTabWidget(parent), weightModel(new WeightModel(this)), @@ -755,15 +756,11 @@ void MainTab::acceptChanges() struct dive *cd = current_dive; // now check if something has changed and if yes, edit the selected dives that // were identical with the master dive shown (and mark the divelist as changed) - if (!same_string(displayed_dive.buddy, cd->buddy)) - MODIFY_SELECTED_DIVES(EDIT_TEXT(buddy)); if (!same_string(displayed_dive.suit, cd->suit)) MODIFY_SELECTED_DIVES(EDIT_TEXT(suit)); if (!same_string(displayed_dive.notes, cd->notes)) MODIFY_SELECTED_DIVES(EDIT_TEXT(notes)); if (!same_string(displayed_dive.divemaster, cd->divemaster)) - MODIFY_SELECTED_DIVES(EDIT_TEXT(divemaster)); - if (displayed_dive.rating != cd->rating) MODIFY_SELECTED_DIVES(EDIT_VALUE(rating)); if (displayed_dive.visibility != cd->visibility) MODIFY_SELECTED_DIVES(EDIT_VALUE(visibility)); @@ -783,6 +780,9 @@ void MainTab::acceptChanges() if (displayed_dive.dive_site_uuid != cd->dive_site_uuid) MODIFY_SELECTED_DIVES(EDIT_VALUE(dive_site_uuid)); + // three text fields are somewhat special and are represented as tags + // in the UI - they need somewhat smarter handling + saveTaggedStrings(); saveTags(); if (editMode != ADD && cylindersModel->changed) { @@ -1135,6 +1135,73 @@ void MainTab::saveTags() ); } +// buddy and divemaster are represented in the UI just like the tags, but the internal +// representation is just a string (with commas as delimiters). So we need to do the same +// thing we did for tags, just differently +void MainTab::saveTaggedStrings() +{ + QStringList addedList, removedList; + struct dive *cd = current_dive; + + diffTaggedStrings(cd->buddy, displayed_dive.buddy, addedList, removedList); + MODIFY_SELECTED_DIVES( + QStringList oldList = QString(mydive->buddy).split(QRegExp("\\s*,\\s*"), QString::SkipEmptyParts); + QString newString; + QString comma; + Q_FOREACH (const QString tag, oldList) { + if (!removedList.contains(tag, Qt::CaseInsensitive)) { + newString += comma + tag; + comma = ", "; + } + } + Q_FOREACH (const QString tag, addedList) { + if (!oldList.contains(tag, Qt::CaseInsensitive)) { + newString += comma + tag; + comma = ", "; + } + } + free(mydive->buddy); + mydive->buddy = copy_string(qPrintable(newString)); + ); + addedList.clear(); + removedList.clear(); + diffTaggedStrings(cd->divemaster, displayed_dive.divemaster, addedList, removedList); + MODIFY_SELECTED_DIVES( + QStringList oldList = QString(mydive->divemaster).split(QRegExp("\\s*,\\s*"), QString::SkipEmptyParts); + QString newString; + QString comma; + Q_FOREACH (const QString tag, oldList) { + if (!removedList.contains(tag, Qt::CaseInsensitive)) { + newString += comma + tag; + comma = ", "; + } + } + Q_FOREACH (const QString tag, addedList) { + if (!oldList.contains(tag, Qt::CaseInsensitive)) { + newString += comma + tag; + comma = ", "; + } + } + free(mydive->divemaster); + mydive->divemaster = copy_string(qPrintable(newString)); + ); +} + +void MainTab::diffTaggedStrings(QString currentString, QString displayedString, QStringList &addedList, QStringList &removedList) +{ + QStringList displayedList, currentList; + currentList = currentString.split(',', QString::SkipEmptyParts); + displayedList = displayedString.split(',', QString::SkipEmptyParts); + Q_FOREACH ( const QString tag, currentList) { + if (!displayedList.contains(tag, Qt::CaseInsensitive)) + removedList << tag.trimmed(); + } + Q_FOREACH (const QString tag, displayedList) { + if (!currentList.contains(tag, Qt::CaseInsensitive)) + addedList << tag.trimmed(); + } +} + void MainTab::on_tagWidget_textChanged() { char buf[1024]; -- cgit v1.2.3-70-g09d2