+ implement PrefQuantitySpinBox

This commit is contained in:
wmayer
2014-12-11 14:23:32 +01:00
parent ebcda68b20
commit 7a6780b86c
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"