10 // Win32 debug includes
11 //#include <QMessageBox>
16 #include "ImageSaveDialog.hpp"
17 #include "ImageScaler.hpp"
21 #include <boost/filesystem/path.hpp>
22 namespace fs = boost::filesystem;
24 ImageSaveDialog::ImageSaveDialog(boost::shared_ptr<QDir> default_dir_,
25 ScrollableSequenceBrowser *scrollSeqBrowser,
28 glwidget(scrollSeqBrowser->browser(), parent),
29 default_dir(default_dir_)
31 setWindowTitle(tr("Mussa Save Image"));
36 imageScaler = new ImageScaler();
38 imageScaler->setWidth(static_cast<int>(glwidget.width()));
39 imageScaler->setHeight(static_cast<int>(glwidget.height()));
42 saveButton = new QPushButton(tr("Save"));
43 saveButton->setDefault(true);
44 connect(saveButton, SIGNAL(clicked()),
45 this, SLOT(accept()));
48 cancelButton = new QPushButton(tr("Cancel"));
49 connect(cancelButton, SIGNAL(clicked()),
50 this, SLOT(reject()));
53 QHBoxLayout *button_layout = new QHBoxLayout;
54 button_layout->addStretch(1);
55 button_layout->addWidget(saveButton);
56 button_layout->addWidget(cancelButton);
58 QHBoxLayout *image_centerer = new QHBoxLayout;
59 image_centerer->addStretch(1);
60 image_centerer->addWidget(imageScaler);
61 image_centerer->addStretch(1);
62 QVBoxLayout *layout = new QVBoxLayout;
64 layout->addStretch(1);
65 layout->addLayout(image_centerer);
66 layout->addStretch(1);
67 layout->addLayout(button_layout);
72 QDir ImageSaveDialog::defaultDir() const
77 void ImageSaveDialog::accept()
79 bool endDialog = true;
80 savePixmap(endDialog);
86 void ImageSaveDialog::reject()
91 void ImageSaveDialog::setSize(int width, int height)
93 imageScaler->setWidth(width);
94 imageScaler->setHeight(height);
97 QSize ImageSaveDialog::getOpenGlPixmapSize()
99 int width = imageScaler->getWidth();
100 int height = imageScaler->getHeight();
102 if (height > 0 && width > 0)
104 return QSize(width, height);
110 QPixmap ImageSaveDialog::renderOpenGlPixmap()
112 QSize size = getOpenGlPixmapSize();
115 double cur_bp_per_pixel = glwidget.zoom();
116 float cur_width = glwidget.width();
117 float cur_height = glwidget.height();
119 // Useful debug code (in windows)... hopefully never to be used again. =o)
120 //QString msg = "Zoom: " + QString().setNum(cur_bp_per_pixel) + "\n" +
121 // "Width: " + QString().setNum(cur_width) + "\n" +
122 // "Height: " + QString().setNum(cur_height) + "\n";
124 //QMessageBox::information(this, "Debug2",
128 // When the width requested by the user is doubled, the
129 // bp per pixel is halved in order to scale the image properly.
130 double new_bp_per_pixel = ( cur_width / size.width() ) * cur_bp_per_pixel;
132 // bp per pixel needs to be scaled based when the width changes
133 glwidget.setZoom(new_bp_per_pixel);
136 QPixmap pixmap = glwidget.renderPixmap(size.width(), size.height());
143 void ImageSaveDialog::savePixmap(bool &endDialog)
146 filePath = QFileDialog::getSaveFileName(this,
147 "Choose a filename to save image",
148 default_dir->absolutePath(),
149 "Images (*.png *.jpg)");
151 if (filePath.isNull()) {
154 } else if (filePath.isEmpty()) {
159 pixmap = renderOpenGlPixmap();
162 //Save pixelmap to file!
163 if (filePath.endsWith(".png"))
164 pixmap.save(filePath, "PNG");
165 else if (filePath.endsWith(".jpg"))
166 pixmap.save(filePath, "JPG");
168 filePath.append(".png");
169 pixmap.save(filePath, "PNG");
171 fs::path path(filePath.toStdString(), fs::native);
172 default_dir->setPath(path.branch_path().native_directory_string().c_str());