Skip to content

Commit 39ca931

Browse files
dl3sdodg0yt
andauthored
MapNotesDialog: Refactor and improved (#2272)
Moved out of MapEditorController. Improved window size calculation using available space while maintaining a readable max width. Use standard buttons and layout for map notes dialog. Co-authored-by: Kai Pastor <[email protected]>
1 parent d0a088f commit 39ca931

File tree

5 files changed

+148
-32
lines changed

5 files changed

+148
-32
lines changed

code-check-wrapper.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ for I in \
7373
map_find_feature.cpp \
7474
map_information.cpp \
7575
map_information_dialog.cpp \
76+
map_notes.cpp \
7677
map_printer \
7778
map_widget.cpp \
7879
mapper_proxystyle.cpp \

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ set(Mapper_Common_SRCS
165165
gui/map/map_editor_activity.cpp
166166
gui/map/map_find_feature.cpp
167167
gui/map/map_information_dialog.cpp
168+
gui/map/map_notes.cpp
168169
gui/map/map_widget.cpp
169170
gui/map/rotate_map_dialog.cpp
170171
gui/map/stretch_map_dialog.cpp

src/gui/map/map_editor.cpp

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <Qt>
3636
#include <QtGlobal>
3737
#include <QtMath>
38-
#include <QAbstractButton>
3938
#include <QAction>
4039
#include <QActionGroup>
4140
#include <QApplication>
@@ -75,7 +74,6 @@
7574
#include <QPoint>
7675
#include <QPointer>
7776
#include <QPointF>
78-
#include <QPushButton>
7977
#include <QRect>
8078
#include <QRectF>
8179
#include <QSettings>
@@ -87,7 +85,6 @@
8785
#include <QSplitter>
8886
#include <QStatusBar>
8987
#include <QStringList>
90-
#include <QTextEdit>
9188
#include <QToolBar>
9289
#include <QToolButton>
9390
#include <QVariant>
@@ -125,6 +122,7 @@
125122
#include "gui/map/map_editor_activity.h"
126123
#include "gui/map/map_find_feature.h"
127124
#include "gui/map/map_information_dialog.h"
125+
#include "gui/map/map_notes.h"
128126
#include "gui/map/map_widget.h"
129127
#include "gui/map/rotate_map_dialog.h"
130128
#include "gui/symbols/symbol_replacement.h"
@@ -2254,36 +2252,11 @@ void MapEditorController::rotateMapClicked()
22542252

22552253
void MapEditorController::mapNotesClicked()
22562254
{
2257-
QDialog dialog(window, Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
2258-
dialog.setWindowTitle(tr("Map notes"));
2259-
dialog.setWindowModality(Qt::WindowModal);
2260-
2261-
auto* text_edit = new QTextEdit();
2262-
text_edit->setPlainText(map->getMapNotes());
2263-
QPushButton* cancel_button = new QPushButton(tr("Cancel"));
2264-
QPushButton* ok_button = new QPushButton(QIcon(QString::fromLatin1(":/images/arrow-right.png")), tr("OK"));
2265-
ok_button->setDefault(true);
2266-
2267-
auto* buttons_layout = new QHBoxLayout();
2268-
buttons_layout->addWidget(cancel_button);
2269-
buttons_layout->addStretch(1);
2270-
buttons_layout->addWidget(ok_button);
2271-
2272-
auto* layout = new QVBoxLayout();
2273-
layout->addWidget(text_edit);
2274-
layout->addLayout(buttons_layout);
2275-
dialog.setLayout(layout);
2276-
2277-
connect(cancel_button, &QAbstractButton::clicked, &dialog, &QDialog::reject);
2278-
connect(ok_button, &QAbstractButton::clicked, &dialog, &QDialog::accept);
2279-
2280-
if (dialog.exec() == QDialog::Accepted)
2255+
if (map)
22812256
{
2282-
if (text_edit->toPlainText() != map->getMapNotes())
2283-
{
2284-
map->setMapNotes(text_edit->toPlainText());
2285-
map->setHasUnsavedChanges(true);
2286-
}
2257+
MapNotesDialog dialog(window, *map);
2258+
dialog.setWindowModality(Qt::WindowModal);
2259+
dialog.exec();
22872260
}
22882261
}
22892262

src/gui/map/map_notes.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

src/gui/map/map_notes.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 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+
#ifndef OPENORIENTEERING_MAP_NOTES_H
21+
#define OPENORIENTEERING_MAP_NOTES_H
22+
23+
#include <QDialog>
24+
#include <QObject>
25+
26+
// IWYU pragma: no_include <QString>
27+
class QTextEdit;
28+
class QWidget;
29+
30+
31+
namespace OpenOrienteering {
32+
33+
class Map;
34+
35+
class MapNotesDialog : public QDialog
36+
{
37+
Q_OBJECT
38+
public:
39+
/**
40+
* Creates a new MapNotesDialog object.
41+
*/
42+
MapNotesDialog(QWidget* parent, Map& map);
43+
44+
~MapNotesDialog() override;
45+
46+
private slots:
47+
void okClicked();
48+
49+
private:
50+
QTextEdit* text_edit;
51+
52+
Map& map;
53+
};
54+
55+
56+
} // namespace OpenOrienteering
57+
58+
#endif // OPENORIENTEERING_MAP_NOTES_H

0 commit comments

Comments
 (0)