Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #include "SnapshotDialog.hh"
52 
53 SnapshotDialog::SnapshotDialog(QString _suggest, bool _captureViewers, int _w, int _h, QWidget *parent)
54  : QDialog(parent),
55  captureViewers_(_captureViewers),
56  aspect_((double)_w / (double)_h),
57  blockSpinBox_(false)
58 {
59  setupUi(this);
60 
61  filename->setText( _suggest );
62 
63  // Disable 'change resolution' button if
64  // in viewer snapshot mode
65  multisampling->setChecked(captureViewers_);
66  changeRes_wdgt->setVisible(!captureViewers_);
67  changeRes_pb->setEnabled(!captureViewers_);
68  transparent->setDisabled(!captureViewers_);
69  hideCoordsys->setDisabled(!captureViewers_);
70  multisampling->setDisabled(!captureViewers_);
71  num_samples->setDisabled(!captureViewers_);
72 
73  snapWidth->setValue(_w);
74  snapHeight->setValue(_h);
75 
76  warning_lb->setText("");
77 
78  // Load button states
79  loadStates();
80 
81  if (keepAspect->isChecked()) {
82  snapHeight->setValue((int)((double)snapWidth->value() / aspect_));
83  }
84 
85  connect(snapWidth, SIGNAL(valueChanged(int)), this, SLOT(snapWidthChanged(int)) );
86  connect(snapHeight, SIGNAL(valueChanged(int)), this, SLOT(snapHeightChanged(int)) );
87  connect(keepAspect, SIGNAL(stateChanged(int)), this, SLOT(keepAspectChanged()) );
88  connect(multisampling, SIGNAL(stateChanged(int)), this, SLOT(multisampleChanged()) );
89 
90  connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()) );
91  connect(findButton, SIGNAL(clicked()), this, SLOT(findFile()) );
92  connect(changeRes_pb, SIGNAL(clicked()), this, SLOT(slotChangeResolution()) );
93  connect(okButton, SIGNAL(clicked()), this, SLOT(slotOk()) );
94 
95  connect(filename, SIGNAL(textChanged(const QString &)), this, SLOT(filenameChanged(const QString &)));
96 }
97 
98 void SnapshotDialog::saveStates() {
99 
100  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/SnapWidth", snapWidth->value());
101  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/SnapHeight", snapHeight->value());
102  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/KeepAspect", keepAspect->isChecked());
103  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/Transparent", transparent->isChecked());
104  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/HideCoordsys", hideCoordsys->isChecked());
105  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/Multisampling", multisampling->isChecked());
106  OpenFlipperSettings().setValue( "Viewer/SnapshotDialog/NumSamples", num_samples->value());
107 }
108 
109 void SnapshotDialog::loadStates() {
110 
111  snapWidth->setValue( OpenFlipperSettings().value( "Viewer/SnapshotDialog/SnapWidth", snapWidth->value()).toInt());
112  snapHeight->setValue( OpenFlipperSettings().value( "Viewer/SnapshotDialog/SnapHeight", snapHeight->value()).toInt());
113  keepAspect->setChecked( OpenFlipperSettings().value( "Viewer/SnapshotDialog/KeepAspect", false).toBool());
114  transparent->setChecked( OpenFlipperSettings().value( "Viewer/SnapshotDialog/Transparent", false).toBool());
115  hideCoordsys->setChecked( OpenFlipperSettings().value( "Viewer/SnapshotDialog/HideCoordsys", false).toBool());
116  multisampling->setChecked( OpenFlipperSettings().value( "Viewer/SnapshotDialog/Multisampling", true).toBool());
117  num_samples->setValue( OpenFlipperSettings().value( "Viewer/SnapshotDialog/NumSamples", 16).toInt());
118 }
119 
120 void SnapshotDialog::snapWidthChanged(int _w) {
121 
122  if(blockSpinBox_) return;
123 
124  if(keepAspect->isChecked()) {
125  blockSpinBox_ = true;
126  snapHeight->setValue((int)((double)_w / aspect_));
127  blockSpinBox_ = false;
128  }
129 }
130 
131 void SnapshotDialog::snapHeightChanged(int _h) {
132 
133  if(blockSpinBox_) return;
134 
135  if(keepAspect->isChecked()) {
136  blockSpinBox_ = true;
137  snapWidth->setValue((int)((double)_h * aspect_));
138  blockSpinBox_ = false;
139  }
140 }
141 
142 void SnapshotDialog::keepAspectChanged() {
143 
144  if(keepAspect->isChecked()) {
145  blockSpinBox_ = true;
146  snapHeight->setValue((int)((double)snapWidth->value() / aspect_));
147  blockSpinBox_ = false;
148  }
149 }
150 
151 void SnapshotDialog::multisampleChanged() {
152  num_samples->setDisabled (!multisampling->isChecked());
153 }
154 void SnapshotDialog::slotChangeResolution()
155 {
156  if ( !captureViewers_ )
157  emit resizeApplication(snapWidth->value(), snapHeight->value());
158 }
159 
160 void SnapshotDialog::slotOk()
161 {
162 
163  if (filename->text() == ""){
164  QMessageBox msgBox;
165  msgBox.setText(tr("The Filename is empty!"));
166  msgBox.exec();
167  return;
168  }
169 
170  if ( !captureViewers_ )
171  emit resizeApplication(snapWidth->value(), snapHeight->value());
172 
173  // Remember button states for next time...
174  saveStates();
175 
176  accept();
177 }
178 
179 void SnapshotDialog::findFile()
180 {
181 
182  QFileInfo fi( filename->text() );
183 
184  QFileDialog dialog(this);
185  dialog.setFileMode(QFileDialog::AnyFile);
186  dialog.setDefaultSuffix("png");
187  dialog.setNameFilter(tr("Images (*.png *.ppm *.jpg)"));
188  dialog.setFileMode(QFileDialog::AnyFile);
189  dialog.setConfirmOverwrite(true);
190  dialog.setDirectory( fi.path() );
191  dialog.selectFile( filename->text() );
192  dialog.setAcceptMode(QFileDialog::AcceptSave);
193  dialog.setWindowTitle(tr("Save Snapshot"));
194 
195  bool ok = dialog.exec();
196 
197  if (ok)
198  filename->setText( dialog.selectedFiles()[0] );
199 }
200 
201 void SnapshotDialog::filenameChanged(const QString &new_filename) {
202  QFileInfo fi(new_filename);
203  if (!QFileInfo(fi.path()).isWritable()) {
204  static const char *style = "background: #ffffcc;";
205  filename->setStyleSheet(style);
206  warning_lb->setText(trUtf8("Warning: Folder not writable."));
207  } else if (fi.exists()) {
208  static const char *style = "background: #ffcccc;";
209  filename->setStyleSheet(style);
210  warning_lb->setText(trUtf8("Warning: File exists and will be "
211  "overwritten without further warning."));
212  } else {
213  static const char *style = "";
214  filename->setStyleSheet(style);
215  warning_lb->setText("");
216  }
217 }
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
void setValue(const QString &key, const QVariant &value)
Wrapper function which makes it possible to enable Debugging output with -DOPENFLIPPER_SETTINGS_DEBUG...