utilities/src/ui/configurationwidget.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Tomassino Ferrauto                              *
00003  *   t_ferrauto@yahoo.it                                                   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "ui/configurationwidget.h"
00022 #include <QVBoxLayout>
00023 #include <QHeaderView>
00024 
00025 namespace farsa {
00026 
00027 ConfigurationWidget::ConfigurationWidget(QWidget *parent) :
00028     QSplitter(Qt::Horizontal, parent),
00029     m_configurationParameters(NULL),
00030     m_tmpParameters(),
00031     m_readOnly(true),
00032     m_valuesChanged(false),
00033     m_treeWidget(NULL),
00034     m_tableWidget(NULL)
00035 {
00036     // Creating a tree widget and adding it to the splitter
00037     m_treeWidget = new QTreeWidget(this);
00038     addWidget(m_treeWidget);
00039     // Only one column
00040     m_treeWidget->setColumnCount(1);
00041     m_treeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00042     m_treeWidget->setHeaderLabel("Groups");
00043     m_treeWidget->header()->setClickable(false);
00044     setStretchFactor(0, 1);
00045 
00046     // Creating a table widget and adding it to the splitter
00047     m_tableWidget = new QTableWidget(this);
00048     addWidget(m_tableWidget);
00049     // Two columns, parameter name and value
00050     m_tableWidget->setColumnCount(2);
00051     m_tableWidget->setHorizontalHeaderLabels(QStringList() << "Parameters" << "Value");
00052     m_tableWidget->horizontalHeader()->setClickable(false);
00053     m_tableWidget->verticalHeader()->setClickable(false);
00054     setStretchFactor(1, 2);
00055 
00056     // Connecting tree and table signals
00057     connect(m_treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(changeTableContent(QTreeWidgetItem *, int)));
00058     connect(m_tableWidget, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(storeChangedItem(QTableWidgetItem *)));
00059 }
00060 
00061 ConfigurationWidget::~ConfigurationWidget()
00062 {
00063     // Nothing to do
00064 }
00065 
00066 void ConfigurationWidget::setConfigurationParameters(ConfigurationParameters *conf)
00067 {
00068     m_configurationParameters = conf;
00069 
00070     updateParameters();
00071 }
00072 
00073 void ConfigurationWidget::fillWidgetTree( QString groupName, QString groupFullPath, QTreeWidgetItem* root) {
00074     // Creating the item in the tree widget
00075     QTreeWidgetItem *widgetRoot = new QTreeWidgetItem(root, QStringList() << groupName);
00076     // Store the full group path for a fast retrieval of it
00077     widgetRoot->setData( 0, Qt::UserRole, groupFullPath );
00078     QStringList groupList = m_tmpParameters.getGroupsList( groupFullPath );
00079     foreach( QString group, groupList ) {
00080         fillWidgetTree( group, groupFullPath + ConfigurationParameters::GroupSeparator() + group, widgetRoot );
00081     }
00082 }
00083 
00084 void ConfigurationWidget::updateParameters()
00085 {
00086     if (m_configurationParameters == NULL) {
00087         return;
00088     }
00089 
00090     // Resetting m_valuesChanged
00091     m_valuesChanged = false;
00092 
00093     // Copying configuration parameters to m_tmpParameters
00094     m_tmpParameters = *m_configurationParameters;
00095 
00096     // Removing everything from the tree
00097     m_treeWidget->clear();
00098 
00099     // Create the root node
00100     QTreeWidgetItem *widgetRoot = new QTreeWidgetItem(QStringList() << "root");
00101     widgetRoot->setData(0, Qt::UserRole, "");
00102     m_treeWidget->insertTopLevelItem(0, widgetRoot);
00103     // Filling the tree
00104     QStringList groupList = m_tmpParameters.getGroupsList( "" );
00105     foreach( QString group, groupList ) {
00106         fillWidgetTree( group, group, widgetRoot );
00107     }
00108     // Setting the current item to tree root
00109     m_treeWidget->setCurrentItem(m_treeWidget->topLevelItem(0));
00110     // Updating tree
00111     m_treeWidget->update();
00112     // Now filling table with root's properties
00113     changeTableContent(m_treeWidget->currentItem(), 0);
00114 }
00115 
00116 void ConfigurationWidget::setReadOnly(bool readOnly)
00117 {
00118     m_readOnly = readOnly;
00119 
00120     // Changing the read-only flag of all items in the table
00121     for (int r = 0; r < m_tableWidget->rowCount(); r++) {
00122         QTableWidgetItem *const item = m_tableWidget->item(r, 1);
00123         if (m_readOnly) {
00124             item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00125         } else {
00126             item->setFlags(item->flags() | Qt::ItemIsEditable);
00127         }
00128     }
00129 
00130     // Updating table
00131     m_tableWidget->update();
00132 }
00133 
00134 void ConfigurationWidget::store()
00135 {
00136     if (m_configurationParameters == NULL) {
00137         return;
00138     }
00139 
00140     *m_configurationParameters = m_tmpParameters;
00141 
00142     // Resetting m_valuesChanged
00143     m_valuesChanged = false;
00144 }
00145 
00146 void ConfigurationWidget::restore()
00147 {
00148     if (m_configurationParameters == NULL) {
00149         return;
00150     }
00151 
00152     m_tmpParameters = *m_configurationParameters;
00153 
00154     // Also resets m_valuesChanged
00155     updateParameters();
00156 }
00157 
00158 void ConfigurationWidget::changeTableContent(QTreeWidgetItem *item, int)
00159 {
00160     // Blocking signals for the table until we finish modifications
00161     m_tableWidget->blockSignals(true);
00162 
00163     // Clearing the table
00164     m_tableWidget->clearContents();
00165 
00166     // Getting the group full path corresponding to item
00167     QString groupFullPath = item->data(0, Qt::UserRole).toString();
00168 
00169     // Now filling the table taking all parameters in node.
00170     QStringList paramList = m_tmpParameters.getParametersList( groupFullPath );
00171     m_tableWidget->setRowCount( paramList.size() );
00172     for (int i = 0; i < paramList.size(); i++) {
00173         // Parameter
00174         QTableWidgetItem *parameter = new QTableWidgetItem( paramList[i] );
00175         parameter->setFlags(Qt::ItemIsEnabled);
00176         m_tableWidget->setItem(i, 0, parameter);
00177         // Value
00178         QString fullName = groupFullPath + ConfigurationParameters::GroupSeparator() + paramList[i];
00179         QTableWidgetItem *value = new QTableWidgetItem( m_tmpParameters.getValue( fullName ) );
00180         value->setData( Qt::UserRole, fullName );
00181         if (m_readOnly) {
00182             value->setFlags(Qt::ItemIsEnabled);
00183         } else {
00184             value->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
00185         }
00186         m_tableWidget->setItem(i, 1, value);
00187     }
00188 
00189     // Re-enabling signals for the table
00190     m_tableWidget->blockSignals(false);
00191 
00192     // Updatig table
00193     m_tableWidget->update();
00194 }
00195 
00196 void ConfigurationWidget::storeChangedItem(QTableWidgetItem *item)
00197 {
00198     // Only taking items in the second column into consideration
00199     if (item->column() != 1) {
00200         return;
00201     }
00202 
00203     // Getting the full name of parameter of corresponding to item
00204     QString paramFullName = item->data( Qt::UserRole ).toString();
00205 
00206     // Taking the parameter, its old and new value
00207     QString parameter = m_tableWidget->item(item->row(), 0)->text();
00208     QString oldValue = m_tmpParameters.getValue( paramFullName );
00209     QString newValue = item->text();
00210 
00211     // Changing parameter
00212     m_tmpParameters.setValue(paramFullName, newValue);
00213 
00214     // Setting m_valuesChanged to true
00215     m_valuesChanged = true;
00216 
00217     // Signalling the change
00218     emit valueHasChanged(paramFullName, oldValue, newValue);
00219 }
00220 
00221 } // end namespace farsa