Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
FileOptionsDialog.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 "FileOptionsDialog.hh"
51 
52 FileOptionsDialog::FileOptionsDialog(std::vector<fileTypes>& _supportedTypes, QStringList _extensions, bool _loadMode, QWidget *parent)
53  : QDialog(parent),
54  ext_(_extensions),
55  supportedTypes_(_supportedTypes),
56  loadMode_(_loadMode),
57  defaultPluginBox_(NULL)
58 {
59 
60  ext_.removeDuplicates();
61 
62  QGridLayout* grid = new QGridLayout;
63 
64  QVector< int > usefulPlugins;
65 
66  //check if more than one plugin is able to handle this extension
67  for (int i=0; i < ext_.size(); i++){
68  int count = 0;
69  QStringList names;
70 
71  for (unsigned int t=0; t < supportedTypes_.size(); t++){
72 
73  QString filters;
74 
75  if (loadMode_)
76  filters = supportedTypes_[t].loadFilters;
77  else
78  filters = supportedTypes_[t].saveFilters;
79 
80  // Only take the parts inside the brackets
81  filters = filters.section("(",1).section(")",0,0);
82 
83  // Split into blocks
84  QStringList separateFilters = filters.split(" ");
85 
86  for ( int filterId = 0 ; filterId < separateFilters.size(); ++filterId ) {
87  separateFilters[filterId] = separateFilters[filterId].trimmed();
88 
89  if (separateFilters[filterId].endsWith("." + ext_[i],Qt::CaseInsensitive)){
90  count++;
91  names.push_back( supportedTypes_[t].name );
92  usefulPlugins.push_back( t );
93  continue;
94  }
95 
96  }
97 
98  }
99 
100  //more than one plugin can handle the extension
101  if (count > 1){
102  QLabel* label = new QLabel( tr("For *.%1 use plugin ").arg(ext_[i]) );
103  QComboBox* box = new QComboBox();
104 
105  defaultPluginBox_ = new QCheckBox(tr("Make this plugin the default"));
106 
107  box->addItems(names);
108  box->setAccessibleName(ext_[i]);
109 
110  currentName_ = box->currentText();
111  currentExtension_ = ext_[i];
112 
113  grid->addWidget(label, grid->rowCount(), 0);
114  grid->addWidget(box, grid->rowCount()-1, 1);
115  grid->addWidget(defaultPluginBox_, grid->rowCount()+1, 0);
116 
117  connect(box, SIGNAL(currentIndexChanged(QString)), this, SLOT(slotPluginChanged(QString)) );
118  connect(defaultPluginBox_, SIGNAL(stateChanged(int)), this, SLOT(slotPluginDefault(int)) );
119  boxes_.push_back(box);
120  }
121  }
122 
123  //force TriangleMesh as initial value if available
124  for (int i=0; i < boxes_.count(); i++){
125 
126  for (int t=0; t < (boxes_[i])->count(); t++)
127  if ( (boxes_[i])->itemText(t).contains("TriangleMesh") ){
128  (boxes_[i])->setCurrentIndex(t);
129  break;
130  }
131  }
132 
133  QGroupBox* group = new QGroupBox(tr("Extensions with multiple plugins"));
134  group->setLayout(grid);
135 
136  if (boxes_.count() == 0)
137  group->setVisible(false);
138 
139 
140  //add option widgets from all fileplugins
141  for (unsigned int t=0; t < supportedTypes_.size(); t++){
142 
143  if ( !usefulPlugins.contains(t) )
144  continue;
145 
146  QWidget* widget;
147 
148  if (loadMode_)
149  widget = supportedTypes_[t].plugin->loadOptionsWidget("");
150  else
151  widget = supportedTypes_[t].plugin->saveOptionsWidget("");
152 
153  if (widget != 0)
154  tabs_.addTab(widget, supportedTypes_[t].name);
155  }
156 
157  //add buttons at bottom
158  QPushButton* cancel = new QPushButton(tr("&Cancel"));
159  QPushButton* ok = new QPushButton(tr("&Ok"));
160 
161  QHBoxLayout* buttonLayout = new QHBoxLayout;
162 
163  buttonLayout->addWidget(cancel);
164  buttonLayout->addStretch();
165  buttonLayout->addWidget(ok);
166 
167  QVBoxLayout* layout = new QVBoxLayout;
168  layout->addWidget(group);
169  layout->addWidget(&tabs_);
170  layout->addLayout(buttonLayout);
171 
172  setLayout(layout);
173 
174  connect( cancel, SIGNAL(clicked()), this, SLOT(reject()) );
175  connect( ok, SIGNAL(clicked()), this, SLOT(accept()) );
176 
177 }
178 
179 FileOptionsDialog::~FileOptionsDialog()
180 {
181  //remove tabs
182  for (int i=tabs_.count()-1; i >= 0; i--)
183  tabs_.removeTab(i);
184 
185  //and set parent of widgets to NULL
186  for (unsigned int t=0; t < supportedTypes_.size(); t++){
187 
188  QWidget* widget;
189 
190  if (loadMode_)
191  widget = supportedTypes_[t].plugin->loadOptionsWidget("");
192  else
193  widget = supportedTypes_[t].plugin->saveOptionsWidget("");
194 
195  if (widget != 0)
196  widget->setParent(0);
197  }
198 }
199 
200 int FileOptionsDialog::exec(){
201 
202  if ( tabs_.count() == 0 && boxes_.count() == 0 )
203  return QDialog::Accepted;
204  else {
205  return QDialog::exec();
206  }
207 }
208 
209 bool FileOptionsDialog::makePluginDefault() {
210  if (!defaultPluginBox_)
211  return false;
212 
213  return defaultPluginBox_->isChecked();
214 }
215 
216 void FileOptionsDialog::slotPluginChanged(QString _name){
217 
218  QComboBox* box = dynamic_cast<QComboBox*>(QObject::sender());
219 
220  for (unsigned int t=0; t < supportedTypes_.size(); t++)
221  if ( supportedTypes_[t].name == _name ){
222 
223  currentName_ = _name;
224  currentExtension_ = box->accessibleName();
225 
226  if (makePluginDefault()) {
227  OpenFlipperSettings().setValue(QString("Core/File/DefaultLoader/").append(currentExtension_), currentName_);
228  }
229 
230  emit setPluginForExtension(box->accessibleName(), t ); //accessibleName contains the extension
231  break;
232  }
233 }
234 
235 void FileOptionsDialog::slotPluginDefault(int _state) {
236  // store the name of the default plugin for loading
237  if (_state == Qt::Checked) {
238  OpenFlipperSettings().setValue(QString("Core/File/DefaultLoader/").append(currentExtension_), currentName_);
239  } else {
240  OpenFlipperSettings().setValue(QString("Core/File/DefaultLoader/").append(currentExtension_), "");
241  }
242 }
243 
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...