23
#include "PreCompiled.h"
25
# include <QContextMenuEvent>
31
#include <Base/Console.h>
32
#include <Base/Tools.h>
35
#include "PrefWidgets.h"
43
PrefWidget::PrefWidget()
51
PrefWidget::~PrefWidget()
53
if (getWindowParameter().isValid())
54
getWindowParameter()->Detach(this);
58
void PrefWidget::setEntryName( const QByteArray& name )
64
void PrefWidget::setPrefEntry(const QByteArray& name)
70
QByteArray PrefWidget::entryName() const
76
void PrefWidget::setParamGrpPath( const QByteArray& path )
79
if (getWindowParameter().isValid())
81
if ( paramGrpPath() != path )
82
Base::Console().Warning("Widget already attached\n");
86
if ( paramGrpPath() != path )
88
if ( setGroupName( path ) )
91
assert(getWindowParameter().isValid());
92
getWindowParameter()->Attach(this);
98
void PrefWidget::setPrefPath(const QByteArray& name)
100
setParamGrpPath(name);
104
QByteArray PrefWidget::paramGrpPath() const
116
void PrefWidget::OnChange(Base::Subject<const char*> &rCaller, const char * sReason)
119
if (std::strcmp(sReason,m_sPrefName) == 0)
120
restorePreferences();
127
void PrefWidget::onSave()
130
if (getWindowParameter().isValid())
131
getWindowParameter()->Notify( entryName() );
134
qFatal( "No parameter group specified!" );
141
void PrefWidget::onRestore()
144
if (getWindowParameter().isNull())
145
qWarning( "No parameter group specified!" );
147
restorePreferences();
151
void PrefWidget::failedToSave(const QString& name) const
153
QByteArray objname = name.toLatin1();
154
if (objname.isEmpty())
155
objname = "Undefined";
156
Console().Warning("Cannot save %s (%s)\n", typeid(*this).name(), objname.constData());
159
void PrefWidget::failedToRestore(const QString& name) const
161
QByteArray objname = name.toLatin1();
162
if (objname.isEmpty())
163
objname = "Undefined";
164
Console().Warning("Cannot restore %s (%s)\n", typeid(*this).name(), objname.constData());
169
PrefSpinBox::PrefSpinBox ( QWidget * parent )
170
: QSpinBox(parent), PrefWidget()
174
PrefSpinBox::~PrefSpinBox() = default;
176
void PrefSpinBox::restorePreferences()
178
if ( getWindowParameter().isNull() )
180
failedToRestore(objectName());
184
int nVal = getWindowParameter()->GetInt( entryName(), QSpinBox::value() );
188
void PrefSpinBox::savePreferences()
190
if (getWindowParameter().isNull())
192
failedToSave(objectName());
196
getWindowParameter()->SetInt( entryName() , (int)value() );
201
PrefDoubleSpinBox::PrefDoubleSpinBox ( QWidget * parent )
202
: QDoubleSpinBox(parent), PrefWidget()
206
PrefDoubleSpinBox::~PrefDoubleSpinBox() = default;
208
void PrefDoubleSpinBox::restorePreferences()
210
if ( getWindowParameter().isNull() )
212
failedToRestore(objectName());
216
double fVal = (double)getWindowParameter()->GetFloat( entryName() , value() );
220
void PrefDoubleSpinBox::savePreferences()
222
if (getWindowParameter().isNull())
224
failedToSave(objectName());
228
getWindowParameter()->SetFloat( entryName(), value() );
233
PrefLineEdit::PrefLineEdit ( QWidget * parent )
234
: QLineEdit(parent), PrefWidget()
238
PrefLineEdit::~PrefLineEdit() = default;
240
void PrefLineEdit::restorePreferences()
242
if (getWindowParameter().isNull())
244
failedToRestore(objectName());
248
QString text = this->text();
249
text = QString::fromUtf8(getWindowParameter()->GetASCII(entryName(), text.toUtf8()).c_str());
253
void PrefLineEdit::savePreferences()
255
if (getWindowParameter().isNull())
257
failedToSave(objectName());
261
getWindowParameter()->SetASCII(entryName(), text().toUtf8());
266
PrefTextEdit::PrefTextEdit(QWidget* parent)
267
: QTextEdit(parent), PrefWidget()
271
PrefTextEdit::~PrefTextEdit() = default;
273
void PrefTextEdit::restorePreferences()
275
if (getWindowParameter().isNull())
277
failedToRestore(objectName());
281
QString text = this->toPlainText();
282
text = QString::fromUtf8(getWindowParameter()->GetASCII(entryName(), text.toUtf8()).c_str());
286
void PrefTextEdit::savePreferences()
288
if (getWindowParameter().isNull())
290
failedToSave(objectName());
294
QString text = this->toPlainText();
295
getWindowParameter()->SetASCII(entryName(), text.toUtf8());
300
PrefFileChooser::PrefFileChooser ( QWidget * parent )
301
: FileChooser(parent), PrefWidget()
305
PrefFileChooser::~PrefFileChooser() = default;
307
void PrefFileChooser::restorePreferences()
309
if (getWindowParameter().isNull())
311
failedToRestore(objectName());
315
QString txt = QString::fromUtf8(getWindowParameter()->GetASCII(entryName(), fileName().toUtf8()).c_str());
319
void PrefFileChooser::savePreferences()
321
if (getWindowParameter().isNull())
323
failedToSave(objectName());
327
getWindowParameter()->SetASCII(entryName(), fileName().toUtf8());
332
PrefComboBox::PrefComboBox ( QWidget * parent )
333
: QComboBox(parent), PrefWidget()
337
PrefComboBox::~PrefComboBox() = default;
339
QMetaType::Type PrefComboBox::getParamType() const
341
return static_cast<QMetaType::Type>(property("prefType").userType());
344
void PrefComboBox::restorePreferences()
346
if (getWindowParameter().isNull())
348
failedToRestore(objectName());
352
m_Default = currentData();
353
m_DefaultText = currentText();
354
m_DefaultIndex = currentIndex();
357
switch(static_cast<int>(getParamType())) {
359
case QMetaType::LongLong:
360
index = findData(static_cast<int>(getWindowParameter()->GetInt(entryName(), m_Default.toInt())));
362
case QMetaType::UInt:
363
case QMetaType::ULongLong:
364
index = findData(static_cast<uint>(getWindowParameter()->GetUnsigned(entryName(), m_Default.toUInt())));
366
case QMetaType::Bool:
367
index = findData(getWindowParameter()->GetBool(entryName(), m_Default.toBool()));
369
case QMetaType::Double:
370
index = findData(getWindowParameter()->GetFloat(entryName(), m_Default.toDouble()));
372
case QMetaType::QString:
373
index = findText(QString::fromUtf8(
374
getWindowParameter()->GetASCII(entryName(), m_DefaultText.toUtf8().constData()).c_str()));
376
case QMetaType::QByteArray:
377
index = findData(QByteArray(getWindowParameter()->GetASCII(entryName(),
378
m_Default.toByteArray().constData()).c_str()));
381
index = getWindowParameter()->GetInt(entryName(), m_DefaultIndex);
384
if (index >= 0 && index < count())
385
setCurrentIndex(index);
388
void PrefComboBox::savePreferences()
390
if (getWindowParameter().isNull())
392
failedToSave(objectName());
396
switch(static_cast<int>(getParamType())) {
398
case QMetaType::LongLong:
399
getWindowParameter()->SetInt(entryName(), currentData().toInt());
401
case QMetaType::UInt:
402
case QMetaType::ULongLong:
403
getWindowParameter()->SetUnsigned(entryName(), currentData().toUInt());
405
case QMetaType::Bool:
406
getWindowParameter()->SetBool(entryName(), currentData().toBool());
408
case QMetaType::Double:
409
getWindowParameter()->SetFloat(entryName(), currentData().toDouble());
411
case QMetaType::QString:
412
getWindowParameter()->SetASCII(entryName(), currentText().toUtf8().constData());
414
case QMetaType::QByteArray:
415
getWindowParameter()->SetASCII(entryName(), currentData().toByteArray().constData());
418
getWindowParameter()->SetInt(entryName(), currentIndex());
425
PrefCheckBox::PrefCheckBox ( QWidget * parent )
426
: QCheckBox(parent), PrefWidget()
430
PrefCheckBox::~PrefCheckBox() = default;
432
void PrefCheckBox::restorePreferences()
434
if (getWindowParameter().isNull())
436
failedToRestore(objectName());
440
bool enable = getWindowParameter()->GetBool( entryName(), isChecked() );
444
void PrefCheckBox::savePreferences()
446
if (getWindowParameter().isNull())
448
failedToSave(objectName());
452
getWindowParameter()->SetBool( entryName(), isChecked() );
457
PrefRadioButton::PrefRadioButton ( QWidget * parent )
458
: QRadioButton(parent), PrefWidget()
462
PrefRadioButton::~PrefRadioButton() = default;
464
void PrefRadioButton::restorePreferences()
466
if (getWindowParameter().isNull())
468
failedToRestore(objectName());
472
bool enable = getWindowParameter()->GetBool( entryName(), isChecked() );
476
void PrefRadioButton::savePreferences()
478
if (getWindowParameter().isNull())
480
failedToSave(objectName());
484
getWindowParameter()->SetBool( entryName() , isChecked() );
489
PrefSlider::PrefSlider ( QWidget * parent )
490
: QSlider(parent), PrefWidget()
494
PrefSlider::~PrefSlider() = default;
496
void PrefSlider::restorePreferences()
498
if ( getWindowParameter().isNull() )
500
failedToRestore(objectName());
504
int nVal = getWindowParameter()->GetInt(entryName(), QSlider::value());
508
void PrefSlider::savePreferences()
510
if (getWindowParameter().isNull())
512
failedToSave(objectName());
516
getWindowParameter()->SetInt(entryName() , (int)value());
521
PrefColorButton::PrefColorButton ( QWidget * parent )
522
: ColorButton(parent), PrefWidget()
526
PrefColorButton::~PrefColorButton() = default;
528
void PrefColorButton::restorePreferences()
530
if (getWindowParameter().isNull()) {
531
failedToRestore(objectName());
538
unsigned int icol = App::Color::asPackedRGBA<QColor>(m_Default);
540
unsigned long lcol = static_cast<unsigned long>(icol);
541
lcol = getWindowParameter()->GetUnsigned(entryName(), lcol);
542
icol = static_cast<unsigned int>(lcol);
543
QColor value = App::Color::fromPackedRGBA<QColor>(icol);
544
if (!this->allowTransparency())
545
value.setAlpha(0xff);
549
void PrefColorButton::savePreferences()
551
if (getWindowParameter().isNull())
553
failedToSave(objectName());
557
QColor col = color();
559
unsigned int icol = App::Color::asPackedRGBA<QColor>(col);
560
unsigned long lcol = static_cast<unsigned long>(icol);
561
getWindowParameter()->SetUnsigned( entryName(), lcol );
566
PrefUnitSpinBox::PrefUnitSpinBox ( QWidget * parent )
567
: QuantitySpinBox(parent), PrefWidget()
571
PrefUnitSpinBox::~PrefUnitSpinBox() = default;
573
void PrefUnitSpinBox::restorePreferences()
575
if (getWindowParameter().isNull()) {
576
failedToRestore(objectName());
580
double fVal = (double)getWindowParameter()->GetFloat( entryName() ,rawValue() );
584
void PrefUnitSpinBox::savePreferences()
586
if (getWindowParameter().isNull()) {
587
failedToSave(objectName());
591
double q = rawValue();
592
getWindowParameter()->SetFloat( entryName(), q );
602
const QStringList& asStringList() const {
605
int maximumSize() const {
608
void setMaximumSize(int num) {
610
while (list.size() > num)
616
void append(const QString& value) {
617
if (!list.isEmpty() && list.back() == value)
619
auto it = std::find(list.begin(), list.end(), value);
620
if (it != list.end())
622
else if (list.size() == max_size)
624
list.push_back(value);
628
class PrefQuantitySpinBoxPrivate
632
bool isSaving = false;
634
QByteArray getHistoryGroupName(QByteArray name) const {
635
return name + "_History";
638
void restoreHistory(ParameterGrp::handle hGrp) {
639
std::vector<std::string> hist = hGrp->GetASCIIs("Hist");
640
for (const auto& it : hist)
641
history.append(QString::fromStdString(it));
643
void clearHistory(ParameterGrp::handle hGrp) {
644
std::vector<std::string> hist = hGrp->GetASCIIs("Hist");
645
for (const auto& it : hist)
646
hGrp->RemoveASCII(it.c_str());
648
void saveHistory(ParameterGrp::handle hGrp) {
651
const QStringList& list = history.asStringList();
652
for (int i = 0; i < list.size(); i++) {
653
QByteArray key("Hist");
654
key.append(QByteArray::number(i));
655
hGrp->SetASCII(key, list[i].toUtf8());
661
PrefQuantitySpinBox::PrefQuantitySpinBox (QWidget * parent)
662
: QuantitySpinBox(parent)
663
, d_ptr(new PrefQuantitySpinBoxPrivate())
667
PrefQuantitySpinBox::~PrefQuantitySpinBox() = default;
669
void PrefQuantitySpinBox::contextMenuEvent(QContextMenuEvent *event)
671
Q_D(PrefQuantitySpinBox);
673
QMenu *editMenu = lineEdit()->createStandardContextMenu();
674
editMenu->setTitle(tr("Edit"));
675
std::unique_ptr<QMenu> menu(new QMenu(QString::fromLatin1("PrefQuantitySpinBox")));
677
menu->addMenu(editMenu);
678
menu->addSeparator();
681
QStringList history = d->history.asStringList();
682
for (const auto & it : history) {
683
QAction* action = menu->addAction(it);
684
action->setProperty("history_value", it);
688
menu->addSeparator();
689
QAction *saveValueAction = menu->addAction(tr("Save value"));
690
QAction *clearListAction = menu->addAction(tr("Clear list"));
691
clearListAction->setDisabled(history.empty());
694
QAction *userAction = menu->exec(event->globalPos());
697
if (userAction == saveValueAction) {
698
pushToHistory(this->text());
700
else if (userAction == clearListAction) {
703
else if (userAction) {
704
QVariant prop = userAction->property("history_value");
705
if (prop.isValid()) {
706
lineEdit()->setText(prop.toString());
711
void PrefQuantitySpinBox::restorePreferences()
713
Q_D(PrefQuantitySpinBox);
719
if (getWindowParameter().isNull() || entryName().isEmpty()) {
720
failedToRestore(objectName());
724
QString text = this->text();
725
text = QString::fromUtf8(getWindowParameter()->GetASCII(entryName(), text.toUtf8()).c_str());
726
lineEdit()->setText(text);
729
auto hGrp = getWindowParameter()->GetGroup(d->getHistoryGroupName(entryName()));
730
d->restoreHistory(hGrp);
733
void PrefQuantitySpinBox::savePreferences()
735
Q_D(PrefQuantitySpinBox);
736
if (getWindowParameter().isNull() || entryName().isEmpty()) {
737
failedToSave(objectName());
741
getWindowParameter()->SetASCII( entryName(), text().toUtf8() );
744
auto hGrp = getWindowParameter()->GetGroup(d->getHistoryGroupName(entryName()));
745
d->saveHistory(hGrp);
748
void PrefQuantitySpinBox::pushToHistory(const QString &value)
750
Q_D(PrefQuantitySpinBox);
751
d->history.append(value.isEmpty() ? this->text() : value);
753
Base::StateLocker lock(d->isSaving);
757
QStringList PrefQuantitySpinBox::getHistory() const
759
Q_D(const PrefQuantitySpinBox);
760
return d->history.asStringList();
763
void PrefQuantitySpinBox::setToLastUsedValue()
765
QStringList hist = getHistory();
767
lineEdit()->setText(hist.front());
770
int PrefQuantitySpinBox::historySize() const
772
Q_D(const PrefQuantitySpinBox);
773
return d->history.maximumSize();
776
void PrefQuantitySpinBox::setHistorySize(int i)
778
Q_D(PrefQuantitySpinBox);
779
d->history.setMaximumSize(i);
784
PrefFontBox::PrefFontBox ( QWidget * parent )
785
: QFontComboBox(parent), PrefWidget()
789
PrefFontBox::~PrefFontBox() = default;
791
void PrefFontBox::restorePreferences()
793
if ( getWindowParameter().isNull() )
795
failedToRestore(objectName());
799
QFont currFont = currentFont();
800
QString currName = currFont.family();
802
std::string prefName = getWindowParameter()->GetASCII(entryName(), currName.toUtf8());
804
currFont.setFamily(QString::fromStdString(prefName));
805
setCurrentFont(currFont);
808
void PrefFontBox::savePreferences()
810
if (getWindowParameter().isNull())
812
failedToSave(objectName());
816
QFont currFont = currentFont();
817
QString currName = currFont.family();
818
getWindowParameter()->SetASCII(entryName(), currName.toUtf8());
821
#include "moc_PrefWidgets.cpp"