Skip to content

Commit c7e6d40

Browse files
committed
MapFindFeature: Center view on found objects
Add checkmark to center the view on the found object(s).
1 parent a152dc6 commit c7e6d40

File tree

6 files changed

+55
-13
lines changed

6 files changed

+55
-13
lines changed

src/core/map.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,10 @@ void Map::ensureVisibilityOfSelectedObjects(SelectionVisibility visibility)
11141114
widget->ensureVisibilityOfRect(rect, MapWidget::DiscreteZoom);
11151115
break;
11161116

1117+
case CenterFullVisibility:
1118+
widget->ensureVisibilityOfRect(rect, MapWidget::DiscreteZoom, true);
1119+
break;
1120+
11171121
case IgnoreVisibilty:
11181122
break; // Do nothing
11191123

src/core/map.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ friend class XMLFileExporter;
121121
{
122122
FullVisibility,
123123
PartialVisibility,
124+
CenterFullVisibility,
124125
IgnoreVisibilty
125126
};
126127

src/gui/map/map_find_feature.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <QAction>
2727
#include <QAbstractButton>
28+
#include <QCheckBox>
2829
#include <QContextMenuEvent>
2930
#include <QDialog>
3031
#include <QDialogButtonBox>
@@ -120,6 +121,9 @@ void MapFindFeature::showDialog()
120121
connect(controller.getMap(), &Map::objectSelectionChanged, this, &MapFindFeature::objectSelectionChanged);
121122
objectSelectionChanged();
122123

124+
center_view = new QCheckBox(tr("Center view"));
125+
connect(center_view, &QCheckBox::stateChanged, this, &MapFindFeature::centerView);
126+
123127
auto tags_button = new QPushButton(tr("Query editor"));
124128
tags_button->setCheckable(true);
125129

@@ -141,9 +145,10 @@ void MapFindFeature::showDialog()
141145
layout->addWidget(find_all, 0, 1, 1, 1);
142146
layout->addWidget(find_next, 1, 1, 1, 1);
143147
layout->addWidget(delete_find_next, 2, 1, 1, 1);
144-
layout->addWidget(tags_button, 4, 1, 1, 1);
145-
layout->addWidget(tag_selector_buttons, 6, 1, 1, 1);
146-
layout->addWidget(button_box, 7, 0, 1, 2);
148+
layout->addWidget(center_view, 3, 1, 1, 1);
149+
layout->addWidget(tags_button, 5, 1, 1, 1);
150+
layout->addWidget(tag_selector_buttons, 7, 1, 1, 1);
151+
layout->addWidget(button_box, 8, 0, 1, 2);
147152

148153
find_dialog->setLayout(layout);
149154
}
@@ -230,7 +235,10 @@ void MapFindFeature::findNext()
230235
if (next_object)
231236
map->addObjectToSelection(next_object, false);
232237
map->emitSelectionChanged();
233-
map->ensureVisibilityOfSelectedObjects(Map::FullVisibility);
238+
if (center_view->isChecked())
239+
map->ensureVisibilityOfSelectedObjects(Map::CenterFullVisibility);
240+
else
241+
map->ensureVisibilityOfSelectedObjects(Map::FullVisibility);
234242

235243
if (!map->selectedObjects().empty())
236244
controller.setEditTool();
@@ -271,7 +279,10 @@ void MapFindFeature::findAll()
271279
map->addObjectToSelection(object, false);
272280
}, search);
273281
map->emitSelectionChanged();
274-
map->ensureVisibilityOfSelectedObjects(Map::FullVisibility);
282+
if (center_view->isChecked())
283+
map->ensureVisibilityOfSelectedObjects(Map::CenterFullVisibility);
284+
else
285+
map->ensureVisibilityOfSelectedObjects(Map::FullVisibility);
275286
controller.getWindow()->showStatusBarMessage(OpenOrienteering::TagSelectWidget::tr("%n object(s) selected", nullptr, map->getNumSelectedObjects()), 2000);
276287

277288
if (!map->selectedObjects().empty())
@@ -287,6 +298,18 @@ void MapFindFeature::objectSelectionChanged()
287298
}
288299

289300

301+
// slot
302+
void MapFindFeature::centerView()
303+
{
304+
if (center_view->isChecked())
305+
{
306+
auto map = controller.getMap();
307+
if (map->getNumSelectedObjects())
308+
map->ensureVisibilityOfSelectedObjects(Map::CenterFullVisibility);
309+
}
310+
}
311+
312+
290313
// slot
291314
void MapFindFeature::showHelp() const
292315
{

src/gui/map/map_find_feature.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright 2017-2019, 2025 Kai Pastor
3+
* Copyright 2025 Matthias Kühlewein
34
*
45
* This file is part of OpenOrienteering.
56
*
@@ -27,6 +28,7 @@
2728
#include <QTextEdit>
2829

2930
class QAction;
31+
class QCheckBox;
3032
class QContextMenuEvent;
3133
class QDialog;
3234
class QPushButton;
@@ -42,8 +44,8 @@ class TagSelectWidget;
4244

4345

4446
/**
45-
* The context menu (right click) is extended by the possibility to insert
46-
* one of the keywords (e.g., SYMBOL, AND...)
47+
* The context menu (right click) is extended by the possibility
48+
* to select and insert one of the keywords (e.g., SYMBOL, AND...)
4749
*/
4850
class MapFindTextEdit : public QTextEdit
4951
{
@@ -88,6 +90,8 @@ private slots:
8890

8991
void objectSelectionChanged();
9092

93+
void centerView();
94+
9195
void showHelp() const;
9296

9397
void tagSelectorToggled(bool active);
@@ -105,6 +109,7 @@ private slots:
105109
TagSelectWidget* tag_selector = nullptr; // child of find_dialog
106110
QWidget* tag_selector_buttons = nullptr; // child of find_dialog
107111
QPushButton* delete_find_next = nullptr; // child of find_dialog
112+
QCheckBox* center_view = nullptr; // child of find_dialog
108113
QAction* show_action = nullptr; // child of this
109114
QAction* find_next_action = nullptr; // child of this
110115

src/gui/map/map_widget.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright 2012-2014 Thomas Schöps
3-
* Copyright 2013-2020 Kai Pastor
3+
* Copyright 2013-2020, 2025 Kai Pastor
44
*
55
* This file is part of OpenOrienteering.
66
*
@@ -426,7 +426,7 @@ void MapWidget::moveMap(int steps_x, int steps_y)
426426
}
427427
}
428428

429-
void MapWidget::ensureVisibilityOfRect(QRectF map_rect, ZoomOption zoom_option)
429+
void MapWidget::ensureVisibilityOfRect(QRectF map_rect, ZoomOption zoom_option, bool center_view)
430430
{
431431
// Amount in pixels that is scrolled "too much" if the rect is not completely visible
432432
// TODO: change to absolute size using dpi value
@@ -436,7 +436,11 @@ void MapWidget::ensureVisibilityOfRect(QRectF map_rect, ZoomOption zoom_option)
436436
// TODO: this method assumes that the viewport is not rotated.
437437

438438
if (rect().contains(viewport_rect.topLeft()) && rect().contains(viewport_rect.bottomRight()))
439+
{
440+
if (center_view)
441+
view->setCenter(MapCoord{ map_rect.center() });
439442
return;
443+
}
440444

441445
auto offset = MapCoordF{ 0, 0 };
442446

@@ -451,7 +455,12 @@ void MapWidget::ensureVisibilityOfRect(QRectF map_rect, ZoomOption zoom_option)
451455
offset.ry() = view->pixelToLength(viewport_rect.bottom() - height() + pixel_border) / 1000.0;
452456

453457
if (!qIsNull(offset.lengthSquared()))
454-
view->setCenter(view->center() + offset);
458+
{
459+
if (center_view)
460+
view->setCenter(MapCoord{ map_rect.center() });
461+
else
462+
view->setCenter(view->center() + offset);
463+
}
455464

456465
// If the rect is still not completely in view, we have to zoom out
457466
viewport_rect = mapToViewport(map_rect).toAlignedRect();

src/gui/map/map_widget.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright 2012-2014 Thomas Schöps
3-
* Copyright 2013-2020 Kai Pastor
3+
* Copyright 2013-2020, 2025 Kai Pastor
44
*
55
* This file is part of OpenOrienteering.
66
*
@@ -214,7 +214,7 @@ friend class MapView;
214214
/**
215215
* Adjusts the viewport so the given rect is inside the view.
216216
*/
217-
void ensureVisibilityOfRect(QRectF map_rect, ZoomOption zoom_option); // clazy:exclude=function-args-by-ref
217+
void ensureVisibilityOfRect(QRectF map_rect, ZoomOption zoom_option, bool center_view = false); // clazy:exclude=function-args-by-ref
218218

219219
/**
220220
* Sets the view so the rect is centered and zooomed to fill the widget.
@@ -584,4 +584,4 @@ MapWidget::CoordsType MapWidget::getCoordsDisplay() const
584584

585585
} // namespace OpenOrienteering
586586

587-
#endif
587+
#endif // OPENORIENTEERING_MAP_WIDGET_H

0 commit comments

Comments
 (0)