ImageSaveDialog
authorBrandon King <kingb@caltech.edu>
Thu, 2 Mar 2006 02:10:26 +0000 (02:10 +0000)
committerBrandon King <kingb@caltech.edu>
Thu, 2 Mar 2006 02:10:26 +0000 (02:10 +0000)
mussagl.pro
qui/ImageSaveDialog.cxx [new file with mode: 0644]
qui/ImageSaveDialog.h [new file with mode: 0644]

index 9a6549582a086a9ff8ffc99610cfa3cdf07069fb..2cd8f34db3c3a82a197049c31f15d4443a23bd2e 100644 (file)
@@ -16,6 +16,7 @@ HEADERS += mussa_exceptions.hh \
            qui/PathScene.h \
            qui/ThresholdWidget.h \
            qui/ImageScaler.h \
+           qui/ImageSaveDialog.h \
            alg/flp.hh \
            alg/glsequence.h \
            alg/mussa_class.hh \
@@ -26,6 +27,7 @@ SOURCES += mussagl.cxx \
            qui/PathScene.cxx \
            qui/ThresholdWidget.cxx \
            qui/ImageScaler.cxx \
+           qui/ImageSaveDialog.cxx \
            alg/flp.cxx \
            alg/flp_seqcomp.cxx \
            alg/glsequence.cxx \
diff --git a/qui/ImageSaveDialog.cxx b/qui/ImageSaveDialog.cxx
new file mode 100644 (file)
index 0000000..c7e4d50
--- /dev/null
@@ -0,0 +1,166 @@
+#include <QDialog>
+#include <QFileDialog>
+#include <QGridLayout>
+#include <QLabel>
+#include <QPixmap>
+#include <QPushButton>
+//#include <QVBoxLayout>
+#include <QGLWidget>
+
+#include "ImageSaveDialog.h"
+#include "ImageScaler.h"
+
+#include <iostream>
+
+ImageSaveDialog::ImageSaveDialog(QGLWidget *newglwidget, QWidget *parent)
+  : QDialog(parent)
+{
+  // Init
+  glwidget = newglwidget;
+  //pixmap = 0;
+  imageScaler = new ImageScaler();
+
+  if (glwidget)
+  {
+    QSize tmpSize;
+    tmpSize = glwidget->size();
+    imageScaler->setWidth(tmpSize.width());
+    imageScaler->setHeight(tmpSize.height());
+  }
+  
+  // File Path Label
+  filePathLabel = new QLabel();
+  filePathLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
+  filePathLabel->setMinimumWidth(30);
+
+  // Browse Button
+  browseButton = new QPushButton(tr("Browse..."));
+  connect(browseButton, SIGNAL(clicked()),
+         this, SLOT(promptFileDialog()));
+
+  // Save Button
+  saveButton = new QPushButton(tr("Save"));
+  saveButton->setDefault(true);
+  connect(saveButton, SIGNAL(clicked()),
+         this, SLOT(accept()));
+
+  // Cancel Button
+  cancelButton = new QPushButton(tr("Cancel"));
+  connect(cancelButton, SIGNAL(clicked()),
+         this, SLOT(reject()));
+
+  // Layout
+  QGridLayout *layout = new QGridLayout;
+  layout->addWidget(imageScaler, 0, 0, 1, 0, Qt::AlignCenter);
+  layout->addWidget(filePathLabel, 1, 0);
+  layout->addWidget(browseButton, 1, 1);
+  layout->addWidget(saveButton, 2, 0);
+  layout->addWidget(cancelButton, 2, 1);
+  setLayout(layout);
+}
+
+
+void ImageSaveDialog::accept()
+{
+  std::cout << "Accepted!\n";
+  savePixmap();
+  done(1);
+}
+
+void ImageSaveDialog::reject()
+{
+  std::cout << "Rejected!\n";
+  done(0);
+}
+
+void ImageSaveDialog::setSize(int width, int height)
+{
+  imageScaler->setWidth(width);
+  imageScaler->setHeight(height);
+}
+
+//int ImageSaveDialog::exec()
+//{
+//  if (glwidget)
+//  {
+//    QSize tmpSize;
+//    tmpSize = glwidget->size();
+//    imageScaler->setWidth(tmpSize.width());
+//    imageScaler->setHeight(tmpSize.height());
+//  }
+//  return QDialog::exec();
+//}
+
+
+void ImageSaveDialog::promptFileDialog()
+{
+  QString filePath;
+  filePath = QFileDialog::getSaveFileName(this,
+                                         "Choose a filename to save image",
+                                         ".",
+                                         "Images (*.png *.jpg)");
+  if (filePath.isEmpty())
+    return;
+
+  filePathLabel->setText(filePath);
+}
+
+QSize ImageSaveDialog::getOpenGlPixmapSize()
+{
+  int width = imageScaler->getWidth();
+  int height = imageScaler->getHeight();
+  
+  if (height > 0 && width > 0)
+  {
+    return QSize(width, height);
+  }
+
+  return QSize();
+}
+
+QPixmap ImageSaveDialog::renderOpenGlPixmap()
+{
+  std::cout << "glwidget: " << glwidget << "\n";
+  if (!glwidget)
+    return QPixmap();
+
+  std::cout << "pre get size\n";
+  QSize size = getOpenGlPixmapSize();
+  std::cout << "post get size\n";
+  if (size.isValid()) 
+  {
+    std::cout << "pre get pixmap\n";
+    QPixmap pixmap = glwidget->renderPixmap(size.width(), size.height());
+    std::cout << "post get pixmap\n";
+    return pixmap;    
+  }
+  std::cout << "return empty\n";
+  return QPixmap();
+}
+
+void ImageSaveDialog::savePixmap()
+{
+  QString filePath;
+  filePath = filePathLabel->text();
+  
+  std::cout << "File Path: " << filePath.toStdString() << "\n";
+
+  if (filePath.isEmpty())
+    //FIXME: Include prompt telling user of failure
+    return;
+
+  QPixmap pixmap;
+  std::cout << "Before render pixmap!\n";
+  pixmap = renderOpenGlPixmap();
+  std::cout << "After render pixmap!\n";
+  
+  //Save pixelmap to file!
+  if (filePath.endsWith(".png"))
+    pixmap.save(filePath, "PNG");
+  else if (filePath.endsWith(".jpg"))
+    pixmap.save(filePath, "JPG");
+  else
+    //FIXME: Include prompt telling user of failure
+    return;
+  return;
+}
diff --git a/qui/ImageSaveDialog.h b/qui/ImageSaveDialog.h
new file mode 100644 (file)
index 0000000..beda6ee
--- /dev/null
@@ -0,0 +1,43 @@
+#ifndef _IMAGESAVEDIALOG_H_
+#define _IMAGESAVEDIALOG_H_
+#include <QDialog>
+
+class ImageScaler;
+class QLabel;
+class QPixmap;
+class QPushButton;
+class QGLWidget;
+
+class ImageSaveDialog : public QDialog
+{
+  Q_OBJECT
+
+public:
+  ImageSaveDialog(QGLWidget *qlwidget = 0, QWidget *parent = 0);
+
+public slots:
+  void accept();
+  void reject();
+  //  int exec();
+  void promptFileDialog();
+  void setSize(int width, int height);
+
+signals:
+
+private:
+  void savePixmap();
+  QSize getOpenGlPixmapSize();
+  QPixmap renderOpenGlPixmap();
+
+  ImageScaler *imageScaler;
+  //QPixmap *pixmap;
+  QLabel *filePathLabel;
+  QPushButton *browseButton;
+  QPushButton *saveButton;
+  QPushButton *cancelButton;
+  QGLWidget *glwidget;
+  
+};
+
+#endif