pass default_dir around
[mussa.git] / qui / ImageSaveDialog.cpp
1 #include <QDialog>
2 #include <QFileDialog>
3 #include <QVBoxLayout>
4 #include <QHBoxLayout>
5 #include <QLabel>
6 #include <QPixmap>
7 #include <QPushButton>
8 #include <QGLWidget>
9
10 #include "ImageSaveDialog.hpp"
11 #include "ImageScaler.hpp"
12
13 #include <iostream>
14
15 #include <boost/filesystem/path.hpp>
16 namespace fs = boost::filesystem;
17
18 ImageSaveDialog::ImageSaveDialog(boost::shared_ptr<QDir> default_dir_,
19                                  QGLWidget *newglwidget, 
20                                  QWidget *parent)
21   : QDialog(parent),
22     default_dir(default_dir_)
23 {
24   setWindowTitle(tr("Mussa Save Image"));
25
26   // Init
27   glwidget = newglwidget;
28   //pixmap = 0;
29   imageScaler = new ImageScaler();
30
31   if (glwidget)
32   {
33     QSize tmpSize;
34     tmpSize = glwidget->size();
35     imageScaler->setWidth(tmpSize.width());
36     imageScaler->setHeight(tmpSize.height());
37   }
38   
39   // Save Button
40   saveButton = new QPushButton(tr("Save"));
41   saveButton->setDefault(true);
42   connect(saveButton, SIGNAL(clicked()),
43           this, SLOT(accept()));
44
45   // Cancel Button
46   cancelButton = new QPushButton(tr("Cancel"));
47   connect(cancelButton, SIGNAL(clicked()),
48           this, SLOT(reject()));
49
50   // Layout
51   QHBoxLayout *button_layout = new QHBoxLayout;
52   button_layout->addStretch(1);
53   button_layout->addWidget(saveButton);
54   button_layout->addWidget(cancelButton);
55   
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;
61   
62   layout->addStretch(1);
63   layout->addLayout(image_centerer);
64   layout->addStretch(1);
65   layout->addLayout(button_layout);
66   setLayout(layout);
67 }
68
69 QDir ImageSaveDialog::defaultDir() const
70 {
71   return *default_dir;
72 }
73
74 void ImageSaveDialog::accept()
75 {
76   bool endDialog = true;
77   savePixmap(endDialog);
78   if (endDialog) {
79     done(1);
80   }
81 }
82
83 void ImageSaveDialog::reject()
84 {
85   done(0);
86 }
87
88 void ImageSaveDialog::setSize(int width, int height)
89 {
90   imageScaler->setWidth(width);
91   imageScaler->setHeight(height);
92 }
93
94 QSize ImageSaveDialog::getOpenGlPixmapSize()
95 {
96   int width = imageScaler->getWidth();
97   int height = imageScaler->getHeight();
98   
99   if (height > 0 && width > 0)
100   {
101     return QSize(width, height);
102   }
103
104   return QSize();
105 }
106
107 QPixmap ImageSaveDialog::renderOpenGlPixmap()
108 {
109   if (!glwidget)
110     return QPixmap();
111
112   
113   QSize size = getOpenGlPixmapSize();
114   if (size.isValid()) 
115   {
116     QPixmap pixmap = glwidget->renderPixmap(size.width(), size.height());
117     return pixmap;    
118   }
119   return QPixmap();
120 }
121
122 void ImageSaveDialog::savePixmap(bool &endDialog)
123 {
124   QString filePath;
125   filePath = QFileDialog::getSaveFileName(this,
126             "Choose a filename to save image",
127             default_dir->absolutePath(),
128             "Images (*.png *.jpg)");  
129
130   if (filePath.isNull()) {
131     // user canceled
132     endDialog = false;;
133   } else if (filePath.isEmpty()) {
134     // somethings wrong
135     endDialog = false;
136   } else {
137     QPixmap pixmap;
138     pixmap = renderOpenGlPixmap();
139   
140     endDialog = true;
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");
146     else {
147       filePath.append(".png");
148       pixmap.save(filePath, "PNG");
149     }
150     fs::path path(filePath.toStdString(), fs::native);
151     default_dir->setPath(path.branch_path().native_directory_string().c_str());
152   }
153 }