configurationwidget.cpp
1 /***************************************************************************
2  * Copyright (C) 2008 by Tomassino Ferrauto *
3  * t_ferrauto@yahoo.it *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program 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 *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #include "ui/configurationwidget.h"
22 #include <QVBoxLayout>
23 #include <QHeaderView>
24 
25 namespace farsa {
26 
28  QSplitter(Qt::Horizontal, parent),
29  m_configurationParameters(NULL),
30  m_tmpParameters(),
31  m_readOnly(true),
32  m_valuesChanged(false),
33  m_treeWidget(NULL),
34  m_tableWidget(NULL)
35 {
36  // Creating a tree widget and adding it to the splitter
37  m_treeWidget = new QTreeWidget(this);
38  addWidget(m_treeWidget);
39  // Only one column
40  m_treeWidget->setColumnCount(1);
41  m_treeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
42  m_treeWidget->setHeaderLabel("Groups");
43  m_treeWidget->header()->setClickable(false);
44  setStretchFactor(0, 1);
45 
46  // Creating a table widget and adding it to the splitter
47  m_tableWidget = new QTableWidget(this);
48  addWidget(m_tableWidget);
49  // Two columns, parameter name and value
50  m_tableWidget->setColumnCount(2);
51  m_tableWidget->setHorizontalHeaderLabels(QStringList() << "Parameters" << "Value");
52  m_tableWidget->horizontalHeader()->setClickable(false);
53  m_tableWidget->verticalHeader()->setClickable(false);
54  setStretchFactor(1, 2);
55 
56  // Connecting tree and table signals
57  connect(m_treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(changeTableContent(QTreeWidgetItem *, int)));
58  connect(m_tableWidget, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(storeChangedItem(QTableWidgetItem *)));
59 }
60 
62 {
63  // Nothing to do
64 }
65 
66 void ConfigurationWidget::setConfigurationParameters(ConfigurationParameters *conf)
67 {
68  m_configurationParameters = conf;
69 
71 }
72 
73 void ConfigurationWidget::fillWidgetTree( QString groupName, QString groupFullPath, QTreeWidgetItem* root) {
74  // Creating the item in the tree widget
75  QTreeWidgetItem *widgetRoot = new QTreeWidgetItem(root, QStringList() << groupName);
76  // Store the full group path for a fast retrieval of it
77  widgetRoot->setData( 0, Qt::UserRole, groupFullPath );
78  QStringList groupList = m_tmpParameters.getGroupsList( groupFullPath );
79  foreach( QString group, groupList ) {
80  fillWidgetTree( group, groupFullPath + ConfigurationParameters::GroupSeparator() + group, widgetRoot );
81  }
82 }
83 
85 {
86  if (m_configurationParameters == NULL) {
87  return;
88  }
89 
90  // Resetting m_valuesChanged
91  m_valuesChanged = false;
92 
93  // Copying configuration parameters to m_tmpParameters
94  m_tmpParameters = *m_configurationParameters;
95 
96  // Removing everything from the tree
97  m_treeWidget->clear();
98 
99  // Create the root node
100  QTreeWidgetItem *widgetRoot = new QTreeWidgetItem(QStringList() << "root");
101  widgetRoot->setData(0, Qt::UserRole, "");
102  m_treeWidget->insertTopLevelItem(0, widgetRoot);
103  // Filling the tree
104  QStringList groupList = m_tmpParameters.getGroupsList( "" );
105  foreach( QString group, groupList ) {
106  fillWidgetTree( group, group, widgetRoot );
107  }
108  // Setting the current item to tree root
109  m_treeWidget->setCurrentItem(m_treeWidget->topLevelItem(0));
110  // Updating tree
111  m_treeWidget->update();
112  // Now filling table with root's properties
113  changeTableContent(m_treeWidget->currentItem(), 0);
114 }
115 
117 {
118  m_readOnly = readOnly;
119 
120  // Changing the read-only flag of all items in the table
121  for (int r = 0; r < m_tableWidget->rowCount(); r++) {
122  QTableWidgetItem *const item = m_tableWidget->item(r, 1);
123  if (m_readOnly) {
124  item->setFlags(item->flags() & ~Qt::ItemIsEditable);
125  } else {
126  item->setFlags(item->flags() | Qt::ItemIsEditable);
127  }
128  }
129 
130  // Updating table
131  m_tableWidget->update();
132 }
133 
135 {
136  if (m_configurationParameters == NULL) {
137  return;
138  }
139 
140  *m_configurationParameters = m_tmpParameters;
141 
142  // Resetting m_valuesChanged
143  m_valuesChanged = false;
144 }
145 
147 {
148  if (m_configurationParameters == NULL) {
149  return;
150  }
151 
152  m_tmpParameters = *m_configurationParameters;
153 
154  // Also resets m_valuesChanged
156 }
157 
158 void ConfigurationWidget::changeTableContent(QTreeWidgetItem *item, int)
159 {
160  // Blocking signals for the table until we finish modifications
161  m_tableWidget->blockSignals(true);
162 
163  // Clearing the table
164  m_tableWidget->clearContents();
165 
166  // Getting the group full path corresponding to item
167  QString groupFullPath = item->data(0, Qt::UserRole).toString();
168 
169  // Now filling the table taking all parameters in node.
170  QStringList paramList = m_tmpParameters.getParametersList( groupFullPath );
171  m_tableWidget->setRowCount( paramList.size() );
172  for (int i = 0; i < paramList.size(); i++) {
173  // Parameter
174  QTableWidgetItem *parameter = new QTableWidgetItem( paramList[i] );
175  parameter->setFlags(Qt::ItemIsEnabled);
176  m_tableWidget->setItem(i, 0, parameter);
177  // Value
178  QString fullName = groupFullPath + ConfigurationParameters::GroupSeparator() + paramList[i];
179  QTableWidgetItem *value = new QTableWidgetItem( m_tmpParameters.getValue( fullName ) );
180  value->setData( Qt::UserRole, fullName );
181  if (m_readOnly) {
182  value->setFlags(Qt::ItemIsEnabled);
183  } else {
184  value->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
185  }
186  m_tableWidget->setItem(i, 1, value);
187  }
188 
189  // Re-enabling signals for the table
190  m_tableWidget->blockSignals(false);
191 
192  // Updatig table
193  m_tableWidget->update();
194 }
195 
196 void ConfigurationWidget::storeChangedItem(QTableWidgetItem *item)
197 {
198  // Only taking items in the second column into consideration
199  if (item->column() != 1) {
200  return;
201  }
202 
203  // Getting the full name of parameter of corresponding to item
204  QString paramFullName = item->data( Qt::UserRole ).toString();
205 
206  // Taking the parameter, its old and new value
207  QString parameter = m_tableWidget->item(item->row(), 0)->text();
208  QString oldValue = m_tmpParameters.getValue( paramFullName );
209  QString newValue = item->text();
210 
211  // Changing parameter
212  m_tmpParameters.setValue(paramFullName, newValue);
213 
214  // Setting m_valuesChanged to true
215  m_valuesChanged = true;
216 
217  // Signalling the change
218  emit valueHasChanged(paramFullName, oldValue, newValue);
219 }
220 
221 } // end namespace farsa