From 65e95f0e65b0673a229faeffea5d336626e3af83 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Thu, 20 May 2021 16:04:59 +0200 Subject: [PATCH] Clean up calcwidget --- src/calcwidget.cpp | 100 +++++++++++++-------------------------------- src/calcwidget.h | 26 +++++------- src/calcwidget.ui | 67 +----------------------------- src/calcwindow.cpp | 11 ----- src/calcwindow.h | 4 -- 5 files changed, 39 insertions(+), 169 deletions(-) diff --git a/src/calcwidget.cpp b/src/calcwidget.cpp index b4ce187..2190c40 100644 --- a/src/calcwidget.cpp +++ b/src/calcwidget.cpp @@ -29,92 +29,48 @@ CalcWidget::CalcWidget(QWidget *parent) ui->lineFrom->setValidator(dv); ui->lineTo->setValidator(dv); - connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &CalcWidget::initFiat); - connect(&appData()->prices, &Prices::cryptoPricesUpdated, this, &CalcWidget::initCrypto);; + connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &CalcWidget::initComboBox); + connect(&appData()->prices, &Prices::cryptoPricesUpdated, this, &CalcWidget::initComboBox); + + connect(ui->lineFrom, &QLineEdit::textEdited, this, [this]{this->convert(false);}); + connect(ui->lineTo, &QLineEdit::textEdited, this, [this]{this->convert(true);}); + + connect(ui->comboCalcFrom, QOverload::of(&QComboBox::currentIndexChanged), [this]{this->convert(false);}); + connect(ui->comboCalcTo, QOverload::of(&QComboBox::currentIndexChanged), [this]{this->convert(true);}); } -void CalcWidget::fromChanged(const QString &data) { - if(!this->m_comboBoxInit) return; - if(this->m_changing){ - this->m_changing = false; +void CalcWidget::convert(bool reverse) { + if (!m_comboBoxInit) return; + + auto lineFrom = reverse ? ui->lineTo : ui->lineFrom; + auto lineTo = reverse ? ui->lineFrom : ui->lineTo; + + auto comboFrom = reverse ? ui->comboCalcTo : ui->comboCalcFrom; + auto comboTo = reverse ? ui->comboCalcFrom : ui->comboCalcTo; + + QString symbolFrom = comboFrom->itemText(comboFrom->currentIndex()); + QString symbolTo = comboTo->itemText(comboTo->currentIndex()); + + if (symbolFrom == symbolTo) { + lineTo->setText(lineFrom->text()); } - QString symbolFrom = ui->comboCalcFrom->itemText(ui->comboCalcFrom->currentIndex()); - QString symbolTo = ui->comboCalcTo->itemText(ui->comboCalcTo->currentIndex()); - - if(symbolFrom == symbolTo){ - ui->lineTo->setText(data); - return; - } - - QString amount_str = ui->lineFrom->text(); - if(amount_str.startsWith('.')){ - ui->lineFrom->setText(ui->lineTo->text()); - return; - } - - double amount = amount_str.toDouble(); + QString amountStr = lineFrom->text(); + double amount = amountStr.toDouble(); double result = appData()->prices.convert(symbolFrom, symbolTo, amount); - this->m_changing = true; - int precision = 10; if (appData()->prices.rates.contains(symbolTo)) precision = 2; - ui->lineTo->setText(QString::number(result, 'f', precision)); -} - -void CalcWidget::toChanged(const QString &data) { - if(!this->m_comboBoxInit) return; - if(this->m_changing){ - this->m_changing = false; - return; - } - - QString symbolFrom = ui->comboCalcFrom->itemText( - ui->comboCalcFrom->currentIndex()); - QString symbolTo = ui->comboCalcTo->itemText( - ui->comboCalcTo->currentIndex()); - - if(symbolFrom == symbolTo){ - ui->lineTo->setText(ui->lineFrom->text()); - return; - } - - QString amount_str = ui->lineTo->text(); - if(amount_str.startsWith('.')){ - ui->lineTo->setText(""); - return; - } - - double amount = amount_str.toDouble(); - double result = appData()->prices.convert(symbolTo, symbolFrom, amount); - - this->m_changing = true; - - int precision = 10; - if(appData()->prices.rates.contains(symbolFrom)) - precision = 2; - - ui->lineFrom->setText(QString::number(result, 'f', precision)); -} - -void CalcWidget::toComboChanged(const QString &data) { - this->fromChanged(data); -} - -void CalcWidget::initCrypto() { - this->initComboBox(); -} - -void CalcWidget::initFiat() { - this->initComboBox(); + lineTo->setText(QString::number(result, 'f', precision)); } void CalcWidget::initComboBox() { - if(m_comboBoxInit) return; + if (m_comboBoxInit) + return; + QList marketsKeys = appData()->prices.markets.keys(); QList ratesKeys = appData()->prices.rates.keys(); if(marketsKeys.count() <= 0 || ratesKeys.count() <= 0) return; diff --git a/src/calcwidget.h b/src/calcwidget.h index 9ad2134..de95604 100644 --- a/src/calcwidget.h +++ b/src/calcwidget.h @@ -1,8 +1,8 @@ // SPDX-License-Identifier: BSD-3-Clause // Copyright (c) 2020-2021, The Monero Project. -#ifndef CALC_H -#define CALC_H +#ifndef FEATHER_CALCWIDGET_H +#define FEATHER_CALCWIDGET_H #include @@ -18,23 +18,17 @@ public: explicit CalcWidget(QWidget *parent = nullptr); ~CalcWidget() override; -signals: - void closed(); - public slots: - void fromChanged(const QString& data); - void toChanged(const QString& data); - void toComboChanged(const QString& data); - void initFiat(); - void initCrypto(); void skinChanged(); -private: - Ui::CalcWidget *ui; - - bool m_comboBoxInit = false; +private slots: void initComboBox(); - bool m_changing = false; + +private: + void convert(bool reverse); + + Ui::CalcWidget *ui; + bool m_comboBoxInit = false; }; -#endif // CALC_H +#endif // FEATHER_CALCWIDGET_H diff --git a/src/calcwidget.ui b/src/calcwidget.ui index dbd49bb..66360ae 100644 --- a/src/calcwidget.ui +++ b/src/calcwidget.ui @@ -167,72 +167,7 @@ - - - lineFrom - textChanged(QString) - CalcWidget - fromChanged(QString) - - - 119 - 77 - - - 427 - -1 - - - - - lineTo - textChanged(QString) - CalcWidget - toChanged(QString) - - - 463 - 86 - - - 640 - -4 - - - - - comboCalcFrom - currentIndexChanged(QString) - CalcWidget - fromChanged(QString) - - - 196 - 77 - - - 330 - -9 - - - - - comboCalcTo - currentIndexChanged(QString) - CalcWidget - toComboChanged(QString) - - - 574 - 82 - - - 265 - -5 - - - - + fromChanged(QString) toChanged(QString) diff --git a/src/calcwindow.cpp b/src/calcwindow.cpp index 3a363e7..1b72fc3 100644 --- a/src/calcwindow.cpp +++ b/src/calcwindow.cpp @@ -17,17 +17,6 @@ CalcWindow::CalcWindow(QWidget *parent) ui->setupUi(this); this->setWindowIcon(icons()->icon("gnome-calc.png")); - - connect(&appData()->prices, &Prices::fiatPricesUpdated, this, &CalcWindow::initFiat); - connect(&appData()->prices, &Prices::cryptoPricesUpdated, this, &CalcWindow::initCrypto); -} - -void CalcWindow::initFiat() { - this->ui->calcWidget->initFiat(); -} - -void CalcWindow::initCrypto() { - this->ui->calcWidget->initCrypto(); } void CalcWindow::closeEvent(QCloseEvent *foo) { diff --git a/src/calcwindow.h b/src/calcwindow.h index 8d6b6ed..886a9fb 100644 --- a/src/calcwindow.h +++ b/src/calcwindow.h @@ -21,10 +21,6 @@ public: signals: void closed(); -public slots: - void initFiat(); - void initCrypto(); - private: void closeEvent(QCloseEvent *bar) override;