Death to stupid mussa pointers
[mussa.git] / qui / ImageSaveDialog.cpp
1 #include <QDialog>
2 #include <QFileDialog>
3 #include <QGridLayout>
4 #include <QLabel>
5 #include <QPixmap>
6 #include <QPushButton>
7 //#include <QVBoxLayout>
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("Save Mussa 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   // File Path Label
34   filePathLabel = new QLabel();
35   filePathLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
36   filePathLabel->setMinimumWidth(30);
37
38   // Browse Button
39   browseButton = new QPushButton(tr("Browse..."));
40   connect(browseButton, SIGNAL(clicked()),
41           this, SLOT(promptFileDialog()));
42
43   // Save Button
44   saveButton = new QPushButton(tr("Save"));
45   saveButton->setDefault(true);
46   connect(saveButton, SIGNAL(clicked()),
47           this, SLOT(accept()));
48
49   // Cancel Button
50   cancelButton = new QPushButton(tr("Cancel"));
51   connect(cancelButton, SIGNAL(clicked()),
52           this, SLOT(reject()));
53
54   // Layout
55   QGridLayout *layout = new QGridLayout;
56   layout->addWidget(imageScaler, 0, 0, 1, 0, Qt::AlignCenter);
57   layout->addWidget(filePathLabel, 1, 0);
58   layout->addWidget(browseButton, 1, 1);
59   layout->addWidget(saveButton, 2, 0);
60   layout->addWidget(cancelButton, 2, 1);
61   setLayout(layout);
62 }
63
64
65 void ImageSaveDialog::accept()
66 {
67   std::cout << "Accepted!\n";
68   savePixmap();
69   done(1);
70 }
71
72 void ImageSaveDialog::reject()
73 {
74   std::cout << "Rejected!\n";
75   done(0);
76 }
77
78 void ImageSaveDialog::setSize(int width, int height)
79 {
80   imageScaler->setWidth(width);
81   imageScaler->setHeight(height);
82 }
83
84 //int ImageSaveDialog::exec()
85 //{
86 //  if (glwidget)
87 //  {
88 //    QSize tmpSize;
89 //    tmpSize = glwidget->size();
90 //    imageScaler->setWidth(tmpSize.width());
91 //    imageScaler->setHeight(tmpSize.height());
92 //  }
93 //  return QDialog::exec();
94 //}
95
96
97 void ImageSaveDialog::promptFileDialog()
98 {
99   QString filePath;
100   filePath = QFileDialog::getSaveFileName(this,
101                                           "Choose a filename to save image",
102                                           ".",
103                                           "Images (*.png *.jpg)");
104   if (filePath.isEmpty())
105     return;
106
107   filePathLabel->setText(filePath);
108 }
109
110 QSize ImageSaveDialog::getOpenGlPixmapSize()
111 {
112   int width = imageScaler->getWidth();
113   int height = imageScaler->getHeight();
114   
115   if (height > 0 && width > 0)
116   {
117     return QSize(width, height);
118   }
119
120   return QSize();
121 }
122
123 QPixmap ImageSaveDialog::renderOpenGlPixmap()
124 {
125   if (!glwidget)
126     return QPixmap();
127
128   
129   QSize size = getOpenGlPixmapSize();
130   if (size.isValid()) 
131   {
132     QPixmap pixmap = glwidget->renderPixmap(size.width(), size.height());
133     return pixmap;    
134   }
135   return QPixmap();
136 }
137
138 void ImageSaveDialog::savePixmap()
139 {
140   QString filePath;
141   filePath = filePathLabel->text();
142   
143   if (filePath.isEmpty())
144     //FIXME: Include prompt telling user of failure
145     return;
146
147   QPixmap pixmap;
148   pixmap = renderOpenGlPixmap();
149   
150   //Save pixelmap to file!
151   if (filePath.endsWith(".png"))
152     pixmap.save(filePath, "PNG");
153   else if (filePath.endsWith(".jpg"))
154     pixmap.save(filePath, "JPG");
155   else
156     //FIXME: Include prompt telling user of failure
157     return;
158   return;
159 }