00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "QmitkPreferencesDialog.h"
00019
00020 #include "berryPlatform.h"
00021 #include "berryIPreferencePage.h"
00022 #include "berryIConfigurationElement.h"
00023 #include "berryIExtensionPointService.h"
00024 #include "berryIExtension.h"
00025
00026 #include <QGridLayout>
00027 #include <QLineEdit>
00028 #include <QTreeWidget>
00029 #include <QLabel>
00030 #include <QPushButton>
00031 #include <QSplitter>
00032 #include <QEvent>
00033 #include <QTreeWidgetItem>
00034 #include <QIcon>
00035 #include <QStackedWidget>
00036 #include <QFileDialog>
00037 #include <QMessageBox>
00038
00039 #include <algorithm>
00040
00041 #include <mitkLogMacros.h>
00042
00043 using namespace std;
00044
00045 QmitkPreferencesDialog::QmitkPreferencesDialog(QWidget * parent, Qt::WindowFlags f)
00046 : QDialog(parent, f), m_CurrentPage(0)
00047 {
00048
00049 m_PreferencesService =
00050 berry::Platform::GetServiceRegistry().GetServiceById<berry::IPreferencesService>(berry::IPreferencesService::ID);
00051
00052
00053 berry::IExtensionPointService::Pointer extensionPointService = berry::Platform::GetExtensionPointService();
00054 berry::IConfigurationElement::vector prefPages(extensionPointService->GetConfigurationElementsFor("org.blueberry.ui.preferencePages"));
00055 berry::IConfigurationElement::vector keywordExts(extensionPointService->GetConfigurationElementsFor("org.blueberry.ui.keywords"));
00056 berry::IConfigurationElement::vector::iterator prefPagesIt;
00057 std::string id;
00058 std::string name;
00059 std::string category;
00060 std::string className;
00061 std::vector<std::string> keywords;
00062 vector<berry::IConfigurationElement::Pointer> keywordRefs;
00063 berry::IConfigurationElement::vector::iterator keywordRefsIt;
00064 berry::IConfigurationElement::vector::iterator keywordExtsIt;
00065 string keywordRefId;
00066 string keywordId;
00067 string keywordLabels;
00068
00069 for (prefPagesIt = prefPages.begin(); prefPagesIt != prefPages.end(); ++prefPagesIt)
00070 {
00071 keywords.clear();
00072 id.clear();
00073 name.clear();
00074 className.clear();
00075 category.clear();
00076 keywordRefId.clear();
00077 keywordId.clear();
00078 keywordLabels.clear();
00079
00080 if((*prefPagesIt)->GetAttribute("id", id)
00081 && (*prefPagesIt)->GetAttribute("name", name)
00082 && (*prefPagesIt)->GetAttribute("class", className))
00083 {
00084 (*prefPagesIt)->GetAttribute("category", category);
00085
00086 keywordRefs = (*prefPagesIt)->GetChildren("keywordreference");
00087 for (keywordRefsIt = keywordRefs.begin()
00088 ; keywordRefsIt != keywordRefs.end(); ++keywordRefsIt)
00089 {
00090 (*keywordRefsIt)->GetAttribute("id", keywordRefId);
00091
00092 for (keywordExtsIt = keywordExts.begin(); keywordExtsIt != keywordExts.end(); ++keywordExtsIt)
00093 {
00094 (*keywordExtsIt)->GetAttribute("id", keywordId);
00095 if(keywordId == keywordRefId)
00096 {
00097
00098 (*keywordExtsIt)->GetAttribute("label", keywordLabels);
00099
00100 break;
00101 }
00102 }
00103 }
00104
00105
00106 m_PrefPages.push_back(PrefPage(id, name, category, className, keywordLabels, berry::IConfigurationElement::Pointer(*prefPagesIt)));
00107 }
00108
00109 }
00110
00111
00112
00113
00114 m_Keyword = new QLineEdit("");
00115
00116 QObject::connect(m_Keyword, SIGNAL(editingFinished()), this, SLOT(OnKeywordEditingFinished()));
00117 QObject::connect(m_Keyword, SIGNAL(textChanged ( const QString & )), this, SLOT(OnKeywordTextChanged(const QString &)));
00118
00119
00120 m_PreferencesTree = new QTreeWidget;
00121 m_PreferencesTree->setHeaderHidden(true);
00122 QObject::connect(m_PreferencesTree, SIGNAL(itemSelectionChanged()), this, SLOT(OnPreferencesTreeItemSelectionChanged()));
00123
00124
00125 m_LeftLayout = new QGridLayout;
00126 m_LeftLayout->setContentsMargins(0,0,0,0);
00127 m_LeftLayout->setSpacing(0);
00128 m_LeftLayout->addWidget(m_Keyword, 0, 0);
00129 m_LeftLayout->addWidget(m_PreferencesTree, 1, 0);
00130
00131
00132 m_LeftPanel = new QWidget;
00133 m_LeftPanel->setLayout(m_LeftLayout);
00134
00135
00136 m_Headline = new QLabel("");
00137 m_Headline->setStyleSheet("background-color: silver; "
00138 "border-style: solid; border-width: 1px;"
00139 "border-color: white; font: bold 13px; color: white;");
00140
00141
00142 m_PreferencesPanel = new QStackedWidget;
00143
00144
00145 m_RightLayout = new QGridLayout;
00146 m_RightLayout->setRowStretch(0, 1);
00147 m_RightLayout->setRowStretch(1, 20);
00148 m_RightLayout->setContentsMargins(0,0,0,0);
00149 m_RightLayout->setSpacing(0);
00150 m_RightLayout->addWidget(m_Headline, 0, 0);
00151 m_RightLayout->addWidget(m_PreferencesPanel, 1, 0);
00152
00153
00154 m_RightPanel = new QWidget;
00155 m_RightPanel->setLayout(m_RightLayout);
00156
00157
00158 m_Splitter = new QSplitter(Qt::Horizontal);
00159 m_Splitter->setContentsMargins(0,0,0,0);
00160
00161 m_Splitter->setLineWidth(2);
00162 m_Splitter->addWidget(m_LeftPanel);
00163 m_Splitter->addWidget(m_RightPanel);
00164
00165
00166 m_Splitter->setStretchFactor(0,0);
00167 m_Splitter->setStretchFactor(1,2);
00168
00169
00170 m_ImportButton = new QPushButton(QString(tr("Import ...")));
00171 QObject::connect(m_ImportButton, SIGNAL(clicked(bool)), this, SLOT(OnImportButtonClicked(bool)));
00172 m_ImportButton->setIcon(QIcon(":/org.mitk.gui.qt.common/edit-redo.png"));
00173 m_ImportButton->setFlat(true);
00174 m_ImportButton->setDefault(false);
00175
00176
00177 m_ExportButton = new QPushButton(QString(tr("Export ...")), this);
00178 QObject::connect(m_ExportButton, SIGNAL(clicked(bool)), this, SLOT(OnExportButtonClicked(bool)));
00179 m_ExportButton->setIcon(QIcon(":/org.mitk.gui.qt.common/edit-undo.png"));
00180 m_ExportButton->setFlat(true);
00181 m_ExportButton->setDefault(false);
00182
00183
00184 m_ApplyButton = new QPushButton(QString(tr("OK")), this);
00185 QObject::connect(m_ApplyButton, SIGNAL(clicked(bool)), this, SLOT(OnApplyButtonClicked(bool)));
00186 m_ApplyButton->setIcon(QIcon(":/org.mitk.gui.qt.common/document-save.png"));
00187 m_ApplyButton->setFlat(true);
00188 m_ApplyButton->setDefault(false);
00189
00190
00191 m_CloseButton = new QPushButton(QString(tr("Close")), this);
00192 QObject::connect(m_CloseButton, SIGNAL(clicked(bool)), this, SLOT(OnCloseButtonClicked(bool)));
00193 m_CloseButton->setIcon(QIcon(":/org.mitk.gui.qt.common/system-log-out.png"));
00194 m_CloseButton->setFlat(true);
00195 m_CloseButton->setDefault(true);
00196 m_CloseButton->setFocus();
00197
00198
00199 m_Layout = new QGridLayout;
00200
00201
00202
00203
00204 m_Layout->addWidget(m_Splitter, 0, 0, 1, 4);
00205 m_Layout->addWidget(m_ImportButton, 2, 0);
00206 m_Layout->addWidget(m_ExportButton, 2, 1);
00207 m_Layout->addWidget(m_ApplyButton, 2, 2);
00208 m_Layout->addWidget(m_CloseButton, 2, 3);
00209
00210
00211 this->setModal(true);
00212 this->setSizeGripEnabled(true);
00213 this->setWhatsThis("Dialog to set application wide preferences");
00214 this->setWindowIcon(QIcon(":/org.mitk.gui.qt.common/preferences-system.png"));
00215 QRect parentGeometry = parent->geometry();
00216 int w = parentGeometry.width()/2; int h = parentGeometry.height()/2;
00217 int x = parentGeometry.x()+(parentGeometry.width()-w)/2;
00218 int y = parentGeometry.y()+(parentGeometry.height()-h)/2;
00219 this->setGeometry(x,y,w,h);
00220 this->setWindowTitle("Preferences");
00221 this->setLayout(m_Layout);
00222 this->UpdateTree();
00223 }
00224
00225 QmitkPreferencesDialog::~QmitkPreferencesDialog()
00226 {
00227 }
00228
00229 void QmitkPreferencesDialog::SetSelectedPage(const std::string& id)
00230 {
00231 for(vector<PrefPage>::iterator it = m_PrefPages.begin();
00232 it != m_PrefPages.end(); ++it)
00233 {
00234 if(it->id == id)
00235 {
00236 m_PreferencesTree->setCurrentItem(it->treeWidgetItem);
00237 break;
00238 }
00239 }
00240 }
00241
00242 void QmitkPreferencesDialog::OnImportButtonClicked( bool )
00243 {
00244 int answer = QMessageBox::question(this, "Importing Preferences"
00245 , "All existing preferences will be overwritten!\nAre you sure that you want to import other preferences?", QMessageBox::Yes | QMessageBox::No );
00246 if(answer == QMessageBox::No)
00247 return;
00248
00249 try
00250 {
00251 berry::IPreferencesService::Pointer prefService = m_PreferencesService.Lock();
00252 if(prefService.IsNotNull())
00253 {
00254 berry::IBerryPreferencesService::Pointer berryPrefService
00255 = prefService.Cast<berry::IBerryPreferencesService>();
00256 if(berryPrefService != 0)
00257 {
00258 QFileDialog fd(this, "Choose file to import preferences", "", "XML files (*.xml)" );
00259 fd.setFileMode(QFileDialog::ExistingFile);
00260 QStringList fileNames;
00261 if (fd.exec())
00262 {
00263 fileNames = fd.selectedFiles();
00264 if(fileNames.size()>0)
00265 {
00266 Poco::File f(fileNames.at(0).toStdString());
00267 berryPrefService->ImportPreferences(f, "");
00268 berry::IQtPreferencePage* prefPage = m_PrefPages[m_CurrentPage].prefPage;
00269 if(prefPage)
00270 prefPage->Update();
00271
00272 MITK_INFO("QmitkPreferencesDialog") << "Preferences successfully imported from " << f.path();
00273 }
00274 }
00275 }
00276 }
00277 }
00278 catch (Poco::Exception& pe)
00279 {
00280 QMessageBox::critical(this, "Error Importing", pe.message().c_str());
00281 MITK_ERROR("QmitkPreferencesDialog") << pe.what();
00282 }
00283 catch (std::exception& e)
00284 {
00285 QMessageBox::critical(this, "Error Importing", e.what());
00286 MITK_ERROR("QmitkPreferencesDialog") << e.what();
00287 }
00288 }
00289
00290 void QmitkPreferencesDialog::OnExportButtonClicked( bool )
00291 {
00292 try
00293 {
00294 berry::IPreferencesService::Pointer prefService = m_PreferencesService.Lock();
00295 if(prefService.IsNotNull())
00296 {
00297 berry::IBerryPreferencesService::Pointer berryPrefService
00298 = prefService.Cast<berry::IBerryPreferencesService>();
00299 if(berryPrefService != 0)
00300 {
00301 QFileDialog fd(this, "Choose file to import preferences", "", "XML files (*.xml)" );
00302 fd.setFileMode(QFileDialog::AnyFile);
00303 QStringList fileNames;
00304 if (fd.exec())
00305 {
00306 fileNames = fd.selectedFiles();
00307 if(fileNames.size()>0)
00308 {
00309 Poco::File f(fileNames.at(0).toStdString());
00310 berryPrefService->ExportPreferences(f, "");
00311 MITK_INFO("QmitkPreferencesDialog") << "Preferences successfully exported to " << f.path();
00312 }
00313 }
00314 }
00315 }
00316 }
00317 catch (Poco::Exception& pe)
00318 {
00319 QMessageBox::critical(this, "Error Exporting", pe.message().c_str());
00320 MITK_ERROR("QmitkPreferencesDialog") << pe.what();
00321 }
00322 catch (std::exception& e)
00323 {
00324 QMessageBox::critical(this, "Error Exporting", e.what());
00325 MITK_ERROR("QmitkPreferencesDialog") << e.what();
00326 }
00327 }
00328
00329 void QmitkPreferencesDialog::OnApplyButtonClicked( bool )
00330 {
00331 berry::IQtPreferencePage* prefPage = 0;
00332
00333 for(vector<PrefPage>::iterator it = m_PrefPages.begin();
00334 it != m_PrefPages.end(); ++it, ++m_CurrentPage)
00335 {
00336 prefPage = it->prefPage;
00337 if(prefPage)
00338 prefPage->PerformOk();
00339 }
00340
00347 berry::IPreferencesService::Pointer prefService = m_PreferencesService.Lock();
00348 if (prefService)
00349 {
00350 prefService->GetSystemPreferences()->Flush();
00351 }
00352
00353 this->done(QDialog::Accepted);
00354 }
00355
00356 void QmitkPreferencesDialog::OnCloseButtonClicked( bool )
00357 {
00358 berry::IQtPreferencePage* prefPage = m_PrefPages[m_CurrentPage].prefPage;
00359 if(prefPage)
00360 prefPage->PerformCancel();
00361
00362 this->done(QDialog::Accepted);
00363 }
00364
00365 void QmitkPreferencesDialog::OnKeywordTextChanged(const QString & )
00366 {
00367
00368 this->UpdateTree();
00369
00370 }
00371
00372 void QmitkPreferencesDialog::OnKeywordEditingFinished()
00373 {
00374 }
00375
00376 bool QmitkPreferencesDialog::eventFilter( QObject *obj, QEvent *event )
00377 {
00378 if(obj == m_Keyword)
00379 {
00380 if(event->type() == QEvent::FocusIn && m_Keyword->text() == "search ...")
00381 {
00382 m_Keyword->setText("");
00383 m_Keyword->setStyleSheet("color: black;");
00384 }
00385 else if(event->type() == QEvent::FocusOut && m_Keyword->text() == "")
00386 {
00387 m_Keyword->setText("search ...");
00388 m_Keyword->setStyleSheet("color: gray;");
00389 }
00390 }
00391 return true;
00392 }
00393
00394 void QmitkPreferencesDialog::OnPreferencesTreeItemSelectionChanged()
00395 {
00396 if(m_PreferencesTree == 0)
00397 return;
00398
00399
00400 QList<QTreeWidgetItem *> selectedItems = m_PreferencesTree->selectedItems();
00401 if(selectedItems.size()>0)
00402 {
00403
00404 m_CurrentPage = 0;
00405 for(vector<PrefPage>::iterator it = m_PrefPages.begin();
00406 it != m_PrefPages.end(); ++it, ++m_CurrentPage)
00407 {
00408 if(it->treeWidgetItem == selectedItems.at(0))
00409 {
00410 m_Headline->setText(QString::fromStdString(it->name));
00411 if(it->prefPage == 0)
00412 {
00413 it->prefPage = dynamic_cast<berry::IQtPreferencePage*>(it->confElem->CreateExecutableExtension<berry::IPreferencePage>("class"));
00414 it->prefPage->CreateQtControl(m_PreferencesPanel);
00415 m_PreferencesPanel->addWidget(it->prefPage->GetQtControl());
00416 }
00417 m_PreferencesPanel->setCurrentWidget(it->prefPage->GetQtControl());
00418
00419 break;
00420 }
00421 }
00422 }
00423 }
00424
00425 void QmitkPreferencesDialog::UpdateTree()
00426 {
00427 if(m_PreferencesTree == 0)
00428 return;
00429
00430
00431 string keyword = m_Keyword->text().toStdString();
00432
00433 map<std::string, QTreeWidgetItem*> items;
00434
00435 for(vector<PrefPage>::iterator it = m_PrefPages.begin();
00436 it != m_PrefPages.end(); ++it)
00437 {
00438 if(it->treeWidgetItem == 0)
00439 {
00440
00441 if(it->category.empty())
00442 {
00443 it->treeWidgetItem = new QTreeWidgetItem(m_PreferencesTree);
00444 }
00445 else
00446 {
00447 it->treeWidgetItem = new QTreeWidgetItem(items[it->category]);
00448 }
00449 it->treeWidgetItem->setText(0, QString::fromStdString(it->name));
00450 items[it->id] = it->treeWidgetItem;
00451 }
00452
00453
00454 if(!keyword.empty())
00455 {
00456 if( it->keywords.find(keyword) == string::npos )
00457 it->treeWidgetItem->setHidden(true);
00458 else
00459 {
00460
00461 QTreeWidgetItem* treeWidgetParent = it->treeWidgetItem->parent();
00462 while(treeWidgetParent!=0)
00463 {
00464 treeWidgetParent->setHidden(false);
00465 treeWidgetParent->setExpanded(true);
00466 treeWidgetParent = treeWidgetParent->parent();
00467 }
00468
00469 it->treeWidgetItem->setHidden(false);
00470 QFont f = it->treeWidgetItem->font(0);
00471 f.setBold(true);
00472 it->treeWidgetItem->setFont(0, f);
00473 }
00474 }
00475 else
00476 {
00477 QFont f = it->treeWidgetItem->font(0);
00478 f.setBold(false);
00479 it->treeWidgetItem->setFont(0, f);
00480 it->treeWidgetItem->setHidden(false);
00481 }
00482 }
00483
00484 if(m_PrefPages.size()>0)
00485 {
00486 if(m_PrefPages.front().treeWidgetItem != 0)
00487 m_PrefPages.front().treeWidgetItem->setSelected(true);
00488 }
00489
00490 }
00491
00492 bool QmitkPreferencesDialog::PrefPage::operator==( const PrefPage& other )
00493 {
00494 return id == other.id;
00495 }
00496
00497 bool QmitkPreferencesDialog::PrefPage::operator<( const PrefPage& other )
00498 {
00499 return name < other.name;
00500 }
00501
00502 QmitkPreferencesDialog::PrefPage::PrefPage( std::string _id, std::string _name, std::string _category
00503 , std::string _className, std::string _keywords
00504 , berry::IConfigurationElement::Pointer _confElem )
00505 : id(_id)
00506 , name(_name)
00507 , category(_category)
00508 , className(_className)
00509 , keywords(_keywords)
00510 , prefPage(0)
00511 , confElem(_confElem)
00512 , treeWidgetItem(0)
00513 {
00514
00515 }