10 #include "ImageSaveDialog.hpp"
11 #include "ImageScaler.hpp"
15 #include <boost/filesystem/path.hpp>
16 namespace fs = boost::filesystem;
18 ImageSaveDialog::ImageSaveDialog(boost::shared_ptr<QDir> default_dir_,
19 QGLWidget *newglwidget,
22 default_dir(default_dir_)
24 setWindowTitle(tr("Mussa Save Image"));
27 glwidget = newglwidget;
29 imageScaler = new ImageScaler();
34 tmpSize = glwidget->size();
35 imageScaler->setWidth(tmpSize.width());
36 imageScaler->setHeight(tmpSize.height());
40 saveButton = new QPushButton(tr("Save"));
41 saveButton->setDefault(true);
42 connect(saveButton, SIGNAL(clicked()),
43 this, SLOT(accept()));
46 cancelButton = new QPushButton(tr("Cancel"));
47 connect(cancelButton, SIGNAL(clicked()),
48 this, SLOT(reject()));
51 QHBoxLayout *button_layout = new QHBoxLayout;
52 button_layout->addStretch(1);
53 button_layout->addWidget(saveButton);
54 button_layout->addWidget(cancelButton);
56 QHBoxLayout *image_centerer = new QHBoxLayout;
57 image_centerer->addStretch(1);
58 image_centerer->addWidget(imageScaler);
59 image_centerer->addStretch(1);
60 QVBoxLayout *layout = new QVBoxLayout;
62 layout->addStretch(1);
63 layout->addLayout(image_centerer);
64 layout->addStretch(1);
65 layout->addLayout(button_layout);
69 QDir ImageSaveDialog::defaultDir() const
74 void ImageSaveDialog::accept()
76 bool endDialog = true;
77 savePixmap(endDialog);
83 void ImageSaveDialog::reject()
88 void ImageSaveDialog::setSize(int width, int height)
90 imageScaler->setWidth(width);
91 imageScaler->setHeight(height);
94 QSize ImageSaveDialog::getOpenGlPixmapSize()
96 int width = imageScaler->getWidth();
97 int height = imageScaler->getHeight();
99 if (height > 0 && width > 0)
101 return QSize(width, height);
107 QPixmap ImageSaveDialog::renderOpenGlPixmap()
113 QSize size = getOpenGlPixmapSize();
116 QPixmap pixmap = glwidget->renderPixmap(size.width(), size.height());
122 void ImageSaveDialog::savePixmap(bool &endDialog)
125 filePath = QFileDialog::getSaveFileName(this,
126 "Choose a filename to save image",
127 default_dir->absolutePath(),
128 "Images (*.png *.jpg)");
130 if (filePath.isNull()) {
133 } else if (filePath.isEmpty()) {
138 pixmap = renderOpenGlPixmap();
141 //Save pixelmap to file!
142 if (filePath.endsWith(".png"))
143 pixmap.save(filePath, "PNG");
144 else if (filePath.endsWith(".jpg"))
145 pixmap.save(filePath, "JPG");
147 filePath.append(".png");
148 pixmap.save(filePath, "PNG");
150 fs::path path(filePath.toStdString(), fs::native);
151 default_dir->setPath(path.branch_path().native_directory_string().c_str());