| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#ifndef GUI_PREFWIDGETS_H |
| 24 |
#define GUI_PREFWIDGETS_H |
| 25 |
|
| 26 |
#include <QCheckBox> |
| 27 |
#include <QComboBox> |
| 28 |
#include <QFontComboBox> |
| 29 |
#include <QGroupBox> |
| 30 |
#include <QRadioButton> |
| 31 |
#include <QTextEdit> |
| 32 |
|
| 33 |
#include <Base/Parameter.h> |
| 34 |
|
| 35 |
#include "Widgets.h" |
| 36 |
#include "FileDialog.h" |
| 37 |
#include "SpinBox.h" |
| 38 |
#include "QuantitySpinBox.h" |
| 39 |
#include "Window.h" |
| 40 |
|
| 41 |
|
| 42 |
namespace Gui |
| 43 |
{ |
| 44 |
class CommandManager; |
| 45 |
class WidgetFactoryInst; |
| 46 |
|
| 47 |
/** The preference widget class. |
| 48 |
* If you want to extend a QWidget class to save/restore its data |
| 49 |
* you just have to derive from this class and implement the methods |
| 50 |
* restorePreferences() and savePreferences(). |
| 51 |
* |
| 52 |
* To restore and save the settings of any widgets in own dialogs you have |
| 53 |
* call onRestore() e.g. in the dialog's constructor and call onSave() e.g. |
| 54 |
* in accept() for each widget you want to enable this mechanism. |
| 55 |
* |
| 56 |
* For more information of how to use these widgets in normal container widgets |
| 57 |
* which are again in a dialog refer to the description of Gui::Dialog::DlgPreferencesImp. |
| 58 |
* |
| 59 |
* \author Werner Mayer |
| 60 |
*/ |
| 61 |
class GuiExport PrefWidget: public WindowParameter |
| 62 |
{ |
| 63 |
public: |
| 64 |
void setEntryName(const QByteArray& name); |
| 65 |
QByteArray entryName() const; |
| 66 |
/** Does the same as setEntryName(). |
| 67 |
* This function is added for convenience because the ui compiler |
| 68 |
* will use this function if the attribute stdset isn't set to 0 in a .ui file. |
| 69 |
*/ |
| 70 |
void setPrefEntry(const QByteArray& name); |
| 71 |
|
| 72 |
void setParamGrpPath(const QByteArray& path); |
| 73 |
QByteArray paramGrpPath() const; |
| 74 |
/** Does the same as setParamGrpPath(). |
| 75 |
* This function is added for convenience because the ui compiler |
| 76 |
* will use this function if the attribute stdset isn't set to 0 in a .ui file. |
| 77 |
*/ |
| 78 |
void setPrefPath(const QByteArray& name); |
| 79 |
|
| 80 |
void OnChange(Base::Subject<const char*>& rCaller, const char* sReason) override; |
| 81 |
void onSave(); |
| 82 |
void onRestore(); |
| 83 |
|
| 84 |
protected: |
| 85 |
/** Restores the preferences |
| 86 |
* Must be reimplemented in any subclasses. |
| 87 |
*/ |
| 88 |
virtual void restorePreferences() = 0; |
| 89 |
/** Save the preferences |
| 90 |
* Must be reimplemented in any subclasses. |
| 91 |
*/ |
| 92 |
virtual void savePreferences() = 0; |
| 93 |
/** Print warning that saving failed. |
| 94 |
*/ |
| 95 |
void failedToSave(const QString&) const; |
| 96 |
/** Print warning that restoring failed. |
| 97 |
*/ |
| 98 |
void failedToRestore(const QString&) const; |
| 99 |
|
| 100 |
PrefWidget(); |
| 101 |
~PrefWidget() override; |
| 102 |
|
| 103 |
private: |
| 104 |
QByteArray m_sPrefName; |
| 105 |
QByteArray m_sPrefGrp; |
| 106 |
|
| 107 |
// friends |
| 108 |
friend class Gui::WidgetFactoryInst; |
| 109 |
|
| 110 |
protected: |
| 111 |
bool m_Restored = false; |
| 112 |
}; |
| 113 |
|
| 114 |
/** The PrefSpinBox class. |
| 115 |
* \author Werner Mayer |
| 116 |
*/ |
| 117 |
class GuiExport PrefSpinBox: public QSpinBox, public PrefWidget |
| 118 |
{ |
| 119 |
Q_OBJECT |
| 120 |
|
| 121 |
Q_PROPERTY( |
| 122 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 123 |
) // clazy:exclude=qproperty-without-notify |
| 124 |
Q_PROPERTY( |
| 125 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 126 |
) // clazy:exclude=qproperty-without-notify |
| 127 |
|
| 128 |
public: |
| 129 |
explicit PrefSpinBox(QWidget* parent = nullptr); |
| 130 |
~PrefSpinBox() override; |
| 131 |
|
| 132 |
protected: |
| 133 |
void wheelEvent(QWheelEvent* event) override; |
| 134 |
// restore from/save to parameters |
| 135 |
void restorePreferences() override; |
| 136 |
void savePreferences() override; |
| 137 |
}; |
| 138 |
|
| 139 |
/** The PrefDoubleSpinBox class. |
| 140 |
* \author Werner Mayer |
| 141 |
*/ |
| 142 |
class GuiExport PrefDoubleSpinBox: public QDoubleSpinBox, public PrefWidget |
| 143 |
{ |
| 144 |
Q_OBJECT |
| 145 |
|
| 146 |
Q_PROPERTY( |
| 147 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 148 |
) // clazy:exclude=qproperty-without-notify |
| 149 |
Q_PROPERTY( |
| 150 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 151 |
) // clazy:exclude=qproperty-without-notify |
| 152 |
|
| 153 |
public: |
| 154 |
explicit PrefDoubleSpinBox(QWidget* parent = nullptr); |
| 155 |
~PrefDoubleSpinBox() override; |
| 156 |
|
| 157 |
protected: |
| 158 |
void wheelEvent(QWheelEvent* event) override; |
| 159 |
// restore from/save to parameters |
| 160 |
void restorePreferences() override; |
| 161 |
void savePreferences() override; |
| 162 |
}; |
| 163 |
|
| 164 |
/** |
| 165 |
* The PrefLineEdit class. |
| 166 |
* \author Werner Mayer |
| 167 |
*/ |
| 168 |
class GuiExport PrefLineEdit: public QLineEdit, public PrefWidget |
| 169 |
{ |
| 170 |
Q_OBJECT |
| 171 |
|
| 172 |
Q_PROPERTY( |
| 173 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 174 |
) // clazy:exclude=qproperty-without-notify |
| 175 |
Q_PROPERTY( |
| 176 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 177 |
) // clazy:exclude=qproperty-without-notify |
| 178 |
|
| 179 |
public: |
| 180 |
explicit PrefLineEdit(QWidget* parent = nullptr); |
| 181 |
~PrefLineEdit() override; |
| 182 |
|
| 183 |
protected: |
| 184 |
// restore from/save to parameters |
| 185 |
void restorePreferences() override; |
| 186 |
void savePreferences() override; |
| 187 |
}; |
| 188 |
|
| 189 |
/** |
| 190 |
* The PrefLineEdit class. |
| 191 |
* \author Chris Hennes |
| 192 |
*/ |
| 193 |
class GuiExport PrefTextEdit: public QTextEdit, public PrefWidget |
| 194 |
{ |
| 195 |
Q_OBJECT |
| 196 |
|
| 197 |
Q_PROPERTY( |
| 198 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 199 |
) // clazy:exclude=qproperty-without-notify |
| 200 |
Q_PROPERTY( |
| 201 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 202 |
) // clazy:exclude=qproperty-without-notify |
| 203 |
|
| 204 |
public: |
| 205 |
explicit PrefTextEdit(QWidget* parent = nullptr); |
| 206 |
~PrefTextEdit() override; |
| 207 |
|
| 208 |
protected: |
| 209 |
// restore from/save to parameters |
| 210 |
void restorePreferences() override; |
| 211 |
void savePreferences() override; |
| 212 |
}; |
| 213 |
|
| 214 |
/** |
| 215 |
* The PrefFileChooser class. |
| 216 |
* \author Werner Mayer |
| 217 |
*/ |
| 218 |
class GuiExport PrefFileChooser: public FileChooser, public PrefWidget |
| 219 |
{ |
| 220 |
Q_OBJECT |
| 221 |
|
| 222 |
Q_PROPERTY( |
| 223 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 224 |
) // clazy:exclude=qproperty-without-notify |
| 225 |
Q_PROPERTY( |
| 226 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 227 |
) // clazy:exclude=qproperty-without-notify |
| 228 |
|
| 229 |
public: |
| 230 |
explicit PrefFileChooser(QWidget* parent = nullptr); |
| 231 |
~PrefFileChooser() override; |
| 232 |
|
| 233 |
protected: |
| 234 |
// restore from/save to parameters |
| 235 |
void restorePreferences() override; |
| 236 |
void savePreferences() override; |
| 237 |
}; |
| 238 |
|
| 239 |
/** |
| 240 |
* The PrefComboBox class. |
| 241 |
* \author Werner Mayer |
| 242 |
* |
| 243 |
* The PrefComboBox supports restoring/saving variant type of item data. You |
| 244 |
* can add a property named 'prefType' with the type you want. If no such |
| 245 |
* property is found, the class defaults to restore/save the item index. |
| 246 |
* |
| 247 |
* Note that there is special handling for 'prefType' of QString, which means |
| 248 |
* to restore/save the item text. This allows the combox to be editable, and |
| 249 |
* accepts user entered value. Use QByteArray if you want to restore/save a |
| 250 |
* non translatable string stored as item data. |
| 251 |
*/ |
| 252 |
class GuiExport PrefComboBox: public QComboBox, public PrefWidget |
| 253 |
{ |
| 254 |
Q_OBJECT |
| 255 |
|
| 256 |
Q_PROPERTY( |
| 257 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 258 |
) // clazy:exclude=qproperty-without-notify |
| 259 |
Q_PROPERTY( |
| 260 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 261 |
) // clazy:exclude=qproperty-without-notify |
| 262 |
|
| 263 |
public: |
| 264 |
explicit PrefComboBox(QWidget* parent = nullptr); |
| 265 |
~PrefComboBox() override; |
| 266 |
|
| 267 |
protected: |
| 268 |
void wheelEvent(QWheelEvent* event) override; |
| 269 |
// restore from/save to parameters |
| 270 |
void restorePreferences() override; |
| 271 |
void savePreferences() override; |
| 272 |
virtual QMetaType::Type getParamType() const; |
| 273 |
|
| 274 |
private: |
| 275 |
QVariant m_Default; |
| 276 |
int m_DefaultIndex; |
| 277 |
QString m_DefaultText; |
| 278 |
}; |
| 279 |
|
| 280 |
/** |
| 281 |
* The PrefCheckBox class. |
| 282 |
* \author Werner Mayer |
| 283 |
*/ |
| 284 |
class GuiExport PrefCheckBox: public QCheckBox, public PrefWidget |
| 285 |
{ |
| 286 |
Q_OBJECT |
| 287 |
|
| 288 |
Q_PROPERTY( |
| 289 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 290 |
) // clazy:exclude=qproperty-without-notify |
| 291 |
Q_PROPERTY( |
| 292 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 293 |
) // clazy:exclude=qproperty-without-notify |
| 294 |
|
| 295 |
public: |
| 296 |
explicit PrefCheckBox(QWidget* parent = nullptr); |
| 297 |
~PrefCheckBox() override; |
| 298 |
|
| 299 |
protected: |
| 300 |
// restore from/save to parameters |
| 301 |
void restorePreferences() override; |
| 302 |
void savePreferences() override; |
| 303 |
}; |
| 304 |
|
| 305 |
/** |
| 306 |
* The PrefRadioButton class. |
| 307 |
* \author Werner Mayer |
| 308 |
*/ |
| 309 |
class GuiExport PrefRadioButton: public QRadioButton, public PrefWidget |
| 310 |
{ |
| 311 |
Q_OBJECT |
| 312 |
|
| 313 |
Q_PROPERTY( |
| 314 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 315 |
) // clazy:exclude=qproperty-without-notify |
| 316 |
Q_PROPERTY( |
| 317 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 318 |
) // clazy:exclude=qproperty-without-notify |
| 319 |
|
| 320 |
public: |
| 321 |
explicit PrefRadioButton(QWidget* parent = nullptr); |
| 322 |
~PrefRadioButton() override; |
| 323 |
|
| 324 |
protected: |
| 325 |
// restore from/save to parameters |
| 326 |
void restorePreferences() override; |
| 327 |
void savePreferences() override; |
| 328 |
}; |
| 329 |
|
| 330 |
/** |
| 331 |
* The PrefSlider class. |
| 332 |
* \author Werner Mayer |
| 333 |
*/ |
| 334 |
class GuiExport PrefSlider: public QSlider, public PrefWidget |
| 335 |
{ |
| 336 |
Q_OBJECT |
| 337 |
|
| 338 |
Q_PROPERTY( |
| 339 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 340 |
) // clazy:exclude=qproperty-without-notify |
| 341 |
Q_PROPERTY( |
| 342 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 343 |
) // clazy:exclude=qproperty-without-notify |
| 344 |
|
| 345 |
public: |
| 346 |
explicit PrefSlider(QWidget* parent = nullptr); |
| 347 |
~PrefSlider() override; |
| 348 |
|
| 349 |
protected: |
| 350 |
// restore from/save to parameters |
| 351 |
void restorePreferences() override; |
| 352 |
void savePreferences() override; |
| 353 |
}; |
| 354 |
|
| 355 |
/** |
| 356 |
* The PrefColorButton class. |
| 357 |
* \author Werner Mayer |
| 358 |
*/ |
| 359 |
class GuiExport PrefColorButton: public ColorButton, public PrefWidget |
| 360 |
{ |
| 361 |
Q_OBJECT |
| 362 |
|
| 363 |
Q_PROPERTY( |
| 364 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 365 |
) // clazy:exclude=qproperty-without-notify |
| 366 |
Q_PROPERTY( |
| 367 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 368 |
) // clazy:exclude=qproperty-without-notify |
| 369 |
|
| 370 |
public: |
| 371 |
explicit PrefColorButton(QWidget* parent = nullptr); |
| 372 |
~PrefColorButton() override; |
| 373 |
|
| 374 |
protected: |
| 375 |
// restore from/save to parameters |
| 376 |
void restorePreferences() override; |
| 377 |
void savePreferences() override; |
| 378 |
|
| 379 |
private: |
| 380 |
QColor m_Default; |
| 381 |
}; |
| 382 |
|
| 383 |
/** The PrefUnitSpinBox class. |
| 384 |
* \author wandererfan |
| 385 |
* a simple Unit aware spin box. |
| 386 |
* See also \ref PrefQuantitySpinBox |
| 387 |
*/ |
| 388 |
class GuiExport PrefUnitSpinBox: public QuantitySpinBox, public PrefWidget |
| 389 |
{ |
| 390 |
Q_OBJECT |
| 391 |
|
| 392 |
Q_PROPERTY( |
| 393 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 394 |
) // clazy:exclude=qproperty-without-notify |
| 395 |
Q_PROPERTY( |
| 396 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 397 |
) // clazy:exclude=qproperty-without-notify |
| 398 |
|
| 399 |
public: |
| 400 |
explicit PrefUnitSpinBox(QWidget* parent = nullptr); |
| 401 |
~PrefUnitSpinBox() override; |
| 402 |
|
| 403 |
protected: |
| 404 |
void wheelEvent(QWheelEvent* event) override; |
| 405 |
// restore from/save to parameters |
| 406 |
void restorePreferences() override; |
| 407 |
void savePreferences() override; |
| 408 |
}; |
| 409 |
|
| 410 |
class PrefQuantitySpinBoxPrivate; |
| 411 |
|
| 412 |
/** |
| 413 |
* The PrefQuantitySpinBox class. |
| 414 |
* \author Werner Mayer |
| 415 |
*/ |
| 416 |
class GuiExport PrefQuantitySpinBox: public QuantitySpinBox, public PrefWidget |
| 417 |
{ |
| 418 |
Q_OBJECT |
| 419 |
|
| 420 |
Q_PROPERTY( |
| 421 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 422 |
) // clazy:exclude=qproperty-without-notify |
| 423 |
Q_PROPERTY( |
| 424 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 425 |
) // clazy:exclude=qproperty-without-notify |
| 426 |
Q_PROPERTY( |
| 427 |
int historySize READ historySize WRITE setHistorySize |
| 428 |
) // clazy:exclude=qproperty-without-notify |
| 429 |
|
| 430 |
public: |
| 431 |
explicit PrefQuantitySpinBox(QWidget* parent = nullptr); |
| 432 |
~PrefQuantitySpinBox() override; |
| 433 |
|
| 434 |
/// set the input field to the last used value (works only if the setParamGrpPath() was called) |
| 435 |
void setToLastUsedValue(); |
| 436 |
/// get the value of the history size property |
| 437 |
int historySize() const; |
| 438 |
/// set the value of the history size property |
| 439 |
void setHistorySize(int); |
| 440 |
|
| 441 |
/** @name history and default management */ |
| 442 |
//@{ |
| 443 |
/// push a new value to the history, if no string given the actual text of the input field is used. |
| 444 |
void pushToHistory(const QString& value = QString()); |
| 445 |
/// get the history of the field, newest first |
| 446 |
QStringList getHistory() const; |
| 447 |
//@} |
| 448 |
|
| 449 |
protected: |
| 450 |
void contextMenuEvent(QContextMenuEvent* event) override; |
| 451 |
void wheelEvent(QWheelEvent* event) override; |
| 452 |
// restore from/save to parameters |
| 453 |
void restorePreferences() override; |
| 454 |
void savePreferences() override; |
| 455 |
|
| 456 |
private: |
| 457 |
QScopedPointer<PrefQuantitySpinBoxPrivate> d_ptr; |
| 458 |
Q_DISABLE_COPY(PrefQuantitySpinBox) |
| 459 |
Q_DECLARE_PRIVATE(PrefQuantitySpinBox) |
| 460 |
}; |
| 461 |
|
| 462 |
/** The PrefFontBox class. |
| 463 |
* \author wandererfan |
| 464 |
*/ |
| 465 |
class GuiExport PrefFontBox: public QFontComboBox, public PrefWidget |
| 466 |
{ |
| 467 |
Q_OBJECT |
| 468 |
|
| 469 |
Q_PROPERTY( |
| 470 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 471 |
) // clazy:exclude=qproperty-without-notify |
| 472 |
Q_PROPERTY( |
| 473 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 474 |
) // clazy:exclude=qproperty-without-notify |
| 475 |
|
| 476 |
public: |
| 477 |
explicit PrefFontBox(QWidget* parent = nullptr); |
| 478 |
~PrefFontBox() override; |
| 479 |
|
| 480 |
protected: |
| 481 |
// restore from/save to parameters |
| 482 |
void restorePreferences() override; |
| 483 |
void savePreferences() override; |
| 484 |
}; |
| 485 |
|
| 486 |
/** |
| 487 |
* The PrefCheckableGroupBox class allows a QGroupBox to act as a boolean preference. |
| 488 |
* Its 'checked' state is saved to and restored from the FreeCAD parameter system |
| 489 |
* using the 'prefEntry' and 'prefPath' dynamic properties set in the .ui file. |
| 490 |
* When the GroupBox is checked, its children are enabled; when unchecked, disabled (standard |
| 491 |
* QGroupBox behavior). |
| 492 |
*/ |
| 493 |
class GuiExport PrefCheckableGroupBox: public QGroupBox, public PrefWidget |
| 494 |
{ |
| 495 |
Q_OBJECT |
| 496 |
|
| 497 |
Q_PROPERTY( |
| 498 |
QByteArray prefEntry READ entryName WRITE setEntryName |
| 499 |
) // clazy:exclude=qproperty-without-notify |
| 500 |
Q_PROPERTY( |
| 501 |
QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath |
| 502 |
) // clazy:exclude=qproperty-without-notify |
| 503 |
|
| 504 |
public: |
| 505 |
explicit PrefCheckableGroupBox(QWidget* parent = nullptr); |
| 506 |
~PrefCheckableGroupBox() override; |
| 507 |
|
| 508 |
protected: |
| 509 |
// restore from/save to parameters |
| 510 |
void restorePreferences() override; |
| 511 |
void savePreferences() override; |
| 512 |
}; |
| 513 |
|
| 514 |
} // namespace Gui |
| 515 |
|
| 516 |
#endif // GUI_PREFWIDGETS_H |