--- /dev/null
+#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);
+ }
+}
--- /dev/null
+#ifndef _IMAGESCALER_H_
+#define _IMAGESCALER_H_
+
+#include <QWidget>
+
+class QSpinBox;
+class QLabel;
+class QAction;
+class QCheckBox;
+
+class ImageScaler : public QWidget
+{
+ Q_OBJECT
+
+public:
+ ImageScaler(QWidget *parent = 0);
+
+ int getMaxHeight() const;
+ int getMaxWidth() const;
+ int getWidth() const;
+ int getHeight() const;
+
+public slots:
+ void setMaxWidth(int newValue);
+ void setMaxHeight(int newValue);
+
+ //! Locks apsect ratio with true/false.
+ void setLockAspectRatio(bool state);
+ //! Locks aspect ratio with parameter Qt::Checked or Qt::Unchecked.
+ void setLockAspectRatio(int state);
+
+
+ void setWidth(int newValue);
+ void setHeight(int newValue);
+
+signals:
+ void maxWidthChanged(int newWidth);
+ void maxHeightChanged(int newHeight);
+ void widthChanged(int newWidth);
+ void heightChanged(int newHeight);
+
+private:
+ QSpinBox *widthBox;
+ QSpinBox *heightBox;
+ QLabel *widthLabel;
+ QLabel *heightLabel;
+ QAction *lockAspectAction;
+ QCheckBox *lockButton;
+
+ bool lockAspectRatio;
+ float aspectRatio;
+ int maxWidth;
+ int minWidth;
+ int curWidth;
+ int maxHeight;
+ int minHeight;
+ int curHeight;
+
+};
+
+#endif