From 5665302521a238b9ee85924778581dca7a696c0d Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 11 Dec 2014 14:23:32 +0100 Subject: [PATCH] + implement PrefQuantitySpinBox --- src/Gui/PrefWidgets.cpp | 371 ++++++++++++++++++++++------------------ src/Gui/PrefWidgets.h | 130 ++++++-------- 2 files changed, 257 insertions(+), 244 deletions(-) diff --git a/src/Gui/PrefWidgets.cpp b/src/Gui/PrefWidgets.cpp index 427db7bf6..8a6880148 100644 --- a/src/Gui/PrefWidgets.cpp +++ b/src/Gui/PrefWidgets.cpp @@ -22,8 +22,13 @@ #include "PreCompiled.h" +#ifndef _PreComp_ +# include +# include +#endif #include +#include #include "PrefWidgets.h" #include "FileDialog.h" @@ -162,26 +167,6 @@ void PrefSpinBox::savePreferences() getWindowParameter()->SetInt( entryName() , (int)value() ); } -QByteArray PrefSpinBox::entryName () const -{ - return PrefWidget::entryName(); -} - -QByteArray PrefSpinBox::paramGrpPath () const -{ - return PrefWidget::paramGrpPath(); -} - -void PrefSpinBox::setEntryName ( const QByteArray& name ) -{ - PrefWidget::setEntryName(name); -} - -void PrefSpinBox::setParamGrpPath ( const QByteArray& name ) -{ - PrefWidget::setParamGrpPath(name); -} - // -------------------------------------------------------------------- PrefDoubleSpinBox::PrefDoubleSpinBox ( QWidget * parent ) @@ -216,26 +201,6 @@ void PrefDoubleSpinBox::savePreferences() getWindowParameter()->SetFloat( entryName(), value() ); } -QByteArray PrefDoubleSpinBox::entryName () const -{ - return PrefWidget::entryName(); -} - -QByteArray PrefDoubleSpinBox::paramGrpPath () const -{ - return PrefWidget::paramGrpPath(); -} - -void PrefDoubleSpinBox::setEntryName ( const QByteArray& name ) -{ - PrefWidget::setEntryName(name); -} - -void PrefDoubleSpinBox::setParamGrpPath ( const QByteArray& name ) -{ - PrefWidget::setParamGrpPath(name); -} - // -------------------------------------------------------------------- PrefLineEdit::PrefLineEdit ( QWidget * parent ) @@ -271,26 +236,6 @@ void PrefLineEdit::savePreferences() getWindowParameter()->SetASCII(entryName(), text().toUtf8()); } -QByteArray PrefLineEdit::entryName () const -{ - return PrefWidget::entryName(); -} - -QByteArray PrefLineEdit::paramGrpPath () const -{ - return PrefWidget::paramGrpPath(); -} - -void PrefLineEdit::setEntryName ( const QByteArray& name ) -{ - PrefWidget::setEntryName(name); -} - -void PrefLineEdit::setParamGrpPath ( const QByteArray& name ) -{ - PrefWidget::setParamGrpPath(name); -} - // -------------------------------------------------------------------- PrefFileChooser::PrefFileChooser ( QWidget * parent ) @@ -325,26 +270,6 @@ void PrefFileChooser::savePreferences() getWindowParameter()->SetASCII(entryName(), fileName().toUtf8()); } -QByteArray PrefFileChooser::entryName () const -{ - return PrefWidget::entryName(); -} - -QByteArray PrefFileChooser::paramGrpPath () const -{ - return PrefWidget::paramGrpPath(); -} - -void PrefFileChooser::setEntryName ( const QByteArray& name ) -{ - PrefWidget::setEntryName(name); -} - -void PrefFileChooser::setParamGrpPath ( const QByteArray& name ) -{ - PrefWidget::setParamGrpPath(name); -} - // -------------------------------------------------------------------- PrefComboBox::PrefComboBox ( QWidget * parent ) @@ -379,26 +304,6 @@ void PrefComboBox::savePreferences() getWindowParameter()->SetInt(entryName() , currentIndex()); } -QByteArray PrefComboBox::entryName () const -{ - return PrefWidget::entryName(); -} - -QByteArray PrefComboBox::paramGrpPath () const -{ - return PrefWidget::paramGrpPath(); -} - -void PrefComboBox::setEntryName ( const QByteArray& name ) -{ - PrefWidget::setEntryName(name); -} - -void PrefComboBox::setParamGrpPath ( const QByteArray& name ) -{ - PrefWidget::setParamGrpPath(name); -} - // -------------------------------------------------------------------- PrefCheckBox::PrefCheckBox ( QWidget * parent ) @@ -433,26 +338,6 @@ void PrefCheckBox::savePreferences() getWindowParameter()->SetBool( entryName(), isChecked() ); } -QByteArray PrefCheckBox::entryName () const -{ - return PrefWidget::entryName(); -} - -QByteArray PrefCheckBox::paramGrpPath () const -{ - return PrefWidget::paramGrpPath(); -} - -void PrefCheckBox::setEntryName ( const QByteArray& name ) -{ - PrefWidget::setEntryName(name); -} - -void PrefCheckBox::setParamGrpPath ( const QByteArray& name ) -{ - PrefWidget::setParamGrpPath(name); -} - // -------------------------------------------------------------------- PrefRadioButton::PrefRadioButton ( QWidget * parent ) @@ -487,26 +372,6 @@ void PrefRadioButton::savePreferences() getWindowParameter()->SetBool( entryName() , isChecked() ); } -QByteArray PrefRadioButton::entryName () const -{ - return PrefWidget::entryName(); -} - -QByteArray PrefRadioButton::paramGrpPath () const -{ - return PrefWidget::paramGrpPath(); -} - -void PrefRadioButton::setEntryName ( const QByteArray& name ) -{ - PrefWidget::setEntryName(name); -} - -void PrefRadioButton::setParamGrpPath ( const QByteArray& name ) -{ - PrefWidget::setParamGrpPath(name); -} - // -------------------------------------------------------------------- PrefSlider::PrefSlider ( QWidget * parent ) @@ -541,26 +406,6 @@ void PrefSlider::savePreferences() getWindowParameter()->SetInt(entryName() , (int)value()); } -QByteArray PrefSlider::entryName () const -{ - return PrefWidget::entryName(); -} - -QByteArray PrefSlider::paramGrpPath () const -{ - return PrefWidget::paramGrpPath(); -} - -void PrefSlider::setEntryName ( const QByteArray& name ) -{ - PrefWidget::setEntryName(name); -} - -void PrefSlider::setParamGrpPath ( const QByteArray& name ) -{ - PrefWidget::setParamGrpPath(name); -} - // -------------------------------------------------------------------- PrefColorButton::PrefColorButton ( QWidget * parent ) @@ -607,24 +452,214 @@ void PrefColorButton::savePreferences() getWindowParameter()->SetUnsigned( entryName(), lcol ); } -QByteArray PrefColorButton::entryName () const +// -------------------------------------------------------------------- + +namespace Gui { +class PrefQuantitySpinBoxPrivate { - return PrefWidget::entryName(); +public: + PrefQuantitySpinBoxPrivate() : + historySize(5), + saveSize(5) + { + } + ~PrefQuantitySpinBoxPrivate() + { + } + + QByteArray prefGrp; + ParameterGrp::handle handle; + int historySize; + int saveSize; +}; } -QByteArray PrefColorButton::paramGrpPath () const +PrefQuantitySpinBox::PrefQuantitySpinBox (QWidget * parent) + : QuantitySpinBox(parent), d_ptr(new PrefQuantitySpinBoxPrivate()) { - return PrefWidget::paramGrpPath(); } -void PrefColorButton::setEntryName ( const QByteArray& name ) +PrefQuantitySpinBox::~PrefQuantitySpinBox() { - PrefWidget::setEntryName(name); } -void PrefColorButton::setParamGrpPath ( const QByteArray& name ) +void PrefQuantitySpinBox::contextMenuEvent(QContextMenuEvent *event) { - PrefWidget::setParamGrpPath(name); + QMenu *editMenu = lineEdit()->createStandardContextMenu(); + editMenu->setTitle(tr("Edit")); + QMenu* menu = new QMenu(QString::fromAscii("PrefQuantitySpinBox")); + + menu->addMenu(editMenu); + menu->addSeparator(); + + // datastructure to remember actions for values + std::vector values; + std::vector actions; + + // add the history menu part... + std::vector history = getHistory(); + + for (std::vector::const_iterator it = history.begin();it!= history.end();++it) { + actions.push_back(menu->addAction(*it)); + values.push_back(*it); + } + + // add the save value portion of the menu + menu->addSeparator(); + QAction *saveValueAction = menu->addAction(tr("Save value")); + std::vector savedValues = getSavedValues(); + + for (std::vector::const_iterator it = savedValues.begin();it!= savedValues.end();++it) { + actions.push_back(menu->addAction(*it)); + values.push_back(*it); + } + + // call the menu and wait until its back + QAction *saveAction = menu->exec(event->globalPos()); + + // look what the user has choosen + if (saveAction == saveValueAction) { + pushToSavedValues(); + } + else { + int i=0; + for (std::vector::const_iterator it = actions.begin();it!=actions.end();++it,i++) { + if (*it == saveAction) + lineEdit()->setText(values[i]); + } + } + + delete menu; +} + +void PrefQuantitySpinBox::pushToHistory(const QString &valueq) +{ + Q_D(PrefQuantitySpinBox); + + QString val; + if (valueq.isEmpty()) + val = this->text(); + else + val = valueq; + + // check if already in: + std::vector hist = getHistory(); + for (std::vector::const_iterator it = hist.begin();it!=hist.end();++it) { + if (*it == val) + return; + } + + std::string value(val.toUtf8()); + if (d->handle.isValid()) { + for (int i = d->historySize -1 ; i>=0 ;i--) { + QByteArray hist1 = "Hist"; + QByteArray hist0 = "Hist"; + hist1.append(QByteArray::number(i+1)); + hist0.append(QByteArray::number(i)); + std::string tHist = d->handle->GetASCII(hist0); + if (!tHist.empty()) + d->handle->SetASCII(hist1,tHist.c_str()); + } + d->handle->SetASCII("Hist0",value.c_str()); + } +} + +std::vector PrefQuantitySpinBox::getHistory() const +{ + Q_D(const PrefQuantitySpinBox); + std::vector res; + + if (d->handle.isValid()) { + std::string tmp; + for (int i = 0 ; i< d->historySize ;i++) { + QByteArray hist = "Hist"; + hist.append(QByteArray::number(i)); + tmp = d->handle->GetASCII(hist); + if (!tmp.empty()) + res.push_back(QString::fromUtf8(tmp.c_str())); + else + break; // end of history reached + } + } + return res; +} + +void PrefQuantitySpinBox::setToLastUsedValue() +{ + std::vector hist = getHistory(); + if (!hist.empty()) + lineEdit()->setText(hist[0]); +} + +void PrefQuantitySpinBox::pushToSavedValues(const QString &valueq) +{ + Q_D(PrefQuantitySpinBox); + std::string value; + if (valueq.isEmpty()) + value = this->text().toUtf8().constData(); + else + value = valueq.toUtf8().constData(); + + if (d->handle.isValid()) { + for (int i = d->saveSize -1 ; i>=0 ;i--) { + QByteArray hist1 = "Save"; + QByteArray hist0 = "Save"; + hist1.append(QByteArray::number(i+1)); + hist0.append(QByteArray::number(i)); + std::string tHist = d->handle->GetASCII(hist0); + if (!tHist.empty()) + d->handle->SetASCII(hist1,tHist.c_str()); + } + d->handle->SetASCII("Save0",value.c_str()); + } +} + +std::vector PrefQuantitySpinBox::getSavedValues() const +{ + Q_D(const PrefQuantitySpinBox); + std::vector res; + + if (d->handle.isValid()) { + std::string tmp; + for (int i = 0 ; i< d->saveSize ;i++) { + QByteArray hist = "Save"; + hist.append(QByteArray::number(i)); + tmp = d->handle->GetASCII(hist); + if (!tmp.empty()) + res.push_back(QString::fromUtf8(tmp.c_str())); + else + break; // end of history reached + } + } + return res; +} + +void PrefQuantitySpinBox::setParamGrpPath(const QByteArray& path) +{ + Q_D(PrefQuantitySpinBox); + d->handle = App::GetApplication().GetParameterGroupByPath(path); + if (d->handle.isValid()) + d->prefGrp = path; +} + +QByteArray PrefQuantitySpinBox::paramGrpPath() const +{ + Q_D(const PrefQuantitySpinBox); + if (d->handle.isValid()) + return d->prefGrp; + return QByteArray(); +} + +int PrefQuantitySpinBox::historySize() const +{ + Q_D(const PrefQuantitySpinBox); + return d->historySize; +} + +void PrefQuantitySpinBox::setHistorySize(int i) +{ + Q_D(PrefQuantitySpinBox); + d->historySize = i; } #include "moc_PrefWidgets.cpp" diff --git a/src/Gui/PrefWidgets.h b/src/Gui/PrefWidgets.h index fa32a689e..284d4e0f6 100644 --- a/src/Gui/PrefWidgets.h +++ b/src/Gui/PrefWidgets.h @@ -32,6 +32,7 @@ #include "Window.h" #include "SpinBox.h" #include "FileDialog.h" +#include "QuantitySpinBox.h" namespace Gui { class CommandManager; @@ -54,11 +55,11 @@ class WidgetFactoryInst; class GuiExport PrefWidget : public WindowParameter { public: - virtual void setEntryName( const QByteArray& name ); - virtual QByteArray entryName() const; + void setEntryName( const QByteArray& name ); + QByteArray entryName() const; - virtual void setParamGrpPath( const QByteArray& path ); - virtual QByteArray paramGrpPath() const; + void setParamGrpPath( const QByteArray& path ); + QByteArray paramGrpPath() const; virtual void OnChange(Base::Subject &rCaller, const char * sReason); void onSave(); @@ -99,14 +100,6 @@ public: PrefSpinBox ( QWidget * parent = 0 ); virtual ~PrefSpinBox(); - // PROPERTIES - // getters - QByteArray entryName () const; - QByteArray paramGrpPath () const; - // setters - void setEntryName ( const QByteArray& name ); - void setParamGrpPath ( const QByteArray& name ); - protected: // restore from/save to parameters void restorePreferences(); @@ -127,14 +120,6 @@ public: PrefDoubleSpinBox ( QWidget * parent = 0 ); virtual ~PrefDoubleSpinBox(); - // PROPERTIES - // getters - QByteArray entryName () const; - QByteArray paramGrpPath () const; - // setters - void setEntryName ( const QByteArray& name ); - void setParamGrpPath ( const QByteArray& name ); - protected: // restore from/save to parameters void restorePreferences(); @@ -156,14 +141,6 @@ public: PrefLineEdit ( QWidget * parent = 0 ); virtual ~PrefLineEdit(); - // PROPERTIES - // getters - QByteArray entryName () const; - QByteArray paramGrpPath () const; - // setters - void setEntryName ( const QByteArray& name ); - void setParamGrpPath ( const QByteArray& name ); - protected: // restore from/save to parameters void restorePreferences(); @@ -185,14 +162,6 @@ public: PrefFileChooser ( QWidget * parent = 0 ); virtual ~PrefFileChooser(); - // PROPERTIES - // getters - QByteArray entryName () const; - QByteArray paramGrpPath () const; - // setters - void setEntryName ( const QByteArray& name ); - void setParamGrpPath ( const QByteArray& name ); - protected: // restore from/save to parameters void restorePreferences(); @@ -214,14 +183,6 @@ public: PrefComboBox ( QWidget * parent = 0 ); virtual ~PrefComboBox(); - // PROPERTIES - // getters - QByteArray entryName () const; - QByteArray paramGrpPath () const; - // setters - void setEntryName ( const QByteArray& name ); - void setParamGrpPath ( const QByteArray& name ); - protected: // restore from/save to parameters void restorePreferences(); @@ -243,14 +204,6 @@ public: PrefCheckBox ( QWidget * parent = 0 ); virtual ~PrefCheckBox(); - // PROPERTIES - // getters - QByteArray entryName () const; - QByteArray paramGrpPath () const; - // setters - void setEntryName ( const QByteArray& name ); - void setParamGrpPath ( const QByteArray& name ); - protected: // restore from/save to parameters void restorePreferences(); @@ -272,14 +225,6 @@ public: PrefRadioButton ( QWidget * parent = 0 ); virtual ~PrefRadioButton(); - // PROPERTIES - // getters - QByteArray entryName () const; - QByteArray paramGrpPath () const; - // setters - void setEntryName ( const QByteArray& name ); - void setParamGrpPath ( const QByteArray& name ); - protected: // restore from/save to parameters void restorePreferences(); @@ -301,14 +246,6 @@ public: PrefSlider ( QWidget * parent = 0 ); virtual ~PrefSlider(); - // PROPERTIES - // getters - QByteArray entryName () const; - QByteArray paramGrpPath () const; - // setters - void setEntryName ( const QByteArray& name ); - void setParamGrpPath ( const QByteArray& name ); - protected: // restore from/save to parameters void restorePreferences(); @@ -330,20 +267,61 @@ public: PrefColorButton ( QWidget * parent = 0 ); virtual ~PrefColorButton(); - // PROPERTIES - // getters - QByteArray entryName () const; - QByteArray paramGrpPath () const; - // setters - void setEntryName ( const QByteArray& name ); - void setParamGrpPath ( const QByteArray& name ); - protected: // restore from/save to parameters void restorePreferences(); void savePreferences(); }; +class PrefQuantitySpinBoxPrivate; + +/** + * The PrefQuantitySpinBox class. + * \author Werner Mayer + */ +class GuiExport PrefQuantitySpinBox : public QuantitySpinBox +{ + Q_OBJECT + + Q_PROPERTY(QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath) + Q_PROPERTY(int historySize READ historySize WRITE setHistorySize) + +public: + PrefQuantitySpinBox (QWidget * parent = 0); + virtual ~PrefQuantitySpinBox(); + + /// set the input field to the last used value (works only if the setParamGrpPath() was called) + void setToLastUsedValue(); + /// get the value of the history size property + int historySize() const; + /// set the value of the history size property + void setHistorySize(int); + + /** @name history and default management */ + //@{ + /// the param group path where the widget writes and reads the default values + QByteArray paramGrpPath() const; + /// set the param group path where the widget writes and reads the default values + void setParamGrpPath(const QByteArray& name); + /// push a new value to the history, if no string given the actual text of the input field is used. + void pushToHistory(const QString &value = QString()); + /// get the history of the field, newest first + std::vector getHistory() const; + /// push a new value to the history, if no string given the actual text of the input field is used. + void pushToSavedValues(const QString &value = QString()); + /// get the history of the field, newest first + std::vector getSavedValues() const; + //@} + +protected: + virtual void contextMenuEvent(QContextMenuEvent * event); + +private: + PrefQuantitySpinBoxPrivate * const d_ptr; + Q_DISABLE_COPY(PrefQuantitySpinBox) + Q_DECLARE_PRIVATE(PrefQuantitySpinBox) +}; + } // namespace Gui #endif // GUI_PREFWIDGETS_H