First version of ImageScaler Widget
authorBrandon King <kingb@caltech.edu>
Wed, 1 Mar 2006 22:53:42 +0000 (22:53 +0000)
committerBrandon King <kingb@caltech.edu>
Wed, 1 Mar 2006 22:53:42 +0000 (22:53 +0000)
mussagl.pro
qui/ImageScaler.cxx [new file with mode: 0644]
qui/ImageScaler.h [new file with mode: 0644]

index 33f71fe8fab8445efa410f4661419a20c5c297ac..9a6549582a086a9ff8ffc99610cfa3cdf07069fb 100644 (file)
@@ -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 (file)
index 0000000..db5cac6
--- /dev/null
@@ -0,0 +1,213 @@
+#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);
+  }
+}
diff --git a/qui/ImageScaler.h b/qui/ImageScaler.h
new file mode 100644 (file)
index 0000000..86c2696
--- /dev/null
@@ -0,0 +1,61 @@
+#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