+#include <QAction>
+#include <QCheckBox>
+#include <QLabel>
+#include <QSpinBox>
+#include <QGridLayout>
+
+//#include <iostream>
+#include <cmath>
+
+#include "ImageScaler.h"
+
+ImageScaler::ImageScaler(QWidget *parent)
+ : QWidget(parent)
+{
+ lockAspectRatio = false;
+ aspectRatio = 1.0;
+ minWidth = 1;
+ minHeight = 1;
+ curWidth = 1;
+ curHeight = 1;
+
+ //lockAspectAction = new QAction(
+ //lockAspectAction->setCheckable(true);
+
+ lockButton = new QCheckBox();
+ lockButton->setIcon(QIcon(tr("icons/lock.png")));
+ lockButton->setToolTip(tr("Lock aspect ratio"));
+ lockButton->setWhatsThis(tr("Lock aspect ratio"));
+ connect(lockButton, SIGNAL(stateChanged(int)),
+ this, SLOT(setLockAspectRatio(int)));
+
+
+
+ widthBox = new QSpinBox();
+ widthBox->setMinimum(minWidth);
+ setMaxWidth(4048);
+ connect(this, SIGNAL(widthChanged(int)),
+ widthBox, SLOT(setValue(int)));
+ connect(widthBox, SIGNAL(valueChanged(int)),
+ this, SLOT(setWidth(int)));
+
+ widthLabel = new QLabel(tr("Width:"));
+ //widthLabel.setBuddy(widthBox);
+
+ heightBox = new QSpinBox();
+ heightBox->setMinimum(1);
+ setMaxHeight(4048);
+ connect(this, SIGNAL(heightChanged(int)),
+ heightBox, SLOT(setValue(int)));
+ connect(heightBox, SIGNAL(valueChanged(int)),
+ this, SLOT(setHeight(int)));
+
+ heightLabel = new QLabel(tr("Height:"));
+ //heightLabel.setBuddy(heightBox);
+
+ QGridLayout *layout = new QGridLayout;
+ layout->addWidget(widthLabel, 0, 0);
+ layout->addWidget(widthBox, 0, 1);
+ layout->addWidget(heightLabel, 1, 0);
+ layout->addWidget(heightBox, 1, 1);
+ layout->addWidget(lockButton, 0, 2, 1, 2, Qt::AlignCenter);
+ setLayout(layout);
+
+ setWidth(curWidth);
+ setHeight(curHeight);
+}
+
+
+int ImageScaler::getMaxWidth() const
+{
+ return maxWidth;
+}
+
+int ImageScaler::getMaxHeight() const
+{
+ return maxHeight;
+}
+
+int ImageScaler::getWidth() const
+{
+ return widthBox->value();
+}
+
+int ImageScaler::getHeight() const
+{
+ return heightBox->value();
+}
+
+
+void ImageScaler::setMaxWidth(int newValue)
+{
+ maxWidth = newValue;
+ widthBox->setMaximum(maxWidth);
+ emit maxWidthChanged(maxWidth);
+}
+
+void ImageScaler::setMaxHeight(int newValue)
+{
+ maxHeight = newValue;
+ heightBox->setMaximum(maxHeight);
+ emit maxHeightChanged(maxHeight);
+}
+
+void ImageScaler::setLockAspectRatio(int state)
+{
+ if (state == Qt::Checked)
+ {
+ setLockAspectRatio(true);
+ }
+ else if (state == Qt::Unchecked)
+ {
+ setLockAspectRatio(false);
+ }
+}
+
+void ImageScaler::setLockAspectRatio(bool state)
+{
+ if (state == true)
+ {
+ lockAspectRatio = true;
+ aspectRatio = (float) curHeight / (float) curWidth;
+ }
+ else if (state == false)
+ {
+ lockAspectRatio = false;
+ }
+}
+
+void ImageScaler::setWidth(int newValue)
+{
+
+ if (curWidth == newValue)
+ {
+ return;
+ }
+
+ // Make sure newValue is within legal range.
+ if (newValue < minWidth || newValue > maxWidth)
+ {
+ return;
+ }
+
+ if (!lockAspectRatio)
+ {
+ curWidth = newValue;
+ emit widthChanged(newValue);
+ return;
+ }
+ else if (lockAspectRatio)
+ {
+
+ if (curHeight <= 0)
+ {
+ curWidth = newValue;
+ emit widthChanged(newValue);
+ return;
+ }
+
+ int newHeight = (int) rint((double) aspectRatio * (double) newValue);
+
+ if (newValue != curWidth)
+ {
+ curWidth = newValue;
+ emit widthChanged(newValue);
+ }
+ curHeight = newHeight;
+ emit heightChanged(newHeight);
+ }
+}
+
+void ImageScaler::setHeight(int newValue)
+{
+
+ if (curHeight == newValue)
+ {
+ return;
+ }
+
+ // Make sure newValue is within legal range.
+ if (newValue < minHeight || newValue > maxHeight)
+ {
+ return;
+ }
+
+ // Just change current height if not in locked
+ // aspect ratio mode.
+ if (!lockAspectRatio)
+ {
+ curHeight = newValue;
+ emit heightChanged(newValue);
+ return;
+ }
+ // Handle locked aspect ratio mode.
+ else if (lockAspectRatio)
+ {
+ if (curWidth <= 0)
+ {
+ curHeight = newValue;
+ emit heightChanged(newValue);
+ return;
+ }
+
+ int newWidth = (int) rint( (1.0 /(double) aspectRatio) * (double) newValue);
+
+ if (newValue != curHeight)
+ {
+ curHeight = newValue;
+ emit heightChanged(newValue);
+ }
+ curWidth = newWidth;
+ emit widthChanged(newWidth);
+ }
+}