#include <QDialog>
#include <QFileDialog>
-#include <QGridLayout>
+#include <QVBoxLayout>
+#include <QHBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QPushButton>
-//#include <QVBoxLayout>
#include <QGLWidget>
+// Win32 debug includes
+//#include <QMessageBox>
+//#include <sstream>
+//#include <string>
+//#include <QString>
+
#include "ImageSaveDialog.hpp"
#include "ImageScaler.hpp"
#include <iostream>
-ImageSaveDialog::ImageSaveDialog(QGLWidget *newglwidget, QWidget *parent)
- : QDialog(parent)
+#include <boost/filesystem/path.hpp>
+namespace fs = boost::filesystem;
+
+ImageSaveDialog::ImageSaveDialog(boost::shared_ptr<QDir> default_dir_,
+ ScrollableSequenceBrowser *scrollSeqBrowser,
+ QWidget *parent)
+ : QDialog(parent),
+ glwidget(scrollSeqBrowser->browser(), parent),
+ default_dir(default_dir_)
{
- setWindowTitle(tr("Save Mussa Image"));
+ setWindowTitle(tr("Mussa Save Image"));
// Init
- glwidget = newglwidget;
+
//pixmap = 0;
imageScaler = new ImageScaler();
- if (glwidget)
- {
- QSize tmpSize;
- tmpSize = glwidget->size();
- imageScaler->setWidth(tmpSize.width());
- imageScaler->setHeight(tmpSize.height());
- }
+ imageScaler->setWidth(static_cast<int>(glwidget.width()));
+ imageScaler->setHeight(static_cast<int>(glwidget.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);
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);
+ QHBoxLayout *button_layout = new QHBoxLayout;
+ button_layout->addStretch(1);
+ button_layout->addWidget(saveButton);
+ button_layout->addWidget(cancelButton);
+
+ QHBoxLayout *image_centerer = new QHBoxLayout;
+ image_centerer->addStretch(1);
+ image_centerer->addWidget(imageScaler);
+ image_centerer->addStretch(1);
+ QVBoxLayout *layout = new QVBoxLayout;
+
+ layout->addStretch(1);
+ layout->addLayout(image_centerer);
+ layout->addStretch(1);
+ layout->addLayout(button_layout);
setLayout(layout);
+
}
+QDir ImageSaveDialog::defaultDir() const
+{
+ return *default_dir;
+}
void ImageSaveDialog::accept()
{
- std::cout << "Accepted!\n";
- savePixmap();
- done(1);
+ bool endDialog = true;
+ savePixmap(endDialog);
+ if (endDialog) {
+ done(1);
+ }
}
void ImageSaveDialog::reject()
{
- std::cout << "Rejected!\n";
done(0);
}
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();
QPixmap ImageSaveDialog::renderOpenGlPixmap()
{
- if (!glwidget)
- return QPixmap();
-
-
QSize size = getOpenGlPixmapSize();
if (size.isValid())
{
- QPixmap pixmap = glwidget->renderPixmap(size.width(), size.height());
+ double cur_bp_per_pixel = glwidget.zoom();
+ float cur_width = glwidget.width();
+ float cur_height = glwidget.height();
+
+ // Useful debug code (in windows)... hopefully never to be used again. =o)
+ //QString msg = "Zoom: " + QString().setNum(cur_bp_per_pixel) + "\n" +
+ // "Width: " + QString().setNum(cur_width) + "\n" +
+ // "Height: " + QString().setNum(cur_height) + "\n";
+ //
+ //QMessageBox::information(this, "Debug2",
+ // msg );
+ //
+
+ // When the width requested by the user is doubled, the
+ // bp per pixel is halved in order to scale the image properly.
+ double new_bp_per_pixel = ( cur_width / size.width() ) * cur_bp_per_pixel;
+
+ // bp per pixel needs to be scaled based when the width changes
+ glwidget.setZoom(new_bp_per_pixel);
+
+ // draw pixmp
+ QPixmap pixmap = glwidget.renderPixmap(size.width(), size.height());
+
return pixmap;
}
return QPixmap();
}
-void ImageSaveDialog::savePixmap()
+void ImageSaveDialog::savePixmap(bool &endDialog)
{
QString filePath;
- filePath = filePathLabel->text();
-
- if (filePath.isEmpty())
- //FIXME: Include prompt telling user of failure
- return;
-
- QPixmap pixmap;
- pixmap = renderOpenGlPixmap();
+ filePath = QFileDialog::getSaveFileName(this,
+ "Choose a filename to save image",
+ default_dir->absolutePath(),
+ "Images (*.png *.jpg)");
+
+ if (filePath.isNull()) {
+ // user canceled
+ endDialog = false;;
+ } else if (filePath.isEmpty()) {
+ // somethings wrong
+ endDialog = false;
+ } else {
+ QPixmap pixmap;
+ pixmap = renderOpenGlPixmap();
- //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;
+ endDialog = true;
+ //Save pixelmap to file!
+ if (filePath.endsWith(".png"))
+ pixmap.save(filePath, "PNG");
+ else if (filePath.endsWith(".jpg"))
+ pixmap.save(filePath, "JPG");
+ else {
+ filePath.append(".png");
+ pixmap.save(filePath, "PNG");
+ }
+ fs::path path(filePath.toStdString(), fs::native);
+ default_dir->setPath(path.branch_path().native_directory_string().c_str());
+ }
}