DlgParameterImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de>              *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <sstream>
00027 # include <QByteArray>
00028 # include <QContextMenuEvent>
00029 # include <QHeaderView>
00030 # include <QInputDialog>
00031 # include <QMessageBox>
00032 # include <QMenu>
00033 # include <QTreeWidget>
00034 #endif
00035 
00036 #include "DlgParameterImp.h"
00037 #include "DlgInputDialogImp.h"
00038 #include "BitmapFactory.h"
00039 #include "FileDialog.h"
00040 #include "SpinBox.h"
00041 
00042 #include <Base/Parameter.h>
00043 #include <Base/Exception.h>
00044 #include <App/Application.h>
00045 
00046 
00047 
00048 using namespace Gui::Dialog;
00049 
00050 /* TRANSLATOR Gui::Dialog::DlgParameterImp */
00051 
00059 DlgParameterImp::DlgParameterImp( QWidget* parent,  Qt::WFlags fl )
00060   : QDialog( parent, fl|Qt::WindowMinMaxButtonsHint )
00061 {
00062     this->setupUi(this);
00063     QStringList groupLabels; 
00064     groupLabels << tr( "Group" );
00065     paramGroup = new ParameterGroup(splitter3);
00066     paramGroup->setHeaderLabels(groupLabels);
00067     paramGroup->setRootIsDecorated(false);
00068 
00069     QStringList valueLabels; 
00070     valueLabels << tr( "Name" ) << tr( "Type" ) << tr( "Value" );
00071     paramValue = new ParameterValue(splitter3);
00072     paramValue->setHeaderLabels(valueLabels);
00073     paramValue->setRootIsDecorated(false);
00074     paramValue->header()->setResizeMode(0, QHeaderView::Stretch);
00075   
00076     QSizePolicy policy = paramValue->sizePolicy();
00077     policy.setHorizontalStretch(3);
00078     paramValue->setSizePolicy(policy);
00079 
00080 #if 0 // This is needed for Qt's lupdate
00081     qApp->translate( "Gui::Dialog::DlgParameterImp", "System parameter" );
00082     qApp->translate( "Gui::Dialog::DlgParameterImp", "User parameter" );
00083 #endif
00084 
00085     const std::map<std::string,ParameterManager *> rcList = App::GetApplication().GetParameterSetList();
00086     for (std::map<std::string,ParameterManager *>::const_iterator it= rcList.begin();it!=rcList.end();++it) {
00087         parameterSet->addItem(tr(it->first.c_str()), QVariant(QByteArray(it->first.c_str())));
00088     }
00089 
00090     QByteArray cStr("User parameter");
00091     parameterSet->setCurrentIndex(parameterSet->findData(cStr));
00092     onChangeParameterSet(parameterSet->currentIndex());
00093     parameterSet->hide();
00094 
00095     connect(parameterSet, SIGNAL(activated(int)), 
00096             this, SLOT(onChangeParameterSet(int)));
00097     connect(paramGroup, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), 
00098             this, SLOT(onGroupSelected(QTreeWidgetItem*)));
00099     onGroupSelected(paramGroup->currentItem());
00100 }
00101 
00105 DlgParameterImp::~DlgParameterImp()
00106 {
00107     // no need to delete child widgets, Qt does it all for us
00108 }
00109 
00114 void DlgParameterImp::changeEvent(QEvent *e)
00115 {
00116     if (e->type() == QEvent::LanguageChange) {
00117         retranslateUi(this);
00118         paramGroup->headerItem()->setText( 0, tr( "Group" ) );
00119         paramValue->headerItem()->setText( 0, tr( "Name" ) );
00120         paramValue->headerItem()->setText( 1, tr( "Type" ) );
00121         paramValue->headerItem()->setText( 2, tr( "Value" ) );
00122     } else {
00123         QDialog::changeEvent(e);
00124     }
00125 }
00126 
00127 void DlgParameterImp::on_closeButton_clicked()
00128 {
00129     close();
00130 }
00131 
00132 void DlgParameterImp::accept()
00133 {
00134     close();
00135 }
00136 
00137 void DlgParameterImp::reject()
00138 {
00139     close();
00140 }
00141 
00142 void DlgParameterImp::showEvent(QShowEvent* )
00143 {
00144     ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter()
00145         .GetGroup("BaseApp")->GetGroup("Preferences");
00146     hGrp = hGrp->GetGroup("ParameterEditor");
00147     std::string buf = hGrp->GetASCII("Geometry", "");
00148     if (!buf.empty()) {
00149         int x1, y1, x2, y2;
00150         char sep;
00151         std::stringstream str(buf);
00152         str >> sep >> x1
00153             >> sep >> y1
00154             >> sep >> x2
00155             >> sep >> y2;
00156         QRect rect;
00157         rect.setCoords(x1, y1, x2, y2);
00158         this->setGeometry(rect);
00159     }
00160 }
00161 
00162 void DlgParameterImp::closeEvent(QCloseEvent* )
00163 {
00164     ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter()
00165         .GetGroup("BaseApp")->GetGroup("Preferences");
00166     hGrp = hGrp->GetGroup("ParameterEditor");
00167     QTreeWidgetItem* current = paramGroup->currentItem();
00168     if (current) {
00169         QStringList paths;
00170         paths << current->text(0);
00171         QTreeWidgetItem* parent = current->parent();
00172         while (parent) {
00173             paths.push_front(parent->text(0));
00174             parent = parent->parent();
00175         }
00176 
00177         QString path = paths.join(QLatin1String("."));
00178         hGrp->SetASCII("LastParameterGroup", (const char*)path.toUtf8());
00179         // save geometry of window
00180         const QRect& r = this->geometry();
00181         std::stringstream str;
00182         str << "(" << r.left() << "," << r.top() << ","
00183             << r.right() << "," << r.bottom() << ")";
00184         hGrp->SetASCII("Geometry", str.str().c_str());
00185     }
00186 }
00187 
00188 void DlgParameterImp::onGroupSelected( QTreeWidgetItem * item )
00189 {
00190     if ( item && item->type() == QTreeWidgetItem::UserType + 1 )
00191     {
00192         paramValue->clear();
00193         Base::Reference<ParameterGrp> _hcGrp = static_cast<ParameterGroupItem*>(item)->_hcGrp;
00194         static_cast<ParameterValue*>(paramValue)->setCurrentGroup( _hcGrp );
00195 
00196         // filling up Text nodes
00197         std::vector<std::pair<std::string,std::string> > mcTextMap = _hcGrp->GetASCIIMap();
00198         for(std::vector<std::pair<std::string,std::string> >::iterator It2=mcTextMap.begin();It2!=mcTextMap.end();It2++)
00199         {
00200             (void)new ParameterText(paramValue,QString::fromUtf8(It2->first.c_str()),
00201                 It2->second.c_str(), _hcGrp);
00202         }
00203 
00204         // filling up Int nodes
00205         std::vector<std::pair<std::string,long> > mcIntMap = _hcGrp->GetIntMap();
00206         for(std::vector<std::pair<std::string,long> >::iterator It3=mcIntMap.begin();It3!=mcIntMap.end();It3++)
00207         {
00208             (void)new ParameterInt(paramValue,QString::fromUtf8(It3->first.c_str()),It3->second, _hcGrp);
00209         }
00210 
00211         // filling up Float nodes
00212         std::vector<std::pair<std::string,double> > mcFloatMap = _hcGrp->GetFloatMap();
00213         for(std::vector<std::pair<std::string,double> >::iterator It4=mcFloatMap.begin();It4!=mcFloatMap.end();It4++)
00214         {
00215             (void)new ParameterFloat(paramValue,QString::fromUtf8(It4->first.c_str()),It4->second, _hcGrp);
00216         }
00217 
00218         // filling up bool nodes
00219         std::vector<std::pair<std::string,bool> > mcBoolMap = _hcGrp->GetBoolMap();
00220         for(std::vector<std::pair<std::string,bool> >::iterator It5=mcBoolMap.begin();It5!=mcBoolMap.end();It5++)
00221         {
00222             (void)new ParameterBool(paramValue,QString::fromUtf8(It5->first.c_str()),It5->second, _hcGrp);
00223         }
00224 
00225         // filling up UInt nodes
00226         std::vector<std::pair<std::string,unsigned long> > mcUIntMap = _hcGrp->GetUnsignedMap();
00227         for(std::vector<std::pair<std::string,unsigned long> >::iterator It6=mcUIntMap.begin();It6!=mcUIntMap.end();It6++)
00228         {
00229             (void)new ParameterUInt(paramValue,QString::fromUtf8(It6->first.c_str()),It6->second, _hcGrp);
00230         }
00231     }
00232 }
00233 
00235 void DlgParameterImp::onChangeParameterSet(int index)
00236 {
00237     ParameterManager* rcParMngr = App::GetApplication().GetParameterSet(parameterSet->itemData(index).toByteArray());
00238     if (!rcParMngr)
00239         return
00240 
00241     // remove all labels
00242     paramGroup->clear();
00243     paramValue->clear();
00244 
00245     // root labels
00246     std::vector<Base::Reference<ParameterGrp> > grps = rcParMngr->GetGroups();
00247     for ( std::vector<Base::Reference<ParameterGrp> >::iterator it = grps.begin(); it != grps.end(); ++it ) {
00248         QTreeWidgetItem* item = new ParameterGroupItem(paramGroup, *it);
00249         paramGroup->expandItem(item);
00250         item->setIcon(0, QApplication::style()->standardPixmap(QStyle::SP_ComputerIcon));
00251     }
00252 
00253     // get the path of the last selected group in the editor
00254     ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences");
00255     hGrp = hGrp->GetGroup("ParameterEditor");
00256     QString path = QString::fromUtf8(hGrp->GetASCII("LastParameterGroup").c_str());
00257     QStringList paths = path.split(QLatin1String("."), QString::SkipEmptyParts);
00258     if (paths.empty())
00259         return;
00260     QTreeWidgetItem* parent = 0;
00261     for (int index=0; index < paramGroup->topLevelItemCount(); index++) {
00262         QTreeWidgetItem* child = paramGroup->topLevelItem(index);
00263         if (child->text(0) == paths.front()) {
00264             paths.pop_front();
00265             parent = child;
00266         }
00267     }
00268 
00269     while (parent && !paths.empty()) {
00270         paramGroup->setItemExpanded(parent, true);
00271         QTreeWidgetItem* item = parent;
00272         parent = 0;
00273         for (int index=0; index < item->childCount(); index++) {
00274             QTreeWidgetItem* child = item->child(index);
00275             if (child->text(0) == paths.front()) {
00276                 paths.pop_front();
00277                 parent = child;
00278                 break;
00279             }
00280         }
00281     }
00282 
00283     if (parent)
00284         paramGroup->setCurrentItem(parent);
00285 }
00286 
00287 void DlgParameterImp::on_buttonSaveToDisk_clicked()
00288 {
00289     ParameterManager* sys = App::GetApplication().GetParameterSet("System parameter");
00290     if (sys) {
00291         sys->SaveDocument(App::Application::Config()["SystemParameter"].c_str());
00292     }
00293     ParameterManager* user = App::GetApplication().GetParameterSet("User parameter");
00294     if (user) {
00295         user->SaveDocument(App::Application::Config()["UserParameter"].c_str());
00296     }
00297 }
00298 
00299 namespace Gui {
00300 bool validateInput(QWidget* parent, const QString& input)
00301 {
00302     if (input.isEmpty())
00303         return false;
00304     for (int i=0; i<input.size(); i++) {
00305         const char c = input.at(i).toAscii();
00306         if ((c < '0' || c > '9') &&  // Numbers
00307             (c < 'A' || c > 'Z') &&  // Uppercase letters
00308             (c < 'a' || c > 'z') &&  // Lowercase letters
00309             (c != ' ')) {            // Space
00310             QMessageBox::warning(parent, DlgParameterImp::tr("Invalid input"), 
00311                                          DlgParameterImp::tr("Invalid key name '%1'").arg(input));
00312             return false;
00313         }
00314     }
00315     return true;
00316 }
00317 }
00318 
00319 // --------------------------------------------------------------------
00320 
00321 /* TRANSLATOR Gui::Dialog::ParameterGroup */
00322 
00323 ParameterGroup::ParameterGroup( QWidget * parent )
00324   : QTreeWidget(parent)
00325 {
00326     menuEdit = new QMenu(this);
00327     expandAct = menuEdit->addAction(tr("Expand"), this, SLOT(onToggleSelectedItem()));
00328     menuEdit->addSeparator();
00329     subGrpAct = menuEdit->addAction(tr("Add sub-group"), this, SLOT(onCreateSubgroup()));
00330     removeAct = menuEdit->addAction(tr("Remove group"), this, SLOT(onDeleteSelectedItem()));
00331     renameAct = menuEdit->addAction(tr("Rename group"), this, SLOT(onRenameSelectedItem()));
00332     menuEdit->addSeparator();
00333     exportAct = menuEdit->addAction(tr("Export parameter"), this, SLOT(onExportToFile()));
00334     importAct = menuEdit->addAction(tr("Import parameter"), this, SLOT(onImportFromFile()));
00335     menuEdit->setDefaultAction(expandAct);
00336 }
00337 
00338 ParameterGroup::~ParameterGroup()
00339 {
00340 }
00341 
00342 void ParameterGroup::contextMenuEvent ( QContextMenuEvent* event )
00343 {
00344     QTreeWidgetItem* item = currentItem();
00345     if (isItemSelected(item))
00346     {
00347         expandAct->setEnabled(item->childCount() > 0);
00348         // do not allow to import parameters from a non-empty parameter group
00349         importAct->setEnabled(item->childCount() == 0);
00350 
00351         if ( isItemExpanded(item) )
00352             expandAct->setText( tr("Collapse") );
00353         else
00354         expandAct->setText( tr("Expand") );
00355         menuEdit->popup(event->globalPos());
00356     }
00357 }
00358 
00359 void ParameterGroup::keyPressEvent (QKeyEvent* event)
00360 {
00361     switch ( tolower(event->key()) ) 
00362     {
00363     case Qt::Key_Delete:
00364         {
00365             onDeleteSelectedItem();
00366         }   break;
00367     default:
00368             QTreeWidget::keyPressEvent(event);
00369   }
00370 }
00371 
00372 void ParameterGroup::onDeleteSelectedItem()
00373 {
00374     QTreeWidgetItem* sel = currentItem();
00375     if (isItemSelected(sel) && sel->parent())
00376     {
00377         if ( QMessageBox::question(this, tr("Remove group"), tr("Do really want to remove this parameter group?"),
00378                                QMessageBox::Yes, QMessageBox::No|QMessageBox::Default|QMessageBox::Escape) == 
00379                                QMessageBox::Yes )
00380         {
00381             QTreeWidgetItem* parent = sel->parent();
00382             int index = parent->indexOfChild(sel);
00383             parent->takeChild(index);
00384             ParameterGroupItem* para = static_cast<ParameterGroupItem*>(parent);
00385             para->_hcGrp->RemoveGrp(sel->text(0).toAscii());
00386             delete sel;
00387         }
00388     }
00389 }
00390 
00391 void ParameterGroup::onToggleSelectedItem()
00392 {
00393     QTreeWidgetItem* sel = currentItem();
00394     if (isItemSelected(sel))
00395     {
00396         if ( isItemExpanded(sel) )
00397             setItemExpanded(sel, false);
00398         else if ( sel->childCount() > 0 )
00399             setItemExpanded(sel, true);
00400     }
00401 }
00402 
00403 void ParameterGroup::onCreateSubgroup()
00404 {
00405     bool ok;
00406     QString name = QInputDialog::getText(this, QObject::tr("New sub-group"), QObject::tr("Enter the name:"),
00407                                          QLineEdit::Normal, QString::null, &ok );
00408 
00409     if (ok && Gui::validateInput(this, name))
00410     {
00411         QTreeWidgetItem* item = currentItem();
00412         if (isItemSelected(item))
00413         {
00414             ParameterGroupItem* para = static_cast<ParameterGroupItem*>(item);
00415             Base::Reference<ParameterGrp> hGrp = para->_hcGrp;
00416 
00417             if ( hGrp->HasGroup( name.toAscii() ) )
00418             {
00419                 QMessageBox::critical( this, tr("Existing sub-group"),
00420                     tr("The sub-group '%1' already exists.").arg( name ) );
00421                 return;
00422             }
00423 
00424             hGrp = hGrp->GetGroup( name.toAscii() );
00425             (void)new ParameterGroupItem(para,hGrp);
00426             expandItem(para);
00427         }
00428     }
00429 }
00430 
00431 void ParameterGroup::onExportToFile()
00432 {
00433     QString file = FileDialog::getSaveFileName( this, tr("Export parameter to file"),
00434         QString::null, tr("XML (*.FCParam)"));
00435     if ( !file.isEmpty() )
00436     {
00437         QTreeWidgetItem* item = currentItem();
00438         if (isItemSelected(item))
00439         {
00440             ParameterGroupItem* para = static_cast<ParameterGroupItem*>(item);
00441             Base::Reference<ParameterGrp> hGrp = para->_hcGrp;
00442             hGrp->exportTo( file.toUtf8() );
00443         }
00444     }
00445 }
00446 
00447 void ParameterGroup::onImportFromFile()
00448 {
00449     QString file = FileDialog::getOpenFileName( this, tr("Import parameter from file"),
00450         QString::null, tr("XML (*.FCParam)"));
00451     if ( !file.isEmpty() )
00452     {
00453         QFileInfo fi(file);
00454         QTreeWidgetItem* item = currentItem();
00455         if (isItemSelected(item))
00456         {
00457             ParameterGroupItem* para = static_cast<ParameterGroupItem*>(item);
00458             Base::Reference<ParameterGrp> hGrp = para->_hcGrp;
00459 
00460             // remove the items and internal parameter values
00461             QList<QTreeWidgetItem*> childs = para->takeChildren();
00462             for (QList<QTreeWidgetItem*>::iterator it = childs.begin(); it != childs.end(); ++it)
00463             {
00464                 delete *it;
00465             }
00466 
00467             try
00468             {
00469                 hGrp->importFrom( file.toUtf8() );
00470                 std::vector<Base::Reference<ParameterGrp> > cSubGrps = hGrp->GetGroups();
00471                 for ( std::vector<Base::Reference<ParameterGrp> >::iterator it = cSubGrps.begin(); it != cSubGrps.end(); ++it )
00472                 {
00473                     new ParameterGroupItem(para,*it);
00474                 }
00475 
00476                 setItemExpanded(para, para->childCount());
00477             }
00478             catch( const Base::Exception& )
00479             {
00480                 QMessageBox::critical(this, tr("Import Error"),tr("Reading from '%1' failed.").arg( file ));
00481             }
00482         }
00483     }
00484 }
00485 
00486 void ParameterGroup::onRenameSelectedItem()
00487 {
00488     QTreeWidgetItem* sel = currentItem();
00489     if (isItemSelected(sel))
00490     {
00491         editItem(sel, 0);
00492     }
00493 }
00494 
00495 void ParameterGroup::changeEvent(QEvent *e)
00496 {
00497     if (e->type() == QEvent::LanguageChange) {
00498         expandAct->setText(tr("Expand"));
00499         subGrpAct->setText(tr("Add sub-group"));
00500         removeAct->setText(tr("Remove group"));
00501         renameAct->setText(tr("Rename group"));
00502         exportAct->setText(tr("Export parameter"));
00503         importAct->setText(tr("Import parameter"));
00504     } else {
00505         QTreeWidget::changeEvent(e);
00506     }
00507 }
00508 
00509 // --------------------------------------------------------------------
00510 
00511 /* TRANSLATOR Gui::Dialog::ParameterValue */
00512 
00513 ParameterValue::ParameterValue( QWidget * parent )
00514   : QTreeWidget(parent)
00515 {
00516     menuEdit = new QMenu(this);
00517     changeAct = menuEdit->addAction(tr("Change value"), this, SLOT(onChangeSelectedItem()));
00518     menuEdit->addSeparator();
00519     removeAct = menuEdit->addAction(tr("Remove key"), this, SLOT(onDeleteSelectedItem()));
00520     renameAct = menuEdit->addAction(tr("Rename key"), this, SLOT(onRenameSelectedItem()));
00521     menuEdit->setDefaultAction(changeAct);
00522 
00523     menuEdit->addSeparator();
00524     menuNew = menuEdit->addMenu(tr("New"));
00525     newStrAct = menuNew->addAction(tr("New string item"), this, SLOT(onCreateTextItem()));
00526     newFltAct = menuNew->addAction(tr("New float item"), this, SLOT(onCreateFloatItem()));
00527     newIntAct = menuNew->addAction(tr("New integer item"), this, SLOT(onCreateIntItem()));
00528     newUlgAct = menuNew->addAction(tr("New unsigned item"), this, SLOT(onCreateUIntItem()));
00529     newBlnAct = menuNew->addAction(tr("New Boolean item"), this, SLOT(onCreateBoolItem()));
00530 
00531     connect(this, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), 
00532             this, SLOT(onChangeSelectedItem(QTreeWidgetItem*, int)));
00533 }
00534 
00535 ParameterValue::~ParameterValue()
00536 {
00537 }
00538 
00539 void ParameterValue::setCurrentGroup( const Base::Reference<ParameterGrp>& hGrp )
00540 {
00541     _hcGrp = hGrp;
00542 }
00543 
00544 bool ParameterValue::edit ( const QModelIndex & index, EditTrigger trigger, QEvent * event )
00545 {
00546     if (index.column() > 0)
00547         return false;
00548     return QTreeWidget::edit(index, trigger, event);
00549 }
00550 
00551 void ParameterValue::contextMenuEvent ( QContextMenuEvent* event )
00552 {
00553     QTreeWidgetItem* item = currentItem();
00554     if (isItemSelected(item))
00555         menuEdit->popup(event->globalPos());
00556     else
00557         menuNew->popup(event->globalPos());
00558 }
00559 
00560 void ParameterValue::keyPressEvent (QKeyEvent* event)
00561 {
00562     switch ( tolower(event->key()) ) 
00563     {
00564     case Qt::Key_Delete:
00565         {
00566             onDeleteSelectedItem();
00567         }   break;
00568     default:
00569             QTreeWidget::keyPressEvent(event);
00570   }
00571 }
00572 
00573 void ParameterValue::onChangeSelectedItem(QTreeWidgetItem* item, int col)
00574 {
00575     if (isItemSelected(item) && col > 0)
00576     {
00577         static_cast<ParameterValueItem*>(item)->changeValue();
00578     }
00579 }
00580 
00581 void ParameterValue::onChangeSelectedItem()
00582 {
00583     onChangeSelectedItem(currentItem(), 1);
00584 }
00585 
00586 void ParameterValue::onDeleteSelectedItem()
00587 {
00588     QTreeWidgetItem* sel = currentItem();
00589     if (isItemSelected(sel))
00590     {
00591         takeTopLevelItem(indexOfTopLevelItem(sel));
00592         static_cast<ParameterValueItem*>(sel)->removeFromGroup();
00593         delete sel;
00594     }
00595 }
00596 
00597 void ParameterValue::onRenameSelectedItem()
00598 {
00599     QTreeWidgetItem* sel = currentItem();
00600     if (isItemSelected(sel))
00601     {
00602         editItem(sel, 0);
00603     }
00604 }
00605 
00606 void ParameterValue::onCreateTextItem()
00607 {
00608     bool ok;
00609     QString name = QInputDialog::getText(this, QObject::tr("New text item"), QObject::tr("Enter the name:"), 
00610                                          QLineEdit::Normal, QString::null, &ok);
00611 
00612     if (!ok || !Gui::validateInput(this, name))
00613         return;
00614 
00615     std::vector<std::pair<std::string,std::string> > smap = _hcGrp->GetASCIIMap();
00616     for (std::vector<std::pair<std::string,std::string> >::iterator it = smap.begin(); it != smap.end(); ++it) {
00617         if (name == QLatin1String(it->first.c_str()))
00618         {
00619             QMessageBox::critical( this, tr("Existing item"),
00620                 tr("The item '%1' already exists.").arg( name ) );
00621             return;
00622         }
00623     }
00624 
00625     QString val = QInputDialog::getText(this, QObject::tr("New text item"), QObject::tr("Enter your text:"), 
00626                                         QLineEdit::Normal, QString::null, &ok);
00627     if ( ok && !val.isEmpty() )
00628     {
00629         ParameterValueItem *pcItem;
00630         pcItem = new ParameterText(this, name, val.toUtf8(), _hcGrp);
00631         pcItem->appendToGroup();
00632     }
00633 }
00634 
00635 void ParameterValue::onCreateIntItem()
00636 {
00637     bool ok;
00638     QString name = QInputDialog::getText(this, QObject::tr("New integer item"), QObject::tr("Enter the name:"), 
00639                                          QLineEdit::Normal, QString::null, &ok);
00640 
00641     if (!ok || !Gui::validateInput(this, name))
00642         return;
00643 
00644     std::vector<std::pair<std::string,long> > lmap = _hcGrp->GetIntMap();
00645     for (std::vector<std::pair<std::string,long> >::iterator it = lmap.begin(); it != lmap.end(); ++it) {
00646         if (name == QLatin1String(it->first.c_str()))
00647         {
00648             QMessageBox::critical( this, tr("Existing item"),
00649                 tr("The item '%1' already exists.").arg( name ) );
00650             return;
00651         }
00652     }
00653 
00654     int val = QInputDialog::getInteger(this, QObject::tr("New integer item"), QObject::tr("Enter your number:"), 
00655                                        0, -2147483647, 2147483647, 1, &ok);
00656 
00657     if ( ok )
00658     {
00659         ParameterValueItem *pcItem;
00660         pcItem = new ParameterInt(this,name,(long)val, _hcGrp);
00661         pcItem->appendToGroup();
00662     }
00663 }
00664 
00665 void ParameterValue::onCreateUIntItem()
00666 {
00667     bool ok;
00668     QString name = QInputDialog::getText(this, QObject::tr("New unsigned item"), QObject::tr("Enter the name:"), 
00669                                          QLineEdit::Normal, QString::null, &ok);
00670 
00671     if (!ok || !Gui::validateInput(this, name))
00672         return;
00673 
00674     std::vector<std::pair<std::string,unsigned long> > lmap = _hcGrp->GetUnsignedMap();
00675     for (std::vector<std::pair<std::string,unsigned long> >::iterator it = lmap.begin(); it != lmap.end(); ++it) {
00676         if (name == QLatin1String(it->first.c_str()))
00677         {
00678             QMessageBox::critical( this, tr("Existing item"),
00679                 tr("The item '%1' already exists.").arg( name ) );
00680             return;
00681         }
00682     }
00683 
00684     DlgInputDialogImp dlg(QObject::tr("Enter your number:"),this, true, DlgInputDialogImp::UIntBox);
00685     dlg.setWindowTitle(QObject::tr("New unsigned item"));
00686     UIntSpinBox* edit = dlg.getUIntBox();
00687     edit->setRange(0,UINT_MAX);
00688     if (dlg.exec() == QDialog::Accepted ) {
00689         QString value = edit->text();
00690         unsigned long val = value.toULong(&ok);
00691 
00692         if ( ok )
00693         {
00694             ParameterValueItem *pcItem;
00695             pcItem = new ParameterUInt(this,name, val, _hcGrp);
00696             pcItem->appendToGroup();
00697         }
00698     }
00699 }
00700 
00701 void ParameterValue::onCreateFloatItem()
00702 {
00703     bool ok;
00704     QString name = QInputDialog::getText(this, QObject::tr("New float item"), QObject::tr("Enter the name:"), 
00705                                          QLineEdit::Normal, QString::null, &ok);
00706 
00707     if (!ok || !Gui::validateInput(this, name))
00708         return;
00709 
00710     std::vector<std::pair<std::string,double> > fmap = _hcGrp->GetFloatMap();
00711     for (std::vector<std::pair<std::string,double> >::iterator it = fmap.begin(); it != fmap.end(); ++it) {
00712         if (name == QLatin1String(it->first.c_str()))
00713         {
00714             QMessageBox::critical( this, tr("Existing item"),
00715                 tr("The item '%1' already exists.").arg( name ) );
00716             return;
00717         }
00718     }
00719   
00720     double val = QInputDialog::getDouble(this, QObject::tr("New float item"), QObject::tr("Enter your number:"), 
00721                                          0, -2147483647, 2147483647, 12, &ok);
00722     if ( ok )
00723     {
00724         ParameterValueItem *pcItem;
00725         pcItem = new ParameterFloat(this,name,val, _hcGrp);
00726         pcItem->appendToGroup();
00727     }
00728 }
00729 
00730 void ParameterValue::onCreateBoolItem()
00731 {
00732     bool ok;
00733     QString name = QInputDialog::getText(this, QObject::tr("New Boolean item"), QObject::tr("Enter the name:"), 
00734                                          QLineEdit::Normal, QString::null, &ok);
00735 
00736     if (!ok || !Gui::validateInput(this, name))
00737         return;
00738 
00739     std::vector<std::pair<std::string,bool> > bmap = _hcGrp->GetBoolMap();
00740     for (std::vector<std::pair<std::string,bool> >::iterator it = bmap.begin(); it != bmap.end(); ++it) {
00741         if (name == QLatin1String(it->first.c_str()))
00742         {
00743             QMessageBox::critical( this, tr("Existing item"),
00744                 tr("The item '%1' already exists.").arg( name ) );
00745             return;
00746         }
00747     }
00748 
00749     QStringList list; list << QString::fromAscii("true")
00750                            << QString::fromAscii("false");
00751     QString val = QInputDialog::getItem (this, QObject::tr("New boolean item"), QObject::tr("Choose an item:"),
00752                                          list, 0, false, &ok);
00753     if ( ok )
00754     {
00755         ParameterValueItem *pcItem;
00756         pcItem = new ParameterBool(this,name,(val == list[0] ? true : false), _hcGrp);
00757         pcItem->appendToGroup();
00758     }
00759 }
00760 
00761 // ---------------------------------------------------------------------------
00762 
00763 ParameterGroupItem::ParameterGroupItem( ParameterGroupItem * parent, const Base::Reference<ParameterGrp> &hcGrp )
00764     : QTreeWidgetItem( parent, QTreeWidgetItem::UserType+1 ), _hcGrp(hcGrp)
00765 {
00766     setFlags(flags() | Qt::ItemIsEditable);
00767     fillUp();
00768 }
00769 
00770 ParameterGroupItem::ParameterGroupItem( QTreeWidget* parent, const Base::Reference<ParameterGrp> &hcGrp)
00771     : QTreeWidgetItem( parent, QTreeWidgetItem::UserType+1 ), _hcGrp(hcGrp)
00772 {
00773     setFlags(flags() | Qt::ItemIsEditable);
00774     fillUp();
00775 }
00776 
00777 ParameterGroupItem::~ParameterGroupItem()
00778 {
00779   // if the group has already been removed from the parameters then clear the observer list
00780   // as we cannot notify the attached observers here
00781   if (_hcGrp.getRefCount() == 1)
00782     _hcGrp->ClearObserver();
00783 }
00784 
00785 void ParameterGroupItem::fillUp(void)
00786 {
00787     // filing up groups
00788     std::vector<Base::Reference<ParameterGrp> > vhcParamGrp = _hcGrp->GetGroups();
00789 
00790     setText(0,QString::fromUtf8(_hcGrp->GetGroupName()));
00791     for(std::vector<Base::Reference<ParameterGrp> >::iterator It=vhcParamGrp.begin();It!=vhcParamGrp.end();It++)
00792         (void)new ParameterGroupItem(this,*It);
00793 }
00794 
00795 void ParameterGroupItem::setData ( int column, int role, const QVariant & value )
00796 {
00797     if (role == Qt::EditRole) {
00798         QString oldName = text(0);
00799         QString newName = value.toString();
00800         if (newName.isEmpty() || oldName == newName)
00801             return;
00802 
00803         if (!Gui::validateInput(treeWidget(), newName))
00804             return;
00805 
00806         // first check if there is already a group with name "newName"
00807         ParameterGroupItem* item = static_cast<ParameterGroupItem*>(parent());
00808         if ( !item )
00809         {
00810             QMessageBox::critical( treeWidget(), QObject::tr("Rename group"),
00811                 QObject::tr("The group '%1' cannot be renamed.").arg( oldName ) );
00812             return;
00813         }
00814         if ( item->_hcGrp->HasGroup( newName.toAscii() ) )
00815         {
00816             QMessageBox::critical( treeWidget(), QObject::tr("Existing group"),
00817                 QObject::tr("The group '%1' already exists.").arg( newName ) );
00818             return;
00819         } 
00820         else 
00821         {
00822             // rename the group by adding a new group, copy the content and remove the old group
00823             Base::Reference<ParameterGrp> hOldGrp = item->_hcGrp->GetGroup( oldName.toAscii() );
00824             Base::Reference<ParameterGrp> hNewGrp = item->_hcGrp->GetGroup( newName.toAscii() );
00825             hOldGrp->copyTo( hNewGrp );
00826             item->_hcGrp->RemoveGrp( oldName.toAscii() );
00827         }
00828     }
00829 
00830     QTreeWidgetItem::setData(column, role, value);
00831 }
00832 
00833 QVariant ParameterGroupItem::data ( int column, int role ) const
00834 {
00835     if (role == Qt::DecorationRole) {
00836         // The root item should keep its special pixmap
00837         if (parent()) {
00838             return treeWidget()->isItemExpanded(this) ?
00839                 QApplication::style()->standardPixmap(QStyle::SP_DirOpenIcon):
00840                 QApplication::style()->standardPixmap(QStyle::SP_DirClosedIcon);
00841         }
00842     }
00843 
00844     return QTreeWidgetItem::data(column, role);
00845 }
00846 
00847 // --------------------------------------------------------------------
00848 
00849 ParameterValueItem::ParameterValueItem ( QTreeWidget* parent, const Base::Reference<ParameterGrp> &hcGrp)
00850   : QTreeWidgetItem( parent ), _hcGrp(hcGrp)
00851 {
00852     setFlags(flags() | Qt::ItemIsEditable);
00853 }
00854 
00855 ParameterValueItem::~ParameterValueItem()
00856 {
00857 }
00858 
00859 void ParameterValueItem::setData ( int column, int role, const QVariant & value )
00860 {
00861     if (role == Qt::EditRole) {
00862         QString oldName = text(0);
00863         QString newName = value.toString();
00864         if (newName.isEmpty() || oldName == newName)
00865             return;
00866 
00867         if (!Gui::validateInput(treeWidget(), newName))
00868             return;
00869 
00870         replace( oldName, newName );
00871     }
00872 
00873     QTreeWidgetItem::setData(column, role, value);
00874 }
00875 
00876 // --------------------------------------------------------------------
00877 
00878 ParameterText::ParameterText ( QTreeWidget * parent, QString label, const char* value, const Base::Reference<ParameterGrp> &hcGrp)
00879   :ParameterValueItem( parent, hcGrp)
00880 {
00881     setIcon(0,BitmapFactory().pixmap("Param_Text") );
00882     setText(0, label);
00883     setText(1, QString::fromAscii("Text"));
00884     setText(2, QString::fromUtf8(value));
00885 }
00886 
00887 ParameterText::~ParameterText()
00888 {
00889 }
00890 
00891 void ParameterText::changeValue()
00892 {
00893     bool ok;
00894     QString txt = QInputDialog::getText(treeWidget(), QObject::tr("Change value"), QObject::tr("Enter your text:"), 
00895                                         QLineEdit::Normal, text(2), &ok);
00896     if ( ok )
00897     {
00898         setText( 2, txt );
00899         _hcGrp->SetASCII(text(0).toAscii(), txt.toUtf8());
00900     }
00901 }
00902 
00903 void ParameterText::removeFromGroup ()
00904 {
00905     _hcGrp->RemoveASCII(text(0).toAscii());
00906 }
00907 
00908 void ParameterText::replace( const QString& oldName, const QString& newName )
00909 {
00910     std::string val = _hcGrp->GetASCII(oldName.toAscii());
00911     _hcGrp->RemoveASCII(oldName.toAscii());
00912     _hcGrp->SetASCII(newName.toAscii(), val.c_str());
00913 }
00914 
00915 void ParameterText::appendToGroup()
00916 {
00917     _hcGrp->SetASCII(text(0).toAscii(), text(2).toUtf8());
00918 }
00919 
00920 // --------------------------------------------------------------------
00921 
00922 ParameterInt::ParameterInt ( QTreeWidget * parent, QString label, long value, const Base::Reference<ParameterGrp> &hcGrp)
00923   :ParameterValueItem( parent, hcGrp)
00924 {
00925     setIcon(0,BitmapFactory().pixmap("Param_Int") );
00926     setText(0, label);
00927     setText(1, QString::fromAscii("Integer"));
00928     setText(2, QString::fromAscii("%1").arg(value));
00929 }
00930 
00931 ParameterInt::~ParameterInt()
00932 {
00933 }
00934 
00935 void ParameterInt::changeValue()
00936 {
00937     bool ok;
00938     int num = QInputDialog::getInteger(treeWidget(), QObject::tr("Change value"), QObject::tr("Enter your number:"), 
00939                                        text(2).toInt(), -2147483647, 2147483647, 1, &ok);
00940     if ( ok )
00941     {
00942         setText(2, QString::fromAscii("%1").arg(num));
00943         _hcGrp->SetInt(text(0).toAscii(), (long)num);
00944     }
00945 }
00946 
00947 void ParameterInt::removeFromGroup ()
00948 {
00949     _hcGrp->RemoveInt(text(0).toAscii());
00950 }
00951 
00952 void ParameterInt::replace( const QString& oldName, const QString& newName )
00953 {
00954     long val = _hcGrp->GetInt(oldName.toAscii());
00955     _hcGrp->RemoveInt(oldName.toAscii());
00956     _hcGrp->SetInt(newName.toAscii(), val);
00957 }
00958 
00959 void ParameterInt::appendToGroup()
00960 {
00961     _hcGrp->SetInt(text(0).toAscii(), text(2).toLong());
00962 }
00963 
00964 // --------------------------------------------------------------------
00965 
00966 ParameterUInt::ParameterUInt ( QTreeWidget * parent, QString label, unsigned long value, const Base::Reference<ParameterGrp> &hcGrp)
00967   :ParameterValueItem( parent, hcGrp)
00968 {
00969     setIcon(0,BitmapFactory().pixmap("Param_UInt") );
00970     setText(0, label);
00971     setText(1, QString::fromAscii("Unsigned"));
00972     setText(2, QString::fromAscii("%1").arg(value));
00973 }
00974 
00975 ParameterUInt::~ParameterUInt()
00976 {
00977 }
00978 
00979 void ParameterUInt::changeValue()
00980 {
00981     bool ok;
00982     DlgInputDialogImp dlg(QObject::tr("Enter your number:"),treeWidget(), true, DlgInputDialogImp::UIntBox);
00983     dlg.setWindowTitle(QObject::tr("Change value"));
00984     UIntSpinBox* edit = dlg.getUIntBox();
00985     edit->setRange(0,UINT_MAX);
00986     edit->setValue(text(2).toULong());
00987     if (dlg.exec() == QDialog::Accepted)
00988     {
00989         QString value = edit->text();
00990         unsigned long num = value.toULong(&ok);
00991 
00992         if ( ok )
00993         {
00994             setText(2, QString::fromAscii("%1").arg(num));
00995             _hcGrp->SetUnsigned(text(0).toAscii(), (unsigned long)num);
00996         }
00997     }
00998 }
00999 
01000 void ParameterUInt::removeFromGroup ()
01001 {
01002     _hcGrp->RemoveUnsigned(text(0).toAscii());
01003 }
01004 
01005 void ParameterUInt::replace( const QString& oldName, const QString& newName )
01006 {
01007     unsigned long val = _hcGrp->GetUnsigned(oldName.toAscii());
01008     _hcGrp->RemoveUnsigned(oldName.toAscii());
01009     _hcGrp->SetUnsigned(newName.toAscii(), val);
01010 }
01011 
01012 void ParameterUInt::appendToGroup()
01013 {
01014     _hcGrp->SetUnsigned(text(0).toAscii(), text(2).toULong());
01015 }
01016 
01017 // --------------------------------------------------------------------
01018 
01019 ParameterFloat::ParameterFloat ( QTreeWidget * parent, QString label, double value, const Base::Reference<ParameterGrp> &hcGrp)
01020   :ParameterValueItem( parent, hcGrp)
01021 {
01022     setIcon(0,BitmapFactory().pixmap("Param_Float") );
01023     setText(0, label);
01024     setText(1, QString::fromAscii("Float"));
01025     setText(2, QString::fromAscii("%1").arg(value));
01026 }
01027 
01028 ParameterFloat::~ParameterFloat()
01029 {
01030 }
01031 
01032 void ParameterFloat::changeValue()
01033 {
01034     bool ok;
01035     double num = QInputDialog::getDouble(treeWidget(), QObject::tr("Change value"), QObject::tr("Enter your number:"), 
01036                                          text(2).toDouble(), -2147483647, 2147483647, 12, &ok);
01037     if ( ok )
01038     {
01039         setText(2, QString::fromAscii("%1").arg(num));
01040         _hcGrp->SetFloat(text(0).toAscii(), num);
01041     }
01042 }
01043 
01044 void ParameterFloat::removeFromGroup ()
01045 {
01046     _hcGrp->RemoveFloat(text(0).toAscii());
01047 }
01048 
01049 void ParameterFloat::replace( const QString& oldName, const QString& newName )
01050 {
01051     double val = _hcGrp->GetFloat(oldName.toAscii());
01052     _hcGrp->RemoveFloat(oldName.toAscii());
01053     _hcGrp->SetFloat(newName.toAscii(), val);
01054 }
01055 
01056 void ParameterFloat::appendToGroup()
01057 {
01058     _hcGrp->SetFloat(text(0).toAscii(), text(2).toDouble());
01059 }
01060 
01061 // --------------------------------------------------------------------
01062 
01063 ParameterBool::ParameterBool ( QTreeWidget * parent, QString label, bool value, const Base::Reference<ParameterGrp> &hcGrp)
01064   :ParameterValueItem( parent, hcGrp)
01065 {
01066     setIcon(0,BitmapFactory().pixmap("Param_Bool") );
01067     setText(0, label);
01068     setText(1, QString::fromAscii("Boolean"));
01069     setText(2, QString::fromAscii((value ? "true" : "false")));
01070 }
01071 
01072 ParameterBool::~ParameterBool()
01073 {
01074 }
01075 
01076 void ParameterBool::changeValue()
01077 {
01078     bool ok;
01079     QStringList list; list << QString::fromAscii("true") 
01080                            << QString::fromAscii("false");
01081     int pos = (text(2) == list[0] ? 0 : 1);
01082 
01083     QString txt = QInputDialog::getItem (treeWidget(), QObject::tr("Change value"), QObject::tr("Choose an item:"),
01084                                          list, pos, false, &ok);
01085     if ( ok )
01086     {
01087         setText( 2, txt );
01088         _hcGrp->SetBool(text(0).toAscii(), (txt == list[0] ? true : false) );
01089     }
01090 }
01091 
01092 void ParameterBool::removeFromGroup ()
01093 {
01094     _hcGrp->RemoveBool(text(0).toAscii());
01095 }
01096 
01097 void ParameterBool::replace( const QString& oldName, const QString& newName )
01098 {
01099     bool val = _hcGrp->GetBool(oldName.toAscii());
01100     _hcGrp->RemoveBool(oldName.toAscii());
01101     _hcGrp->SetBool(newName.toAscii(), val);
01102 }
01103 
01104 void ParameterBool::appendToGroup()
01105 {
01106     bool val = (text(2) == QLatin1String("true") ? true : false);
01107     _hcGrp->SetBool(text(0).toAscii(), val);
01108 }
01109 
01110 #include "moc_DlgParameterImp.cpp"

Generated on Wed Nov 23 19:00:06 2011 for FreeCAD by  doxygen 1.6.1