PrefWidgets.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net>     *
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 
00026 #include <Base/Console.h>
00027 
00028 #include "PrefWidgets.h"
00029 #include "FileDialog.h"
00030 #include <cstring>
00031 
00032 using Base::Console;
00033 using namespace Gui;
00034 
00037 PrefWidget::PrefWidget()
00038  : WindowParameter("")
00039 {
00040 }
00041 
00045 PrefWidget::~PrefWidget()
00046 {
00047   if (getWindowParameter().isValid())
00048     getWindowParameter()->Detach(this);
00049 }
00050 
00052 void PrefWidget::setEntryName( const QByteArray& name )
00053 {
00054   m_sPrefName = name;
00055 }
00056 
00058 QByteArray PrefWidget::entryName() const
00059 {
00060   return m_sPrefName;
00061 }
00062 
00064 void PrefWidget::setParamGrpPath( const QByteArray& path )
00065 {
00066 #ifdef FC_DEBUG
00067   if (getWindowParameter().isValid())
00068   {
00069     if ( paramGrpPath() != path )
00070       Base::Console().Warning("Widget already attached\n");
00071   }
00072 #endif
00073 
00074   if ( paramGrpPath() != path )
00075   {
00076     if ( setGroupName( path ) )
00077     {
00078       m_sPrefGrp = path;
00079       assert(getWindowParameter().isValid());
00080       getWindowParameter()->Attach(this);
00081     }
00082   }
00083 }
00084 
00086 QByteArray PrefWidget::paramGrpPath() const
00087 {
00088   return m_sPrefGrp;
00089 }
00090 
00098 void PrefWidget::OnChange(Base::Subject<const char*> &rCaller, const char * sReason)
00099 {
00100     if (std::strcmp(sReason,m_sPrefName) == 0)
00101         restorePreferences();
00102 }
00103 
00108 void PrefWidget::onSave()
00109 {
00110   savePreferences();
00111   if (getWindowParameter().isValid())
00112     getWindowParameter()->Notify( entryName() );
00113 #ifdef FC_DEBUG
00114   else
00115     qFatal( "No parameter group specified!" );
00116 #endif
00117 }
00118 
00122 void PrefWidget::onRestore()
00123 {
00124 #ifdef FC_DEBUG
00125   if (getWindowParameter().isNull())
00126     qWarning( "No parameter group specified!" );
00127 #endif
00128   restorePreferences();
00129 }
00130 
00131 // --------------------------------------------------------------------
00132 
00133 PrefSpinBox::PrefSpinBox ( QWidget * parent )
00134   : QSpinBox(parent), PrefWidget()
00135 {
00136 }
00137 
00138 PrefSpinBox::~PrefSpinBox()
00139 {
00140 }
00141 
00142 void PrefSpinBox::restorePreferences()
00143 {
00144   if ( getWindowParameter().isNull() )
00145   {
00146     Console().Warning("Cannot restore!\n");
00147     return;
00148   }
00149 
00150   int nVal = getWindowParameter()->GetInt( entryName(), QSpinBox::value() );
00151   setValue( nVal );
00152 }
00153 
00154 void PrefSpinBox::savePreferences()
00155 {
00156   if (getWindowParameter().isNull())
00157   {
00158     Console().Warning("Cannot save!\n");
00159     return;
00160   }
00161 
00162   getWindowParameter()->SetInt( entryName() , (int)value() );
00163 }
00164 
00165 QByteArray PrefSpinBox::entryName () const
00166 {
00167   return PrefWidget::entryName();
00168 }
00169 
00170 QByteArray PrefSpinBox::paramGrpPath () const
00171 {
00172   return PrefWidget::paramGrpPath();
00173 }
00174 
00175 void PrefSpinBox::setEntryName ( const QByteArray& name )
00176 {
00177   PrefWidget::setEntryName(name);
00178 }
00179 
00180 void PrefSpinBox::setParamGrpPath ( const QByteArray& name )
00181 {
00182   PrefWidget::setParamGrpPath(name);
00183 }
00184 
00185 // --------------------------------------------------------------------
00186 
00187 PrefDoubleSpinBox::PrefDoubleSpinBox ( QWidget * parent )
00188   : QDoubleSpinBox(parent), PrefWidget()
00189 {
00190 }
00191 
00192 PrefDoubleSpinBox::~PrefDoubleSpinBox()
00193 {
00194 }
00195 
00196 void PrefDoubleSpinBox::restorePreferences()
00197 {
00198   if ( getWindowParameter().isNull() )
00199   {
00200     Console().Warning("Cannot restore!\n");
00201     return;
00202   }
00203 
00204   double fVal = (double)getWindowParameter()->GetFloat( entryName() , value() );
00205   setValue(fVal);
00206 }
00207 
00208 void PrefDoubleSpinBox::savePreferences()
00209 {
00210   if (getWindowParameter().isNull())
00211   {
00212     Console().Warning("Cannot save!\n");
00213     return;
00214   }
00215 
00216   getWindowParameter()->SetFloat( entryName(), value() );
00217 }
00218 
00219 QByteArray PrefDoubleSpinBox::entryName () const
00220 {
00221   return PrefWidget::entryName();
00222 }
00223 
00224 QByteArray PrefDoubleSpinBox::paramGrpPath () const
00225 {
00226   return PrefWidget::paramGrpPath();
00227 }
00228 
00229 void PrefDoubleSpinBox::setEntryName ( const QByteArray& name )
00230 {
00231   PrefWidget::setEntryName(name);
00232 }
00233 
00234 void PrefDoubleSpinBox::setParamGrpPath ( const QByteArray& name )
00235 {
00236   PrefWidget::setParamGrpPath(name);
00237 }
00238 
00239 // --------------------------------------------------------------------
00240 
00241 PrefLineEdit::PrefLineEdit ( QWidget * parent )
00242   : QLineEdit(parent), PrefWidget()
00243 {
00244 }
00245 
00246 PrefLineEdit::~PrefLineEdit()
00247 {
00248 }
00249 
00250 void PrefLineEdit::restorePreferences()
00251 {
00252   if (getWindowParameter().isNull())
00253   {
00254     Console().Warning("Cannot restore!\n");
00255     return;
00256   }
00257 
00258   QString text = this->text();
00259   text = QString::fromUtf8(getWindowParameter()->GetASCII(entryName(), text.toUtf8()).c_str());
00260   setText(text);
00261 }
00262 
00263 void PrefLineEdit::savePreferences()
00264 {
00265   if (getWindowParameter().isNull())
00266   {
00267     Console().Warning("Cannot save!\n");
00268     return;
00269   }
00270 
00271   getWindowParameter()->SetASCII(entryName(), text().toUtf8());
00272 }
00273 
00274 QByteArray PrefLineEdit::entryName () const
00275 {
00276   return PrefWidget::entryName();
00277 }
00278 
00279 QByteArray PrefLineEdit::paramGrpPath () const
00280 {
00281   return PrefWidget::paramGrpPath();
00282 }
00283 
00284 void PrefLineEdit::setEntryName ( const QByteArray& name )
00285 {
00286   PrefWidget::setEntryName(name);
00287 }
00288 
00289 void PrefLineEdit::setParamGrpPath ( const QByteArray& name )
00290 {
00291   PrefWidget::setParamGrpPath(name);
00292 }
00293 
00294 // --------------------------------------------------------------------
00295 
00296 PrefFileChooser::PrefFileChooser ( QWidget * parent )
00297   : FileChooser(parent), PrefWidget()
00298 {
00299 }
00300 
00301 PrefFileChooser::~PrefFileChooser()
00302 {
00303 }
00304 
00305 void PrefFileChooser::restorePreferences()
00306 {
00307   if (getWindowParameter().isNull())
00308   {
00309     Console().Warning("Cannot restore!\n");
00310     return;
00311   }
00312 
00313   QString txt = QString::fromUtf8(getWindowParameter()->GetASCII(entryName(), fileName().toUtf8()).c_str());
00314   setFileName(txt);
00315 }
00316 
00317 void PrefFileChooser::savePreferences()
00318 {
00319   if (getWindowParameter().isNull())
00320   {
00321     Console().Warning("Cannot save!\n");
00322     return;
00323   }
00324 
00325   getWindowParameter()->SetASCII(entryName(), fileName().toUtf8());
00326 }
00327 
00328 QByteArray PrefFileChooser::entryName () const
00329 {
00330   return PrefWidget::entryName();
00331 }
00332 
00333 QByteArray PrefFileChooser::paramGrpPath () const
00334 {
00335   return PrefWidget::paramGrpPath();
00336 }
00337 
00338 void PrefFileChooser::setEntryName ( const QByteArray& name )
00339 {
00340   PrefWidget::setEntryName(name);
00341 }
00342 
00343 void PrefFileChooser::setParamGrpPath ( const QByteArray& name )
00344 {
00345   PrefWidget::setParamGrpPath(name);
00346 }
00347 
00348 // --------------------------------------------------------------------
00349 
00350 PrefComboBox::PrefComboBox ( QWidget * parent )
00351   : QComboBox(parent), PrefWidget()
00352 {
00353 }
00354 
00355 PrefComboBox::~PrefComboBox()
00356 {
00357 }
00358 
00359 void PrefComboBox::restorePreferences()
00360 {
00361   if (getWindowParameter().isNull())
00362   {
00363     Console().Warning("Cannot restore!\n");
00364     return;
00365   }
00366 
00367   int index = getWindowParameter()->GetInt(entryName(), currentIndex());
00368   setCurrentIndex(index);
00369 }
00370 
00371 void PrefComboBox::savePreferences()
00372 {
00373   if (getWindowParameter().isNull())
00374   {
00375     Console().Warning("Cannot save!\n");
00376     return;
00377   }
00378 
00379   getWindowParameter()->SetInt(entryName() , currentIndex());
00380 }
00381 
00382 QByteArray PrefComboBox::entryName () const
00383 {
00384   return PrefWidget::entryName();
00385 }
00386 
00387 QByteArray PrefComboBox::paramGrpPath () const
00388 {
00389   return PrefWidget::paramGrpPath();
00390 }
00391 
00392 void PrefComboBox::setEntryName ( const QByteArray& name )
00393 {
00394   PrefWidget::setEntryName(name);
00395 }
00396 
00397 void PrefComboBox::setParamGrpPath ( const QByteArray& name )
00398 {
00399   PrefWidget::setParamGrpPath(name);
00400 }
00401 
00402 // --------------------------------------------------------------------
00403 
00404 PrefCheckBox::PrefCheckBox ( QWidget * parent )
00405   : QCheckBox(parent), PrefWidget()
00406 {
00407 }
00408 
00409 PrefCheckBox::~PrefCheckBox()
00410 {
00411 }
00412 
00413 void PrefCheckBox::restorePreferences()
00414 {
00415   if (getWindowParameter().isNull())
00416   {
00417     Console().Warning("Cannot restore!\n");
00418     return;
00419   }
00420 
00421   bool enable = getWindowParameter()->GetBool( entryName(), isChecked() );
00422   setChecked(enable);
00423 }
00424 
00425 void PrefCheckBox::savePreferences()
00426 {
00427   if (getWindowParameter().isNull())
00428   {
00429     Console().Warning("Cannot save!\n");
00430     return;
00431   }
00432 
00433   getWindowParameter()->SetBool( entryName(), isChecked() );
00434 }
00435 
00436 QByteArray PrefCheckBox::entryName () const
00437 {
00438   return PrefWidget::entryName();
00439 }
00440 
00441 QByteArray PrefCheckBox::paramGrpPath () const
00442 {
00443   return PrefWidget::paramGrpPath();
00444 }
00445 
00446 void PrefCheckBox::setEntryName ( const QByteArray& name )
00447 {
00448   PrefWidget::setEntryName(name);
00449 }
00450 
00451 void PrefCheckBox::setParamGrpPath ( const QByteArray& name )
00452 {
00453   PrefWidget::setParamGrpPath(name);
00454 }
00455 
00456 // --------------------------------------------------------------------
00457 
00458 PrefRadioButton::PrefRadioButton ( QWidget * parent )
00459   : QRadioButton(parent), PrefWidget()
00460 {
00461 }
00462 
00463 PrefRadioButton::~PrefRadioButton()
00464 {
00465 }
00466 
00467 void PrefRadioButton::restorePreferences()
00468 {
00469   if (getWindowParameter().isNull())
00470   {
00471     Console().Warning("Cannot restore!\n");
00472     return;
00473   }
00474 
00475   bool enable = getWindowParameter()->GetBool( entryName(), isChecked() );
00476   setChecked(enable);
00477 }
00478 
00479 void PrefRadioButton::savePreferences()
00480 {
00481   if (getWindowParameter().isNull())
00482   {
00483     Console().Warning("Cannot save!\n");
00484     return;
00485   }
00486 
00487   getWindowParameter()->SetBool( entryName() , isChecked() );
00488 }
00489 
00490 QByteArray PrefRadioButton::entryName () const
00491 {
00492   return PrefWidget::entryName();
00493 }
00494 
00495 QByteArray PrefRadioButton::paramGrpPath () const
00496 {
00497   return PrefWidget::paramGrpPath();
00498 }
00499 
00500 void PrefRadioButton::setEntryName ( const QByteArray& name )
00501 {
00502   PrefWidget::setEntryName(name);
00503 }
00504 
00505 void PrefRadioButton::setParamGrpPath ( const QByteArray& name )
00506 {
00507   PrefWidget::setParamGrpPath(name);
00508 }
00509 
00510 // --------------------------------------------------------------------
00511 
00512 PrefSlider::PrefSlider ( QWidget * parent )
00513   : QSlider(parent), PrefWidget()
00514 {
00515 }
00516 
00517 PrefSlider::~PrefSlider()
00518 {
00519 }
00520 
00521 void PrefSlider::restorePreferences()
00522 {
00523   if ( getWindowParameter().isNull() )
00524   {
00525     Console().Warning("Cannot restore!\n");
00526     return;
00527   }
00528 
00529   int nVal = getWindowParameter()->GetInt(entryName(), QSlider::value());
00530   setValue(nVal);
00531 }
00532 
00533 void PrefSlider::savePreferences()
00534 {
00535   if (getWindowParameter().isNull())
00536   {
00537     Console().Warning("Cannot save!\n");
00538     return;
00539   }
00540 
00541   getWindowParameter()->SetInt(entryName() , (int)value());
00542 }
00543 
00544 QByteArray PrefSlider::entryName () const
00545 {
00546   return PrefWidget::entryName();
00547 }
00548 
00549 QByteArray PrefSlider::paramGrpPath () const
00550 {
00551   return PrefWidget::paramGrpPath();
00552 }
00553 
00554 void PrefSlider::setEntryName ( const QByteArray& name )
00555 {
00556   PrefWidget::setEntryName(name);
00557 }
00558 
00559 void PrefSlider::setParamGrpPath ( const QByteArray& name )
00560 {
00561   PrefWidget::setParamGrpPath(name);
00562 }
00563 
00564 // --------------------------------------------------------------------
00565 
00566 PrefColorButton::PrefColorButton ( QWidget * parent )
00567   : ColorButton(parent), PrefWidget()
00568 {
00569 }
00570 
00571 PrefColorButton::~PrefColorButton()
00572 {
00573 }
00574 
00575 void PrefColorButton::restorePreferences()
00576 {
00577   if (getWindowParameter().isNull())
00578   {
00579     Console().Warning("Cannot restore!\n");
00580     return;
00581   }
00582 
00583   QColor col = color();
00584 
00585   unsigned long lcol = (col.red() << 24) | (col.green() << 16) | (col.blue() << 8);
00586 
00587   lcol = getWindowParameter()->GetUnsigned( entryName(), lcol );
00588   int r = (lcol >> 24)&0xff;
00589   int g = (lcol >> 16)&0xff;
00590   int b = (lcol >>  8)&0xff;
00591 
00592   setColor(QColor(r,g,b));
00593 }
00594 
00595 void PrefColorButton::savePreferences()
00596 {
00597   if (getWindowParameter().isNull())
00598   {
00599     Console().Warning("Cannot save!\n");
00600     return;
00601   }
00602 
00603   QColor col = color();
00604   // (r,g,b,a) with a = 255 (opaque)
00605   unsigned long lcol = (col.red() << 24) | (col.green() << 16) | (col.blue() << 8) | 255;
00606 
00607   getWindowParameter()->SetUnsigned( entryName(), lcol );
00608 }
00609 
00610 QByteArray PrefColorButton::entryName () const
00611 {
00612   return PrefWidget::entryName();
00613 }
00614 
00615 QByteArray PrefColorButton::paramGrpPath () const
00616 {
00617   return PrefWidget::paramGrpPath();
00618 }
00619 
00620 void PrefColorButton::setEntryName ( const QByteArray& name )
00621 {
00622   PrefWidget::setEntryName(name);
00623 }
00624 
00625 void PrefColorButton::setParamGrpPath ( const QByteArray& name )
00626 {
00627   PrefWidget::setParamGrpPath(name);
00628 }
00629 
00630 #include "moc_PrefWidgets.cpp"

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