From 2351d115b9903ae69b2e3562f98e5a2474d63f69 Mon Sep 17 00:00:00 2001 From: jriegel Date: Tue, 3 Dec 2013 18:47:13 +0100 Subject: [PATCH] History and Save managment in InputField --- src/Gui/DlgUnitsCalculatorImp.cpp | 2 + src/Gui/InputField.cpp | 129 +++++++++++++++++++++++++++--- src/Gui/InputField.h | 15 +++- 3 files changed, 131 insertions(+), 15 deletions(-) diff --git a/src/Gui/DlgUnitsCalculatorImp.cpp b/src/Gui/DlgUnitsCalculatorImp.cpp index 7cd7389caade..2d3635ef5e7c 100644 --- a/src/Gui/DlgUnitsCalculatorImp.cpp +++ b/src/Gui/DlgUnitsCalculatorImp.cpp @@ -59,6 +59,7 @@ DlgUnitsCalculator::DlgUnitsCalculator( QWidget* parent, Qt::WFlags fl ) connect(this->ValueInput, SIGNAL(parseError(QString)), this, SLOT(parseError(QString))); connect(this->UnitInput, SIGNAL(parseError(QString)), this, SLOT(parseError(QString))); + this->ValueInput->setParamGrpPath(QByteArray("User parameter:BaseApp/History/UnitsCalculator")); actUnit.setInvalid(); } @@ -136,6 +137,7 @@ void DlgUnitsCalculator::help(void) void DlgUnitsCalculator::returnPressed(void) { this->textEdit->append(this->ValueInput->text() + QString::fromAscii(" = ") + this->ValueOutput->text()); + this->ValueInput->pushToHistory(); } diff --git a/src/Gui/InputField.cpp b/src/Gui/InputField.cpp index 588ce4820ba0..3792aadb39d2 100644 --- a/src/Gui/InputField.cpp +++ b/src/Gui/InputField.cpp @@ -40,7 +40,12 @@ using namespace Base; // -------------------------------------------------------------------- InputField::InputField ( QWidget * parent ) - : QLineEdit(parent), StepSize(1.0), Maximum(DOUBLE_MAX),Minimum(-DOUBLE_MAX) + : QLineEdit(parent), + StepSize(1.0), + Maximum(DOUBLE_MAX), + Minimum(-DOUBLE_MAX), + HistorySize(5), + SaveSize(5) { this->setContextMenuPolicy(Qt::DefaultContextMenu); @@ -54,10 +59,48 @@ InputField::~InputField() void InputField::contextMenuEvent(QContextMenuEvent *event) { - QMenu *menu = createStandardContextMenu(); - QAction *saveAction = menu->addAction(tr("My Menu Item")); - //... - QAction *saveAction2 = menu->exec(event->globalPos()); + QMenu *editMenu = createStandardContextMenu(); + editMenu->setTitle(tr("Edit")); + QMenu* menu = new QMenu(QString::fromAscii("InputFieldContextmenu")); + + 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(QString::fromAscii(it->c_str()))); + 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(QString::fromAscii(it->c_str()))); + 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) + this->setText(QString::fromAscii(values[i].c_str())); + } + delete menu; } @@ -87,8 +130,18 @@ void InputField::newInput(const QString & text) void InputField::pushToHistory(std::string value) { + if(value == "") + value = this->text().toAscii(); if(_handle.isValid()){ - _handle->SetASCII("Hist1",_handle->GetASCII("Hist0","").c_str()); + char hist1[21]; + char hist0[21]; + for(int i = HistorySize -1 ; i>=0 ;i--){ + snprintf(hist1,20,"Hist%i",i+1); + snprintf(hist0,20,"Hist%i",i); + std::string tHist = _handle->GetASCII(hist0,""); + if(tHist != "") + _handle->SetASCII(hist1,tHist.c_str()); + } _handle->SetASCII("Hist0",value.c_str()); } } @@ -99,14 +152,51 @@ std::vector InputField::getHistory(void) if(_handle.isValid()){ std::string tmp; - tmp = _handle->GetASCII("Hist0",""); - if( tmp != ""){ - res.push_back(tmp); - tmp = _handle->GetASCII("Hist1",""); - if( tmp != ""){ + char hist[21]; + for(int i = 0 ; i< HistorySize ;i++){ + snprintf(hist,20,"Hist%i",i); + tmp = _handle->GetASCII(hist,""); + if( tmp != "") + res.push_back(tmp); + else + break; // end of history reached + } + } + return res; +} + +void InputField::pushToSavedValues(std::string value) +{ + if(value == "") + value = this->text().toAscii(); + if(_handle.isValid()){ + char hist1[21]; + char hist0[21]; + for(int i = SaveSize -1 ; i>=0 ;i--){ + snprintf(hist1,20,"Save%i",i+1); + snprintf(hist0,20,"Save%i",i); + std::string tHist = _handle->GetASCII(hist0,""); + if(tHist != "") + _handle->SetASCII(hist1,tHist.c_str()); + } + _handle->SetASCII("Save0",value.c_str()); + } +} + +std::vector InputField::getSavedValues(void) +{ + std::vector res; + + if(_handle.isValid()){ + std::string tmp; + char hist[21]; + for(int i = 0 ; i< SaveSize ;i++){ + snprintf(hist,20,"Save%i",i); + tmp = _handle->GetASCII(hist,""); + if( tmp != "") res.push_back(tmp); - //tmp = _handle->GetASCII("Hist2",""); - } + else + break; // end of history reached } } return res; @@ -182,6 +272,19 @@ void InputField::setMinimum(double m) Minimum = m; } +// get the value of the minimum property +int InputField::historySize(void)const +{ + return HistorySize; +} +// set the value of the minimum property +void InputField::setHistorySize(int i) +{ + assert(i>=0); + assert(i<100); + + HistorySize = i; +} // -------------------------------------------------------------------- diff --git a/src/Gui/InputField.h b/src/Gui/InputField.h index 8ac314a6eb8c..7f2290cb3fd8 100644 --- a/src/Gui/InputField.h +++ b/src/Gui/InputField.h @@ -51,6 +51,7 @@ class GuiExport InputField : public QLineEdit Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep ) Q_PROPERTY(double maximum READ maximum WRITE setMaximum ) Q_PROPERTY(double minimum READ minimum WRITE setMinimum ) + Q_PROPERTY(int historySize READ historySize WRITE setHistorySize ) public: @@ -79,6 +80,10 @@ class GuiExport InputField : public QLineEdit double minimum(void)const; /// set the value of the minimum property void setMinimum(double); + /// get the value of the minimum property + int historySize(void)const; + /// set the value of the minimum property + void setHistorySize(int); /** @name history and default management */ //@{ @@ -86,10 +91,14 @@ class GuiExport InputField : public QLineEdit QByteArray paramGrpPath () const; /// set the param group path where the widget write and read the dafault values void setParamGrpPath ( const QByteArray& name ); - /// push a new value to the history - void pushToHistory(std::string value); + /// push a new value to the history, if no string given the actual text of the input field is used. + void pushToHistory(std::string value = std::string()); /// get the history of the field, newest first std::vector getHistory(void); + /// push a new value to the history, if no string given the actual text of the input field is used. + void pushToSavedValues(std::string value = std::string()); + /// get the history of the field, newest first + std::vector getSavedValues(void); //@} @@ -132,6 +141,8 @@ protected Q_SLOTS: double Maximum; double Minimum; double StepSize; + int HistorySize; + int SaveSize; };