-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathQGVWidgetZoom.cpp
More file actions
112 lines (99 loc) · 3.52 KB
/
Copy pathQGVWidgetZoom.cpp
File metadata and controls
112 lines (99 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/***************************************************************************
* QGeoView is a Qt / C ++ widget for visualizing geographic data.
* Copyright (C) 2018-2025 Andrey Yaroshenko.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see https://www.gnu.org/licenses.
****************************************************************************/
#include "QGVWidgetZoom.h"
#include <QHBoxLayout>
#include <QIcon>
#include <QImage>
#include <QPainter>
#include <QVBoxLayout>
#include <QtMath>
namespace {
QSize iconSize = QSize(24, 24);
double zoomExponentDown = qPow(2, 1.0 / 5.0);
double zoomExponentUp = 1.0 / qPow(2, 1.0 / 5.0);
}
QGVWidgetZoom::QGVWidgetZoom()
{
mButtonPlus.reset(new QToolButton());
mButtonPlus->setAutoRepeat(true);
mButtonPlus->setIconSize(iconSize);
mButtonPlus->setToolButtonStyle(Qt::ToolButtonIconOnly);
mButtonPlus->setIcon(QIcon(createPixmap(iconSize * 0.75, "+")));
connect(mButtonPlus.data(), &QToolButton::clicked, this, &QGVWidgetZoom::zoomPlus);
mButtonMinus.reset(new QToolButton());
mButtonMinus->setAutoRepeat(true);
mButtonMinus->setIconSize(iconSize);
mButtonMinus->setToolButtonStyle(Qt::ToolButtonIconOnly);
mButtonMinus->setIcon(QIcon(createPixmap(iconSize * 0.75, "-")));
connect(mButtonMinus.data(), &QToolButton::clicked, this, &QGVWidgetZoom::zoomMinus);
setOrientation(Qt::Vertical);
setAnchor(QPoint(10, 0), { Qt::LeftEdge });
}
void QGVWidgetZoom::setOrientation(Qt::Orientation orientation)
{
if (layout() != nullptr) {
delete layout();
}
mOrientation = orientation;
if (mOrientation == Qt::Horizontal) {
setLayout(new QHBoxLayout(this));
} else {
setLayout(new QVBoxLayout(this));
}
layout()->setSpacing(0);
layout()->setSizeConstraint(QLayout::SetMinimumSize);
layout()->setContentsMargins(0, 0, 0, 0);
layout()->addWidget(mButtonPlus.data());
layout()->addWidget(mButtonMinus.data());
}
Qt::Orientation QGVWidgetZoom::getOrientation() const
{
return mOrientation;
}
QToolButton* QGVWidgetZoom::plus()
{
return mButtonPlus.data();
}
QToolButton* QGVWidgetZoom::minus()
{
return mButtonMinus.data();
}
QPixmap QGVWidgetZoom::createPixmap(const QSize& size, const QString& text)
{
QImage image(size, QImage::Format_ARGB32_Premultiplied);
image.fill(qRgba(0, 0, 0, 0));
QPixmap pixmap = QPixmap::fromImage(image, Qt::NoFormatConversion);
QPainter painter(&pixmap);
QPen pen = QPen(Qt::black);
pen.setWidth(1);
pen.setCosmetic(true);
painter.setPen(pen);
QBrush brush = QBrush(Qt::blue);
painter.setBrush(brush);
const auto path = QGV::createTextPath(QRect(QPoint(0, 0), size), text, font(), pen.width());
painter.drawPath(path);
return pixmap;
}
void QGVWidgetZoom::zoomPlus()
{
getMap()->cameraTo(QGVCameraActions(getMap()).scaleBy(zoomExponentDown));
}
void QGVWidgetZoom::zoomMinus()
{
getMap()->cameraTo(QGVCameraActions(getMap()).scaleBy(zoomExponentUp));
}