From 20f13eee21503cc29eb6b8c02d76a07349e2dd1e Mon Sep 17 00:00:00 2001 From: Brandon King Date: Wed, 1 Mar 2006 22:53:42 +0000 Subject: [PATCH] First version of ImageScaler Widget --- mussagl.pro | 2 + qui/ImageScaler.cxx | 213 ++++++++++++++++++++++++++++++++++++++++++++ qui/ImageScaler.h | 61 +++++++++++++ 3 files changed, 276 insertions(+) create mode 100644 qui/ImageScaler.cxx create mode 100644 qui/ImageScaler.h diff --git a/mussagl.pro b/mussagl.pro index 33f71fe..9a65495 100644 --- a/mussagl.pro +++ b/mussagl.pro @@ -15,6 +15,7 @@ HEADERS += mussa_exceptions.hh \ qui/PathWindow.h \ qui/PathScene.h \ qui/ThresholdWidget.h \ + qui/ImageScaler.h \ alg/flp.hh \ alg/glsequence.h \ alg/mussa_class.hh \ @@ -24,6 +25,7 @@ SOURCES += mussagl.cxx \ qui/PathWindow.cxx \ qui/PathScene.cxx \ qui/ThresholdWidget.cxx \ + qui/ImageScaler.cxx \ alg/flp.cxx \ alg/flp_seqcomp.cxx \ alg/glsequence.cxx \ diff --git a/qui/ImageScaler.cxx b/qui/ImageScaler.cxx new file mode 100644 index 0000000..db5cac6 --- /dev/null +++ b/qui/ImageScaler.cxx @@ -0,0 +1,213 @@ +#include +#include +#include +#include +#include + +//#include +#include + +#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); + } +} diff --git a/qui/ImageScaler.h b/qui/ImageScaler.h new file mode 100644 index 0000000..86c2696 --- /dev/null +++ b/qui/ImageScaler.h @@ -0,0 +1,61 @@ +#ifndef _IMAGESCALER_H_ +#define _IMAGESCALER_H_ + +#include + +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 -- 2.30.2