|
| 1 | +/* |
| 2 | + * Copyright 2012 Thomas Schöps |
| 3 | + * Copyright 2024 Matthias Kühlewein |
| 4 | + * |
| 5 | + * This file is part of OpenOrienteering. |
| 6 | + * |
| 7 | + * OpenOrienteering is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * OpenOrienteering is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | + |
| 22 | +#include "map_notes.h" |
| 23 | + |
| 24 | +#include <Qt> |
| 25 | +#include <QtGlobal> |
| 26 | +#include <QDialogButtonBox> |
| 27 | +#include <QFlags> |
| 28 | +#include <QFontMetrics> |
| 29 | +#include <QLatin1Char> |
| 30 | +#include <QRect> |
| 31 | +#include <QSize> |
| 32 | +#include <QTextEdit> |
| 33 | +#include <QVBoxLayout> |
| 34 | +#include <QWidget> |
| 35 | + |
| 36 | +#include "core/map.h" |
| 37 | + |
| 38 | + |
| 39 | +namespace OpenOrienteering { |
| 40 | + |
| 41 | +MapNotesDialog::MapNotesDialog(QWidget* parent, Map& map) |
| 42 | +: QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint) |
| 43 | +, map { map } |
| 44 | +{ |
| 45 | + setWindowTitle(tr("Map notes")); |
| 46 | + |
| 47 | + text_edit = new QTextEdit(); |
| 48 | + text_edit->setPlainText(map.getMapNotes()); |
| 49 | + auto* button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 50 | + |
| 51 | + auto* layout = new QVBoxLayout(); |
| 52 | + layout->addWidget(text_edit); |
| 53 | + layout->addWidget(button_box); |
| 54 | + setLayout(layout); |
| 55 | + |
| 56 | + const auto max_size = parent ? parent->window()->size() : QSize{800, 600}; |
| 57 | + const QFontMetrics text_font_metrics(text_edit->currentFont()); |
| 58 | + auto preferred_width = text_font_metrics.boundingRect(QString(60, QLatin1Char('m'))).width(); |
| 59 | + const auto bounding_rect = text_font_metrics.boundingRect(0, 0, preferred_width, max_size.height(), Qt::TextWordWrap, map.getMapNotes()); |
| 60 | + auto width = qBound(300, bounding_rect.width() + 60, max_size.width()); |
| 61 | + auto height = qBound(200, bounding_rect.height() + 80, max_size.height()); |
| 62 | + resize(width, height); |
| 63 | + |
| 64 | + connect(button_box, &QDialogButtonBox::rejected, this, &QDialog::reject); |
| 65 | + connect(button_box, &QDialogButtonBox::accepted, this, &MapNotesDialog::okClicked); |
| 66 | +} |
| 67 | + |
| 68 | +MapNotesDialog::~MapNotesDialog() = default; |
| 69 | + |
| 70 | +// slot |
| 71 | +void MapNotesDialog::okClicked() |
| 72 | +{ |
| 73 | + auto text = text_edit->toPlainText(); |
| 74 | + if (text != map.getMapNotes()) |
| 75 | + { |
| 76 | + map.setMapNotes(text); |
| 77 | + map.setHasUnsavedChanges(true); |
| 78 | + } |
| 79 | + |
| 80 | + accept(); |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace OpenOrienteering |
0 commit comments