cleanup image save dialog aesthetics
[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 ImageSaveDialog::ImageSaveDialog(QGLWidget *newglwidget, QWidget *parent)
16   : QDialog(parent)
17 {
18   setWindowTitle(tr("Mussa Save Image"));
19
20   // Init
21   glwidget = newglwidget;
22   //pixmap = 0;
23   imageScaler = new ImageScaler();
24
25   if (glwidget)
26   {
27     QSize tmpSize;
28     tmpSize = glwidget->size();
29     imageScaler->setWidth(tmpSize.width());
30     imageScaler->setHeight(tmpSize.height());
31   }
32   
33   // Save Button
34   saveButton = new QPushButton(tr("Save"));
35   saveButton->setDefault(true);
36   connect(saveButton, SIGNAL(clicked()),
37           this, SLOT(accept()));
38
39   // Cancel Button
40   cancelButton = new QPushButton(tr("Cancel"));
41   connect(cancelButton, SIGNAL(clicked()),
42           this, SLOT(reject()));
43
44   // Layout
45   QHBoxLayout *button_layout = new QHBoxLayout;
46   button_layout->addStretch(1);
47   button_layout->addWidget(saveButton);
48   button_layout->addWidget(cancelButton);
49   
50   QHBoxLayout *image_centerer = new QHBoxLayout;
51   image_centerer->addStretch(1);
52   image_centerer->addWidget(imageScaler);
53   image_centerer->addStretch(1);
54   QVBoxLayout *layout = new QVBoxLayout;
55   
56   layout->addStretch(1);
57   layout->addLayout(image_centerer);
58   layout->addStretch(1);
59   layout->addLayout(button_layout);
60   setLayout(layout);
61 }
62
63 void ImageSaveDialog::accept()
64 {
65   bool endDialog = true;
66   savePixmap(endDialog);
67   if (endDialog) {
68     done(1);
69   }
70 }
71
72 void ImageSaveDialog::reject()
73 {
74   done(0);
75 }
76
77 void ImageSaveDialog::setSize(int width, int height)
78 {
79   imageScaler->setWidth(width);
80   imageScaler->setHeight(height);
81 }
82
83 QSize ImageSaveDialog::getOpenGlPixmapSize()
84 {
85   int width = imageScaler->getWidth();
86   int height = imageScaler->getHeight();
87   
88   if (height > 0 && width > 0)
89   {
90     return QSize(width, height);
91   }
92
93   return QSize();
94 }
95
96 QPixmap ImageSaveDialog::renderOpenGlPixmap()
97 {
98   if (!glwidget)
99     return QPixmap();
100
101   
102   QSize size = getOpenGlPixmapSize();
103   if (size.isValid()) 
104   {
105     QPixmap pixmap = glwidget->renderPixmap(size.width(), size.height());
106     return pixmap;    
107   }
108   return QPixmap();
109 }
110
111 void ImageSaveDialog::savePixmap(bool &endDialog)
112 {
113   QString filePath;
114   filePath = QFileDialog::getSaveFileName(this,
115             "Choose a filename to save image",
116             ".",
117             "Images (*.png *.jpg)");  
118
119   if (filePath.isNull()) {
120     // user canceled
121     endDialog = false;;
122   } else if (filePath.isEmpty()) {
123     // somethings wrong
124     endDialog = false;
125   } else {
126     QPixmap pixmap;
127     pixmap = renderOpenGlPixmap();
128   
129     endDialog = true;
130     //Save pixelmap to file!
131     if (filePath.endsWith(".png"))
132       pixmap.save(filePath, "PNG");
133     else if (filePath.endsWith(".jpg"))
134       pixmap.save(filePath, "JPG");
135     else {
136       filePath.append(".png");
137       pixmap.save(filePath, "PNG");
138     }
139   }
140 }