refactoring qad widgets part 1

c++ cast, nullptr, forward declaration, agregate ui, connect to member functions, order and clear includes
This commit is contained in:
2022-12-11 16:27:04 +03:00
parent 5d9381dd37
commit 728c132f2b
22 changed files with 561 additions and 404 deletions

View File

@@ -1,18 +1,19 @@
#include "evalspinbox.h"
#include "qad_types.h"
#include "qpievaluator_p.h"
#include <QLineEdit>
#include <QLabel>
#include <QDebug>
#include <QPainter>
#include <QTimer>
#include <QStyle>
#include <QStyleOptionSpinBox>
#include <QMouseEvent>
#if QT_VERSION_MAJOR <= 5
# include <QRegExp>
#else
# include <QRegularExpression>
#endif
#include <QPainter>
#include <QTimer>
#include <QStyle>
#include <QStyleOptionSpinBox>
#include "qad_types.h"
#include "qpievaluator_p.h"
EvalSpinBox::EvalSpinBox(QWidget * parent): QAbstractSpinBox(parent) {
@@ -21,7 +22,6 @@ EvalSpinBox::EvalSpinBox(QWidget * parent): QAbstractSpinBox(parent) {
label = new QLabel(lineEdit());
eval = new QPIEvaluator();
precision_ = -1;
// label->hide();
clear_im.load(":/icons/edit-clear-locationbar-rtl.png");
icon_ok.load(":/icons/dialog-ok-apply.png");
icon_fail.load(":/icons/dialog-warning.png");
@@ -35,14 +35,11 @@ EvalSpinBox::EvalSpinBox(QWidget * parent): QAbstractSpinBox(parent) {
cw->hide();
cw_visible = false;
calc_visible = true;
//lineEdit()->setStyleSheet("color: darkgreen;");
//lineEdit()->setText(eval.expression() + " -> " + QString::number(value(), 'G', 10));
cw->installEventFilter(this);
status->installEventFilter(this);
connect(lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(textChanged_(QString)));
connect(this, SIGNAL(editingFinished()), this, SLOT(setExpression_()));
connect(this, SIGNAL(editingFinished()), this, SLOT(setExpressionSlot()));
label->setText("0");
//connect(cw, SIGNAL(mouseReleaseEvent(QMouseEvent * )), this, SLOT(clearMouseRelease(QMouseEvent * )));
}
@@ -57,11 +54,17 @@ EvalSpinBox::~EvalSpinBox() {
bool EvalSpinBox::eventFilter(QObject * o, QEvent * e) {
switch (e->type()) {
case QEvent::MouseButtonRelease:
if (o == cw) clearMouseRelease((QMouseEvent * )e);
if (o == cw) {
clearMouseRelease(static_cast<QMouseEvent *>(e));
}
break;
case QEvent::Paint:
if (o == status) statusPaintEvent();
if (o == cw) cwPaintEvent();
if (o == status) {
statusPaintEvent();
}
if (o == cw) {
cwPaintEvent();
}
break;
default : break;
}
@@ -141,8 +144,9 @@ void EvalSpinBox::textChanged_(const QString & text) {
icon = icon_ok;
status->setToolTip("OK -> "+QString::number(value(), 'G', 10));
}
// qDebug() << "value =" << value();
if (pv != value()) emit valueChanged(value());
if (pv != value()) {
emit valueChanged(value());
}
} else {
icon = icon_fail;
status->setToolTip(eval->error());
@@ -151,7 +155,7 @@ void EvalSpinBox::textChanged_(const QString & text) {
}
void EvalSpinBox::setExpression_() {
void EvalSpinBox::setExpressionSlot() {
bool td = false;
double pv = value();
QString t = text();
@@ -160,8 +164,7 @@ void EvalSpinBox::setExpression_() {
t.chop(1);
}
if (eval->check(t)) {
/*if (eval.expression() == "0") lineEdit()->clear();
else*/ lineEdit()->setText(eval->expression());
lineEdit()->setText(eval->expression());
eval->evaluate();
if (td) lineEdit()->setText(QString::number(value(), 'G', precision_ > 0 ? precision_ : 16));
status->setToolTip("OK -> " + QString::number(value(), 'G', 10));
@@ -169,13 +172,8 @@ void EvalSpinBox::setExpression_() {
} else {
icon = icon_fail;
status->setToolTip(eval->error());
// qDebug() << eval.expression();
}
if (!label->isHidden()) {
// if (eval.expression() != QString::number(value(), 'G', 10) && eval.expression() != QString::number(value(), 'G', 11) && eval.isCorrect())
// label->setText("<html><head/><body><p>" + eval.expression() + " <span style=\"color:#005500;\">-&gt; " + QString::number(value(), 'G', 10) + "</span></p></body></html>");
// else
// label->setText(eval.expression());
if (eval->expression() != QString::number(value(), 'G', 10) && eval->expression() != QString::number(value(), 'G', 11) && eval->isCorrect())
label->setText("<html><head/><body><p><span style=\"color:#005500;\">-&gt; " + QString::number(value(), 'G', 10) + "</span></p></body></html>");
else
@@ -188,29 +186,24 @@ void EvalSpinBox::setExpression_() {
lineEdit()->setStyleSheet("");
status->hide();
}
// lineEdit()->setText(eval.expression() + " -> " + QString::number(value(), 'G', 10));
//lineEdit()->setText("");
lineEdit()->blockSignals(false);
}
// qDebug() << "value =" << value();
if (pv != value()) emit valueChanged(value());
}
void EvalSpinBox::setExpression(const QString & expr) {
lineEdit()->setText(expr);
//if (eval.expression() == "0") lineEdit()->clear();
cw->setVisible(text() != dt && cw_visible);
setExpression_();
setExpressionSlot();
}
void EvalSpinBox::setValue(double val) {
lineEdit()->setText(QString::number(val, 'G', precision_ > 0 ? precision_ : 16));
//if (val == 0) lineEdit()->clear();
cw->setVisible(text() != dt && cw_visible);
setExpression_();
setExpressionSlot();
}
@@ -221,7 +214,7 @@ void EvalSpinBox::stepBy(int steps) {
void EvalSpinBox::clear() {
lineEdit()->setText(dt);
setExpression_();
setExpressionSlot();
cw->hide();
resizeIcons();
emit cleared();
@@ -259,7 +252,6 @@ QAbstractSpinBox::StepEnabled EvalSpinBox::stepEnabled() const {
void EvalSpinBox::focusInEvent(QFocusEvent * event) {
// qDebug() << "focus_in";
label->hide();
status->show();
lineEdit()->blockSignals(true);
@@ -273,29 +265,24 @@ void EvalSpinBox::focusInEvent(QFocusEvent * event) {
void EvalSpinBox::focusOutEvent(QFocusEvent * event) {
QAbstractSpinBox::focusOutEvent(event);
// qDebug() << eval.expression() << QString::number(value(), 'G', 10);
// if (eval.expression() != QString::number(value(), 'G', 10) && eval.expression() != QString::number(value(), 'G', 11) && eval.isCorrect())
// label->setText("<html><head/><body><p>" + eval.expression() + " <span style=\"color:#005500;\">-&gt; " + QString::number(value(), 'G', 10) + "</span></p></body></html>");
// else
// label->setText(eval.expression());
if (eval->expression() != QString::number(value(), 'G', 10) && eval->expression() != QString::number(value(), 'G', 11) && eval->isCorrect())
if (eval->expression() != QString::number(value(), 'G', 10) && eval->expression() != QString::number(value(), 'G', 11) && eval->isCorrect()) {
label->setText("<html><head/><body><p><span style=\"color:#005500;\">-&gt; " + QString::number(value(), 'G', 10) + "</span></p></body></html>");
else
} else {
label->setText("");
if (calc_visible)
}
if (calc_visible) {
label->show();
}
lineEdit()->blockSignals(true);
if (!eval->isCorrect()) lineEdit()->setStyleSheet("color: darkred;");
else status->hide();
// lineEdit()->setText(eval.expression() + " -> " + QString::number(value(), 'G', 10));
//lineEdit()->clear();
lineEdit()->blockSignals(false);
resizeIcons();
}
void EvalSpinBox::wheelEvent(QWheelEvent * event) {
if (event->modifiers().testFlag(Qt::ShiftModifier))
if (event->modifiers().testFlag(Qt::ShiftModifier)) {
stepByDouble(
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
event->delta()
@@ -303,13 +290,13 @@ void EvalSpinBox::wheelEvent(QWheelEvent * event) {
event->angleDelta().y()
#endif
> 0 ? 0.1 : -0.1);
else
} else {
QAbstractSpinBox::wheelEvent(event);
}
}
void EvalSpinBox::stepByDouble(double steps) {
//qDebug() << "step" << steps;
if (isReadOnly()) return;
QString t = text();
if (eval->check(t)) {
@@ -347,13 +334,7 @@ void EvalSpinBox::stepByDouble(double steps) {
void EvalSpinBox::setDefaultText(const QString & t) {
// bool def = (!cw->isHidden());
dt = t;
// if (def) {
// lineEdit()->setText(dt);
// setExpression_();
// }
//if (t == eval.expression() || (value() == 0 && t.isEmpty())) clear();
cw->setVisible((eval->expression() != dt || (dt.isEmpty() && eval->expression() == "0")) && cw_visible);
resizeIcons();
}
@@ -368,7 +349,8 @@ void EvalSpinBox::setClearButtonVisible(bool visible) {
void EvalSpinBox::setCalculationVisible(bool visible) {
calc_visible = visible;
setExpression_();
if (!calc_visible)
setExpressionSlot();
if (!calc_visible) {
label->hide();
}
}