From cb8b9c7f5e9ce34c40f6616ec79ff06874327769 Mon Sep 17 00:00:00 2001 From: Brandon King Date: Thu, 2 Mar 2006 02:10:26 +0000 Subject: [PATCH] ImageSaveDialog --- mussagl.pro | 2 + qui/ImageSaveDialog.cxx | 166 ++++++++++++++++++++++++++++++++++++++++ qui/ImageSaveDialog.h | 43 +++++++++++ 3 files changed, 211 insertions(+) create mode 100644 qui/ImageSaveDialog.cxx create mode 100644 qui/ImageSaveDialog.h diff --git a/mussagl.pro b/mussagl.pro index 9a65495..2cd8f34 100644 --- a/mussagl.pro +++ b/mussagl.pro @@ -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 index 0000000..c7e4d50 --- /dev/null +++ b/qui/ImageSaveDialog.cxx @@ -0,0 +1,166 @@ +#include +#include +#include +#include +#include +#include +//#include +#include + +#include "ImageSaveDialog.h" +#include "ImageScaler.h" + +#include + +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 index 0000000..beda6ee --- /dev/null +++ b/qui/ImageSaveDialog.h @@ -0,0 +1,43 @@ +#ifndef _IMAGESAVEDIALOG_H_ +#define _IMAGESAVEDIALOG_H_ + +#include + +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 -- 2.30.2