Clean up calcwidget

This commit is contained in:
tobtoht 2021-05-20 16:04:59 +02:00
parent 1e23b0679e
commit 65e95f0e65
No known key found for this signature in database
GPG key ID: 1CADD27F41F45C3C
5 changed files with 39 additions and 169 deletions

View file

@ -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<int>::of(&QComboBox::currentIndexChanged), [this]{this->convert(false);});
connect(ui->comboCalcTo, QOverload<int>::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;
}
QString symbolFrom = ui->comboCalcFrom->itemText(ui->comboCalcFrom->currentIndex());
QString symbolTo = ui->comboCalcTo->itemText(ui->comboCalcTo->currentIndex());
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) {
ui->lineTo->setText(data);
return;
lineTo->setText(lineFrom->text());
}
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<QString> marketsKeys = appData()->prices.markets.keys();
QList<QString> ratesKeys = appData()->prices.rates.keys();
if(marketsKeys.count() <= 0 || ratesKeys.count() <= 0) return;

View file

@ -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 <QWidget>
@ -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

View file

@ -167,72 +167,7 @@
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>lineFrom</sender>
<signal>textChanged(QString)</signal>
<receiver>CalcWidget</receiver>
<slot>fromChanged(QString)</slot>
<hints>
<hint type="sourcelabel">
<x>119</x>
<y>77</y>
</hint>
<hint type="destinationlabel">
<x>427</x>
<y>-1</y>
</hint>
</hints>
</connection>
<connection>
<sender>lineTo</sender>
<signal>textChanged(QString)</signal>
<receiver>CalcWidget</receiver>
<slot>toChanged(QString)</slot>
<hints>
<hint type="sourcelabel">
<x>463</x>
<y>86</y>
</hint>
<hint type="destinationlabel">
<x>640</x>
<y>-4</y>
</hint>
</hints>
</connection>
<connection>
<sender>comboCalcFrom</sender>
<signal>currentIndexChanged(QString)</signal>
<receiver>CalcWidget</receiver>
<slot>fromChanged(QString)</slot>
<hints>
<hint type="sourcelabel">
<x>196</x>
<y>77</y>
</hint>
<hint type="destinationlabel">
<x>330</x>
<y>-9</y>
</hint>
</hints>
</connection>
<connection>
<sender>comboCalcTo</sender>
<signal>currentIndexChanged(QString)</signal>
<receiver>CalcWidget</receiver>
<slot>toComboChanged(QString)</slot>
<hints>
<hint type="sourcelabel">
<x>574</x>
<y>82</y>
</hint>
<hint type="destinationlabel">
<x>265</x>
<y>-5</y>
</hint>
</hints>
</connection>
</connections>
<connections/>
<slots>
<slot>fromChanged(QString)</slot>
<slot>toChanged(QString)</slot>

View file

@ -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) {

View file

@ -21,10 +21,6 @@ public:
signals:
void closed();
public slots:
void initFiat();
void initCrypto();
private:
void closeEvent(QCloseEvent *bar) override;