1 // Copyright (C) 2020 by Yuri Victorovich. All rights reserved.
2
3 #include "scale-image-widget.h"
4
5 #include <QLineEdit>
6
7 #include "misc.h"
8
ScaleImageWidget(QWidget * parent)9 ScaleImageWidget::ScaleImageWidget(QWidget *parent)
10 : QWidget(parent)
11 , layout(this)
12 , widthLabel("width", this)
13 , widthSpinBox(this)
14 , heightLabel("height", this)
15 , heightSpinBox(this)
16 , unitsLabel("%", this)
17 , self(false)
18 {
19 layout.addWidget(&widthLabel);
20 layout.addWidget(&widthSpinBox);
21 layout.addWidget(&heightLabel);
22 layout.addWidget(&heightSpinBox);
23 layout.addWidget(&unitsLabel);
24
25 // alignment
26 widthLabel .setAlignment(Qt::AlignRight|Qt::AlignVCenter);
27 heightLabel.setAlignment(Qt::AlignRight|Qt::AlignVCenter);
28 unitsLabel .setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
29
30 // size policies
31 widthLabel .setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
32 widthSpinBox .setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
33 heightLabel .setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
34 heightSpinBox .setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
35 unitsLabel .setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
36
37 // margins
38 setContentsMargins(0,0,0,0);
39 layout.setContentsMargins(0,0,0,0);
40 // XXX this attempt to shrink spinboxes doesn't work, and they still slightly stick outside of QLabels vertically
41 widthSpinBox.findChild<QLineEdit*>()->setTextMargins(0,0,0,0);
42 heightSpinBox.findChild<QLineEdit*>()->setTextMargins(0,0,0,0);
43
44 // values
45 widthSpinBox .setMinimum(1); // 1% and up
46 heightSpinBox.setMinimum(1);
47 widthSpinBox .setMaximum(maxValue);
48 heightSpinBox.setMaximum(maxValue);
49
50 // connect signals
51 connect(&widthSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), [this](int i) {
52 if (self)
53 return;
54
55 self = true;
56 heightSpinBox.setValue(i);
57 self = false;
58
59 emitSignal();
60 });
61 connect(&heightSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), [this](int i) {
62 if (self)
63 return;
64
65 self = true;
66 widthSpinBox.setValue(i);
67 self = false;
68
69 emitSignal();
70 });
71 }
72
73 /// interface
74
setFactor(unsigned factor)75 void ScaleImageWidget::setFactor(unsigned factor) {
76 self = true;
77 heightSpinBox.setValue(factor);
78 widthSpinBox.setValue(factor);
79 self = false;
80 }
81
82 /// internals
83
emitSignal()84 void ScaleImageWidget::emitSignal() {
85 emit scalingFactorChanged((unsigned)widthSpinBox.value(), (unsigned)heightSpinBox.value());
86 }
87
88