|
| 1 | +/* |
| 2 | + * Copyright 2025 Matthias Kühlewein |
| 3 | + * |
| 4 | + * This file is part of OpenOrienteering. |
| 5 | + * |
| 6 | + * OpenOrienteering is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * OpenOrienteering is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "tag_remove_widget.h" |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <functional> |
| 24 | +#include <set> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include <Qt> |
| 28 | +#include <QtGlobal> |
| 29 | +#include <QChar> |
| 30 | +#include <QCheckBox> |
| 31 | +#include <QCoreApplication> |
| 32 | +#include <QComboBox> |
| 33 | +#include <QDialogButtonBox> |
| 34 | +#include <QHBoxLayout> |
| 35 | +#include <QLabel> |
| 36 | +#include <QLatin1String> |
| 37 | +#include <QLineEdit> |
| 38 | +#include <QMessageBox> |
| 39 | +#include <QPushButton> |
| 40 | +#include <QVBoxLayout> |
| 41 | +#include <QWidget> |
| 42 | + |
| 43 | +#include "core/map.h" |
| 44 | +#include "core/objects/object.h" |
| 45 | +#include "gui/util_gui.h" |
| 46 | +#include "undo/object_undo.h" |
| 47 | + |
| 48 | + |
| 49 | +namespace { |
| 50 | + |
| 51 | +struct CompOpStruct { |
| 52 | + const QString op; |
| 53 | + const std::function<bool (const QString&, const QString&)> fn; |
| 54 | +}; |
| 55 | + |
| 56 | +static const CompOpStruct compare_operations[4] = { |
| 57 | + { QCoreApplication::translate("OpenOrienteering::TagRemoveDialog", "equal to"), [](const QString& key, const QString& pattern) { return key == pattern; } }, |
| 58 | + { QCoreApplication::translate("OpenOrienteering::TagRemoveDialog", "not equal to"), [](const QString& key, const QString& pattern) { return key != pattern; } }, |
| 59 | + { QCoreApplication::translate("OpenOrienteering::TagRemoveDialog", "containing"), [](const QString& key, const QString& pattern) { return key.contains(pattern); } }, |
| 60 | + { QCoreApplication::translate("OpenOrienteering::TagRemoveDialog", "not containing"), [](const QString& key, const QString& pattern) { return !key.contains(pattern); } } |
| 61 | +}; |
| 62 | + |
| 63 | +} // namespace |
| 64 | + |
| 65 | + |
| 66 | +namespace OpenOrienteering { |
| 67 | + |
| 68 | +TagRemoveDialog::TagRemoveDialog(QWidget* parent, Map* map) |
| 69 | +: QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint) |
| 70 | +, map { map } |
| 71 | +{ |
| 72 | + setWindowTitle(tr("Remove Tags")); |
| 73 | + |
| 74 | + auto* h_layout = new QHBoxLayout(); |
| 75 | + h_layout->addWidget(new QLabel(tr("Remove all tags"))); |
| 76 | + |
| 77 | + compare_op = new QComboBox(); |
| 78 | + for (auto& compare_operation : compare_operations) |
| 79 | + { |
| 80 | + compare_op->addItem(compare_operation.op); |
| 81 | + } |
| 82 | + h_layout->addWidget(compare_op); |
| 83 | + |
| 84 | + pattern_edit = new QLineEdit(); |
| 85 | + |
| 86 | + undo_check = new QCheckBox(tr("Add undo step")); |
| 87 | + |
| 88 | + auto* button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 89 | + ok_button = button_box->button(QDialogButtonBox::Ok); |
| 90 | + ok_button->setEnabled(false); |
| 91 | + |
| 92 | + auto* layout = new QVBoxLayout(); |
| 93 | + layout->addLayout(h_layout); |
| 94 | + layout->addWidget(pattern_edit); |
| 95 | + layout->addWidget(new QLabel(tr("from the selected objects."))); |
| 96 | + layout->addWidget(undo_check); |
| 97 | + layout->addItem(Util::SpacerItem::create(this)); |
| 98 | + layout->addStretch(); |
| 99 | + layout->addWidget(button_box); |
| 100 | + setLayout(layout); |
| 101 | + |
| 102 | + connect(button_box, &QDialogButtonBox::rejected, this, &QDialog::reject); |
| 103 | + connect(button_box, &QDialogButtonBox::accepted, this, &TagRemoveDialog::okClicked); |
| 104 | + connect(pattern_edit, &QLineEdit::textChanged, this, &TagRemoveDialog::textChanged); |
| 105 | +} |
| 106 | + |
| 107 | +TagRemoveDialog::~TagRemoveDialog() = default; |
| 108 | + |
| 109 | + |
| 110 | +// slot |
| 111 | +void TagRemoveDialog::textChanged(const QString &text) |
| 112 | +{ |
| 113 | + ok_button->setEnabled(!text.trimmed().isEmpty()); |
| 114 | +} |
| 115 | + |
| 116 | +// slot |
| 117 | +void TagRemoveDialog::okClicked() |
| 118 | +{ |
| 119 | + const auto pattern = pattern_edit->text(); |
| 120 | + auto op = compare_op->currentIndex(); |
| 121 | + |
| 122 | + int objects_count = 0; |
| 123 | + std::set<QString> matching_keys; |
| 124 | + |
| 125 | + for (const auto& object : map->selectedObjects()) |
| 126 | + { |
| 127 | + bool object_matched = false; |
| 128 | + for (const auto& tag : object->tags()) |
| 129 | + { |
| 130 | + if ((compare_operations[op].fn)(tag.key, pattern)) |
| 131 | + { |
| 132 | + matching_keys.insert(tag.key); |
| 133 | + object_matched = true; |
| 134 | + } |
| 135 | + } |
| 136 | + if (object_matched) |
| 137 | + ++objects_count; |
| 138 | + } |
| 139 | + if (matching_keys.empty()) |
| 140 | + { |
| 141 | + QMessageBox::information(this, tr("Information"), tr("No matching object tags found."), QMessageBox::Ok); |
| 142 | + return; |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + QString detailed_text = std::accumulate( begin(matching_keys), |
| 147 | + end(matching_keys), |
| 148 | + QString(tr("The following object tags will be removed:")), |
| 149 | + [](const QString& a, const QString& b) -> QString { return a + QChar::LineFeed + b; } |
| 150 | + ); |
| 151 | + QMessageBox msgBox; |
| 152 | + msgBox.setWindowTitle(tr("Confirmation")); |
| 153 | + msgBox.setIcon(QMessageBox::Warning); |
| 154 | + QString question = tr("Do you want to remove %n tag(s)", nullptr, matching_keys.size()); |
| 155 | + question += QChar::Space + tr("from %n object(s)?", nullptr, objects_count); |
| 156 | + msgBox.setText(question); |
| 157 | + msgBox.setDetailedText(detailed_text); |
| 158 | + msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); |
| 159 | + |
| 160 | + if (msgBox.exec() == QMessageBox::Yes) |
| 161 | + { |
| 162 | + const auto add_undo = undo_check->isChecked(); |
| 163 | + CombinedUndoStep* combined_step; |
| 164 | + if (add_undo) |
| 165 | + combined_step = new CombinedUndoStep(map); |
| 166 | + std::vector<QString> matching_keys; |
| 167 | + for (const auto& object : map->selectedObjects()) |
| 168 | + { |
| 169 | + matching_keys.clear(); |
| 170 | + for (const auto& tag : object->tags()) |
| 171 | + { |
| 172 | + if ((compare_operations[op].fn)(tag.key, pattern)) |
| 173 | + { |
| 174 | + matching_keys.push_back(tag.key); |
| 175 | + } |
| 176 | + } |
| 177 | + if (add_undo && !matching_keys.empty()) |
| 178 | + { |
| 179 | + auto undo_step = new ObjectTagsUndoStep(map); |
| 180 | + undo_step->addObject(map->getCurrentPart()->findObjectIndex(object)); |
| 181 | + combined_step->push(undo_step); |
| 182 | + } |
| 183 | + for (const auto& key : matching_keys) |
| 184 | + { |
| 185 | + object->removeTag(key); |
| 186 | + } |
| 187 | + } |
| 188 | + if (add_undo) |
| 189 | + map->push(combined_step); |
| 190 | + } |
| 191 | + } |
| 192 | + accept(); |
| 193 | +} |
| 194 | + |
| 195 | + |
| 196 | +} // namespace OpenOrienteering |
0 commit comments