-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapperGLCanvasToolbar.cpp
153 lines (133 loc) · 5.3 KB
/
MapperGLCanvasToolbar.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* MapperGLCanvasToolbar.cpp
*
* (c) 2016 Sofian Audry -- info(@)sofianaudry(.)com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MapperGLCanvasToolbar.h"
namespace mmp {
MapperGLCanvasToolbar::MapperGLCanvasToolbar(MapperGLCanvas* canvas, QWidget* parent)
: QWidget(parent),
_canvas(canvas)
{
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
createZoomToolsLayout();
// Disable zoom tool buttons
enableZoomToolBar(false);
}
MapperGLCanvasToolbar::~MapperGLCanvasToolbar() {
}
void MapperGLCanvasToolbar::createZoomToolsLayout()
{
// Create zoom tool bar
setObjectName("zoom-toolbox");
// Create label for title
_titleLabel = new QLabel;
// Create horizontale layout for widgets
QHBoxLayout* toolbarLayout = new QHBoxLayout;
toolbarLayout->setContentsMargins(0, 0, 5, 0);
// Create buttons
// Zoom In button
_zoomInButton = new QPushButton;
_zoomInButton->setIcon(QIcon(":/zoom-in"));
_zoomInButton->setIconSize(QSize(MM::ZOOM_TOOLBAR_ICON_SIZE, MM::ZOOM_TOOLBAR_ICON_SIZE));
_zoomInButton->setToolTip(tr("Enlarge the shape"));
_zoomInButton->setFixedSize(MM::ZOOM_TOOLBAR_BUTTON_SIZE, MM::ZOOM_TOOLBAR_BUTTON_SIZE);
_zoomInButton->setObjectName("zoom-in");
connect(_zoomInButton, SIGNAL(clicked()), _canvas, SLOT(increaseZoomLevel()));
// Zoom Out button
_zoomOutButton = new QPushButton;
_zoomOutButton->setIcon(QIcon(":/zoom-out"));
_zoomOutButton->setIconSize(QSize(MM::ZOOM_TOOLBAR_ICON_SIZE, MM::ZOOM_TOOLBAR_ICON_SIZE));
_zoomOutButton->setToolTip(tr("Shrink the shape"));
_zoomOutButton->setFixedSize(MM::ZOOM_TOOLBAR_BUTTON_SIZE, MM::ZOOM_TOOLBAR_BUTTON_SIZE);
_zoomOutButton->setObjectName("zoom-out");
connect(_zoomOutButton, SIGNAL(clicked()), _canvas, SLOT(decreaseZoomLevel()));
// Reset to normal size button.
_resetZoomButton = new QPushButton;
_resetZoomButton->setIcon(QIcon(":/reset-zoom"));
_resetZoomButton->setIconSize(QSize(MM::ZOOM_TOOLBAR_ICON_SIZE, MM::ZOOM_TOOLBAR_ICON_SIZE));
_resetZoomButton->setToolTip(tr("Reset the shape to the normal size"));
_resetZoomButton->setFixedSize(MM::ZOOM_TOOLBAR_BUTTON_SIZE, MM::ZOOM_TOOLBAR_BUTTON_SIZE);
_resetZoomButton->setObjectName("reset-zoom");
connect(_resetZoomButton, SIGNAL(clicked()), _canvas, SLOT(resetZoomLevel()));
// Fit to view button
_fitToViewButton = new QPushButton;
_fitToViewButton->setIcon(QIcon(":/zoom-fit"));
_fitToViewButton->setIconSize(QSize(MM::ZOOM_TOOLBAR_ICON_SIZE, MM::ZOOM_TOOLBAR_ICON_SIZE));
_fitToViewButton->setToolTip(tr("Fit the shape to content view"));
_fitToViewButton->setFixedSize(MM::ZOOM_TOOLBAR_BUTTON_SIZE, MM::ZOOM_TOOLBAR_BUTTON_SIZE);
_fitToViewButton->setObjectName("zoom-fit");
connect(_fitToViewButton, SIGNAL(clicked()), _canvas, SLOT(fitShapeInView()));
// Create separator
QFrame *separator = new QFrame(this);
separator->setFixedSize(5, 30);
separator->setFrameShape(QFrame::VLine);
// Create the dropdowm menu
_dropdownMenu = new QComboBox;
_dropdownMenu->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
// make some settings
_dropdownMenu->setObjectName("dropdown-menu");
// Create if empty or update list
updateDropdownMenu();
// And listen
connect(_dropdownMenu, SIGNAL(activated(QString)), _canvas, SLOT(setZoomFromMenu(QString)));
// Add widgets into layout
toolbarLayout->addWidget(_titleLabel);
toolbarLayout->addWidget(_zoomInButton);
toolbarLayout->addWidget(_zoomOutButton);
toolbarLayout->addWidget(_resetZoomButton);
toolbarLayout->addWidget(_fitToViewButton);
toolbarLayout->addWidget(separator);
toolbarLayout->addWidget(_dropdownMenu);
// Insert layout in widget
setLayout(toolbarLayout);
connect(_canvas, SIGNAL(zoomFactorChanged(qreal)), this, SLOT(updateDropdownMenu(qreal)));
}
void MapperGLCanvasToolbar::showZoomToolBar(bool visible)
{
if (visible)
show();
else
hide();
}
void MapperGLCanvasToolbar::enableZoomToolBar(bool enabled)
{
for (QPushButton *button: findChildren<QPushButton *>()) {
button->setEnabled(enabled);
}
_dropdownMenu->setEnabled(enabled);
// Notify changes
_areEnable = enabled;
}
void MapperGLCanvasToolbar::updateDropdownMenu(qreal factor)
{
// Get current zoom factor percentage
QString zoomFactor = QString::number(int(factor * 100)).append(QChar('%'));
//Create list
QStringList zoomFactorList;
zoomFactorList << "400%" << "300%" << "200%" << "150%" << "125%" <<
"100%" << "75%" << "50%" << "25%" << "12.5%";
// Avoid duplicate
if (!zoomFactorList.contains(zoomFactor))
zoomFactorList.append(zoomFactor);
// Clear if is not empty
_dropdownMenu->clear();
// Add list item
_dropdownMenu->addItems(zoomFactorList);
// Select 100% by default
_dropdownMenu->setCurrentText(zoomFactor);
}
}