10 #include "ImageScaler.hpp"
12 ImageScaler::ImageScaler(QWidget *parent)
15 lockAspectRatio = false;
22 setFrameStyle(QFrame::StyledPanel);
24 lockButton = new QCheckBox();
25 lockButton->setIcon(QIcon(":icons/lock.png"));
26 lockButton->setToolTip(tr("Lock aspect ratio"));
27 lockButton->setWhatsThis(tr("Lock aspect ratio"));
28 connect(lockButton, SIGNAL(stateChanged(int)),
29 this, SLOT(setLockAspectRatio(int)));
33 widthBox = new QSpinBox();
34 widthBox->setMinimum(minWidth);
36 connect(this, SIGNAL(widthChanged(int)),
37 widthBox, SLOT(setValue(int)));
38 connect(widthBox, SIGNAL(valueChanged(int)),
39 this, SLOT(setWidth(int)));
41 widthLabel = new QLabel(tr("Width:"));
42 //widthLabel.setBuddy(widthBox);
44 heightBox = new QSpinBox();
45 heightBox->setMinimum(1);
47 connect(this, SIGNAL(heightChanged(int)),
48 heightBox, SLOT(setValue(int)));
49 connect(heightBox, SIGNAL(valueChanged(int)),
50 this, SLOT(setHeight(int)));
52 heightLabel = new QLabel(tr("Height:"));
53 //heightLabel.setBuddy(heightBox);
55 QGridLayout *layout = new QGridLayout;
56 layout->addWidget(widthLabel, 0, 0);
57 layout->addWidget(widthBox, 0, 1);
58 layout->addWidget(heightLabel, 1, 0);
59 layout->addWidget(heightBox, 1, 1);
60 layout->addWidget(lockButton, 0, 2, 1, 2, Qt::AlignCenter);
68 int ImageScaler::getMaxWidth() const
73 int ImageScaler::getMaxHeight() const
78 int ImageScaler::getWidth() const
80 return widthBox->value();
83 int ImageScaler::getHeight() const
85 return heightBox->value();
89 void ImageScaler::setMaxWidth(int newValue)
92 widthBox->setMaximum(maxWidth);
93 emit maxWidthChanged(maxWidth);
96 void ImageScaler::setMaxHeight(int newValue)
99 heightBox->setMaximum(maxHeight);
100 emit maxHeightChanged(maxHeight);
103 void ImageScaler::setLockAspectRatio(int state)
105 if (state == Qt::Checked)
107 setLockAspectRatio(true);
109 else if (state == Qt::Unchecked)
111 setLockAspectRatio(false);
115 void ImageScaler::setLockAspectRatio(bool state)
119 lockAspectRatio = true;
120 aspectRatio = (float) curHeight / (float) curWidth;
122 else if (state == false)
124 lockAspectRatio = false;
128 void ImageScaler::setWidth(int newValue)
131 if (curWidth == newValue)
136 // Make sure newValue is within legal range.
137 if (newValue < minWidth || newValue > maxWidth)
142 if (!lockAspectRatio)
145 emit widthChanged(newValue);
148 else if (lockAspectRatio)
154 emit widthChanged(newValue);
158 int newHeight = (int) rint((double) aspectRatio * (double) newValue);
160 if (newValue != curWidth)
163 emit widthChanged(newValue);
165 curHeight = newHeight;
166 emit heightChanged(newHeight);
170 void ImageScaler::setHeight(int newValue)
173 if (curHeight == newValue)
178 // Make sure newValue is within legal range.
179 if (newValue < minHeight || newValue > maxHeight)
184 // Just change current height if not in locked
185 // aspect ratio mode.
186 if (!lockAspectRatio)
188 curHeight = newValue;
189 emit heightChanged(newValue);
192 // Handle locked aspect ratio mode.
193 else if (lockAspectRatio)
197 curHeight = newValue;
198 emit heightChanged(newValue);
202 int newWidth = (int) rint( (1.0 /(double) aspectRatio) * (double) newValue);
204 if (newValue != curHeight)
206 curHeight = newValue;
207 emit heightChanged(newValue);
210 emit widthChanged(newWidth);