28 #include "internal/org_mitk_gui_qt_application_Activator.h"
30 #include <ui_QmitkPreferencesDialog.h>
32 #include <QFileDialog>
33 #include <QMessageBox>
34 #include <QPushButton>
42 static std::vector<std::string>
splitString(
const std::string &s,
char delim=
' ')
44 std::vector < std::string > elems;
45 std::stringstream ss(s);
47 while (std::getline(ss, item, delim))
49 elems.push_back(item);
54 class QmitkPreferencesDialogPrivate :
public Ui::QmitkPreferencesDialog
63 PrefPage(QString _id, QString _name, QString _category
65 : id(_id), name(_name), category(_category), className(_className), keywords(_keywords),
66 prefPage(nullptr), confElem(_confElem), treeWidgetItem(nullptr)
70 {
return id == other.id; }
72 bool operator<(
const PrefPage& other)
73 {
return name < other.name; }
82 QTreeWidgetItem* treeWidgetItem;
85 QmitkPreferencesDialogPrivate()
93 QList<berry::IConfigurationElement::Pointer>::iterator prefPagesIt;
95 QList<berry::IConfigurationElement::Pointer>::iterator keywordRefsIt;
97 for (prefPagesIt = prefPages.begin(); prefPagesIt != prefPages.end(); ++prefPagesIt)
99 QString
id = (*prefPagesIt)->GetAttribute(
"id");
100 QString name = (*prefPagesIt)->GetAttribute(
"name");
101 QString className = (*prefPagesIt)->GetAttribute(
"class");
102 if(!
id.isEmpty() && !name.isEmpty() && !className.isEmpty())
104 QString keywordLabels;
106 QString category = (*prefPagesIt)->GetAttribute(
"category");
108 QList<berry::IConfigurationElement::Pointer> keywordRefs = (*prefPagesIt)->GetChildren(
"keywordreference");
109 for (keywordRefsIt = keywordRefs.begin()
110 ; keywordRefsIt != keywordRefs.end(); ++keywordRefsIt)
112 QString keywordRefId = (*keywordRefsIt)->GetAttribute(
"id");
114 for (QList<berry::IConfigurationElement::Pointer>::iterator keywordExtsIt = keywordExts.begin();
115 keywordExtsIt != keywordExts.end(); ++keywordExtsIt)
117 QString keywordId = (*keywordExtsIt)->GetAttribute(
"id");
118 if(keywordId == keywordRefId)
121 QString currLabel = (*keywordExtsIt)->GetAttribute(
"label");
122 currLabel = currLabel.toLower();
123 if (!currLabel.isEmpty()) keywordLabels += QString(
" ") + currLabel;
140 QList<PrefPage> m_PrefPages;
146 : QDialog(parent, f), d(new QmitkPreferencesDialogPrivate)
151 QObject::connect(
d->m_Keyword, SIGNAL(textChanged(QString)),
this, SLOT(
OnKeywordTextChanged(QString)));
155 QPushButton* importButton =
d->buttonBox->addButton(
"Import...", QDialogButtonBox::ActionRole);
158 QPushButton* exportButton =
d->buttonBox->addButton(
"Export...", QDialogButtonBox::ActionRole);
173 for(QList<QmitkPreferencesDialogPrivate::PrefPage>::iterator it = d->m_PrefPages.begin(); it != d->m_PrefPages.end(); ++it)
177 d->m_PreferencesTree->setCurrentItem(it->treeWidgetItem);
185 int answer = QMessageBox::question(
this,
"Importing Preferences"
186 ,
"All existing preferences will be overwritten!\nAre you sure that you want to import other preferences?", QMessageBox::Yes | QMessageBox::No );
187 if(answer == QMessageBox::No)
194 if(berryPrefService !=
nullptr)
196 static QString importDir =
"";
197 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Choose file to import preferences"),
198 importDir, tr(
"XML files (*.xml)"));
200 if(!fileName.isEmpty())
202 importDir = QFileInfo(fileName).absoluteDir().path();
208 MITK_INFO(
"QmitkPreferencesDialog") <<
"Preferences successfully imported from " << fileName;
212 catch (Poco::Exception& pe)
214 QMessageBox::critical(
this,
"Error Importing", pe.message().c_str());
215 MITK_ERROR(
"QmitkPreferencesDialog") << pe.what();
217 catch (std::exception& e)
219 QMessageBox::critical(
this,
"Error Importing", e.what());
220 MITK_ERROR(
"QmitkPreferencesDialog") << e.what();
230 if(berryPrefService !=
nullptr)
233 static QString exportDir =
"";
234 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Choose file to export preferences"),
235 exportDir, tr(
"XML files (*.xml)"));
237 if(!fileName.isEmpty())
239 if(QFileInfo(fileName).suffix() !=
".xml")
243 exportDir = QFileInfo(fileName).absoluteDir().path();
245 MITK_INFO(
"QmitkPreferencesDialog") <<
"Preferences successfully exported to " << fileName;
249 catch (Poco::Exception& pe)
251 QMessageBox::critical(
this,
"Error Exporting", pe.message().c_str());
252 MITK_ERROR(
"QmitkPreferencesDialog") << pe.what();
254 catch (std::exception& e)
256 QMessageBox::critical(
this,
"Error Exporting", e.what());
257 MITK_ERROR(
"QmitkPreferencesDialog") << e.what();
265 for(QList<QmitkPreferencesDialogPrivate::PrefPage>::iterator it = d->m_PrefPages.begin(); it != d->m_PrefPages.end(); ++it)
267 prefPage = it->prefPage;
284 this->SavePreferences();
324 if(d->m_PreferencesTree ==
nullptr)
328 QList<QTreeWidgetItem *> selectedItems = d->m_PreferencesTree->selectedItems();
329 if(selectedItems.size()>0)
332 d->m_CurrentPage = 0;
334 for(QList<QmitkPreferencesDialogPrivate::PrefPage>::iterator it = d->m_PrefPages.begin(); it != d->m_PrefPages.end(); ++it, ++d->m_CurrentPage)
336 if(it->treeWidgetItem == selectedItems.at(0))
338 d->m_Headline->setText(it->name);
339 if(it->prefPage ==
nullptr)
344 it->prefPage->CreateQtControl(d->m_PreferencesPanel);
345 d->m_PreferencesPanel->addWidget(it->prefPage->GetQtControl());
347 d->m_PreferencesPanel->setCurrentWidget(it->prefPage->GetQtControl());
357 if(d->m_PreferencesTree ==
nullptr)
360 d->m_PreferencesTree->sortByColumn(0, Qt::AscendingOrder);
363 QString keyword = d->m_Keyword->text().toLower();
365 map<QString, QTreeWidgetItem*> items;
366 std::list< QList<QmitkPreferencesDialogPrivate::PrefPage>::iterator > deferredItems;
368 for (QList<QmitkPreferencesDialogPrivate::PrefPage>::iterator it = d->m_PrefPages.begin();
369 it != d->m_PrefPages.end(); ++it)
371 if (it->treeWidgetItem ==
nullptr)
373 if (it->category.isEmpty())
375 it->treeWidgetItem =
new QTreeWidgetItem(d->m_PreferencesTree);
376 it->treeWidgetItem->setText(0, it->name);
377 items[it->id] = it->treeWidgetItem;
382 deferredItems.push_back(it);
389 auto currentItem = deferredItems.begin();
391 while (currentItem != deferredItems.end())
393 auto currentItemContent = *currentItem;
394 if (items[currentItemContent->category] !=
nullptr)
396 currentItemContent->treeWidgetItem =
new QTreeWidgetItem(items[currentItemContent->category]);
397 currentItemContent->treeWidgetItem->setText(0, currentItemContent->name);
398 items[currentItemContent->id] = currentItemContent->treeWidgetItem;
400 deferredItems.erase(currentItem);
401 currentItem = deferredItems.begin();
409 if (!deferredItems.empty())
411 MITK_ERROR <<
"Unknown preference category. They are added top-level.";
413 for (
const auto ¤tItemContent : deferredItems)
415 currentItemContent->treeWidgetItem =
new QTreeWidgetItem(d->m_PreferencesTree);
416 currentItemContent->treeWidgetItem->setText(0, currentItemContent->name);
417 items[currentItemContent->id] = currentItemContent->treeWidgetItem;
426 if(!keyword.isEmpty())
428 for (QList<QmitkPreferencesDialogPrivate::PrefPage>::iterator it = d->m_PrefPages.begin();
429 it != d->m_PrefPages.end(); ++it)
431 it->treeWidgetItem->setHidden(
true);
436 for (QList<QmitkPreferencesDialogPrivate::PrefPage>::iterator it = d->m_PrefPages.begin();
437 it != d->m_PrefPages.end(); ++it)
440 if(!keyword.isEmpty())
443 vector<string> keywordsAvailable =
splitString(keyword.toUtf8().constData());
444 bool foundAll =
true;
447 for(
unsigned int i = 0; i < keywordsAvailable.size(); i++)
449 if (it->keywords.indexOf(QString::fromStdString(keywordsAvailable[i])) == -1)
459 QTreeWidgetItem* treeWidgetParent = it->treeWidgetItem->parent();
460 while(treeWidgetParent!=
nullptr)
462 treeWidgetParent->setHidden(
false);
463 treeWidgetParent->setExpanded(
true);
464 treeWidgetParent = treeWidgetParent->parent();
467 it->treeWidgetItem->setHidden(
false);
468 QFont f = it->treeWidgetItem->font(0);
470 it->treeWidgetItem->setFont(0, f);
475 QFont f = it->treeWidgetItem->font(0);
477 it->treeWidgetItem->setFont(0, f);
478 it->treeWidgetItem->setHidden(
false);
482 if(d->m_PrefPages.size()>0)
484 if(d->m_PrefPages.front().treeWidgetItem !=
nullptr)
485 d->m_PrefPages.front().treeWidgetItem->setSelected(
true);
QmitkPreferencesDialog(QWidget *parent=nullptr, Qt::WindowFlags f=0)
virtual void Init(IWorkbench::Pointer workbench)=0
virtual void ExportPreferences(const QString &f, const QString &name="")=0
static std::vector< std::string > splitString(const std::string &s, char delim=' ')
virtual SmartPointer< IPreferences > GetSystemPreferences()=0
virtual bool PerformOk()=0
MITKCORE_EXPORT bool operator==(const InteractionEvent &a, const InteractionEvent &b)
virtual QList< SmartPointer< IConfigurationElement > > GetConfigurationElementsFor(const QString &extensionPointId) const =0
QScopedPointer< QmitkPreferencesDialogPrivate > d
void OnImportButtonClicked()
virtual void PerformCancel()=0
void OnExportButtonClicked()
void SetSelectedPage(const QString &id)
void OnKeywordEditingFinished()
virtual void ImportPreferences(const QString &f, const QString &name="")=0
~QmitkPreferencesDialog()
void OnKeywordTextChanged(const QString &s)
void OnPreferencesTreeItemSelectionChanged()