Update mussa to build on ubuntu 10.04 with qt 4.6.2 +boost 1.40.0.1
[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 // Win32 debug includes
11 //#include <QMessageBox>
12 //#include <sstream>
13 //#include <string>
14 //#include <QString>
15
16 #include "ImageSaveDialog.hpp"
17 #include "ImageScaler.hpp"
18
19 #include <iostream>
20
21 #include <boost/filesystem/path.hpp>
22 namespace fs = boost::filesystem;
23
24 ImageSaveDialog::ImageSaveDialog(boost::shared_ptr<QDir> default_dir_,
25                                  ScrollableSequenceBrowser *scrollSeqBrowser, 
26                                  QWidget *parent)
27   : QDialog(parent),
28     glwidget(scrollSeqBrowser->browser(), parent),
29     default_dir(default_dir_)
30 {
31   setWindowTitle(tr("Mussa Save Image"));
32
33   // Init
34
35   //pixmap = 0;
36   imageScaler = new ImageScaler();
37
38   imageScaler->setWidth(static_cast<int>(glwidget.width()));
39   imageScaler->setHeight(static_cast<int>(glwidget.height()));
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   QHBoxLayout *button_layout = new QHBoxLayout;
54   button_layout->addStretch(1);
55   button_layout->addWidget(saveButton);
56   button_layout->addWidget(cancelButton);
57   
58   QHBoxLayout *image_centerer = new QHBoxLayout;
59   image_centerer->addStretch(1);
60   image_centerer->addWidget(imageScaler);
61   image_centerer->addStretch(1);
62   QVBoxLayout *layout = new QVBoxLayout;
63   
64   layout->addStretch(1);
65   layout->addLayout(image_centerer);
66   layout->addStretch(1);
67   layout->addLayout(button_layout);
68   setLayout(layout);
69
70 }
71
72 QDir ImageSaveDialog::defaultDir() const
73 {
74   return *default_dir;
75 }
76
77 void ImageSaveDialog::accept()
78 {
79   bool endDialog = true;
80   savePixmap(endDialog);
81   if (endDialog) {
82     done(1);
83   }
84 }
85
86 void ImageSaveDialog::reject()
87 {
88   done(0);
89 }
90
91 void ImageSaveDialog::setSize(int width, int height)
92 {
93   imageScaler->setWidth(width);
94   imageScaler->setHeight(height);
95 }
96
97 QSize ImageSaveDialog::getOpenGlPixmapSize()
98 {
99   int width = imageScaler->getWidth();
100   int height = imageScaler->getHeight();
101   
102   if (height > 0 && width > 0)
103   {
104     return QSize(width, height);
105   }
106
107   return QSize();
108 }
109
110 QPixmap ImageSaveDialog::renderOpenGlPixmap()
111 {
112   QSize size = getOpenGlPixmapSize();
113   if (size.isValid()) 
114   {
115     double cur_bp_per_pixel = glwidget.zoom();
116     float cur_width = glwidget.width();
117     float cur_height = glwidget.height();
118     
119     // Useful debug code (in windows)... hopefully never to be used again. =o)
120     //QString msg = "Zoom:   " + QString().setNum(cur_bp_per_pixel) + "\n" + 
121     //  "Width:  " + QString().setNum(cur_width) + "\n" +
122     //  "Height: " + QString().setNum(cur_height) + "\n";
123     //
124     //QMessageBox::information(this, "Debug2", 
125     //                       msg );
126     //
127
128     // When the width requested by the user is doubled, the 
129     // bp per pixel is halved in order to scale the image properly.
130     double new_bp_per_pixel = ( cur_width / size.width() ) * cur_bp_per_pixel;
131
132     // bp per pixel needs to be scaled based when the width changes
133     glwidget.setZoom(new_bp_per_pixel);
134
135     // draw pixmp
136     QPixmap pixmap = glwidget.renderPixmap(size.width(), size.height());
137
138     return pixmap;    
139   }
140   return QPixmap();
141 }
142
143 void ImageSaveDialog::savePixmap(bool &endDialog)
144 {
145   QString filePath;
146   filePath = QFileDialog::getSaveFileName(this,
147             "Choose a filename to save image",
148             default_dir->absolutePath(),
149             "Images (*.png *.jpg)");  
150
151   if (filePath.isNull()) {
152     // user canceled
153     endDialog = false;;
154   } else if (filePath.isEmpty()) {
155     // somethings wrong
156     endDialog = false;
157   } else {
158     QPixmap pixmap;
159     pixmap = renderOpenGlPixmap();
160   
161     endDialog = true;
162     //Save pixelmap to file!
163     if (filePath.endsWith(".png"))
164       pixmap.save(filePath, "PNG");
165     else if (filePath.endsWith(".jpg"))
166       pixmap.save(filePath, "JPG");
167     else {
168       filePath.append(".png");
169       pixmap.save(filePath, "PNG");
170     }
171     fs::path path(filePath.toStdString(), fs::native);
172     default_dir->setPath(path.branch_path().native_directory_string().c_str());
173   }
174 }