Developer Documentation
SnapshotDialog.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 #include "SnapshotDialog.hh"
46 
47 #include <QMessageBox>
48 #include <QFileDialog>
49 
50 SnapshotDialog::SnapshotDialog(QString _suggest, bool _captureViewers, int _w, int _h, QWidget *parent)
51  : QDialog(parent),
52  captureViewers_(_captureViewers),
53  aspect_((double)_w / (double)_h),
54  blockSpinBox_(false)
55 {
56  setupUi(this);
57 
58  filename->setText( _suggest );
59 
60  // Disable 'change resolution' button if
61  // in viewer snapshot mode
62  multisampling->setChecked(captureViewers_);
63  changeRes_wdgt->setVisible(!captureViewers_);
64  changeRes_pb->setEnabled(!captureViewers_);
65  transparent->setDisabled(!captureViewers_);
66  hideCoordsys->setDisabled(!captureViewers_);
67  multisampling->setDisabled(!captureViewers_);
68  num_samples->setDisabled(!captureViewers_);
69 
70  snapWidth->setValue(_w);
71  snapHeight->setValue(_h);
72 
73  warning_lb->setText("");
74 
75  // Load button states
76  loadStates();
77 
78  if (keepAspect->isChecked()) {
79  snapHeight->setValue((int)((double)snapWidth->value() / aspect_));
80  }
81 
82  connect(snapWidth, SIGNAL(valueChanged(int)), this, SLOT(snapWidthChanged(int)) );
83  connect(snapHeight, SIGNAL(valueChanged(int)), this, SLOT(snapHeightChanged(int)) );
84  connect(keepAspect, SIGNAL(stateChanged(int)), this, SLOT(keepAspectChanged()) );
85  connect(multisampling, SIGNAL(stateChanged(int)), this, SLOT(multisampleChanged()) );
86 
87  connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()) );
88  connect(findButton, SIGNAL(clicked()), this, SLOT(findFile()) );
89  connect(changeRes_pb, SIGNAL(clicked()), this, SLOT(slotChangeResolution()) );
90  connect(okButton, SIGNAL(clicked()), this, SLOT(slotOk()) );
91 
92  connect(filename, SIGNAL(textChanged(const QString &)), this, SLOT(filenameChanged(const QString &)));
93 }
94 
95 void SnapshotDialog::saveStates() {
96 
97  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/SnapWidth", snapWidth->value());
98  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/SnapHeight", snapHeight->value());
99  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/KeepAspect", keepAspect->isChecked());
100  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/Transparent", transparent->isChecked());
101  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/HideCoordsys", hideCoordsys->isChecked());
102  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/Multisampling", multisampling->isChecked());
103  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/NumSamples", num_samples->value());
104 }
105 
106 void SnapshotDialog::loadStates() {
107 
108  snapWidth->setValue( OpenFlipperSettings().value( "Viewer/SnapshotDialog/SnapWidth", snapWidth->value()).toInt());
109  snapHeight->setValue( OpenFlipperSettings().value( "Viewer/SnapshotDialog/SnapHeight", snapHeight->value()).toInt());
110  keepAspect->setChecked( OpenFlipperSettings().value( "Viewer/SnapshotDialog/KeepAspect", false).toBool());
111  transparent->setChecked( OpenFlipperSettings().value( "Viewer/SnapshotDialog/Transparent", false).toBool());
112  hideCoordsys->setChecked( OpenFlipperSettings().value( "Viewer/SnapshotDialog/HideCoordsys", false).toBool());
113  multisampling->setChecked( OpenFlipperSettings().value( "Viewer/SnapshotDialog/Multisampling", true).toBool());
114  num_samples->setValue( OpenFlipperSettings().value( "Viewer/SnapshotDialog/NumSamples", 16).toInt());
115 }
116 
117 void SnapshotDialog::snapWidthChanged(int _w) {
118 
119  if(blockSpinBox_) return;
120 
121  if(keepAspect->isChecked()) {
122  blockSpinBox_ = true;
123  snapHeight->setValue((int)((double)_w / aspect_));
124  blockSpinBox_ = false;
125  }
126 }
127 
128 void SnapshotDialog::snapHeightChanged(int _h) {
129 
130  if(blockSpinBox_) return;
131 
132  if(keepAspect->isChecked()) {
133  blockSpinBox_ = true;
134  snapWidth->setValue((int)((double)_h * aspect_));
135  blockSpinBox_ = false;
136  }
137 }
138 
139 void SnapshotDialog::keepAspectChanged() {
140 
141  if(keepAspect->isChecked()) {
142  blockSpinBox_ = true;
143  snapHeight->setValue((int)((double)snapWidth->value() / aspect_));
144  blockSpinBox_ = false;
145  }
146 }
147 
148 void SnapshotDialog::multisampleChanged() {
149  num_samples->setDisabled (!multisampling->isChecked());
150 }
151 void SnapshotDialog::slotChangeResolution()
152 {
153  if ( !captureViewers_ )
154  emit resizeApplication(snapWidth->value(), snapHeight->value());
155 }
156 
157 void SnapshotDialog::slotOk()
158 {
159 
160  if (filename->text() == ""){
161  QMessageBox msgBox;
162  msgBox.setText(tr("The Filename is empty!"));
163  msgBox.exec();
164  return;
165  }
166 
167  if ( !captureViewers_ )
168  emit resizeApplication(snapWidth->value(), snapHeight->value());
169 
170  // Remember button states for next time...
171  saveStates();
172 
173  accept();
174 }
175 
176 void SnapshotDialog::findFile()
177 {
178 
179  QFileInfo fi( filename->text() );
180 
181  QFileDialog dialog(this);
182  dialog.setFileMode(QFileDialog::AnyFile);
183  dialog.setDefaultSuffix("png");
184  dialog.setNameFilter(tr("Images (*.png *.ppm *.jpg)"));
185  dialog.setFileMode(QFileDialog::AnyFile);
186  dialog.setOption(QFileDialog::DontConfirmOverwrite, false);
187  dialog.setDirectory( fi.path() );
188  dialog.selectFile( filename->text() );
189  dialog.setAcceptMode(QFileDialog::AcceptSave);
190  dialog.setWindowTitle(tr("Save Snapshot"));
191 
192  bool ok = dialog.exec();
193 
194  if (ok)
195  filename->setText( dialog.selectedFiles()[0] );
196 }
197 
198 void SnapshotDialog::filenameChanged(const QString &new_filename) {
199  QFileInfo fi(new_filename);
200  if (!QFileInfo(fi.path()).isWritable()) {
201  static const char *style = "background: #ffffcc;";
202  filename->setStyleSheet(style);
203  warning_lb->setText(tr("Warning: Folder not writable."));
204  } else if (fi.exists()) {
205  static const char *style = "background: #ffcccc;";
206  filename->setStyleSheet(style);
207  warning_lb->setText(tr("Warning: File exists and will be "
208  "overwritten without further warning."));
209  } else {
210  static const char *style = "";
211  filename->setStyleSheet(style);
212  warning_lb->setText("");
213  }
214 }
void setValue(const QString &key, const QVariant &value)
Wrapper function which makes it possible to enable Debugging output with -DOPENFLIPPER_SETTINGS_DEBUG...
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.