From a224b7b968cd3b3597d31ee82555696fa4343d96 Mon Sep 17 00:00:00 2001 From: luoqing Date: Fri, 4 Nov 2022 09:37:23 +0800 Subject: [PATCH] fix(history-recoder):Fix crash when closing calculator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复关闭计算器时出现崩溃的问题 --- ...der-Fix-crash-when-closing-calculato.patch | 334 ++++++++++++++++++ kiran-calculator.spec | 7 +- 2 files changed, 340 insertions(+), 1 deletion(-) create mode 100644 0001-fix-history-recoder-Fix-crash-when-closing-calculato.patch diff --git a/0001-fix-history-recoder-Fix-crash-when-closing-calculato.patch b/0001-fix-history-recoder-Fix-crash-when-closing-calculato.patch new file mode 100644 index 0000000..ad7e712 --- /dev/null +++ b/0001-fix-history-recoder-Fix-crash-when-closing-calculato.patch @@ -0,0 +1,334 @@ +From dfc5fccc5e84d11c03267dd58695cbedb3d35a04 Mon Sep 17 00:00:00 2001 +From: luoqing +Date: Thu, 3 Nov 2022 17:35:30 +0800 +Subject: [PATCH] fix(history-recoder):Fix crash when closing calculator +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +- 修复关闭计算器时出现崩溃的问题 +--- + src/gui/history-recoder.cpp | 152 +++++++++--------- + src/gui/history-recoder.h | 11 +- + .../programmer-mode-page.cpp | 1 + + .../science-mode-page/science-mode-page.cpp | 1 + + .../standard-mode-page/standard-mode-page.cpp | 1 + + 5 files changed, 80 insertions(+), 86 deletions(-) + +diff --git a/src/gui/history-recoder.cpp b/src/gui/history-recoder.cpp +index 169774c..0267b22 100644 +--- a/src/gui/history-recoder.cpp ++++ b/src/gui/history-recoder.cpp +@@ -25,7 +25,6 @@ + #include "programmer-mode-page/programmer-expr-calculator.h" + #include "utils.h" + +-#include + #include + #include + #include +@@ -35,11 +34,11 @@ + + HistoryRecoder::HistoryRecoder(QWidget *parent) : QListWidget(parent) + { +- connect(this, SIGNAL(itemClicked(QListWidgetItem *)),this, +- SLOT(handleItem(QListWidgetItem *))); +- + setWordWrap(true); + adjustSize(); ++ ++ connect(this, SIGNAL(itemClicked(QListWidgetItem *)),this, ++ SLOT(handleItem(QListWidgetItem *))); + } + + void HistoryRecoder::setSession(Session *session) +@@ -53,58 +52,56 @@ void HistoryRecoder::setCalculateMode(int mode) + } + + //重新更新历史记录时,要释放之前的内存 ++//TODO:优化历史代码 + void HistoryRecoder::updateHistory() + { +- //注意释放内存时返回到首位 +- if(m_labelExpr != nullptr){ +- // m_labelExpr head ptr position +- m_labelExpr -=m_lastHistoryCount; +- m_labelValue -=m_lastHistoryCount; +- m_vboxlayout -=m_lastHistoryCount; +- m_historyWidget -=m_lastHistoryCount; +- m_historyItem -=m_lastHistoryCount; +- +- delete []m_labelExpr; +- m_labelExpr == nullptr; +- delete []m_labelValue; +- m_labelValue == nullptr; +- delete []m_vboxlayout; +- m_vboxlayout == nullptr; +- delete []m_historyWidget; +- m_historyWidget == nullptr; +- delete []m_historyItem; +- m_historyItem == nullptr; ++ if(m_historyWidgetList.count() != 0) ++ { ++ //All items will be permanently deleted ++ this->clear(); ++ for(int i = 0; i < m_historyWidgetList.count() ; ++i) ++ { ++ delete m_historyWidgetList.value(i); ++ } ++ m_labelExprList.clear(); ++ m_labelValueList.clear(); ++ m_vboxlayoutList.clear(); ++ m_historyWidgetList.clear(); ++ m_historyItemList.clear(); + } + + m_evaluator = Evaluator::instance(); + m_evaluator->setSession(m_session); + QList history = Evaluator::instance()->session()->historyToList(); +- m_lastHistoryCount = history.count(); + clear(); + clearSelection(); + QStringList historyList; + QString expression; + Quantity value; + +- +- //指针数组 +- m_labelExpr = new QLabel[history.count()]; +- m_labelValue = new QLabel[history.count()]; +- m_vboxlayout = new QVBoxLayout[history.count()]; +- m_historyWidget = new QWidget[history.count()]; +- m_historyItem = new QListWidgetItem[history.count()]; +- +- for(int i=0; isetObjectName(labelExprObjectName); ++ labelExpr->setObjectName(labelExprObjectName); + + QString labelValueObjectName = "labelValue_"; + labelValueObjectName.append(QString::number(i)); +- m_labelValue->setObjectName(labelValueObjectName); ++ labelValue->setObjectName(labelValueObjectName); + + QString reformatExpr; + if(m_currentMode == Calculation_Mode_Programmer) +@@ -127,23 +124,23 @@ void HistoryRecoder::updateHistory() + break; + } + } +- m_labelExpr->setWordWrap(true); ++ labelExpr->setWordWrap(true); + if(m_currentMode == Calculation_Mode_Programmer) +- m_labelExpr->setText(historyWordWrap((reformatExpr + "="), 24)); ++ labelExpr->setText(historyWordWrap((reformatExpr + "="), 24)); + else +- m_labelExpr->setText(historyWordWrap((Utils::reformatSeparators(expression) + "="), 24)); ++ labelExpr->setText(historyWordWrap((Utils::reformatSeparators(expression) + "="), 24)); + +- m_labelExpr->setStyleSheet("padding-right:4px;color:#919191;font-size:14px;font-family: Noto Sans CJK SC Regular;"); +- m_labelExpr->setAlignment(Qt::AlignRight); +- m_labelExpr->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Preferred); +- m_labelExpr->setFixedWidth(214); +- m_labelExpr->setMinimumHeight(18); +- m_labelExpr->adjustSize(); +- m_vboxlayout->addWidget(m_labelExpr); ++ labelExpr->setStyleSheet("padding-right:4px;color:#919191;font-size:14px;font-family: Noto Sans CJK SC Regular;"); ++ labelExpr->setAlignment(Qt::AlignRight); ++ labelExpr->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Preferred); ++ labelExpr->setFixedWidth(214); ++ labelExpr->setMinimumHeight(18); ++ labelExpr->adjustSize(); ++ vboxlayout->addWidget(labelExpr); + + if(!value.isNan()) + { +- m_labelValue->setWordWrap(true); ++ labelValue->setWordWrap(true); + if(m_currentMode == Calculation_Mode_Programmer) + { + QString reformatValue; +@@ -161,68 +158,65 @@ void HistoryRecoder::updateHistory() + reformatValue = Utils::reformatSeparatorsPro(DMath::format(value, Quantity::Format::Complement() + Quantity::Format::Binary() + Quantity::Format::Fixed()).remove("0b"), 2); + break; + } +- m_labelValue->setText(historyWordWrap(reformatValue.replace("-",QString::fromUtf8("−")), 16)); ++ labelValue->setText(historyWordWrap(reformatValue.replace("-",QString::fromUtf8("−")), 16)); + } + else if(m_currentMode == Calculation_Mode_Science) + { + if(m_historyFEIndex.contains(i)) + { +- m_labelValue->setText(historyWordWrap(Utils::reformatSeparators(DMath::format(value, Quantity::Format::Scientific())), 16)); ++ labelValue->setText(historyWordWrap(Utils::reformatSeparators(DMath::format(value, Quantity::Format::Scientific())), 16)); + } + else + { + QString scienceValueLength = NumberFormatter::format(value); + if(scienceValueLength.remove(".").length() > 24) +- m_labelValue->setText(historyWordWrap(Utils::reformatSeparators(DMath::format(value, Quantity::Format::Scientific())), 16)); ++ labelValue->setText(historyWordWrap(Utils::reformatSeparators(DMath::format(value, Quantity::Format::Scientific())), 16)); + else +- m_labelValue->setText(historyWordWrap(Utils::reformatSeparators(NumberFormatter::format(value)), 16)); ++ labelValue->setText(historyWordWrap(Utils::reformatSeparators(NumberFormatter::format(value)), 16)); + } + } + else + { + QString standardValueLength = NumberFormatter::format(value); + if(standardValueLength.remove(".").length() > 16) +- m_labelValue->setText(historyWordWrap(Utils::reformatSeparators(DMath::format(value, Quantity::Format::Scientific())), 16)); ++ labelValue->setText(historyWordWrap(Utils::reformatSeparators(DMath::format(value, Quantity::Format::Scientific())), 16)); + else +- m_labelValue->setText(historyWordWrap(Utils::reformatSeparators(NumberFormatter::format(value)), 16)); ++ labelValue->setText(historyWordWrap(Utils::reformatSeparators(NumberFormatter::format(value)), 16)); + + } + +- m_labelValue->setStyleSheet("color:#FFFFFF;font-size:24px;font-family: Noto Sans CJK SC Regular;padding-bottom:14px;"); +- m_labelValue->setAlignment(Qt::AlignRight); +- m_labelValue->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Preferred); +- m_labelValue->setFixedWidth(214); +- m_labelValue->setMinimumHeight(30); +- m_labelValue->adjustSize(); +- m_vboxlayout->addWidget(m_labelValue); ++ labelValue->setStyleSheet("color:#FFFFFF;font-size:24px;font-family: Noto Sans CJK SC Regular;padding-bottom:14px;"); ++ labelValue->setAlignment(Qt::AlignRight); ++ labelValue->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Preferred); ++ labelValue->setFixedWidth(214); ++ labelValue->setMinimumHeight(30); ++ labelValue->adjustSize(); ++ vboxlayout->addWidget(labelValue); + + } +- m_vboxlayout->setMargin(0); +- m_vboxlayout->setSpacing(4); + +- m_historyWidget->setLayout(m_vboxlayout); ++ vboxlayout->setMargin(0); ++ vboxlayout->setSpacing(4); ++ ++ historyWidget->setLayout(vboxlayout); + +- m_historyWidget->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Preferred); +- m_historyWidget->setFixedWidth(214); +- m_historyWidget->setMinimumHeight(54); ++ historyWidget->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Preferred); ++ historyWidget->setFixedWidth(214); ++ historyWidget->setMinimumHeight(54); + +- m_historyWidget->resize(204,m_labelExpr->rect().height() + m_labelValue->rect().height() +- + m_vboxlayout->spacing()); ++ historyWidget->resize(204,labelExpr->rect().height() + labelValue->rect().height() ++ + vboxlayout->spacing()); + +- m_historyItem->setSizeHint(QSize(214, +- m_labelExpr->rect().height() + m_labelValue->rect().height() +- + m_vboxlayout->spacing())); ++ historyItem->setSizeHint(QSize(214, ++ labelExpr->rect().height() + labelValue->rect().height() ++ + vboxlayout->spacing())); + +- QListWidget::addItem(m_historyItem); +- QListWidget::setItemWidget(m_historyItem,m_historyWidget); ++ this->addItem(historyItem); ++ this->setItemWidget(historyItem,historyWidget); + +- m_labelExpr++; +- m_labelValue++; +- m_vboxlayout++; +- m_historyWidget++; +- m_historyItem++; + } +- QListWidget::scrollToBottom(); ++ this->scrollToBottom(); ++ + } + + void HistoryRecoder::historyFormatChanged(int format) +@@ -307,8 +301,6 @@ QItemSelectionModel::SelectionFlags HistoryRecoder::selectionCommand(const QMode + + void HistoryRecoder::handleItem(QListWidgetItem *item) + { +- qDebug() << "handleItem_row:"; +- qDebug() << QListWidget::row(item); + + auto widget = itemWidget(item); + QString labelValueName = "labelValue_"; +diff --git a/src/gui/history-recoder.h b/src/gui/history-recoder.h +index e8e2370..25f6980 100644 +--- a/src/gui/history-recoder.h ++++ b/src/gui/history-recoder.h +@@ -66,13 +66,12 @@ private: + Evaluator* m_evaluator; + QList m_historyFEIndex; + +- QLabel *m_labelExpr = nullptr; +- QLabel *m_labelValue = nullptr; +- QVBoxLayout *m_vboxlayout = nullptr; +- QWidget *m_historyWidget = nullptr; +- QListWidgetItem *m_historyItem = nullptr; ++ QList m_labelExprList; ++ QList m_labelValueList; ++ QList m_vboxlayoutList; ++ QListm_historyWidgetList; ++ QListm_historyItemList; + +- int m_lastHistoryCount = 0; + int m_currentFormat = Num_Format_Dec; + int m_currentMode = Calculation_Mode_Standard; + bool m_FE = false; //科学计算法模式默认关闭 +diff --git a/src/gui/programmer-mode-page/programmer-mode-page.cpp b/src/gui/programmer-mode-page/programmer-mode-page.cpp +index 41efc9a..4d6b5a5 100644 +--- a/src/gui/programmer-mode-page/programmer-mode-page.cpp ++++ b/src/gui/programmer-mode-page/programmer-mode-page.cpp +@@ -94,6 +94,7 @@ ProgrammerModePage::ProgrammerModePage(QWidget *parent) : + ProgrammerModePage::~ProgrammerModePage() + { + delete ui; ++ delete m_programmerSession; + } + + void ProgrammerModePage::handleProgrammerKeysButtonClicked(Button button) +diff --git a/src/gui/science-mode-page/science-mode-page.cpp b/src/gui/science-mode-page/science-mode-page.cpp +index b3c2917..1090cf3 100644 +--- a/src/gui/science-mode-page/science-mode-page.cpp ++++ b/src/gui/science-mode-page/science-mode-page.cpp +@@ -76,6 +76,7 @@ ScienceModePage::ScienceModePage(QWidget *parent) : + ScienceModePage::~ScienceModePage() + { + delete ui; ++ delete m_scienceSession; + } + + void ScienceModePage::showEvent(QShowEvent *event) +diff --git a/src/gui/standard-mode-page/standard-mode-page.cpp b/src/gui/standard-mode-page/standard-mode-page.cpp +index 144f398..1be09e8 100644 +--- a/src/gui/standard-mode-page/standard-mode-page.cpp ++++ b/src/gui/standard-mode-page/standard-mode-page.cpp +@@ -127,4 +127,5 @@ void StandardModePage::showEvent(QShowEvent *event) + StandardModePage::~StandardModePage() + { + delete ui; ++ delete m_standardSession; + } +-- +2.33.0 + diff --git a/kiran-calculator.spec b/kiran-calculator.spec index 72d8b4c..32e2160 100644 --- a/kiran-calculator.spec +++ b/kiran-calculator.spec @@ -1,12 +1,14 @@ Name: kiran-calculator Version: 2.2.0 -Release: 5 +Release: 6 Summary: Kiran Calculator License: GPLv3 Source0: %{name}-%{version}.tar.gz +Patch0001: 0001-fix-history-recoder-Fix-crash-when-closing-calculato.patch + BuildRequires: cmake BuildRequires: gcc-c++ BuildRequires: qt5-qtbase-devel @@ -44,6 +46,9 @@ gtk-update-icon-cache -f /usr/share/icons/hicolor/ rm -rf ${buildroot} %changelog +* Fri Nov 04 2022 luoqing - 2.2.0-6 +- KYOS-B: Fix crash when closing calculator + * Tue Mar 01 2022 longcheng - 2.2.0-5 - KYOS-B: Add Requires gtk-update-icon-cache -- Gitee