1 /*
2  * Copyright (C) 2020 Damir Porobic <damir.porobic@gmx.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "ZoomPicker.h"
21 
22 namespace kImageAnnotator {
23 
ZoomPicker(QWidget * parent)24 ZoomPicker::ZoomPicker(QWidget *parent) :
25 	SettingsPickerWidget(parent),
26 	mLayout(new QHBoxLayout),
27 	mLabel(new QLabel(this)),
28 	mSpinBox(new CustomSpinBox(this)),
29 	mZoomInAction(new QAction(this)),
30 	mZoomOutAction(new QAction(this)),
31 	mResetZoomAction(new QAction(this))
32 {
33 	initGui();
34 }
35 
expandingWidget()36 QWidget *ZoomPicker::expandingWidget()
37 {
38 	return mSpinBox;
39 }
40 
initGui()41 void ZoomPicker::initGui()
42 {
43 	mLayout->setContentsMargins(1, 0, 0, 0);
44 
45 	auto icon = IconLoader::load(QLatin1String("zoom.svg"));
46 	mLabel->setPixmap(icon.pixmap(ScaledSizeProvider::settingsWidgetIconSize()));
47 
48 	mSpinBox->setFocusPolicy(Qt::NoFocus);
49 	mSpinBox->setRange(10, 800);
50 	mSpinBox->setSingleStep(10);
51 	mSpinBox->setSuffix(QLatin1String("%"));
52 	mSpinBox->setWrapping(false);
53 
54 	mZoomInAction->setShortcut(QKeySequence::ZoomIn);
55 	mZoomOutAction->setShortcut(QKeySequence::ZoomOut);
56 	mResetZoomAction->setShortcut(Qt::CTRL + Qt::Key_0);
57 
58 	setToolTip(getToolTip());
59 
60 	connect(mZoomInAction, &QAction::triggered, this, &ZoomPicker::zoomIn);
61 	connect(mZoomOutAction, &QAction::triggered, this, &ZoomPicker::zoomOut);
62 	connect(mResetZoomAction, &QAction::triggered, this, &ZoomPicker::resetZoomOut);
63 
64 	addAction(mZoomInAction);
65 	addAction(mZoomOutAction);
66 	addAction(mResetZoomAction);
67 
68 	connect(mSpinBox, &CustomSpinBox::valueChanged, this, &ZoomPicker::notifyZoomValueChanged);
69 
70 	mLayout->addWidget(mLabel);
71 	mLayout->addWidget(mSpinBox);
72 	mLayout->setAlignment(Qt::AlignLeft);
73 
74 	setLayout(mLayout);
75 }
76 
getToolTip() const77 QString ZoomPicker::getToolTip() const
78 {
79 	auto newLine = QLatin1String("\n");
80 	auto zoomIn = tr("Zoom In (%1)").arg(mZoomInAction->shortcut().toString());
81 	auto zoomOut = tr("Zoom Out (%1)").arg(mZoomOutAction->shortcut().toString());
82 	auto resetZoom = tr("Reset Zoom (%1)").arg(mResetZoomAction->shortcut().toString());
83 	return zoomIn + newLine + zoomOut + newLine + resetZoom;
84 }
85 
setZoomValue(double value)86 void ZoomPicker::setZoomValue(double value)
87 {
88 	auto zoomValue = qRound(value * 100);
89 	mSpinBox->setValueSilent(zoomValue);
90 }
91 
notifyZoomValueChanged(double value)92 void ZoomPicker::notifyZoomValueChanged(double value)
93 {
94 	emit zoomValueChanged(value / 100.0);
95 }
96 
zoomIn()97 void ZoomPicker::zoomIn()
98 {
99 	int currentZoom = mSpinBox->value();
100 	notifyZoomValueChanged(currentZoom + 10);
101 }
102 
zoomOut()103 void ZoomPicker::zoomOut()
104 {
105 	auto currentZoom = mSpinBox->value();
106 	notifyZoomValueChanged(currentZoom - 10);
107 }
108 
resetZoomOut()109 void ZoomPicker::resetZoomOut()
110 {
111 	notifyZoomValueChanged(100);
112 }
113 
114 } // namespace kImageAnnotator
115