10 #include "ImageScaler.hpp"
12 ImageScaler::ImageScaler(QWidget *parent)
15 lockAspectRatio = false;
22 //lockAspectAction = new QAction(
23 //lockAspectAction->setCheckable(true);
25 lockButton = new QCheckBox();
26 lockButton->setIcon(QIcon(tr("icons/lock.png")));
27 lockButton->setToolTip(tr("Lock aspect ratio"));
28 lockButton->setWhatsThis(tr("Lock aspect ratio"));
29 connect(lockButton, SIGNAL(stateChanged(int)),
30 this, SLOT(setLockAspectRatio(int)));
34 widthBox = new QSpinBox();
35 widthBox->setMinimum(minWidth);
37 connect(this, SIGNAL(widthChanged(int)),
38 widthBox, SLOT(setValue(int)));
39 connect(widthBox, SIGNAL(valueChanged(int)),
40 this, SLOT(setWidth(int)));
42 widthLabel = new QLabel(tr("Width:"));
43 //widthLabel.setBuddy(widthBox);
45 heightBox = new QSpinBox();
46 heightBox->setMinimum(1);
48 connect(this, SIGNAL(heightChanged(int)),
49 heightBox, SLOT(setValue(int)));
50 connect(heightBox, SIGNAL(valueChanged(int)),
51 this, SLOT(setHeight(int)));
53 heightLabel = new QLabel(tr("Height:"));
54 //heightLabel.setBuddy(heightBox);
56 QGridLayout *layout = new QGridLayout;
57 layout->addWidget(widthLabel, 0, 0);
58 layout->addWidget(widthBox, 0, 1);
59 layout->addWidget(heightLabel, 1, 0);
60 layout->addWidget(heightBox, 1, 1);
61 layout->addWidget(lockButton, 0, 2, 1, 2, Qt::AlignCenter);
69 int ImageScaler::getMaxWidth() const
74 int ImageScaler::getMaxHeight() const
79 int ImageScaler::getWidth() const
81 return widthBox->value();
84 int ImageScaler::getHeight() const
86 return heightBox->value();
90 void ImageScaler::setMaxWidth(int newValue)
93 widthBox->setMaximum(maxWidth);
94 emit maxWidthChanged(maxWidth);
97 void ImageScaler::setMaxHeight(int newValue)
100 heightBox->setMaximum(maxHeight);
101 emit maxHeightChanged(maxHeight);
104 void ImageScaler::setLockAspectRatio(int state)
106 if (state == Qt::Checked)
108 setLockAspectRatio(true);
110 else if (state == Qt::Unchecked)
112 setLockAspectRatio(false);
116 void ImageScaler::setLockAspectRatio(bool state)
120 lockAspectRatio = true;
121 aspectRatio = (float) curHeight / (float) curWidth;
123 else if (state == false)
125 lockAspectRatio = false;
129 void ImageScaler::setWidth(int newValue)
132 if (curWidth == newValue)
137 // Make sure newValue is within legal range.
138 if (newValue < minWidth || newValue > maxWidth)
143 if (!lockAspectRatio)
146 emit widthChanged(newValue);
149 else if (lockAspectRatio)
155 emit widthChanged(newValue);
159 int newHeight = (int) rint((double) aspectRatio * (double) newValue);
161 if (newValue != curWidth)
164 emit widthChanged(newValue);
166 curHeight = newHeight;
167 emit heightChanged(newHeight);
171 void ImageScaler::setHeight(int newValue)
174 if (curHeight == newValue)
179 // Make sure newValue is within legal range.
180 if (newValue < minHeight || newValue > maxHeight)
185 // Just change current height if not in locked
186 // aspect ratio mode.
187 if (!lockAspectRatio)
189 curHeight = newValue;
190 emit heightChanged(newValue);
193 // Handle locked aspect ratio mode.
194 else if (lockAspectRatio)
198 curHeight = newValue;
199 emit heightChanged(newValue);
203 int newWidth = (int) rint( (1.0 /(double) aspectRatio) * (double) newValue);
205 if (newValue != curHeight)
207 curHeight = newValue;
208 emit heightChanged(newValue);
211 emit widthChanged(newWidth);