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