From efbf100822f8a50abdcfb061b7b1bf1be06b868c Mon Sep 17 00:00:00 2001 From: Ken Moore Date: Tue, 1 Nov 2016 08:22:38 -0400 Subject: 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 # 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). --- src-qt5/desktop-utils/lumina-calculator/mainUI.cpp | 55 ++++++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) (limited to 'src-qt5/desktop-utils/lumina-calculator/mainUI.cpp') 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 #include +#include +#include #include #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; ilist_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=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+")"); + } +} -- cgit