+ implement PrefQuantitySpinBox

This commit is contained in:
wmayer 2014-12-11 14:23:32 +01:00
parent ae3755cf01
commit 5665302521
2 changed files with 257 additions and 244 deletions

View File

@ -22,8 +22,13 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QContextMenuEvent>
# include <QMenu>
#endif
#include <Base/Console.h>
#include <App/Application.h>
#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<QString> values;
std::vector<QAction *> actions;
// add the history menu part...
std::vector<QString> history = getHistory();
for (std::vector<QString>::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<QString> savedValues = getSavedValues();
for (std::vector<QString>::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<QAction *>::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<QString> hist = getHistory();
for (std::vector<QString>::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<QString> PrefQuantitySpinBox::getHistory() const
{
Q_D(const PrefQuantitySpinBox);
std::vector<QString> 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<QString> 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<QString> PrefQuantitySpinBox::getSavedValues() const
{
Q_D(const PrefQuantitySpinBox);
std::vector<QString> 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"

View File

@ -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<const char*> &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<QString> 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<QString> 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