1 // PARAVIS : ParaView wrapper SALOME module
3 // Copyright (C) 2010-2013 CEA/DEN, EDF R&D
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // File : PVGUI_ParaViewSettingsPane.cxx
22 // Author : Vitaly Smetannikov
25 #include "PVGUI_ParaViewSettingsPane.h"
26 #include "ui_pqOptionsDialog.h"
28 #include <pqOptionsContainer.h>
29 #include <pqOptionsPage.h>
30 #include <pqUndoStack.h>
31 #include "pqApplicationCore.h"
32 #include "pqApplicationOptions.h"
33 #include "pqGlobalRenderViewOptions.h"
34 #include "pqPluginManager.h"
35 #include "pqViewOptionsInterface.h"
36 #include "pqInterfaceTracker.h"
38 #include <QAbstractItemModel>
39 #include <QHeaderView>
45 class OptionsDialogModelItem
48 OptionsDialogModelItem();
49 OptionsDialogModelItem(const QString &name);
50 ~OptionsDialogModelItem();
52 OptionsDialogModelItem *Parent;
54 QList<OptionsDialogModelItem *> Children;
58 class OptionsDialogModel : public QAbstractItemModel
61 OptionsDialogModel(QObject *parent=0);
62 virtual ~OptionsDialogModel();
64 virtual int rowCount(const QModelIndex &parent=QModelIndex()) const;
65 virtual int columnCount(const QModelIndex &parent=QModelIndex()) const;
66 virtual QModelIndex index(int row, int column,
67 const QModelIndex &parent=QModelIndex()) const;
68 virtual QModelIndex parent(const QModelIndex &child) const;
70 virtual QVariant data(const QModelIndex &index,
71 int role=Qt::DisplayRole) const;
73 QModelIndex getIndex(const QString &path) const;
74 QString getPath(const QModelIndex &index) const;
75 void addPath(const QString &path);
76 bool removeIndex(const QModelIndex &index);
79 QModelIndex getIndex(OptionsDialogModelItem *item) const;
82 OptionsDialogModelItem *Root;
86 class OptionsDialogForm : public Ui::pqOptionsFrame
92 QMap<QString, pqOptionsPage *> Pages;
93 OptionsDialogModel *Model;
99 //----------------------------------------------------------------------------
100 OptionsDialogModelItem::OptionsDialogModelItem()
106 OptionsDialogModelItem::OptionsDialogModelItem(const QString &name)
107 : Name(name), Children()
112 OptionsDialogModelItem::~OptionsDialogModelItem()
114 QList<OptionsDialogModelItem *>::Iterator iter = this->Children.begin();
115 for( ; iter != this->Children.end(); ++iter)
122 //----------------------------------------------------------------------------
123 OptionsDialogModel::OptionsDialogModel(QObject *parentObject)
124 : QAbstractItemModel(parentObject)
126 this->Root = new OptionsDialogModelItem();
129 OptionsDialogModel::~OptionsDialogModel()
134 int OptionsDialogModel::rowCount(const QModelIndex &parentIndex) const
136 OptionsDialogModelItem *item = this->Root;
137 if(parentIndex.isValid())
139 item = reinterpret_cast<OptionsDialogModelItem *>(
140 parentIndex.internalPointer());
143 return item->Children.size();
146 int OptionsDialogModel::columnCount(const QModelIndex &) const
151 QModelIndex OptionsDialogModel::index(int row, int column,
152 const QModelIndex &parentIndex) const
154 OptionsDialogModelItem *item = this->Root;
155 if(parentIndex.isValid())
157 item = reinterpret_cast<OptionsDialogModelItem *>(
158 parentIndex.internalPointer());
161 if(column == 0 && row >= 0 && row < item->Children.size())
163 return this->createIndex(row, column, item->Children[row]);
166 return QModelIndex();
169 QModelIndex OptionsDialogModel::parent(const QModelIndex &child) const
173 OptionsDialogModelItem *item =
174 reinterpret_cast<OptionsDialogModelItem *>(child.internalPointer());
175 return this->getIndex(item->Parent);
178 return QModelIndex();
181 QVariant OptionsDialogModel::data(const QModelIndex &idx, int role) const
185 OptionsDialogModelItem *item =
186 reinterpret_cast<OptionsDialogModelItem *>(idx.internalPointer());
187 if(role == Qt::DisplayRole || role == Qt::ToolTipRole)
189 return QVariant(item->Name);
196 QModelIndex OptionsDialogModel::getIndex(const QString &path) const
198 OptionsDialogModelItem *item = this->Root;
199 QStringList names = path.split(".");
200 QStringList::Iterator iter = names.begin();
201 for( ; item && iter != names.end(); ++iter)
203 OptionsDialogModelItem *child = 0;
204 QList<OptionsDialogModelItem *>::Iterator jter = item->Children.begin();
205 for( ; jter != item->Children.end(); ++jter)
207 if((*jter)->Name == *iter)
217 if(item && item != this->Root)
219 return this->getIndex(item);
222 return QModelIndex();
225 QString OptionsDialogModel::getPath(const QModelIndex &idx) const
230 OptionsDialogModelItem *item =
231 reinterpret_cast<OptionsDialogModelItem *>(idx.internalPointer());
238 while(item && item != this->Root)
240 path.prepend(".").prepend(item->Name);
250 void OptionsDialogModel::addPath(const QString &path)
252 OptionsDialogModelItem *item = this->Root;
253 QStringList names = path.split(".");
254 QStringList::Iterator iter = names.begin();
255 for( ; iter != names.end(); ++iter)
257 OptionsDialogModelItem *child = 0;
258 QList<OptionsDialogModelItem *>::Iterator jter = item->Children.begin();
259 for( ; jter != item->Children.end(); ++jter)
261 if((*jter)->Name == *iter)
270 child = new OptionsDialogModelItem(*iter);
271 child->Parent = item;
272 QModelIndex parentIndex = this->getIndex(item);
273 int row = item->Children.size();
274 this->beginInsertRows(parentIndex, row, row);
275 item->Children.append(child);
276 this->endInsertRows();
283 bool OptionsDialogModel::removeIndex(const QModelIndex &idx)
287 OptionsDialogModelItem *item =
288 reinterpret_cast<OptionsDialogModelItem *>(idx.internalPointer());
289 if(item->Children.size() == 0)
291 QModelIndex parentIndex = this->getIndex(item->Parent);
292 this->beginRemoveRows(parentIndex, idx.row(), idx.row());
293 item->Parent->Children.removeAt(idx.row());
294 this->endRemoveRows();
303 QModelIndex OptionsDialogModel::getIndex(
304 OptionsDialogModelItem *item) const
306 if(item && item->Parent)
308 return this->createIndex(item->Parent->Children.indexOf(item), 0, item);
311 return QModelIndex();
315 //----------------------------------------------------------------------------
316 OptionsDialogForm::OptionsDialogForm()
317 : Ui::pqOptionsFrame(), Pages()
319 this->Model = new OptionsDialogModel();
320 this->ApplyUseCount = 0;
321 //this->ApplyNeeded = false;
324 OptionsDialogForm::~OptionsDialogForm()
330 //----------------------------------------------------------------------------
331 PVGUI_ParaViewSettingsPane::PVGUI_ParaViewSettingsPane(QWidget *widgetParent)
332 : QtxUserDefinedContent(widgetParent)
334 this->Form = new OptionsDialogForm();
335 this->Form->setupUi(this);
336 this->Form->PageNames->setModel(this->Form->Model);
338 // Hide the tree widget header view.
339 this->Form->PageNames->header()->hide();
341 // Hide the apply and reset buttons until they are needed.
342 //this->Form->ApplyButton->setEnabled(false);
343 //this->Form->ResetButton->setEnabled(false);
344 //this->Form->ApplyButton->hide();
345 //this->Form->ResetButton->hide();
347 this->connect(this->Form->PageNames->selectionModel(),
348 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
349 this, SLOT(changeCurrentPage()));
350 //this->connect(this->Form->ApplyButton, SIGNAL(clicked()),
351 // this, SLOT(applyChanges()));
352 //this->connect(this->Form->ResetButton, SIGNAL(clicked()),
353 // this, SLOT(resetChanges()));
354 //this->connect(this->Form->CloseButton, SIGNAL(clicked()),
355 // this, SLOT(accept()));
357 // Code From pqApplicationOptionsDialog
358 //this->setApplyNeeded(true);
360 pqApplicationOptions* appOptions = new pqApplicationOptions;
361 this->addOptions(appOptions);
363 pqGlobalRenderViewOptions* renOptions = new pqGlobalRenderViewOptions;
364 this->addOptions(renOptions);
366 QStringList pages = appOptions->getPageList();
369 this->setCurrentPage(pages[0]);
372 /// Add panes as plugins are loaded.
373 QObject::connect(pqApplicationCore::instance()->getPluginManager(),
374 SIGNAL(guiInterfaceLoaded(QObject*)),
375 this, SLOT(pluginLoaded(QObject*)));
377 // Load panes from already loaded plugins.
378 foreach (QObject* plugin_interface, pqApplicationCore::instance()->interfaceTracker()->interfaces())
379 //pqApplicationCore::instance()->getPluginManager()->interfaces())
381 this->pluginLoaded(plugin_interface);
385 PVGUI_ParaViewSettingsPane::~PVGUI_ParaViewSettingsPane()
390 bool PVGUI_ParaViewSettingsPane::isApplyNeeded() const
392 return this->Form->ApplyNeeded;
396 void PVGUI_ParaViewSettingsPane::setApplyNeeded(bool applyNeeded)
398 if(applyNeeded != this->Form->ApplyNeeded)
402 this->Form->ApplyNeeded = false;
403 //this->Form->ApplyButton->setEnabled(false);
404 //this->Form->ResetButton->setEnabled(false);
406 else if(this->Form->ApplyUseCount > 0)
408 this->Form->ApplyNeeded = true;
409 //this->Form->ApplyButton->setEnabled(true);
410 //this->Form->ResetButton->setEnabled(true);
415 void PVGUI_ParaViewSettingsPane::addOptions(const QString &path, pqOptionsPage *options)
422 // See if the page is a container.
423 pqOptionsContainer *container = qobject_cast<pqOptionsContainer *>(options);
424 if(!container && path.isEmpty())
429 // See if the page/container uses the apply button.
430 if(options->isApplyUsed())
432 this->Form->ApplyUseCount++;
433 /*if(this->Form->ApplyUseCount == 1)
435 //this->Form->ApplyButton->show();
436 //this->Form->ResetButton->show();
437 //QObject::connect(this, SIGNAL(accepted()), this, SLOT(applyChanges()));
440 //this->connect(options, SIGNAL(changesAvailable()),
441 //this, SLOT(enableButtons()));
444 // Add the widget to the stack.
445 this->Form->Stack->addWidget(options);
447 // Add the page(s) to the map and the model.
450 // If the path is not empty, use it as the page prefix.
458 container->setPagePrefix(prefix);
460 // Get the list of pages from the container.
461 QStringList pathList = container->getPageList();
462 QStringList::Iterator iter = pathList.begin();
463 for( ; iter != pathList.end(); ++iter)
465 this->Form->Pages.insert(prefix + *iter, options);
466 this->Form->Model->addPath(prefix + *iter);
471 this->Form->Pages.insert(path, options);
472 this->Form->Model->addPath(path);
476 void PVGUI_ParaViewSettingsPane::addOptions(pqOptionsContainer *options)
478 this->addOptions(QString(), options);
481 void PVGUI_ParaViewSettingsPane::removeOptions(pqOptionsPage *options)
488 // Remove the widget from the stack.
489 this->Form->Stack->removeWidget(options);
491 // See if the options use the apply button.
492 if(options->isApplyUsed())
494 this->Form->ApplyUseCount--;
495 /*if(this->Form->ApplyUseCount == 0)
497 //this->Form->ApplyNeeded = false;
498 //this->Form->ApplyButton->setEnabled(false);
499 //this->Form->ResetButton->setEnabled(false);
500 //this->Form->ApplyButton->hide();
501 //this->Form->ResetButton->hide();
502 //QObject::disconnect(this, SIGNAL(accepted()), this, SLOT(applyChanges()));
505 this->disconnect(options, 0, this, 0);
508 // Search the map for the paths to remove.
509 QMap<QString, pqOptionsPage *>::Iterator iter = this->Form->Pages.begin();
510 while(iter != this->Form->Pages.end())
514 QString path = iter.key();
515 iter = this->Form->Pages.erase(iter);
517 // Remove the item from the tree model as well.
518 QModelIndex index = this->Form->Model->getIndex(path);
519 QPersistentModelIndex parentIndex = index.parent();
520 if(this->Form->Model->removeIndex(index))
522 // Remove any empty parent items.
523 while(parentIndex.isValid() &&
524 this->Form->Model->rowCount(parentIndex) == 0)
527 parentIndex = index.parent();
529 // Make sure the index path isn't in the map.
530 path = this->Form->Model->getPath(index);
531 if(this->Form->Pages.find(path) == this->Form->Pages.end())
533 if(!this->Form->Model->removeIndex(index))
548 void PVGUI_ParaViewSettingsPane::setCurrentPage(const QString &path)
550 QModelIndex current = this->Form->Model->getIndex(path);
551 this->Form->PageNames->setCurrentIndex(current);
554 void PVGUI_ParaViewSettingsPane::applyChanges()
556 //if(this->Form->ApplyNeeded)
558 BEGIN_UNDO_SET("Changed View Settings");
559 emit this->aboutToApplyChanges();
560 QMap<QString, pqOptionsPage *>::Iterator iter = this->Form->Pages.begin();
561 for( ; iter != this->Form->Pages.end(); ++iter)
563 (*iter)->applyChanges();
566 //this->setApplyNeeded(false);
567 emit this->appliedChanges();
572 void PVGUI_ParaViewSettingsPane::resetChanges()
574 //if(this->Form->ApplyNeeded)
576 QMap<QString, pqOptionsPage *>::Iterator iter = this->Form->Pages.begin();
577 for( ; iter != this->Form->Pages.end(); ++iter)
579 (*iter)->resetChanges();
582 //this->setApplyNeeded(false);
586 void PVGUI_ParaViewSettingsPane::changeCurrentPage()
588 // Get the current index from the view.
589 QModelIndex current = this->Form->PageNames->currentIndex();
591 // Look up the path for the current index.
592 QString path = this->Form->Model->getPath(current);
593 QMap<QString, pqOptionsPage *>::Iterator iter = this->Form->Pages.find(path);
594 if(iter == this->Form->Pages.end())
596 // If no page is found, show the blank page.
597 this->Form->Stack->setCurrentWidget(this->Form->BlankPage);
601 this->Form->Stack->setCurrentWidget(*iter);
602 pqOptionsContainer *container = qobject_cast<pqOptionsContainer *>(*iter);
605 // Get the path prefix from the container.
606 QString prefix = container->getPagePrefix();
607 if(!prefix.isEmpty())
609 // Remove the prefix from the path.
610 path.remove(0, prefix.length());
613 // Set the page on the container object.
614 container->setPage(path);
619 //-----------------------------------------------------------------------------
620 void PVGUI_ParaViewSettingsPane::pluginLoaded(QObject* iface)
622 pqViewOptionsInterface* viewOptions =
623 qobject_cast<pqViewOptionsInterface*>(iface);
626 foreach(QString viewtype, viewOptions->viewTypes())
628 // Try to create global view options
629 pqOptionsContainer* globalOptions =
630 viewOptions->createGlobalViewOptions(viewtype, this);
633 this->addOptions(globalOptions);
639 void PVGUI_ParaViewSettingsPane::store(QtxResourceMgr* , QtxPreferenceMgr* )
644 void PVGUI_ParaViewSettingsPane::retrieve(QtxResourceMgr* , QtxPreferenceMgr* )