| 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 |
|
| 24 |
#include <QContextMenuEvent> |
| 25 |
#include <QMenu> |
| 26 |
|
| 27 |
|
| 28 |
#include <cstring> |
| 29 |
|
| 30 |
#include <Base/Console.h> |
| 31 |
#include <Base/Tools.h> |
| 32 |
#include <Base/Color.h> |
| 33 |
|
| 34 |
#include "PrefWidgets.h" |
| 35 |
|
| 36 |
|
| 37 |
using Base::Console; |
| 38 |
using namespace Gui; |
| 39 |
|
| 40 |
/** Constructs a preference widget. |
| 41 |
*/ |
| 42 |
PrefWidget::PrefWidget() |
| 43 |
: WindowParameter("") |
| 44 |
{} |
| 45 |
|
| 46 |
/** |
| 47 |
* Destroys the widget and detaches it from its parameter group. |
| 48 |
*/ |
| 49 |
PrefWidget::~PrefWidget() |
| 50 |
{ |
| 51 |
if (getWindowParameter().isValid()) { |
| 52 |
getWindowParameter()->Detach(this); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** Sets the preference name to \a name. */ |
| 57 |
void PrefWidget::setEntryName(const QByteArray& name) |
| 58 |
{ |
| 59 |
m_sPrefName = name; |
| 60 |
} |
| 61 |
|
| 62 |
/** Sets the preference name to \a name. */ |
| 63 |
void PrefWidget::setPrefEntry(const QByteArray& name) |
| 64 |
{ |
| 65 |
setEntryName(name); |
| 66 |
} |
| 67 |
|
| 68 |
/** Returns the widget's preference name. */ |
| 69 |
QByteArray PrefWidget::entryName() const |
| 70 |
{ |
| 71 |
return m_sPrefName; |
| 72 |
} |
| 73 |
|
| 74 |
/** Sets the preference path to \a path. */ |
| 75 |
void PrefWidget::setParamGrpPath(const QByteArray& path) |
| 76 |
{ |
| 77 |
#ifdef FC_DEBUG |
| 78 |
if (getWindowParameter().isValid()) { |
| 79 |
if (paramGrpPath() != path) { |
| 80 |
Base::Console().warning("Widget already attached\n"); |
| 81 |
} |
| 82 |
} |
| 83 |
#endif |
| 84 |
|
| 85 |
if (paramGrpPath() != path) { |
| 86 |
if (setGroupName(path)) { |
| 87 |
m_sPrefGrp = path; |
| 88 |
assert(getWindowParameter().isValid()); |
| 89 |
getWindowParameter()->Attach(this); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** Sets the preference path to \a path. */ |
| 95 |
void PrefWidget::setPrefPath(const QByteArray& name) |
| 96 |
{ |
| 97 |
setParamGrpPath(name); |
| 98 |
} |
| 99 |
|
| 100 |
/** Returns the widget's preferences path. */ |
| 101 |
QByteArray PrefWidget::paramGrpPath() const |
| 102 |
{ |
| 103 |
return m_sPrefGrp; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* This method is called if one or more values in the parameter settings are changed |
| 108 |
* where getParamGrp() points to. |
| 109 |
* Note: This method is called for each parameter inside the parameter group. So |
| 110 |
* you have to filter out the appropriate parameter with the name \a sReason. |
| 111 |
* \a rCaller calls this method. |
| 112 |
*/ |
| 113 |
void PrefWidget::OnChange(Base::Subject<const char*>& rCaller, const char* sReason) |
| 114 |
{ |
| 115 |
Q_UNUSED(rCaller); |
| 116 |
if (std::strcmp(sReason, m_sPrefName) == 0) { |
| 117 |
restorePreferences(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Saves the current preferences of the widget. |
| 123 |
* All preference widget attached to the same parameter group are notified. |
| 124 |
*/ |
| 125 |
void PrefWidget::onSave() |
| 126 |
{ |
| 127 |
savePreferences(); |
| 128 |
if (getWindowParameter().isValid()) { |
| 129 |
getWindowParameter()->Notify(entryName()); |
| 130 |
} |
| 131 |
#ifdef FC_DEBUG |
| 132 |
else { |
| 133 |
qFatal("No parameter group specified!"); |
| 134 |
} |
| 135 |
#endif |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Restores the preferences of the widget. |
| 140 |
*/ |
| 141 |
void PrefWidget::onRestore() |
| 142 |
{ |
| 143 |
#ifdef FC_DEBUG |
| 144 |
if (getWindowParameter().isNull()) { |
| 145 |
qWarning("No parameter group specified!"); |
| 146 |
} |
| 147 |
#endif |
| 148 |
restorePreferences(); |
| 149 |
m_Restored = true; |
| 150 |
} |
| 151 |
|
| 152 |
void PrefWidget::failedToSave(const QString& name) const |
| 153 |
{ |
| 154 |
QByteArray objname = name.toLatin1(); |
| 155 |
if (objname.isEmpty()) { |
| 156 |
objname = "Undefined"; |
| 157 |
} |
| 158 |
Console().warning("Cannot save %s (%s)\n", typeid(*this).name(), objname.constData()); |
| 159 |
} |
| 160 |
|
| 161 |
void PrefWidget::failedToRestore(const QString& name) const |
| 162 |
{ |
| 163 |
QByteArray objname = name.toLatin1(); |
| 164 |
if (objname.isEmpty()) { |
| 165 |
objname = "Undefined"; |
| 166 |
} |
| 167 |
Console().warning("Cannot restore %s (%s)\n", typeid(*this).name(), objname.constData()); |
| 168 |
} |
| 169 |
|
| 170 |
// -------------------------------------------------------------------- |
| 171 |
|
| 172 |
PrefSpinBox::PrefSpinBox(QWidget* parent) |
| 173 |
: QSpinBox(parent) |
| 174 |
, PrefWidget() |
| 175 |
{ |
| 176 |
setFocusPolicy(Qt::FocusPolicy::StrongFocus); |
| 177 |
} |
| 178 |
|
| 179 |
void PrefSpinBox::wheelEvent(QWheelEvent* event) |
| 180 |
{ |
| 181 |
if (hasFocus()) { |
| 182 |
QSpinBox::wheelEvent(event); |
| 183 |
} |
| 184 |
else { |
| 185 |
event->ignore(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
PrefSpinBox::~PrefSpinBox() = default; |
| 190 |
|
| 191 |
void PrefSpinBox::restorePreferences() |
| 192 |
{ |
| 193 |
if (getWindowParameter().isNull()) { |
| 194 |
failedToRestore(objectName()); |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
int nVal = getWindowParameter()->GetInt(entryName(), QSpinBox::value()); |
| 199 |
setValue(nVal); |
| 200 |
} |
| 201 |
|
| 202 |
void PrefSpinBox::savePreferences() |
| 203 |
{ |
| 204 |
if (getWindowParameter().isNull()) { |
| 205 |
failedToSave(objectName()); |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
getWindowParameter()->SetInt(entryName(), (int)value()); |
| 210 |
} |
| 211 |
|
| 212 |
// -------------------------------------------------------------------- |
| 213 |
|
| 214 |
PrefDoubleSpinBox::PrefDoubleSpinBox(QWidget* parent) |
| 215 |
: QDoubleSpinBox(parent) |
| 216 |
, PrefWidget() |
| 217 |
{ |
| 218 |
setFocusPolicy(Qt::FocusPolicy::StrongFocus); |
| 219 |
} |
| 220 |
|
| 221 |
void PrefDoubleSpinBox::wheelEvent(QWheelEvent* event) |
| 222 |
{ |
| 223 |
if (hasFocus()) { |
| 224 |
QDoubleSpinBox::wheelEvent(event); |
| 225 |
} |
| 226 |
else { |
| 227 |
event->ignore(); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
PrefDoubleSpinBox::~PrefDoubleSpinBox() = default; |
| 232 |
|
| 233 |
void PrefDoubleSpinBox::restorePreferences() |
| 234 |
{ |
| 235 |
if (getWindowParameter().isNull()) { |
| 236 |
failedToRestore(objectName()); |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
double fVal = (double)getWindowParameter()->GetFloat(entryName(), value()); |
| 241 |
setValue(fVal); |
| 242 |
} |
| 243 |
|
| 244 |
void PrefDoubleSpinBox::savePreferences() |
| 245 |
{ |
| 246 |
if (getWindowParameter().isNull()) { |
| 247 |
failedToSave(objectName()); |
| 248 |
return; |
| 249 |
} |
| 250 |
|
| 251 |
getWindowParameter()->SetFloat(entryName(), value()); |
| 252 |
} |
| 253 |
|
| 254 |
// -------------------------------------------------------------------- |
| 255 |
|
| 256 |
PrefLineEdit::PrefLineEdit(QWidget* parent) |
| 257 |
: QLineEdit(parent) |
| 258 |
, PrefWidget() |
| 259 |
{} |
| 260 |
|
| 261 |
PrefLineEdit::~PrefLineEdit() = default; |
| 262 |
|
| 263 |
void PrefLineEdit::restorePreferences() |
| 264 |
{ |
| 265 |
if (getWindowParameter().isNull()) { |
| 266 |
failedToRestore(objectName()); |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
QString text = this->text(); |
| 271 |
text = QString::fromUtf8(getWindowParameter()->GetASCII(entryName(), text.toUtf8()).c_str()); |
| 272 |
setText(text); |
| 273 |
} |
| 274 |
|
| 275 |
void PrefLineEdit::savePreferences() |
| 276 |
{ |
| 277 |
if (getWindowParameter().isNull()) { |
| 278 |
failedToSave(objectName()); |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
getWindowParameter()->SetASCII(entryName(), text().toUtf8()); |
| 283 |
} |
| 284 |
|
| 285 |
// -------------------------------------------------------------------- |
| 286 |
|
| 287 |
PrefTextEdit::PrefTextEdit(QWidget* parent) |
| 288 |
: QTextEdit(parent) |
| 289 |
, PrefWidget() |
| 290 |
{} |
| 291 |
|
| 292 |
PrefTextEdit::~PrefTextEdit() = default; |
| 293 |
|
| 294 |
void PrefTextEdit::restorePreferences() |
| 295 |
{ |
| 296 |
if (getWindowParameter().isNull()) { |
| 297 |
failedToRestore(objectName()); |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
QString text = this->toPlainText(); |
| 302 |
text = QString::fromUtf8(getWindowParameter()->GetASCII(entryName(), text.toUtf8()).c_str()); |
| 303 |
setText(text); |
| 304 |
} |
| 305 |
|
| 306 |
void PrefTextEdit::savePreferences() |
| 307 |
{ |
| 308 |
if (getWindowParameter().isNull()) { |
| 309 |
failedToSave(objectName()); |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
QString text = this->toPlainText(); |
| 314 |
getWindowParameter()->SetASCII(entryName(), text.toUtf8()); |
| 315 |
} |
| 316 |
|
| 317 |
// -------------------------------------------------------------------- |
| 318 |
|
| 319 |
PrefFileChooser::PrefFileChooser(QWidget* parent) |
| 320 |
: FileChooser(parent) |
| 321 |
, PrefWidget() |
| 322 |
{} |
| 323 |
|
| 324 |
PrefFileChooser::~PrefFileChooser() = default; |
| 325 |
|
| 326 |
void PrefFileChooser::restorePreferences() |
| 327 |
{ |
| 328 |
if (getWindowParameter().isNull()) { |
| 329 |
failedToRestore(objectName()); |
| 330 |
return; |
| 331 |
} |
| 332 |
|
| 333 |
QString txt = QString::fromUtf8( |
| 334 |
getWindowParameter()->GetASCII(entryName(), fileName().toUtf8()).c_str() |
| 335 |
); |
| 336 |
setFileName(txt); |
| 337 |
} |
| 338 |
|
| 339 |
void PrefFileChooser::savePreferences() |
| 340 |
{ |
| 341 |
if (getWindowParameter().isNull()) { |
| 342 |
failedToSave(objectName()); |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
getWindowParameter()->SetASCII(entryName(), fileName().toUtf8()); |
| 347 |
} |
| 348 |
|
| 349 |
// -------------------------------------------------------------------- |
| 350 |
|
| 351 |
PrefComboBox::PrefComboBox(QWidget* parent) |
| 352 |
: QComboBox(parent) |
| 353 |
, PrefWidget() |
| 354 |
{ |
| 355 |
setFocusPolicy(Qt::FocusPolicy::StrongFocus); |
| 356 |
} |
| 357 |
|
| 358 |
void PrefComboBox::wheelEvent(QWheelEvent* event) |
| 359 |
{ |
| 360 |
if (hasFocus()) { |
| 361 |
QComboBox::wheelEvent(event); |
| 362 |
} |
| 363 |
else { |
| 364 |
event->ignore(); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
PrefComboBox::~PrefComboBox() = default; |
| 369 |
|
| 370 |
QMetaType::Type PrefComboBox::getParamType() const |
| 371 |
{ |
| 372 |
return static_cast<QMetaType::Type>(property("prefType").userType()); |
| 373 |
} |
| 374 |
|
| 375 |
void PrefComboBox::restorePreferences() |
| 376 |
{ |
| 377 |
if (getWindowParameter().isNull()) { |
| 378 |
failedToRestore(objectName()); |
| 379 |
return; |
| 380 |
} |
| 381 |
if (!m_Restored) { |
| 382 |
m_Default = currentData(); |
| 383 |
m_DefaultText = currentText(); |
| 384 |
m_DefaultIndex = currentIndex(); |
| 385 |
} |
| 386 |
int index = -1; |
| 387 |
switch (static_cast<int>(getParamType())) { |
| 388 |
case QMetaType::Int: |
| 389 |
case QMetaType::LongLong: |
| 390 |
index = findData( |
| 391 |
static_cast<int>(getWindowParameter()->GetInt(entryName(), m_Default.toInt())) |
| 392 |
); |
| 393 |
break; |
| 394 |
case QMetaType::UInt: |
| 395 |
case QMetaType::ULongLong: |
| 396 |
index = findData( |
| 397 |
static_cast<uint>(getWindowParameter()->GetUnsigned(entryName(), m_Default.toUInt())) |
| 398 |
); |
| 399 |
break; |
| 400 |
case QMetaType::Bool: |
| 401 |
index = findData(getWindowParameter()->GetBool(entryName(), m_Default.toBool())); |
| 402 |
break; |
| 403 |
case QMetaType::Double: |
| 404 |
index = findData(getWindowParameter()->GetFloat(entryName(), m_Default.toDouble())); |
| 405 |
break; |
| 406 |
case QMetaType::QString: |
| 407 |
index = findText( |
| 408 |
QString::fromUtf8( |
| 409 |
getWindowParameter()->GetASCII(entryName(), m_DefaultText.toUtf8().constData()).c_str() |
| 410 |
) |
| 411 |
); |
| 412 |
break; |
| 413 |
case QMetaType::QByteArray: |
| 414 |
index = findData(QByteArray( |
| 415 |
getWindowParameter()->GetASCII(entryName(), m_Default.toByteArray().constData()).c_str() |
| 416 |
)); |
| 417 |
break; |
| 418 |
default: |
| 419 |
index = getWindowParameter()->GetInt(entryName(), m_DefaultIndex); |
| 420 |
break; |
| 421 |
} |
| 422 |
if (index >= 0 && index < count()) { |
| 423 |
setCurrentIndex(index); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
void PrefComboBox::savePreferences() |
| 428 |
{ |
| 429 |
if (getWindowParameter().isNull()) { |
| 430 |
failedToSave(objectName()); |
| 431 |
return; |
| 432 |
} |
| 433 |
|
| 434 |
switch (static_cast<int>(getParamType())) { |
| 435 |
case QMetaType::Int: |
| 436 |
case QMetaType::LongLong: |
| 437 |
getWindowParameter()->SetInt(entryName(), currentData().toInt()); |
| 438 |
break; |
| 439 |
case QMetaType::UInt: |
| 440 |
case QMetaType::ULongLong: |
| 441 |
getWindowParameter()->SetUnsigned(entryName(), currentData().toUInt()); |
| 442 |
break; |
| 443 |
case QMetaType::Bool: |
| 444 |
getWindowParameter()->SetBool(entryName(), currentData().toBool()); |
| 445 |
break; |
| 446 |
case QMetaType::Double: |
| 447 |
getWindowParameter()->SetFloat(entryName(), currentData().toDouble()); |
| 448 |
break; |
| 449 |
case QMetaType::QString: |
| 450 |
getWindowParameter()->SetASCII(entryName(), currentText().toUtf8().constData()); |
| 451 |
break; |
| 452 |
case QMetaType::QByteArray: |
| 453 |
getWindowParameter()->SetASCII(entryName(), currentData().toByteArray().constData()); |
| 454 |
break; |
| 455 |
default: |
| 456 |
getWindowParameter()->SetInt(entryName(), currentIndex()); |
| 457 |
break; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
// -------------------------------------------------------------------- |
| 462 |
|
| 463 |
PrefCheckBox::PrefCheckBox(QWidget* parent) |
| 464 |
: QCheckBox(parent) |
| 465 |
, PrefWidget() |
| 466 |
{} |
| 467 |
|
| 468 |
PrefCheckBox::~PrefCheckBox() = default; |
| 469 |
|
| 470 |
void PrefCheckBox::restorePreferences() |
| 471 |
{ |
| 472 |
if (getWindowParameter().isNull()) { |
| 473 |
failedToRestore(objectName()); |
| 474 |
return; |
| 475 |
} |
| 476 |
|
| 477 |
bool enable = getWindowParameter()->GetBool(entryName(), isChecked()); |
| 478 |
setChecked(enable); |
| 479 |
} |
| 480 |
|
| 481 |
void PrefCheckBox::savePreferences() |
| 482 |
{ |
| 483 |
if (getWindowParameter().isNull()) { |
| 484 |
failedToSave(objectName()); |
| 485 |
return; |
| 486 |
} |
| 487 |
|
| 488 |
getWindowParameter()->SetBool(entryName(), isChecked()); |
| 489 |
} |
| 490 |
|
| 491 |
// -------------------------------------------------------------------- |
| 492 |
|
| 493 |
PrefRadioButton::PrefRadioButton(QWidget* parent) |
| 494 |
: QRadioButton(parent) |
| 495 |
, PrefWidget() |
| 496 |
{} |
| 497 |
|
| 498 |
PrefRadioButton::~PrefRadioButton() = default; |
| 499 |
|
| 500 |
void PrefRadioButton::restorePreferences() |
| 501 |
{ |
| 502 |
if (getWindowParameter().isNull()) { |
| 503 |
failedToRestore(objectName()); |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
bool enable = getWindowParameter()->GetBool(entryName(), isChecked()); |
| 508 |
setChecked(enable); |
| 509 |
} |
| 510 |
|
| 511 |
void PrefRadioButton::savePreferences() |
| 512 |
{ |
| 513 |
if (getWindowParameter().isNull()) { |
| 514 |
failedToSave(objectName()); |
| 515 |
return; |
| 516 |
} |
| 517 |
|
| 518 |
getWindowParameter()->SetBool(entryName(), isChecked()); |
| 519 |
} |
| 520 |
|
| 521 |
// -------------------------------------------------------------------- |
| 522 |
|
| 523 |
PrefSlider::PrefSlider(QWidget* parent) |
| 524 |
: QSlider(parent) |
| 525 |
, PrefWidget() |
| 526 |
{} |
| 527 |
|
| 528 |
PrefSlider::~PrefSlider() = default; |
| 529 |
|
| 530 |
void PrefSlider::restorePreferences() |
| 531 |
{ |
| 532 |
if (getWindowParameter().isNull()) { |
| 533 |
failedToRestore(objectName()); |
| 534 |
return; |
| 535 |
} |
| 536 |
|
| 537 |
int nVal = getWindowParameter()->GetInt(entryName(), QSlider::value()); |
| 538 |
setValue(nVal); |
| 539 |
} |
| 540 |
|
| 541 |
void PrefSlider::savePreferences() |
| 542 |
{ |
| 543 |
if (getWindowParameter().isNull()) { |
| 544 |
failedToSave(objectName()); |
| 545 |
return; |
| 546 |
} |
| 547 |
|
| 548 |
getWindowParameter()->SetInt(entryName(), (int)value()); |
| 549 |
} |
| 550 |
|
| 551 |
// -------------------------------------------------------------------- |
| 552 |
|
| 553 |
PrefColorButton::PrefColorButton(QWidget* parent) |
| 554 |
: ColorButton(parent) |
| 555 |
, PrefWidget() |
| 556 |
{} |
| 557 |
|
| 558 |
PrefColorButton::~PrefColorButton() = default; |
| 559 |
|
| 560 |
void PrefColorButton::restorePreferences() |
| 561 |
{ |
| 562 |
if (getWindowParameter().isNull()) { |
| 563 |
failedToRestore(objectName()); |
| 564 |
return; |
| 565 |
} |
| 566 |
|
| 567 |
if (!m_Restored) { |
| 568 |
m_Default = color(); |
| 569 |
} |
| 570 |
|
| 571 |
unsigned int icol = Base::Color::asPackedRGBA<QColor>(m_Default); |
| 572 |
|
| 573 |
unsigned long lcol = static_cast<unsigned long>(icol); |
| 574 |
lcol = getWindowParameter()->GetUnsigned(entryName(), lcol); |
| 575 |
icol = static_cast<unsigned int>(lcol); |
| 576 |
QColor value = Base::Color::fromPackedRGBA<QColor>(icol); |
| 577 |
if (!this->allowTransparency()) { |
| 578 |
value.setAlpha(0xff); |
| 579 |
} |
| 580 |
setColor(value); |
| 581 |
} |
| 582 |
|
| 583 |
void PrefColorButton::savePreferences() |
| 584 |
{ |
| 585 |
if (getWindowParameter().isNull()) { |
| 586 |
failedToSave(objectName()); |
| 587 |
return; |
| 588 |
} |
| 589 |
|
| 590 |
QColor col = color(); |
| 591 |
// (r,g,b,a) with a = 255 (opaque) |
| 592 |
unsigned int icol = Base::Color::asPackedRGBA<QColor>(col); |
| 593 |
unsigned long lcol = static_cast<unsigned long>(icol); |
| 594 |
getWindowParameter()->SetUnsigned(entryName(), lcol); |
| 595 |
} |
| 596 |
|
| 597 |
// -------------------------------------------------------------------- |
| 598 |
|
| 599 |
PrefUnitSpinBox::PrefUnitSpinBox(QWidget* parent) |
| 600 |
: QuantitySpinBox(parent) |
| 601 |
, PrefWidget() |
| 602 |
{ |
| 603 |
setFocusPolicy(Qt::FocusPolicy::StrongFocus); |
| 604 |
} |
| 605 |
|
| 606 |
void PrefUnitSpinBox::wheelEvent(QWheelEvent* event) |
| 607 |
{ |
| 608 |
if (hasFocus()) { |
| 609 |
QuantitySpinBox::wheelEvent(event); |
| 610 |
} |
| 611 |
else { |
| 612 |
event->ignore(); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
PrefUnitSpinBox::~PrefUnitSpinBox() = default; |
| 617 |
|
| 618 |
void PrefUnitSpinBox::restorePreferences() |
| 619 |
{ |
| 620 |
if (getWindowParameter().isNull()) { |
| 621 |
failedToRestore(objectName()); |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
double fVal = (double)getWindowParameter()->GetFloat(entryName(), rawValue()); |
| 626 |
setValue(fVal); |
| 627 |
} |
| 628 |
|
| 629 |
void PrefUnitSpinBox::savePreferences() |
| 630 |
{ |
| 631 |
if (getWindowParameter().isNull()) { |
| 632 |
failedToSave(objectName()); |
| 633 |
return; |
| 634 |
} |
| 635 |
|
| 636 |
double q = rawValue(); |
| 637 |
getWindowParameter()->SetFloat(entryName(), q); |
| 638 |
} |
| 639 |
|
| 640 |
// -------------------------------------------------------------------- |
| 641 |
|
| 642 |
namespace Gui |
| 643 |
{ |
| 644 |
class HistoryList |
| 645 |
{ |
| 646 |
QStringList list; |
| 647 |
int max_size = 5; |
| 648 |
|
| 649 |
public: |
| 650 |
const QStringList& asStringList() const |
| 651 |
{ |
| 652 |
return list; |
| 653 |
} |
| 654 |
int maximumSize() const |
| 655 |
{ |
| 656 |
return max_size; |
| 657 |
} |
| 658 |
void setMaximumSize(int num) |
| 659 |
{ |
| 660 |
max_size = num; |
| 661 |
while (list.size() > num) { |
| 662 |
list.pop_front(); |
| 663 |
} |
| 664 |
} |
| 665 |
void clear() |
| 666 |
{ |
| 667 |
list.clear(); |
| 668 |
} |
| 669 |
void append(const QString& value) |
| 670 |
{ |
| 671 |
if (!list.isEmpty() && list.back() == value) { |
| 672 |
return; |
| 673 |
} |
| 674 |
const auto it = std::ranges::find(list, value); |
| 675 |
if (it != list.end()) { |
| 676 |
list.erase(it); |
| 677 |
} |
| 678 |
else if (list.size() == max_size) { |
| 679 |
list.pop_front(); |
| 680 |
} |
| 681 |
list.push_back(value); |
| 682 |
} |
| 683 |
}; |
| 684 |
|
| 685 |
class PrefQuantitySpinBoxPrivate |
| 686 |
{ |
| 687 |
public: |
| 688 |
HistoryList history; |
| 689 |
bool isSaving = false; |
| 690 |
|
| 691 |
QByteArray getHistoryGroupName(QByteArray name) const |
| 692 |
{ |
| 693 |
return name + "_History"; |
| 694 |
} |
| 695 |
|
| 696 |
void restoreHistory(ParameterGrp::handle hGrp) |
| 697 |
{ |
| 698 |
std::vector<std::string> hist = hGrp->GetASCIIs("Hist"); |
| 699 |
for (const auto& it : hist) { |
| 700 |
history.append(QString::fromStdString(it)); |
| 701 |
} |
| 702 |
} |
| 703 |
void clearHistory(ParameterGrp::handle hGrp) |
| 704 |
{ |
| 705 |
std::vector<std::string> hist = hGrp->GetASCIIs("Hist"); |
| 706 |
for (const auto& it : hist) { |
| 707 |
hGrp->RemoveASCII(it.c_str()); |
| 708 |
} |
| 709 |
} |
| 710 |
void saveHistory(ParameterGrp::handle hGrp) |
| 711 |
{ |
| 712 |
clearHistory(hGrp); |
| 713 |
|
| 714 |
const QStringList& list = history.asStringList(); |
| 715 |
for (int i = 0; i < list.size(); i++) { |
| 716 |
QByteArray key("Hist"); |
| 717 |
key.append(QByteArray::number(i)); |
| 718 |
hGrp->SetASCII(key, list[i].toUtf8()); |
| 719 |
} |
| 720 |
} |
| 721 |
}; |
| 722 |
} // namespace Gui |
| 723 |
|
| 724 |
PrefQuantitySpinBox::PrefQuantitySpinBox(QWidget* parent) |
| 725 |
: QuantitySpinBox(parent) |
| 726 |
, d_ptr(new PrefQuantitySpinBoxPrivate()) |
| 727 |
{ |
| 728 |
setFocusPolicy(Qt::FocusPolicy::StrongFocus); |
| 729 |
} |
| 730 |
|
| 731 |
PrefQuantitySpinBox::~PrefQuantitySpinBox() = default; |
| 732 |
|
| 733 |
void PrefQuantitySpinBox::contextMenuEvent(QContextMenuEvent* event) |
| 734 |
{ |
| 735 |
Q_D(PrefQuantitySpinBox); |
| 736 |
|
| 737 |
QMenu* editMenu = lineEdit()->createStandardContextMenu(); |
| 738 |
editMenu->setTitle(tr("Edit")); |
| 739 |
std::unique_ptr<QMenu> menu(new QMenu(QStringLiteral("PrefQuantitySpinBox"))); |
| 740 |
|
| 741 |
menu->addMenu(editMenu); |
| 742 |
menu->addSeparator(); |
| 743 |
|
| 744 |
// data structure to remember actions for values |
| 745 |
QStringList history = d->history.asStringList(); |
| 746 |
for (const auto& it : history) { |
| 747 |
QAction* action = menu->addAction(it); |
| 748 |
action->setProperty("history_value", it); |
| 749 |
} |
| 750 |
|
| 751 |
// add the save value portion of the menu |
| 752 |
menu->addSeparator(); |
| 753 |
QAction* saveValueAction = menu->addAction(tr("Save Value")); |
| 754 |
QAction* clearListAction = menu->addAction(tr("Clear List")); |
| 755 |
clearListAction->setDisabled(history.empty()); |
| 756 |
|
| 757 |
// call the menu |
| 758 |
QAction* userAction = menu->exec(event->globalPos()); |
| 759 |
|
| 760 |
// look what the user has chosen |
| 761 |
if (userAction == saveValueAction) { |
| 762 |
pushToHistory(this->text()); |
| 763 |
} |
| 764 |
else if (userAction == clearListAction) { |
| 765 |
d->history.clear(); |
| 766 |
} |
| 767 |
else if (userAction) { |
| 768 |
QVariant prop = userAction->property("history_value"); |
| 769 |
if (prop.isValid()) { |
| 770 |
lineEdit()->setText(prop.toString()); |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
void PrefQuantitySpinBox::wheelEvent(QWheelEvent* event) |
| 776 |
{ |
| 777 |
if (hasFocus()) { |
| 778 |
QuantitySpinBox::wheelEvent(event); |
| 779 |
} |
| 780 |
else { |
| 781 |
event->ignore(); |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
void PrefQuantitySpinBox::restorePreferences() |
| 786 |
{ |
| 787 |
Q_D(PrefQuantitySpinBox); |
| 788 |
|
| 789 |
// Do not restore values while saving them |
| 790 |
if (d->isSaving) { |
| 791 |
return; |
| 792 |
} |
| 793 |
|
| 794 |
if (getWindowParameter().isNull() || entryName().isEmpty()) { |
| 795 |
failedToRestore(objectName()); |
| 796 |
return; |
| 797 |
} |
| 798 |
|
| 799 |
QString text = this->text(); |
| 800 |
text = QString::fromUtf8(getWindowParameter()->GetASCII(entryName(), text.toUtf8()).c_str()); |
| 801 |
lineEdit()->setText(text); |
| 802 |
|
| 803 |
// Restore history |
| 804 |
auto hGrp = getWindowParameter()->GetGroup(d->getHistoryGroupName(entryName())); |
| 805 |
d->restoreHistory(hGrp); |
| 806 |
} |
| 807 |
|
| 808 |
void PrefQuantitySpinBox::savePreferences() |
| 809 |
{ |
| 810 |
Q_D(PrefQuantitySpinBox); |
| 811 |
if (getWindowParameter().isNull() || entryName().isEmpty()) { |
| 812 |
failedToSave(objectName()); |
| 813 |
return; |
| 814 |
} |
| 815 |
|
| 816 |
getWindowParameter()->SetASCII(entryName(), text().toUtf8()); |
| 817 |
|
| 818 |
// Save history |
| 819 |
auto hGrp = getWindowParameter()->GetGroup(d->getHistoryGroupName(entryName())); |
| 820 |
d->saveHistory(hGrp); |
| 821 |
} |
| 822 |
|
| 823 |
void PrefQuantitySpinBox::pushToHistory(const QString& value) |
| 824 |
{ |
| 825 |
Q_D(PrefQuantitySpinBox); |
| 826 |
d->history.append(value.isEmpty() ? this->text() : value); |
| 827 |
|
| 828 |
Base::StateLocker lock(d->isSaving); |
| 829 |
onSave(); |
| 830 |
} |
| 831 |
|
| 832 |
QStringList PrefQuantitySpinBox::getHistory() const |
| 833 |
{ |
| 834 |
Q_D(const PrefQuantitySpinBox); |
| 835 |
return d->history.asStringList(); |
| 836 |
} |
| 837 |
|
| 838 |
void PrefQuantitySpinBox::setToLastUsedValue() |
| 839 |
{ |
| 840 |
QStringList hist = getHistory(); |
| 841 |
if (!hist.empty()) { |
| 842 |
lineEdit()->setText(hist.front()); |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
int PrefQuantitySpinBox::historySize() const |
| 847 |
{ |
| 848 |
Q_D(const PrefQuantitySpinBox); |
| 849 |
return d->history.maximumSize(); |
| 850 |
} |
| 851 |
|
| 852 |
void PrefQuantitySpinBox::setHistorySize(int i) |
| 853 |
{ |
| 854 |
Q_D(PrefQuantitySpinBox); |
| 855 |
d->history.setMaximumSize(i); |
| 856 |
} |
| 857 |
|
| 858 |
// -------------------------------------------------------------------- |
| 859 |
|
| 860 |
PrefFontBox::PrefFontBox(QWidget* parent) |
| 861 |
: QFontComboBox(parent) |
| 862 |
, PrefWidget() |
| 863 |
{} |
| 864 |
|
| 865 |
PrefFontBox::~PrefFontBox() = default; |
| 866 |
|
| 867 |
void PrefFontBox::restorePreferences() |
| 868 |
{ |
| 869 |
if (getWindowParameter().isNull()) { |
| 870 |
failedToRestore(objectName()); |
| 871 |
return; |
| 872 |
} |
| 873 |
|
| 874 |
QFont currFont = currentFont(); // QFont from selector widget |
| 875 |
QString currName = currFont.family(); |
| 876 |
|
| 877 |
std::string prefName |
| 878 |
= getWindowParameter()->GetASCII(entryName(), currName.toUtf8()); // font name from cfg file |
| 879 |
|
| 880 |
currFont.setFamily(QString::fromStdString(prefName)); |
| 881 |
setCurrentFont(currFont); // set selector widget to name from cfg file |
| 882 |
} |
| 883 |
|
| 884 |
void PrefFontBox::savePreferences() |
| 885 |
{ |
| 886 |
if (getWindowParameter().isNull()) { |
| 887 |
failedToSave(objectName()); |
| 888 |
return; |
| 889 |
} |
| 890 |
|
| 891 |
QFont currFont = currentFont(); |
| 892 |
QString currName = currFont.family(); |
| 893 |
getWindowParameter()->SetASCII(entryName(), currName.toUtf8()); |
| 894 |
} |
| 895 |
|
| 896 |
// -------------------------------------------------------------------- |
| 897 |
|
| 898 |
PrefCheckableGroupBox::PrefCheckableGroupBox(QWidget* parent) |
| 899 |
: QGroupBox(parent) |
| 900 |
, PrefWidget() |
| 901 |
{} |
| 902 |
|
| 903 |
PrefCheckableGroupBox::~PrefCheckableGroupBox() = default; |
| 904 |
|
| 905 |
void PrefCheckableGroupBox::restorePreferences() |
| 906 |
{ |
| 907 |
if (getWindowParameter().isNull() || entryName().isEmpty()) { |
| 908 |
failedToRestore(objectName()); |
| 909 |
return; |
| 910 |
} |
| 911 |
|
| 912 |
// Default value is the current state of the checkbox (usually from .ui on first load) |
| 913 |
bool defaultValueInUi = isChecked(); |
| 914 |
bool actualValue = getWindowParameter()->GetBool(entryName(), defaultValueInUi); |
| 915 |
setChecked(actualValue); |
| 916 |
} |
| 917 |
|
| 918 |
void PrefCheckableGroupBox::savePreferences() |
| 919 |
{ |
| 920 |
if (getWindowParameter().isNull() || entryName().isEmpty()) { |
| 921 |
failedToSave(objectName()); |
| 922 |
return; |
| 923 |
} |
| 924 |
|
| 925 |
getWindowParameter()->SetBool(entryName(), isChecked()); |
| 926 |
} |
| 927 |
|
| 928 |
#include "moc_PrefWidgets.cpp" |