diff options
author | Ken Moore <ken@ixsystems.com> | 2016-11-01 08:22:38 -0400 |
---|---|---|
committer | Ken Moore <ken@ixsystems.com> | 2016-11-01 08:22:38 -0400 |
commit | efbf100822f8a50abdcfb061b7b1bf1be06b868c (patch) | |
tree | 29d5767ed1a773d138ac2be42515df8afe01bfc7 /src-qt5/desktop-utils/lumina-calculator | |
parent | Merge branch 'master' of github.com:trueos/lumina (diff) | |
download | lumina-efbf100822f8a50abdcfb061b7b1bf1be06b868c.tar.gz lumina-efbf100822f8a50abdcfb061b7b1bf1be06b868c.tar.bz2 lumina-efbf100822f8a50abdcfb061b7b1bf1be06b868c.zip |
Another update to the calculator:
1) Add buttons to clear/save the history (saves as plaintext to a file)
2) Tag each result with a number (#1), and allow the usage of #<number> to recall a history item
3) When recalling history, see if the result looks like it was rounded using the double->text routine for display. If so, substitute the equation, otherwise just use the result (tries to ensure no rounding error-continuity by using previous results).
Diffstat (limited to 'src-qt5/desktop-utils/lumina-calculator')
4 files changed, 102 insertions, 17 deletions
diff --git a/src-qt5/desktop-utils/lumina-calculator/EqValidator.h b/src-qt5/desktop-utils/lumina-calculator/EqValidator.h index 8218efdf..ab9bcc22 100644 --- a/src-qt5/desktop-utils/lumina-calculator/EqValidator.h +++ b/src-qt5/desktop-utils/lumina-calculator/EqValidator.h @@ -7,10 +7,10 @@ #ifndef _LUMINA_CALCULATOR_VALIDATOR_H #define _LUMINA_CALCULATOR_VALIDATOR_H -#define VALIDCHARS QString("x*+-/^%eE().0123456789") +#define VALIDCHARS QString("x*+-/^%eE().0123456789#") #define NOSTARTCHARS QString("x*/^%)eE.") -#define NOENDCHARS QString("x*/^(eE.") -#define NOCHANGE QString("().") +#define NOENDCHARS QString("x*/^(eE.#") +#define NOCHANGE QString("().#") #include <QValidator> #include <QString> diff --git a/src-qt5/desktop-utils/lumina-calculator/mainUI.cpp b/src-qt5/desktop-utils/lumina-calculator/mainUI.cpp index b26acb73..2f38abae 100644 --- a/src-qt5/desktop-utils/lumina-calculator/mainUI.cpp +++ b/src-qt5/desktop-utils/lumina-calculator/mainUI.cpp @@ -9,7 +9,9 @@ #include <QDebug> #include <QClipboard> +#include <QFileDialog> +#include <LuminaUtils.h> #include <LuminaXDG.h> #include "EqValidator.h" @@ -41,6 +43,8 @@ mainUI::mainUI() : QMainWindow(), ui(new Ui::mainUI()){ connect(ui->button_Percent, SIGNAL(clicked()), this, SLOT(captureButtonPercent()) ); connect(ui->button_Equal, SIGNAL (clicked()), this, SLOT (start_calc())); connect(ui->list_results, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(insert_history(QListWidgetItem*)) ); + connect(ui->tool_history_clear, SIGNAL(clicked()), ui->list_results, SLOT(clear()) ); + connect(ui->tool_history_save, SIGNAL(clicked()), this, SLOT(saveHistory()) ); //connect(ui->list_results, SIGNAL(itemRightClicked(QListWidgetItem*)), this, SLOT(copt_to_clipboard(QListWidgetItem*)) ); this->setWindowTitle(tr("Calculator")); updateIcons(); @@ -54,6 +58,8 @@ mainUI::~mainUI(){ void mainUI::updateIcons(){ this->setWindowIcon( LXDG::findIcon("accessories-calculator","") ); ui->tool_clear->setIcon( LXDG::findIcon("edit-clear-locationbar-rtl","dialog-cancel") ); + ui->tool_history_clear->setIcon( LXDG::findIcon("document-close","edit-clear-list") ); + ui->tool_history_save->setIcon( LXDG::findIcon("document-save-as","edit-copy") ); } void mainUI::start_calc(){ @@ -62,8 +68,8 @@ void mainUI::start_calc(){ eq.replace("%","/(100)"); double result = strToNumber(eq); if(result!=result){ return; } //bad calculation - NaN's values are special in that they don't equal itself - QString res = "%1 \t= [ %2 ]"; - ui->list_results->addItem(res.arg(QString::number(result), ui->line_eq->text())); + QString res = "[#%1] %2 \t= [ %3 ]"; + ui->list_results->addItem(res.arg(QString::number(ui->list_results->count()+1), QString::number(result), ui->line_eq->text())); ui->list_results->scrollToItem( ui->list_results->item( ui->list_results->count()-1) ); ui->line_eq->clear(); } @@ -92,7 +98,7 @@ void mainUI::captureButtonDecimal(){ ui->line_eq->insert(ui->button_Decimal->tex void mainUI::captureButtonPercent(){ ui->line_eq->insert(ui->button_Percent->text()); } void mainUI::insert_history(QListWidgetItem *it){ - QString txt = it->text().section("[",1,-1).section("]",0,0).simplified(); + QString txt = it->text().section("[",-1).section("]",0,0).simplified(); ui->line_eq->insert("("+txt+")"); } @@ -104,12 +110,20 @@ void mainUI::copy_to_clipboard(QListWidgetItem *it){ void mainUI::checkInput(const QString &str){ if(str.length()==1 && ui->list_results->count()>0){ if(OPS.contains(str)){ - QString lastresult = ui->list_results->item( ui->list_results->count()-1)->text().section("=",0,0).simplified(); + QString lastresult = ui->list_results->item( ui->list_results->count()-1)->text().section("]",0,0).section("[",-1).simplified(); ui->line_eq->setText( lastresult+str); } } } +void mainUI::saveHistory(){ + QStringList history; + for(int i=0; i<ui->list_results->count(); i++){ history << ui->list_results->item(i)->text(); } + QString file = QFileDialog::getSaveFileName(this, tr("Save Calculator History"), QDir::homePath() ); + if(file.section(".",-1).isEmpty()){ file.append(".txt"); } + LUtils::writeFile(file, history, true); +} + // ===================== // PRIVATE FUNCTIONS // ===================== @@ -126,6 +140,23 @@ double mainUI::performOperation(double LHS, double RHS, QChar symbol){ } double mainUI::strToNumber(QString str){ + //Look for history replacements first + while(str.contains("#")){ + int index = str.indexOf("#"); + int num = -1; //history number + for(int i=index+1; i<str.length(); i++){ + if(!str[i].isNumber() || i==(str.length()-1) ){ + if(!str[i].isNumber()){ i--; } //go back to the last valid char + //qDebug() << "Replace History:" << str << index << i << str.mid(index+1, i-index); + num = str.mid(index+1, i-index).toInt(); + //qDebug() << " H number:" << num; + str.replace(index, i-index+1, getHistory(num)); + //qDebug() << " After Replace:" << str; + break; //finished with this history item + } + } + if(num<1){ return BADVALUE; } //could not perform substitution + } //Look for perentheses first //qDebug() << "String to Number: " << str; if(str.indexOf("(")>=0){ @@ -177,3 +208,19 @@ double mainUI::strToNumber(QString str){ //qDebug() << " - Found Number:" << str << str.toDouble(); return str.toDouble(); } + +QString mainUI::getHistory(int number){ + if(number <1 || number > ui->list_results->count()){ + //use the last history item + number = ui->list_results->count(); + } + QString ans = ui->list_results->item(number-1)->text().section("=",0,0).section("]",-1).simplified(); + QString eq = ui->list_results->item(number-1)->text().section("[",-1).section("]",0,0).simplified(); + //See if the text answer is accurate enough (does not look rounded) + if(ans.length()<7){ + return ("("+ans+")"); //short enough answer that it was probably not rounded + }else{ + //need to re-calculate the last equation instead for exact result + return ("("+eq+")"); + } +} diff --git a/src-qt5/desktop-utils/lumina-calculator/mainUI.h b/src-qt5/desktop-utils/lumina-calculator/mainUI.h index cf09801e..9dc28f3c 100644 --- a/src-qt5/desktop-utils/lumina-calculator/mainUI.h +++ b/src-qt5/desktop-utils/lumina-calculator/mainUI.h @@ -50,9 +50,12 @@ private slots: void copy_to_clipboard(QListWidgetItem *it); void checkInput(const QString&); + void saveHistory(); + private: Ui::mainUI *ui; double performOperation(double LHS, double RHS, QChar symbol); double strToNumber(QString str); //this is highly-recursive + QString getHistory(int number = -1); }; #endif diff --git a/src-qt5/desktop-utils/lumina-calculator/mainUI.ui b/src-qt5/desktop-utils/lumina-calculator/mainUI.ui index 7a0ccfed..4ff5c421 100644 --- a/src-qt5/desktop-utils/lumina-calculator/mainUI.ui +++ b/src-qt5/desktop-utils/lumina-calculator/mainUI.ui @@ -6,8 +6,8 @@ <rect> <x>0</x> <y>0</y> - <width>171</width> - <height>258</height> + <width>204</width> + <height>260</height> </rect> </property> <property name="windowTitle"> @@ -50,14 +50,49 @@ <widget class="QWidget" name="layoutWidget"> <layout class="QVBoxLayout" name="verticalLayout"> <item> - <widget class="QListWidget" name="list_results"> - <property name="focusPolicy"> - <enum>Qt::NoFocus</enum> - </property> - <property name="selectionMode"> - <enum>QAbstractItemView::NoSelection</enum> - </property> - </widget> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <item> + <widget class="QListWidget" name="list_results"> + <property name="focusPolicy"> + <enum>Qt::NoFocus</enum> + </property> + <property name="selectionMode"> + <enum>QAbstractItemView::NoSelection</enum> + </property> + </widget> + </item> + <item> + <layout class="QVBoxLayout" name="verticalLayout_3"> + <item> + <widget class="QToolButton" name="tool_history_clear"> + <property name="text"> + <string notr="true"/> + </property> + </widget> + </item> + <item> + <widget class="QToolButton" name="tool_history_save"> + <property name="text"> + <string notr="true"/> + </property> + </widget> + </item> + <item> + <spacer name="verticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + </layout> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout"> |