NCL Composer  0.1.5
 All Classes Functions Variables Pages
PreferencesDialog.cpp
1 /* Copyright (c) 2011 Telemidia/PUC-Rio.
2  * All rights reserved. This program and the accompanying materials
3  * are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors:
8  * Telemidia/PUC-Rio - initial API and implementation
9  */
10 #include "PreferencesDialog.h"
11 #include "ui_PreferencesDialog.h"
12 
13 #include <QMetaObject>
14 #include <QMetaMethod>
15 
16 #include <QDialogButtonBox>
17 #include <QListWidgetItem>
18 
19 namespace composer {
20 namespace gui {
21 
22 PreferencesDialog::PreferencesDialog(QWidget *parent) :
23  QDialog(parent),
24  ui(new Ui::PreferencesDialog)
25 {
26  ui->setupUi(this);
27  this->setModal(true);
28 
29  loadPreferencesPages();
30 
31  connect( ui->listWidget,
32  SIGNAL(itemSelectionChanged()),
33  this,
34  SLOT(changeActivePage()));
35 
36  connect(ui->buttonBox_2,
37  SIGNAL(clicked(QAbstractButton*)),
38  this,
39  SLOT(buttonClicked(QAbstractButton*)));
40 
41 
42  connect(this, SIGNAL(accepted()), this, SLOT(applyCurrentValues()));
43 
44  currentItem = NULL;
45 }
46 
47 PreferencesDialog::~PreferencesDialog()
48 {
49  delete ui;
50 }
51 
52 void PreferencesDialog::addPreferencePage(IPluginFactory *pF)
53 {
54  if (pF->getPreferencePageWidget() == NULL) return;
55 
56 
57  addPreferencePage(pF->icon(), pF->name(), pF->getPreferencePageWidget());
58 }
59 
60 void PreferencesDialog::addPreferencePage(QIcon icon, QString name,
61  QWidget *page)
62 {
63  new QListWidgetItem(icon, name, ui->listWidget, 0);
64 
65  pages[name] = page;
66  page->hide();
67  ui->scrollAreaVerticalLayout->addWidget(page);
68 }
69 
70 void PreferencesDialog::addPreferencePage(IPreferencesPage *page)
71 {
72  new QListWidgetItem(page->getIcon(), page->getName(), ui->listWidget, 0);
73 
74  pages[page->getName()] = page;
75  page->hide();
76  ui->scrollAreaVerticalLayout->addWidget(page);
77 }
78 
79 void PreferencesDialog::loadPreferencesPages()
80 {
81  QList<IPluginFactory*> plugins = PluginControl::getInstance()->
82  getLoadedPlugins();
83  QList<IPluginFactory*>::iterator it;
84 
85  for (it = plugins.begin(); it != plugins.end(); it++)
86  {
87  IPluginFactory *pF = *it;
88  if (pF->getPreferencePageWidget() == NULL) continue;
89 
90  new QListWidgetItem(pF->icon(),
91  pF->name(),
92  ui->listWidget, 0);
93 
94  QWidget *page = pF->getPreferencePageWidget();
95  pages[pF->name()] = page;
96  page->hide();
97  ui->scrollAreaVerticalLayout->addWidget(page);
98  }
99 }
100 
101 void PreferencesDialog::changeActivePage()
102 {
103  if (currentItem != NULL)
104  {
105  if(pages.contains(currentItem->text())){
106  pages[currentItem->text()]->hide();
107  }
108  }
109 
110  currentItem = ui->listWidget->currentItem();
111  if(currentItem != NULL)
112  {
113  if(pages.contains(currentItem->text()))
114  {
115  pages[currentItem->text()]->show();
116  currentPage = pages[currentItem->text()];
117  }
118  }
119 }
120 
121 void PreferencesDialog::buttonClicked(QAbstractButton* button)
122 {
123  QDialogButtonBox::StandardButton std = ui->buttonBox_2->standardButton(button);
124 
125  if(std == QDialogButtonBox::Apply)
126  applyCurrentValues();
127 }
128 
129 void PreferencesDialog::applyCurrentValues()
130 {
131  QString slotName("applyValues()");
132 
133  QWidget *inst = currentPage;
134  int idxSlot = inst->metaObject()
135  ->indexOfSlot( slotName.toStdString().c_str() );
136  if(idxSlot != -1)
137  {
138  QMetaMethod method = inst->metaObject()->method(idxSlot);
139  method.invoke(inst, Qt::DirectConnection);
140  }
141 }
142 
143 /* void PreferencesDialog::show()
144 {
145  qDebug() << "eu aqui1";
146  QDialog::show();
147  selectFirst();
148 }
149 
150 void PreferencesDialog::selectFirst()
151 {
152  // \todo And if we do not have any child?
153  ui->listWidget->item(0)->setSelected(true);
154 } */
155 
156 }} //end namespace