DlgPreferencesImp.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 <cstring>
00027 # include <algorithm>
00028 # include <QMessageBox>
00029 #endif
00030 
00031 #include <Base/Exception.h>
00032 #include <Base/Console.h> 
00033 
00034 #include "DlgPreferencesImp.h"
00035 #include "PropertyPage.h"
00036 #include "WidgetFactory.h"
00037 #include "BitmapFactory.h"
00038 #include "MainWindow.h"
00039 
00040 
00041 using namespace Gui::Dialog;
00042 
00043 /* TRANSLATOR Gui::Dialog::DlgPreferencesImp */
00044 
00045 std::list<DlgPreferencesImp::TGroupPages> DlgPreferencesImp::_pages;
00046 
00054 DlgPreferencesImp::DlgPreferencesImp( QWidget* parent, Qt::WFlags fl )
00055     : QDialog(parent, fl)
00056 {
00057     this->setupUi(this);
00058     connect( buttonHelp,  SIGNAL ( clicked() ), getMainWindow(), SLOT ( whatsThis() ));
00059     connect(listBox, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
00060             this, SLOT(changeGroup(QListWidgetItem *, QListWidgetItem*)));
00061 
00062     setupPages();
00063 }
00064 
00068 DlgPreferencesImp::~DlgPreferencesImp()
00069 {
00070   // no need to delete child widgets, Qt does it all for us
00071 }
00072 
00073 void DlgPreferencesImp::setupPages()
00074 {
00075     // make sure that pages are ready to create
00076     GetWidgetFactorySupplier();
00077     for (std::list<TGroupPages>::iterator it = _pages.begin(); it != _pages.end(); ++it) {
00078         QTabWidget* tabWidget = new QTabWidget;
00079         this->tabWidgetStack->addWidget(tabWidget);
00080         
00081         QByteArray group = it->first.c_str();
00082         QListWidgetItem *item = new QListWidgetItem(listBox);
00083         item->setData(Qt::UserRole, QVariant(group));
00084         item->setText(QObject::tr(group.constData()));
00085         std::string fileName = it->first;
00086         for (std::string::iterator ch = fileName.begin(); ch != fileName.end(); ++ch) {
00087             if (*ch == ' ') *ch = '_';
00088             else *ch = tolower(*ch);
00089         }
00090         fileName = std::string("preferences-") + fileName;
00091         QPixmap icon = Gui::BitmapFactory().pixmapFromSvg(fileName.c_str(), QSize(96,96));
00092         item->setIcon(icon);
00093         item->setTextAlignment(Qt::AlignHCenter);
00094         item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
00095         for (std::list<std::string>::iterator jt = it->second.begin(); jt != it->second.end(); ++jt) {
00096             PreferencePage* page = WidgetFactory().createPreferencePage(jt->c_str());
00097             if (page) {
00098                 tabWidget->addTab(page, page->windowTitle());
00099                 page->loadSettings();
00100             }
00101             else {
00102                 Base::Console().Warning("%s is not a preference page\n", jt->c_str());
00103             }
00104         }
00105     }
00106 
00107     // show the first group
00108     listBox->setCurrentRow(0);
00109 }
00110 
00111 void DlgPreferencesImp::changeGroup(QListWidgetItem *current, QListWidgetItem *previous)
00112 {
00113     if (!current)
00114         current = previous;
00115     tabWidgetStack->setCurrentIndex(listBox->row(current));
00116 }
00117 
00125 void DlgPreferencesImp::addPage(const std::string& className, const std::string& group)
00126 {
00127     for (std::list<TGroupPages>::iterator it = _pages.begin(); it != _pages.end(); ++it) {
00128         if (it->first == group) {
00129             it->second.push_back(className);
00130             return;
00131         }
00132     }
00133 
00134     std::list<std::string> pages;
00135     pages.push_back(className);
00136     _pages.push_back(std::make_pair(group, pages));
00137 }
00138 
00139 void DlgPreferencesImp::removePage(const std::string& className, const std::string& group)
00140 {
00141     for (std::list<TGroupPages>::iterator it = _pages.begin(); it != _pages.end(); ++it) {
00142         if (it->first == group) {
00143             if (className.empty()) {
00144                 _pages.erase(it);
00145                 return;
00146             }
00147             else {
00148                 std::list<std::string>& p = it->second;
00149                 for (std::list<std::string>::iterator jt = p.begin(); jt != p.end(); ++jt) {
00150                     if (*jt == className) {
00151                         p.erase(jt);
00152                         if (p.empty())
00153                             _pages.erase(it);
00154                         return;
00155                     }
00156                 }
00157             }
00158         }
00159     }
00160 }
00161 
00165 void DlgPreferencesImp::activateGroupPage(const QString& group, int index)
00166 {
00167     int ct = listBox->count();
00168     for (int i=0; i<ct; i++) {
00169         QListWidgetItem* item = listBox->item(i);
00170         if (item->data(Qt::UserRole).toString() == group) {
00171             listBox->setCurrentItem(item);
00172             QTabWidget* tabWidget = (QTabWidget*)tabWidgetStack->widget(i);
00173             tabWidget->setCurrentIndex(index);
00174             break;
00175         }
00176     }
00177 }
00178 
00179 void DlgPreferencesImp::accept()
00180 {
00181     this->invalidParameter = false;
00182     on_buttonApply_clicked();
00183     if (!this->invalidParameter)
00184         QDialog::accept();
00185 }
00186 
00187 void DlgPreferencesImp::on_buttonApply_clicked()
00188 {
00189     try {
00190         for (int i=0; i<tabWidgetStack->count(); i++) {
00191             QTabWidget* tabWidget = (QTabWidget*)tabWidgetStack->widget(i);
00192             for (int j=0; j<tabWidget->count(); j++) {
00193                 QWidget* page = tabWidget->widget(j);
00194                 int index = page->metaObject()->indexOfMethod("checkSettings()");
00195                 try {
00196                     if (index >= 0) {
00197                         page->qt_metacall(QMetaObject::InvokeMetaMethod, index, 0);
00198                     }
00199                 } catch (const Base::Exception& e) {
00200                     listBox->setCurrentRow(i);
00201                     tabWidget->setCurrentIndex(j);
00202                     QMessageBox::warning(this, tr("Wrong parameter"), QString::fromAscii(e.what()));
00203                     throw;
00204                 }
00205             }
00206         }
00207     } catch (const Base::Exception&) {
00208         this->invalidParameter = true;
00209         return;
00210     }
00211 
00212     for (int i=0; i<tabWidgetStack->count(); i++) {
00213         QTabWidget* tabWidget = (QTabWidget*)tabWidgetStack->widget(i);
00214         for (int j=0; j<tabWidget->count(); j++) {
00215             PreferencePage* page = qobject_cast<PreferencePage*>(tabWidget->widget(j));
00216             if (page)
00217                 page->saveSettings();
00218         }
00219     }
00220 }
00221 
00222 void DlgPreferencesImp::changeEvent(QEvent *e)
00223 {
00224     if (e->type() == QEvent::LanguageChange) {
00225         retranslateUi(this);
00226         // update the widgets' tabs
00227         for (int i=0; i<tabWidgetStack->count(); i++) {
00228             QTabWidget* tabWidget = (QTabWidget*)tabWidgetStack->widget(i);
00229             for (int j=0; j<tabWidget->count(); j++) {
00230                 QWidget* page = tabWidget->widget(j);
00231                 tabWidget->setTabText(j, page->windowTitle());
00232             }
00233         }
00234         // update the items' text
00235         for (int i=0; i<listBox->count(); i++) {
00236             QListWidgetItem *item = listBox->item(i);
00237             QByteArray group = item->data(Qt::UserRole).toByteArray();
00238             item->setText(QObject::tr(group.constData()));
00239         }
00240     } else {
00241         QWidget::changeEvent(e);
00242     }
00243 }
00244 
00245 #include "moc_DlgPreferencesImp.cpp"

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