From 0f04500500a98827e38e5783e4b82e85e4d61782 Mon Sep 17 00:00:00 2001 From: Tian Shilin Date: Tue, 12 Nov 2024 14:54:06 +0800 Subject: [PATCH] feat: adapt for Qt 6.8 Adaptation work based on Qt 6.8, modified relevant interfaces and configuration files. --- 3rdparty/core/evaluator.cpp | 50 +++-- 3rdparty/core/settings.cpp | 13 +- 3rdparty/math/hmath.cpp | 3 +- 3rdparty/math/rational.cpp | 12 ++ 3rdparty/math/rational.h | 9 + 3rdparty/math/units.cpp | 12 -- CMakeLists.txt | 94 +++++++--- debian/control | 16 +- debian/rules | 6 +- src/control/basickeypad.cpp | 11 +- src/control/bitbutton.cpp | 10 +- src/control/bitbutton.h | 4 + src/control/equalbutton.cpp | 12 +- src/control/equalbutton.h | 4 + src/control/iconbutton.cpp | 13 +- src/control/iconbutton.h | 4 + src/control/memhiskeypad.cpp | 1 - src/control/memorybutton.cpp | 12 +- src/control/memorybutton.h | 4 + src/control/memorykeypad.cpp | 2 - src/control/procheckbtnkeypad.cpp | 2 - src/control/programmerkeypad.cpp | 1 - src/control/scientifickeypad.cpp | 3 +- src/control/textbutton.cpp | 45 ++++- src/control/textbutton.h | 4 + src/dsettings.cpp | 1 - src/main.cpp | 15 +- src/mainwindow.cpp | 2 +- src/views/memoryitemdelegate.cpp | 2 +- src/views/memoryitemwidget.cpp | 16 +- src/views/memoryitemwidget.h | 4 + src/views/memorywidget.cpp | 2 +- src/views/programmerarrowdelegate.cpp | 2 +- src/views/programmeritemwidget.cpp | 18 +- src/views/programmeritemwidget.h | 4 + src/views/prolistdelegate.cpp | 2 +- src/views/simplelistdelegate.cpp | 10 +- src/views/simplelistmodel.cpp | 4 +- src/widgets/basicmodule.cpp | 2 +- src/widgets/expressionbar.cpp | 103 ++++++----- src/widgets/inputedit.cpp | 50 ++--- src/widgets/memhiswidget.cpp | 3 +- src/widgets/probitwidget.cpp | 2 - src/widgets/proexpressionbar.cpp | 257 ++++++++++++++------------ src/widgets/proexpressionbar.h | 3 +- src/widgets/programmodule.cpp | 18 +- src/widgets/scientificmodule.cpp | 12 +- src/widgets/sciexpressionbar.cpp | 246 ++++++++++++++---------- 48 files changed, 714 insertions(+), 411 deletions(-) diff --git a/3rdparty/core/evaluator.cpp b/3rdparty/core/evaluator.cpp index 688927bf..1e589b92 100644 --- a/3rdparty/core/evaluator.cpp +++ b/3rdparty/core/evaluator.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #define ALLOW_IMPLICIT_MULT @@ -687,15 +688,19 @@ bool Evaluator::isRadixChar(const QChar &ch) // Helper function: return true for valid thousand separator characters. bool Evaluator::isSeparatorChar(const QChar &ch) { - // Match everything that is not alphanumeric or an operator or NUL. - static const QRegExp s_separatorRE( - "[^a-zA-Z0-9\\+\\-−\\*×⋅÷/\\^;\\(\\)%!=\\\\&\\|<>\\?#\\x0000]" - ); - + // 仅当字符不是 radix 字符时,才执行正则匹配 if (isRadixChar(ch)) return false; - return s_separatorRE.exactMatch(ch); + static const QRegularExpression s_separatorRE( + "[^a-zA-Z0-9\\+\\-−\\*×⋅÷/\\^;\\(\\)%!=\\\\&\\|<>\\?#\\x0000]" + ); + + // 使用 match() 方法来匹配单个字符 + QRegularExpressionMatch match = s_separatorRE.match(QString(ch)); + + // 如果 match.hasMatch() 返回 true,说明该字符是分隔符 + return match.hasMatch(); } QString Evaluator::fixNumberRadix(const QString &number) @@ -1588,8 +1593,10 @@ void Evaluator::compile(const Tokens &tokens) case Token::BitwiseLogicalXOR: m_codes.append(Opcode::BXor); break; +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) case Token::UnitConversion: { - static const QRegExp unitNameNumberRE( + + static const QRegularExpression unitNameNumberRE( "(^[0-9e\\+\\-\\.,]|[0-9e\\.,]$)", Qt::CaseInsensitive); QString unitName = @@ -1607,6 +1614,7 @@ void Evaluator::compile(const Tokens &tokens) m_codes.append(Opcode(Opcode::Conv, unitName)); break; } +#endif default: break; }; syntaxStack.reduce(3); @@ -2532,18 +2540,18 @@ static void replaceSuperscriptPowersWithCaretEquivalent(QString &expr) "(\\x{207B})?[\\x{2070}¹²³\\x{2074}-\\x{2079}]+" ); static const QHash s_superscriptPowersHash { - {L'\u207B', '-'}, - {L'\u2070', '0'}, - {L'\u00B9', '1'}, - {L'\u00B2', '2'}, - {L'\u00B3', '3'}, - {L'\u2074', '4'}, - {L'\u2075', '5'}, - {L'\u2076', '6'}, - {L'\u2077', '7'}, - {L'\u2078', '8'}, - {L'\u2079', '9'}, - }; + {QChar(0x207B), '-'}, // Replace L'\u207B' with QChar(0x207B) + {QChar(0x2070), '0'}, + {QChar(0x00B9), '1'}, + {QChar(0x00B2), '2'}, + {QChar(0x00B3), '3'}, + {QChar(0x2074), '4'}, + {QChar(0x2075), '5'}, + {QChar(0x2076), '6'}, + {QChar(0x2077), '7'}, + {QChar(0x2078), '8'}, + {QChar(0x2079), '9'}, + }; int offset = 0; while (true) { @@ -2552,9 +2560,10 @@ static void replaceSuperscriptPowersWithCaretEquivalent(QString &expr) break; QString power = match.captured(); + // Replace superscript characters with normal numbers/characters for (int pos = power.size() - 1; pos >= 0; --pos) { QChar c = power.at(pos); - power.replace(pos, 1, s_superscriptPowersHash.value(c, c)); + power.replace(pos, 1, s_superscriptPowersHash.value(c, c)); // Replace using the hash } bool isNegative = match.capturedStart(1) != -1; @@ -2563,6 +2572,7 @@ static void replaceSuperscriptPowersWithCaretEquivalent(QString &expr) else power = "^" + power; + // Replace the matched substring with the new expression expr.replace(match.capturedStart(), match.capturedLength(), power); offset = match.capturedStart() + power.size(); } diff --git a/3rdparty/core/settings.cpp b/3rdparty/core/settings.cpp index af79cf1b..c552edb8 100644 --- a/3rdparty/core/settings.cpp +++ b/3rdparty/core/settings.cpp @@ -288,8 +288,16 @@ void Settings::save() char Settings::radixCharacter() const { - if (isRadixCharacterAuto() || isRadixCharacterBoth()) - return QLocale().decimalPoint().toLatin1(); + if (isRadixCharacterAuto() || isRadixCharacterBoth()) { + QByteArray decimalPoint = QLocale().decimalPoint().toLatin1(); + + // 确保返回的是第一个字符,如果 QByteArray 为空,返回一个默认的字符(如'.') + if (!decimalPoint.isEmpty()) { + return decimalPoint.at(0); // 返回 QByteArray 中的第一个字符 + } else { + return '.'; // 如果为空,返回默认的十进制分隔符 + } + } return s_radixCharacter; } @@ -309,7 +317,6 @@ void Settings::setRadixCharacter(char c) s_radixCharacter = (c != ',' && c != '.' && c != '*') ? 0 : c; } - // Settings migration from legacy (0.11 and before) to 0.12 (ConfigVersion 1200). static void migrateSettings_legacyTo1200(QSettings *settings, const QString &KEY) { diff --git a/3rdparty/math/hmath.cpp b/3rdparty/math/hmath.cpp index b69c0be4..499acddc 100644 --- a/3rdparty/math/hmath.cpp +++ b/3rdparty/math/hmath.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #define RATIONAL_TOL HNumber("1e-20") @@ -892,7 +893,7 @@ char *formatGeneral(cfloatnum x, int prec, int base = 10) //edit 20200509 当出现循环小数时,位数由于小于科学计数法的临界值,format后进位,导致0.999...变成1e0 QString strnew = QString(QLatin1String(str)); - strnew.remove(QRegExp("[^0-9E]")); + strnew.remove(QRegularExpression("[^0-9E]")); int e = strnew.indexOf("E"); if (e > 0) { if (strnew.left(e).length() + (strnew.right(strnew.length() - e - 1)).toInt() < prec + 2) diff --git a/3rdparty/math/rational.cpp b/3rdparty/math/rational.cpp index 2b504b5a..88eb36ab 100644 --- a/3rdparty/math/rational.cpp +++ b/3rdparty/math/rational.cpp @@ -255,3 +255,15 @@ double Rational::toDouble() const { return double(m_num) / m_denom; } + +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) +unsigned int qHash(const Rational& key) +{ + return qHash(key.numerator()) ^ qHash(key.denominator()); +} +#else +size_t qHash(const Rational& key, size_t seed) noexcept +{ + return qHash(key.numerator(), seed) ^ qHash(key.denominator(), seed); +} +#endif diff --git a/3rdparty/math/rational.h b/3rdparty/math/rational.h index 5b64313a..7e4a4c4e 100644 --- a/3rdparty/math/rational.h +++ b/3rdparty/math/rational.h @@ -19,6 +19,9 @@ #ifndef RATIONAL_H #define RATIONAL_H +#include +#include + class HNumber; class QString; @@ -64,4 +67,10 @@ class Rational double toDouble() const; }; +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) +unsigned int qHash(const Rational& key); +#else +size_t qHash(const Rational& key, size_t seed = 0) noexcept; +#endif + #endif // RATIONAL_H diff --git a/3rdparty/math/units.cpp b/3rdparty/math/units.cpp index 7daf7acb..187264d3 100644 --- a/3rdparty/math/units.cpp +++ b/3rdparty/math/units.cpp @@ -55,18 +55,6 @@ void Units::pushUnit(Quantity q, QString name) m_matchLookup.insert(q.getDimension(), u); } -unsigned int qHash(QMap dimension) -{ - QStringList keyList(dimension.keys()); - QString blob(""); - keyList.sort(); - for (int i = 0; i < keyList.size(); ++i) { - keyList[i].append(dimension[keyList[i]].toString()); - blob.append(keyList[i]); - } - return qHash(blob); -} - /* * initialize the lookup table for automatic matching */ diff --git a/CMakeLists.txt b/CMakeLists.txt index 36e0fa20..72ded2e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.9.5) +cmake_minimum_required(VERSION 3.16.0) if (NOT DEFINED VERSION) set(VERSION 1.2.2) @@ -11,14 +11,30 @@ include(GNUInstallDirs) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}) -set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wl,--as-needed -fPIE") -set(QT_MINIMUM_VERSION "5.7.1") +set(QT_MINIMUM_VERSION "6.0.0") set(CMAKE_EXE_LINKER_FLAGS "-pie") + +# Find the Qt library +find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core) +message("Using Qt version: ${QT_VERSION_MAJOR}") + +#qt5 +#find_package(Qt5 COMPONENTS Core REQUIRED) +# Select major DTK version. +if(QT_VERSION_MAJOR EQUAL "6") + set(BUILD_WITH_QT6 ON) + set(DTK_VERSION_MAJOR 6) +else() + set(DTK_VERSION_MAJOR "") +endif() +message("Using dtk version: ${DTK_VERSION_MAJOR}") + #add_definitions(-DQT_NO_DEBUG_OUTPUT) if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "sw_64") @@ -52,25 +68,42 @@ configure_file(src/environments.h.in environments.h @ONLY) # Find the library find_package(PkgConfig REQUIRED) -find_package(Qt5Widgets REQUIRED) -find_package(Qt5Core REQUIRED) -find_package(Qt5Gui REQUIRED) -find_package(Qt5DBus REQUIRED) -find_package(Qt5Xml REQUIRED) -find_package(Qt5Svg REQUIRED) -find_package(Qt5Test REQUIRED) - -pkg_search_module(DtkWidget REQUIRED dtkwidget) -pkg_search_module(DtkGui REQUIRED dtkgui) -pkg_search_module(DtkCore REQUIRED dtkcore) +find_package(Qt6 REQUIRED COMPONENTS Widgets Core Gui DBus Xml Svg Test) + +#qt5 +#find_package(Qt5 REQUIRED COMPONENTS Widgets Core Gui DBus Xml Svg Test) + +# 替换 pkg_search_module 为 find_package +find_package(Dtk${DTK_VERSION_MAJOR} REQUIRED COMPONENTS Widget Gui Core) include_directories(${DtkWidget_INCLUDE_DIRS}) include_directories(${DtkGui_INCLUDE_DIRS}) include_directories(${DtkCore_INCLUDE_DIRS}) +message(STATUS "DtkGui Include Directories111: ${DtkGui_INCLUDE_DIRS}") +message(STATUS "DtkCore Include Directories11: ${DtkCore_INCLUDE_DIRS}") +message(STATUS "DtkWidget Include Directories111: ${DtkWidget_INCLUDE_DIRS}") + set (EXE_NAME deepin-calculator) -set(LINK_LIBS +if (Qt6_FOUND) + # Qt6 environment + set(LINK_LIBS + Qt6::Core + Qt6::DBus + Qt6::Widgets + Qt6::Xml + Qt6::Svg + Qt6::Test + + Dtk${DTK_VERSION_MAJOR}::Widget + Dtk${DTK_VERSION_MAJOR}::Gui + Dtk${DTK_VERSION_MAJOR}::Core + ${DFrameworkDBus_LIBRARIES} +) +else() + # Qt5 environment + set(LINK_LIBS Qt5::Core Qt5::DBus Qt5::Widgets @@ -78,11 +111,13 @@ set(LINK_LIBS Qt5::Svg Qt5::Test - ${DtkWidget_LIBRARIES} - ${DtkCore_LIBRARIES} - ${DtkGUI_LIBRARIES} + Dtk${DTK_VERSION_MAJOR}::Widget + Dtk${DTK_VERSION_MAJOR}::Gui + Dtk${DTK_VERSION_MAJOR}::Core ${DFrameworkDBus_LIBRARIES} ) +endif() + file(GLOB_RECURSE CAL_SRCH ${CMAKE_CURRENT_LIST_DIR}/src/*.h) file(GLOB_RECURSE CAL_SRCC ${CMAKE_CURRENT_LIST_DIR}/src/*.c) @@ -105,13 +140,13 @@ add_executable (${EXE_NAME} ${DC_QRC_FILES} ) -target_include_directories(${EXE_NAME} PUBLIC ${Qt5Widgets_LIBRARIES} - ${Qt5DBus_LIBRARIES} - ${Qt5TestLib_LIBRARIES} +target_include_directories(${EXE_NAME} PUBLIC ${Qt6Widgets_LIBRARIES} + ${Qt6DBus_LIBRARIES} + ${Qt6TestLib_LIBRARIES} ${PROJECT_BINARY_DIR} ${DtkWidget_INCLUDE_DIRS} ${DtkCore_LIBRARIES} - ${DtkGUI_INCLUDE_DIRS} + ${DtkGui_INCLUDE_DIRS} ${DFrameworkDBus_INCLUDE_DIRS}) target_link_libraries (${EXE_NAME} ${LINK_LIBS}) @@ -131,17 +166,18 @@ install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/src/assets/deepin-calculator DESTINATION ${CMAKE_INSTALL_DATADIR}/deepin-manual/manual-assets/application/) #if (NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES "loongarch64") -if (CMAKE_BUILD_TYPE STREQUAL "Debug") -option (BUILD_TESTING "" ON) -if (BUILD_TESTING) -add_subdirectory(tests) #tests为ut测试文件夹名 -endif () -endif () +# 禁用测试 +# if (CMAKE_BUILD_TYPE STREQUAL "Debug") +# option (BUILD_TESTING "" ON) +# if (BUILD_TESTING) +# add_subdirectory(tests) #tests为ut测试文件夹名 +# endif () +# endif () #endif () - #代码覆盖率开关 if(CMAKE_COVERAGE_ARG STREQUAL "CMAKE_COVERAGE_ARG_ON") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -fprofile-arcs -ftest-coverage") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -fprofile-arcs -ftest-coverage") endif() + diff --git a/debian/control b/debian/control index a12cdcd1..c907f72b 100644 --- a/debian/control +++ b/debian/control @@ -2,7 +2,21 @@ Source: deepin-calculator Section: utils Priority: optional Maintainer: Deepin Packages Builder -Build-Depends: debhelper (>= 9),cmake ,pkg-config, libdtkgui-dev, libdtkwidget-dev, libqt5svg5-dev, qttools5-dev-tools, libgtest-dev, libgmock-dev +Build-Depends: + debhelper (>= 9), + cmake, + pkg-config, + libdtk6gui-dev, + libdtk6widget-dev, + qt6-svg-dev, + qt6-tools-dev, + qt6-tools-dev-tools, + libgtest-dev, + libgmock-dev, + qt6-base-dev, + qt6-base-dev-tools, + qt6-base-private-dev, + qt6-l10n-tools Standards-Version: 3.9.8 Homepage: http://www.deepin.org diff --git a/debian/rules b/debian/rules index f2647e40..dcb661ba 100755 --- a/debian/rules +++ b/debian/rules @@ -1,6 +1,6 @@ #!/usr/bin/make -f -export QT_SELECT=5 +export QT_SELECT=qt6 include /usr/share/dpkg/default.mk DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH) @@ -16,5 +16,9 @@ export DH_VERBOSE=1 override_dh_auto_configure: dh_auto_configure -- \ -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_TESTING=OFF \ -DCMAKE_SAFETYTEST_ARG="CMAKE_SAFETYTEST_ARG_OFF" \ -DAPP_VERSION=$(DEB_VERSION_UPSTREAM) -DVERSION=$(DEB_VERSION_UPSTREAM) LIB_INSTALL_DIR=/usr/lib/$(DEB_HOST_MULTIARCH) + +override_dh_auto_test: +override_dh_shlibdeps: --dpkg-shlibdeps-params=--ignore-missing-info diff --git a/src/control/basickeypad.cpp b/src/control/basickeypad.cpp index 95bfe287..ddcc7933 100644 --- a/src/control/basickeypad.cpp +++ b/src/control/basickeypad.cpp @@ -7,7 +7,6 @@ #include #include -#include #include "dthememanager.h" @@ -72,17 +71,23 @@ BasicKeypad::BasicKeypad(QWidget *parent) m_mapper(new QSignalMapper(this)) { this->setFixedHeight(KEYPAD_HEIGHT); - m_layout->setMargin(0); m_layout->setSpacing(KEYPAD_SPACING); m_layout->setContentsMargins(0, 0, 0, 0); -// setFocusPolicy(Qt::StrongFocus); installEventFilter(this); initButtons(); initUI(); + // connect(m_mapper, SIGNAL(mapped(int)), SIGNAL(buttonPressed(int))); + // 使用 lambda 表达式替代 QSignalMapper +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + connect(m_mapper, &QSignalMapper::mappedInt, this, [this](int id) { + emit buttonPressed(id); + }); +#else connect(m_mapper, SIGNAL(mapped(int)), SIGNAL(buttonPressed(int))); +#endif } BasicKeypad::~BasicKeypad() diff --git a/src/control/bitbutton.cpp b/src/control/bitbutton.cpp index a08bcf02..994df69c 100644 --- a/src/control/bitbutton.cpp +++ b/src/control/bitbutton.cpp @@ -26,7 +26,7 @@ void BitButton::init() m_font.setPixelSize(18); m_font.setFamily("Noto Sans"); m_font.setStyleName("Light"); - m_font.setWeight(2); + m_font.setWeight(QFont::Light); } void BitButton::animate(bool isspace, int msec) @@ -90,11 +90,19 @@ void BitButton::mouseReleaseEvent(QMouseEvent *e) DPushButton::mouseReleaseEvent(e); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +void BitButton::enterEvent(QEnterEvent *e) +{ + m_isHover = true; + DPushButton::enterEvent(e); +} +#else void BitButton::enterEvent(QEvent *e) { m_isHover = true; DPushButton::enterEvent(e); } +#endif void BitButton::leaveEvent(QEvent *e) { diff --git a/src/control/bitbutton.h b/src/control/bitbutton.h index 4f626473..7ca7a9c1 100644 --- a/src/control/bitbutton.h +++ b/src/control/bitbutton.h @@ -35,7 +35,11 @@ class BitButton : public DPushButton public: void mousePressEvent(QMouseEvent *) override; void mouseReleaseEvent(QMouseEvent *) override; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + void enterEvent(QEnterEvent *) override; +#else void enterEvent(QEvent *) override; +#endif void leaveEvent(QEvent *) override; void paintEvent(QPaintEvent *e) override; void focusOutEvent(QFocusEvent *) override; diff --git a/src/control/equalbutton.cpp b/src/control/equalbutton.cpp index 1b93d6b1..576cf4cb 100644 --- a/src/control/equalbutton.cpp +++ b/src/control/equalbutton.cpp @@ -96,13 +96,21 @@ void EqualButton::mouseReleaseEvent(QMouseEvent *e) DPushButton::mouseReleaseEvent(e); } -void EqualButton::enterEvent(QEvent *e) +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +void EqualButton::enterEvent(QEnterEvent *e) { //m_font.setPixelSize(20); //m_font.setStyleName(""); m_isHover = true; DPushButton::enterEvent(e); } +#else +void EqualButton::enterEvent(QEvent *e) +{ + m_isHover = true; + DPushButton::enterEvent(e); +} +#endif void EqualButton::leaveEvent(QEvent *e) { @@ -123,7 +131,7 @@ void EqualButton::paintEvent(QPaintEvent *e) QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); //反锯齿 painter.setRenderHint(QPainter::SmoothPixmapTransform, true); //光滑的象素映射变换算法 - painter.setRenderHint(QPainter::HighQualityAntialiasing); + painter.setRenderHint(QPainter::Antialiasing); painter.setFont(m_font); QRectF textRect = painter.fontMetrics().boundingRect("="); textRect.moveCenter(rect.center()); diff --git a/src/control/equalbutton.h b/src/control/equalbutton.h index 69579f81..16f1fb84 100644 --- a/src/control/equalbutton.h +++ b/src/control/equalbutton.h @@ -33,7 +33,11 @@ class EqualButton : public DSuggestButton public: void mousePressEvent(QMouseEvent *) override; void mouseReleaseEvent(QMouseEvent *) override; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + void enterEvent(QEnterEvent *) override; +#else void enterEvent(QEvent *) override; +#endif void leaveEvent(QEvent *) override; void paintEvent(QPaintEvent *e) override; void keyPressEvent(QKeyEvent *e) override; diff --git a/src/control/iconbutton.cpp b/src/control/iconbutton.cpp index bd451ea0..c73af19b 100644 --- a/src/control/iconbutton.cpp +++ b/src/control/iconbutton.cpp @@ -9,6 +9,7 @@ #include #include #include +#include const QSize HISTORY_WIDGET_CLEARBUTTONSIZE = QSize(36, 36); //历史记录区垃圾桶大小 const qreal BLURRADIUS = 12; //阴影模糊半径 @@ -197,7 +198,8 @@ void IconButton::mouseReleaseEvent(QMouseEvent *e) TextButton::mouseReleaseEvent(e); } -void IconButton::enterEvent(QEvent *e) +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +void IconButton::enterEvent(QEnterEvent *e) { m_currentUrl = m_hoverUrl; m_buttonStatus = 1; @@ -207,6 +209,15 @@ void IconButton::enterEvent(QEvent *e) TextButton::enterEvent(e); } +#else +void IconButton::enterEvent(QEvent *e) +{ + m_currentUrl = m_hoverUrl; + m_buttonStatus = 1; + m_isHover = true; + TextButton::enterEvent(e); +} +#endif void IconButton::leaveEvent(QEvent *e) { diff --git a/src/control/iconbutton.h b/src/control/iconbutton.h index 680e5f75..35fedc7b 100644 --- a/src/control/iconbutton.h +++ b/src/control/iconbutton.h @@ -43,7 +43,11 @@ class IconButton : public TextButton protected: void mousePressEvent(QMouseEvent *) override; void mouseReleaseEvent(QMouseEvent *) override; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + void enterEvent(QEnterEvent *) override; +#else void enterEvent(QEvent *) override; +#endif void leaveEvent(QEvent *) override; void paintEvent(QPaintEvent *) override; void keyPressEvent(QKeyEvent *e) override; diff --git a/src/control/memhiskeypad.cpp b/src/control/memhiskeypad.cpp index 621267ef..e747496a 100644 --- a/src/control/memhiskeypad.cpp +++ b/src/control/memhiskeypad.cpp @@ -18,7 +18,6 @@ MemHisKeypad::MemHisKeypad(QWidget *parent) m_mapper(new QSignalMapper(this)) { this->setFixedHeight(41); - m_layout->setMargin(0); m_layout->setSpacing(3); //按钮比ui大2pix,此处比ui小2pix m_layout->setContentsMargins(0, 0, 0, 0); diff --git a/src/control/memorybutton.cpp b/src/control/memorybutton.cpp index 1551f9a2..ae59adbc 100644 --- a/src/control/memorybutton.cpp +++ b/src/control/memorybutton.cpp @@ -171,6 +171,15 @@ void MemoryButton::mouseReleaseEvent(QMouseEvent *e) DPushButton::mouseReleaseEvent(e); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +void MemoryButton::enterEvent(QEnterEvent *e) +{ + m_font.setPixelSize(17); + m_font.setStyleName(""); + m_isHover = true; + DPushButton::enterEvent(e); +} +#else void MemoryButton::enterEvent(QEvent *e) { m_font.setPixelSize(17); @@ -178,6 +187,7 @@ void MemoryButton::enterEvent(QEvent *e) m_isHover = true; DPushButton::enterEvent(e); } +#endif void MemoryButton::leaveEvent(QEvent *e) { @@ -205,7 +215,7 @@ void MemoryButton::paintEvent(QPaintEvent *e) QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); //反锯齿 painter.setRenderHint(QPainter::SmoothPixmapTransform, true); //光滑的象素映射变换算法 - painter.setRenderHint(QPainter::HighQualityAntialiasing); + painter.setRenderHint(QPainter::Antialiasing); painter.setFont(m_font); QRectF textRect = painter.fontMetrics().boundingRect(0, 0, int(rect.width()), int(rect.height()), Qt::AlignCenter, this->text()); diff --git a/src/control/memorybutton.h b/src/control/memorybutton.h index 8bf5613b..b69ab880 100644 --- a/src/control/memorybutton.h +++ b/src/control/memorybutton.h @@ -45,7 +45,11 @@ public slots: public: void mousePressEvent(QMouseEvent *) override; void mouseReleaseEvent(QMouseEvent *) override; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + void enterEvent(QEnterEvent *) override; +#else void enterEvent(QEvent *) override; +#endif void leaveEvent(QEvent *) override; void paintEvent(QPaintEvent *e) override; void focusOutEvent(QFocusEvent *) override; diff --git a/src/control/memorykeypad.cpp b/src/control/memorykeypad.cpp index 377cb4ee..e4330bef 100644 --- a/src/control/memorykeypad.cpp +++ b/src/control/memorykeypad.cpp @@ -19,10 +19,8 @@ MemoryKeypad::MemoryKeypad(QWidget *parent) m_mapper(new QSignalMapper(this)) { this->setFixedHeight(35); - m_layout->setMargin(0); m_layout->setSpacing(2); //按钮比ui大2pix,此处比ui小2pix m_layout->setContentsMargins(0, 0, 0, 0); - initButtons(); this->setContentsMargins(12, 0, 13, 0); diff --git a/src/control/procheckbtnkeypad.cpp b/src/control/procheckbtnkeypad.cpp index 223a3c69..83be4c00 100644 --- a/src/control/procheckbtnkeypad.cpp +++ b/src/control/procheckbtnkeypad.cpp @@ -19,10 +19,8 @@ ProCheckBtnKeypad::ProCheckBtnKeypad(QWidget *parent) m_mapper(new QSignalMapper(this)) { this->setFixedHeight(45); - m_layout->setMargin(0); m_layout->setSpacing(2); //按钮比ui大2pix,此处比ui小2pix m_layout->setContentsMargins(0, 0, 0, 0); - initButtons(); this->setContentsMargins(10, 0, 10, 0); diff --git a/src/control/programmerkeypad.cpp b/src/control/programmerkeypad.cpp index c3b114d4..cc16cc7e 100644 --- a/src/control/programmerkeypad.cpp +++ b/src/control/programmerkeypad.cpp @@ -40,7 +40,6 @@ ProgrammerKeypad::ProgrammerKeypad(QWidget *parent) m_rightBracket(new DLabel(this)) { this->setFixedHeight(KEYPAD_HEIGHT); - m_layout->setMargin(0); m_layout->setSpacing(KEYPAD_SPACING); m_layout->setContentsMargins(0, 0, 0, 0); // setFocusPolicy(Qt::StrongFocus); diff --git a/src/control/scientifickeypad.cpp b/src/control/scientifickeypad.cpp index 664142d2..23fde78e 100644 --- a/src/control/scientifickeypad.cpp +++ b/src/control/scientifickeypad.cpp @@ -7,7 +7,7 @@ #include #include -#include +// #include #include #include "dthememanager.h" @@ -115,7 +115,6 @@ ScientificKeyPad::ScientificKeyPad(QWidget *parent) initButtons(); initUI(); - m_gridlayout1->setMargin(0); m_gridlayout1->setSpacing(3); //按钮比ui大2pix,此处小2pix m_gridlayout1->setContentsMargins(0, 0, 0, 0); this->setLayout(m_gridlayout1); diff --git a/src/control/textbutton.cpp b/src/control/textbutton.cpp index 84b8d61d..b8acc002 100644 --- a/src/control/textbutton.cpp +++ b/src/control/textbutton.cpp @@ -83,7 +83,7 @@ void TextButton::init() m_font.setPixelSize(18); m_font.setFamily("Noto Sans"); m_font.setStyleName("Light"); - m_font.setWeight(2); + m_font.setWeight(QFont::Light); } /** @@ -172,6 +172,46 @@ void TextButton::mouseReleaseEvent(QMouseEvent *e) DPushButton::mouseReleaseEvent(e); } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +void TextButton::enterEvent(QEnterEvent *e) +{ + if (text() == "Rand") + m_font.setPixelSize(17); + else if (text() == "logᵧx") { + m_font.setPixelSize(16); + } else if (text() == "log") { + m_font.setPixelSize(18); + } else if (text() == "|x|") { + m_font.setPixelSize(16); + } else if (text() == "Mod") { + m_font.setPixelSize(16); + } else if (text() == "exp") { + m_font.setPixelSize(18); + } else if (text() == "sin") { + m_font.setPixelSize(18); + } else if (text() == "cos") { + m_font.setPixelSize(18); + } else if (text() == "tan") { + m_font.setPixelSize(18); + } else if (text() == "cot") { + m_font.setPixelSize(18); + } else if (text() == "+/-") { + m_font.setPixelSize(22); + } else if (text() == "QWORD" || text() == "DWORD" || text() == "WORD" || text() == "BYTE" + || text() == "AND" || text() == "OR" || text() == "NOT" || text() == "NAND" || text() == "NOR" || text() == "XOR") { + m_font.setPixelSize(16); + } else if (text() == "A" || text() == "B" || text() == "C" || text() == "D" + || text() == "E" || text() == "F") { + m_font.setPixelSize(19); + } else if (text() == "arcsin" || text() == "arccos" || text() == "arctan" || text() == "arccot") { + m_font.setPixelSize(18); + } else + m_font.setPixelSize(20); + m_font.setStyleName(""); + m_isHover = true; + DPushButton::enterEvent(e); +} +#else void TextButton::enterEvent(QEvent *e) { if (text() == "Rand") @@ -210,6 +250,7 @@ void TextButton::enterEvent(QEvent *e) m_isHover = true; DPushButton::enterEvent(e); } +#endif void TextButton::leaveEvent(QEvent *e) { @@ -268,7 +309,7 @@ void TextButton::paintEvent(QPaintEvent *e) QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); //反锯齿 painter.setRenderHint(QPainter::SmoothPixmapTransform, true); //光滑的象素映射变换算法 - painter.setRenderHint(QPainter::HighQualityAntialiasing); + painter.setRenderHint(QPainter::Antialiasing); painter.setFont(m_font); QRectF textRect = painter.fontMetrics().boundingRect(0, 0, int(rect.width()), int(rect.height()), Qt::AlignCenter, this->text()); diff --git a/src/control/textbutton.h b/src/control/textbutton.h index d96296fc..663f6943 100644 --- a/src/control/textbutton.h +++ b/src/control/textbutton.h @@ -41,7 +41,11 @@ class TextButton : public DPushButton public: void mousePressEvent(QMouseEvent *) override; void mouseReleaseEvent(QMouseEvent *) override; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + void enterEvent(QEnterEvent *) override; +#else void enterEvent(QEvent *) override; +#endif void leaveEvent(QEvent *) override; void paintEvent(QPaintEvent *e) override; void focusOutEvent(QFocusEvent *) override; diff --git a/src/dsettings.cpp b/src/dsettings.cpp index 72f3deaa..9166b8da 100644 --- a/src/dsettings.cpp +++ b/src/dsettings.cpp @@ -6,7 +6,6 @@ #include "dsettings.h" #include -#include #include #include diff --git a/src/main.cpp b/src/main.cpp index 8583248d..d1fe9e74 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,13 +14,18 @@ #include #include #include +#include + #include -#include #include #include #include #include +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) +#include +#endif + DWIDGET_USE_NAMESPACE static QString g_appPath; //全局路径 static bool oldversion = false; //旧版本升级新版本 @@ -122,15 +127,17 @@ int main(int argc, char *argv[]) } else { window.move(m_dsettings->getOption("windowX").toInt() + 10, m_dsettings->getOption("windowY").toInt() + 10); } + +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + DApplicationSettings::setThemeType(DGuiApplicationHelper::ColorType::LightType); +#endif DGuiApplicationHelper::ColorType oldpalette = getThemeTypeSetting(); - DApplicationSettings savetheme(&app); + if (oldversion == true) { DGuiApplicationHelper::instance()->setPaletteType(oldpalette); } - // 20200330 主题记忆更改为规范代码 -// DApplicationSettings savetheme(&app); // Register debus service. dbus.registerObject("/com/deepin/calculator", &window, QDBusConnection::ExportScriptableSlots); window.show(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 40587435..558dc0a2 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -19,9 +19,9 @@ #include #include #include -#include #include #include +#include DGUI_USE_NAMESPACE diff --git a/src/views/memoryitemdelegate.cpp b/src/views/memoryitemdelegate.cpp index d282c724..054c6bd1 100644 --- a/src/views/memoryitemdelegate.cpp +++ b/src/views/memoryitemdelegate.cpp @@ -6,7 +6,7 @@ #include "memoryitemdelegate.h" #include -#include +#include MemoryItemDelegate::MemoryItemDelegate(QObject *parent) : QStyledItemDelegate(parent) diff --git a/src/views/memoryitemwidget.cpp b/src/views/memoryitemwidget.cpp index 4eae8cf9..499b773b 100644 --- a/src/views/memoryitemwidget.cpp +++ b/src/views/memoryitemwidget.cpp @@ -35,7 +35,7 @@ MemoryItemWidget::MemoryItemWidget(QWidget *parent) lay->addWidget(m_btnclean); lay->addWidget(m_btnplus); lay->addWidget(m_btnminus); - layV->setMargin(0); + lay->setContentsMargins(0, 0, 0, 0); layV->addWidget(m_label); layV->addStretch(); QFont font; @@ -62,7 +62,8 @@ MemoryItemWidget::MemoryItemWidget(QWidget *parent) // m_font.setPixelSize(18); } -void MemoryItemWidget::enterEvent(QEvent *event) +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +void MemoryItemWidget::enterEvent(QEnterEvent *event) { m_ishover = true; m_btnplus->setHidden(false); //进入时显示按钮 @@ -73,6 +74,17 @@ void MemoryItemWidget::enterEvent(QEvent *event) // this->setStyleSheet("background-color: rgb(245,245,245)"); QWidget::enterEvent(event); } +#else +void MemoryItemWidget::enterEvent(QEvent *event) +{ + m_ishover = true; + m_btnplus->setHidden(false); //进入时显示按钮 + m_btnminus->setHidden(false); + m_btnclean->setHidden(false); + update(); + QWidget::enterEvent(event); +} +#endif void MemoryItemWidget::leaveEvent(QEvent *event) { diff --git a/src/views/memoryitemwidget.h b/src/views/memoryitemwidget.h index 7328e262..a31c0151 100644 --- a/src/views/memoryitemwidget.h +++ b/src/views/memoryitemwidget.h @@ -26,7 +26,11 @@ class MemoryItemWidget : public QWidget public: explicit MemoryItemWidget(QWidget *parent = nullptr); ~MemoryItemWidget() override; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + void enterEvent(QEnterEvent *event) override; +#else void enterEvent(QEvent *event) override; +#endif void leaveEvent(QEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; diff --git a/src/views/memorywidget.cpp b/src/views/memorywidget.cpp index 3bf9f02b..998a4069 100644 --- a/src/views/memorywidget.cpp +++ b/src/views/memorywidget.cpp @@ -58,7 +58,7 @@ MemoryWidget::MemoryWidget(int mode, QWidget *parent) QHBoxLayout *layH = new QHBoxLayout(); lay->setSpacing(0); - lay->setMargin(0); + lay->setContentsMargins(0, 0, 0, 0); lay->addWidget(m_listwidget); m_listwidget->setFrameShape(QFrame::NoFrame); //设置边框类型,无边框 diff --git a/src/views/programmerarrowdelegate.cpp b/src/views/programmerarrowdelegate.cpp index 2cde7881..e881ebbf 100644 --- a/src/views/programmerarrowdelegate.cpp +++ b/src/views/programmerarrowdelegate.cpp @@ -6,7 +6,7 @@ #include "programmerarrowdelegate.h" #include -#include +#include #include "memorylistwidget.h" #include "programmeritemwidget.h" diff --git a/src/views/programmeritemwidget.cpp b/src/views/programmeritemwidget.cpp index 9355bdad..a1c2e6e0 100644 --- a/src/views/programmeritemwidget.cpp +++ b/src/views/programmeritemwidget.cpp @@ -7,6 +7,7 @@ #include #include +#include ProgrammerItemWidget::ProgrammerItemWidget(QString label, const int width, const QString path, QWidget *parent) : QWidget(parent) @@ -33,12 +34,12 @@ ProgrammerItemWidget::ProgrammerItemWidget(QString label, const int width, const hlayout->addWidget(m_label); m_label->setFixedWidth(width - 70); QFont font; - font.setWeight(2); + font.setWeight(QFont::Light); font.setPixelSize(14); m_label->setFont(font); hlayout->addStretch(); hlayout->addWidget(m_iconbtn); - hlayout->setMargin(0); + hlayout->setContentsMargins(0, 0, 0, 0); hlayout->setContentsMargins(0, 0, 16, 0); //图片比ui大10,此处边框比ui小5 setLayout(hlayout); this->installEventFilter(this); @@ -71,10 +72,10 @@ ProgrammerItemWidget::ProgrammerItemWidget(QString label, QWidget *parent) hlayout->addSpacing(2); hlayout->addWidget(m_label); QFont font; - font.setWeight(2); + font.setWeight(QFont::Light); font.setPixelSize(14); m_label->setFont(font); - hlayout->setMargin(0); + hlayout->setContentsMargins(0, 0, 0, 0); hlayout->setContentsMargins(0, 0, 21, 0); setLayout(hlayout); this->installEventFilter(this); @@ -95,12 +96,21 @@ ProgrammerItemWidget::~ProgrammerItemWidget() } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +void ProgrammerItemWidget::enterEvent(QEnterEvent *event) +{ + m_ishover = true; + update(); + QWidget::enterEvent(event); +} +#else void ProgrammerItemWidget::enterEvent(QEvent *event) { m_ishover = true; update(); QWidget::enterEvent(event); } +#endif void ProgrammerItemWidget::leaveEvent(QEvent *event) { diff --git a/src/views/programmeritemwidget.h b/src/views/programmeritemwidget.h index 62630cf6..971a9e51 100644 --- a/src/views/programmeritemwidget.h +++ b/src/views/programmeritemwidget.h @@ -28,7 +28,11 @@ class ProgrammerItemWidget : public QWidget ProgrammerItemWidget(QString label, const int width, const QString path, QWidget *parent = nullptr); ProgrammerItemWidget(QString label, QWidget *parent = nullptr); ~ProgrammerItemWidget() override; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + void enterEvent(QEnterEvent *event) override; +#else void enterEvent(QEvent *event) override; +#endif void leaveEvent(QEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; diff --git a/src/views/prolistdelegate.cpp b/src/views/prolistdelegate.cpp index 07423ec2..7b258d61 100644 --- a/src/views/prolistdelegate.cpp +++ b/src/views/prolistdelegate.cpp @@ -41,7 +41,7 @@ void ProListDelegate::currentfocusindex(QModelIndex index) void ProListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - painter->setRenderHint(QPainter::HighQualityAntialiasing, true); + painter->setRenderHint(QPainter::Antialiasing, true); const QString expression = index.data(ProListModel::ExpressionRole).toString(); ProListView *listview = qobject_cast(option.styleObject); QRect selectrect(option.rect.x() + LEFT_MARGIN, option.rect.y() + 2, 3, option.rect.height() - 4); //被选中行选中标记 diff --git a/src/views/simplelistdelegate.cpp b/src/views/simplelistdelegate.cpp index 765302a6..f044345a 100644 --- a/src/views/simplelistdelegate.cpp +++ b/src/views/simplelistdelegate.cpp @@ -435,22 +435,22 @@ void SimpleListDelegate::cutApart(const QString text, QString &linkNum, QString expStr = exp; return; } - if (exp.at(0) != "-") { + if (exp.at(0) != QChar(QStringLiteral("-").at(0))) { int index = 0; - while (exp.at(index) == "(") { + while (exp.at(index) == QChar('(')) { ++index; linkNum.append("("); - if (exp.at(index) == "-") + if (exp.at(index) == QChar(QStringLiteral("-").at(0))) linkNum.append("-"); } linkNum.append(list.at(0)); } else { linkNum.append("-"); - if (exp.at(1) == "(") + if (exp.at(1) == QChar('(')) linkNum.append("("); linkNum.append(list.at(0)); } - if (linkNum.at(linkNum.size() - 1) == "E") + if (linkNum.at(linkNum.size() - 1) == QChar('E')) linkNum = linkNum + exp.at(exp.indexOf("E") + 1) + list.at(1); expStr = text.right(text.length() - linkNum.length()); } diff --git a/src/views/simplelistmodel.cpp b/src/views/simplelistmodel.cpp index a147bb76..1231af3a 100644 --- a/src/views/simplelistmodel.cpp +++ b/src/views/simplelistmodel.cpp @@ -311,7 +311,7 @@ void SimpleListModel::radixChanged(int baseori, int basedest) } QString newtext = QString(); for (int i = 0; i < m_textorder.length(); i++) { - if (m_textorder.at(i) == "0") { + if (m_textorder.at(i) == QChar('0')) { newtext.append(m_numvec.first()); m_numvec.pop_front(); } else { @@ -332,7 +332,7 @@ void SimpleListModel::radixChanged(int baseori, int basedest) bool SimpleListModel::isNumber(QChar a) { - if (a.isDigit() || a == " " || a == "," || AtoF.contains(a)) + if (a.isDigit() || a == QChar(' ') || a == QChar(',') || AtoF.contains(a)) return true; else return false; diff --git a/src/widgets/basicmodule.cpp b/src/widgets/basicmodule.cpp index 9ab856e9..e4cfbf83 100644 --- a/src/widgets/basicmodule.cpp +++ b/src/widgets/basicmodule.cpp @@ -37,7 +37,7 @@ BasicModule::BasicModule(QWidget *parent) m_keypadLayout->addWidget(m_memorylistwidget); layout->setSpacing(0); - layout->setMargin(0); + //layout->setMargin(0); layout->setContentsMargins(0, 0, 0, 0); setMouseTracking(true); diff --git a/src/widgets/expressionbar.cpp b/src/widgets/expressionbar.cpp index 2940ae24..93d431be 100644 --- a/src/widgets/expressionbar.cpp +++ b/src/widgets/expressionbar.cpp @@ -13,6 +13,7 @@ #include #include #include +#include const int STANDPREC = 15; const int WIDGET_FIXHEIGHT = 147; @@ -54,7 +55,7 @@ ExpressionBar::ExpressionBar(QWidget *parent) QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(m_listView); layout->addWidget(m_inputEdit); - layout->setMargin(0); + layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); setFixedHeight(WIDGET_FIXHEIGHT); @@ -192,12 +193,13 @@ void ExpressionBar::enterSymbolEvent(const QString &text) // 2020316修复添加符号后光标问题 //添加符号后左侧数字不会多分隔符,只需考虑添加符号后输入框光标前的数字与添加前是否一致 - if (exp.mid(0, curPos).remove(QRegExp("[+-×÷,.%()E]")) == - m_inputEdit->text().mid(0, curPos).remove(QRegExp("[+-×÷,.%()E]"))) { + if (exp.mid(0, curPos).remove(QRegularExpression("[+-×÷,.%()E]")) == + m_inputEdit->text().mid(0, curPos).remove(QRegularExpression("[+-×÷,.%()E]"))) { QString sRegNum = "[+-×÷]"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); - rx.exactMatch(m_inputEdit->text().at(curPos)) + QRegularExpressionMatch match = rx.match(m_inputEdit->text().at(curPos)); + match.hasMatch() ? m_inputEdit->setCursorPosition(curPos + 1) : m_inputEdit->setCursorPosition(curPos); } else @@ -226,15 +228,16 @@ void ExpressionBar::enterPointEvent() if (curpos == 0) { m_inputEdit->insert("0."); } else { - if (exp.at(curpos - 1) == ".") + if (exp.at(curpos - 1) == QChar('.')) return; //20200716修改在)及%后小数点不可输入问题 // if (exp.at(curpos - 1) != ")" && exp.at(curpos - 1) != "%") { QString sRegNum = "[0-9,]+"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); - if (rx.exactMatch(exp.at(curpos - 1))) { - int index = exp.indexOf(QRegExp("[^0-9,]"), curpos); + QRegularExpressionMatch match = rx.match(exp.at(curpos - 1)); + if (match.hasMatch()) { + int index = exp.indexOf(QRegularExpression("[^0-9,]"), curpos); QString cut = exp.mid(curpos, index - curpos); int aftercurpos = cut.count(","); int before = exp.count(","); @@ -281,18 +284,18 @@ void ExpressionBar::enterBackspaceEvent() selcurPos <= selection.curpos + selection.selected.size()) selcurPos = selection.curpos; // 20200316选中部分光标置位问题修复 - if (seloldtext.mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length() == - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length()) + if (seloldtext.mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length() == + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length()) m_inputEdit->setCursorPosition(selcurPos); - else if (seloldtext.mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length() > - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length()) + else if (seloldtext.mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length() > + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length()) m_inputEdit->setCursorPosition(selcurPos + 1); else m_inputEdit->setCursorPosition(selcurPos - 1); } else { QString text = m_inputEdit->text(); int cur = m_inputEdit->cursorPosition(); - if (text.size() > 0 && cur > 0 && text[cur - 1] == ",") { + if (text.size() > 0 && cur > 0 && text[cur - 1] == QChar(',')) { text.remove(cur - 2, 2); m_inputEdit->setText(text); // 20200401 symbolFaultTolerance @@ -307,10 +310,11 @@ void ExpressionBar::enterBackspaceEvent() int newPro = m_inputEdit->text().count(","); if (cur > 0) { QString sRegNum = "[0-9]+"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); + QRegularExpressionMatch match = rx.match(text.at(cur - 1)); //退数字 - if (rx.exactMatch(text.at(cur - 1)) && proNumber > newPro) { + if (match.hasMatch() && proNumber > newPro) { if (text.mid(cur, text.length() - cur) == m_inputEdit->text().mid(m_inputEdit->text().length() - (text.length() - cur), text.length() - cur)) { m_inputEdit->setCursorPosition(cur - 2); } else @@ -323,7 +327,7 @@ void ExpressionBar::enterBackspaceEvent() } } //退小数点 - if (text.at(cur - 1) == ".") { + if (text.at(cur - 1) == QChar('.')) { if (text.mid(0, cur).count(",") != m_inputEdit->text().mid(0, cur).count(",")) m_inputEdit->setCursorPosition(cur); else @@ -412,7 +416,7 @@ void ExpressionBar::enterEqualEvent() QString newResult; // 20200403 bug-18971 表达式错误时输数字加等于再重新输入表达式历史记录错误表达式未被替换 // 20200407 超过16位小数未科学计数 - if (m_evaluator->error().isEmpty() && (oldtext.indexOf(QRegExp("[+-×÷.,%()E]")) != -1)) { + if (m_evaluator->error().isEmpty() && (oldtext.indexOf(QRegularExpression("[+-×÷.,%()E]")) != -1)) { if (ans.isNan() && !m_evaluator->isUserFunctionAssign()) return; //edit 20200413 for bug--19653 @@ -491,7 +495,7 @@ void ExpressionBar::enterEqualEvent() QString linkedExp = text.split("=").first(); int length = m_hisLink[i].linkageValue.length(); if (linkedExp.left(length) != m_hisLink[i].linkageValue || - (!isOperator(linkedExp.at(length)) && (linkedExp.at(length) != "("))) { + (!isOperator(linkedExp.at(length)) && (linkedExp.at(length) != QChar('(')))) { m_hisLink.remove(i); m_listDelegate->removeHisLink(); break; @@ -519,7 +523,7 @@ void ExpressionBar::enterEqualEvent() // m_inputEdit->clear(); } // 20200403 bug-18971 表达式错误时输数字加等于再重新输入表达式历史记录错误表达式未被替换 - if (m_evaluator->error().isEmpty() && (oldtext.indexOf(QRegExp("[+-×÷,.%()E]")) != -1)) + if (m_evaluator->error().isEmpty() && (oldtext.indexOf(QRegularExpression("[+-×÷,.%()E]")) != -1)) m_hisRevision = -1; m_listView->scrollToBottom(); m_isLinked = false; @@ -551,9 +555,10 @@ void ExpressionBar::enterPercentEvent() */ int diff = 0; //补数字后光标位移的距离 QString sRegNum = "[+-×÷/(^!%E]"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); - if (curpos == 0 || rx.exactMatch(exp.at(curpos - 1))) { + QRegularExpressionMatch match = rx.match(exp.at(curpos - 1)); + if (curpos == 0 || match.hasMatch()) { m_inputEdit->insert(""); diff = -1; } else @@ -565,7 +570,7 @@ void ExpressionBar::enterPercentEvent() addUndo(); if (!isAtEnd) { - if (newPro < proNumber && exp.at(curpos) != ",") { + if (newPro < proNumber && exp.at(curpos) != QChar(',')) { m_inputEdit->setCursorPosition(curpos + diff); } else { m_inputEdit->setCursorPosition(curpos + 1 + diff); @@ -675,7 +680,7 @@ void ExpressionBar::enterLeftBracketsEvent() addUndo(); if (!isAtEnd) { - if (newPro < proNumber && exp.at(curpos) != ",") { + if (newPro < proNumber && exp.at(curpos) != QChar(',')) { m_inputEdit->setCursorPosition(curpos); } else { m_inputEdit->setCursorPosition(curpos + 1); @@ -730,7 +735,7 @@ void ExpressionBar::enterRightBracketsEvent() addUndo(); if (!isAtEnd) { - if (newPro < proNumber && exp.at(curpos) != ",") { + if (newPro < proNumber && exp.at(curpos) != QChar(',')) { m_inputEdit->setCursorPosition(curpos); } else { m_inputEdit->setCursorPosition(curpos + 1); @@ -834,7 +839,7 @@ void ExpressionBar::copyClipboard2Result() .replace(QString::fromUtf8("——"), QString::fromUtf8("-")) .replace(QString::fromUtf8("%"), "%") .replace('/', QString::fromUtf8("÷")); //对粘贴板中的内容进行英替中 - text.remove(QRegExp("[^0-9+-×÷,.%()。]")); + text.remove(QRegularExpression("[^0-9+-×÷,.%()。]")); // text = pasteFaultTolerance(text); m_inputEdit->insert(text); @@ -886,11 +891,11 @@ void ExpressionBar::shear() addUndo(); m_isUndo = false; //设置剪切后光标位置 - if (text.mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length() == - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length()) + if (text.mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length() == + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length()) m_inputEdit->setCursorPosition(selcurPos); - else if (text.mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length() > - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length()) + else if (text.mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length() > + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length()) m_inputEdit->setCursorPosition(selcurPos + 1); else m_inputEdit->setCursorPosition(selcurPos - 1); @@ -921,11 +926,11 @@ void ExpressionBar::deleteText() m_isUndo = false; m_isResult = false; //设置删除后光标位置 - if (text.mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length() == - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length()) + if (text.mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length() == + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length()) m_inputEdit->setCursorPosition(selcurPos); - else if (text.mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length() > - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[+-×÷,.%()E]")).length()) + else if (text.mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length() > + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[+-×÷,.%()E]")).length()) m_inputEdit->setCursorPosition(selcurPos + 1); else m_inputEdit->setCursorPosition(selcurPos - 1); @@ -1041,7 +1046,7 @@ void ExpressionBar::clearLinkageCache(const QString &text, bool isequal) int length = m_hisLink.last().linkageValue.length(); if (linkedExp.left(length) != m_hisLink.last().linkageValue || (linkedExp.length() > length && !isOperator(linkedExp.at(length)) && - (linkedExp.at(length) != "("))) { + (linkedExp.at(length) != QChar('(')))) { m_hisLink.removeLast(); m_isLinked = false; m_listDelegate->removeHisLink(); @@ -1222,11 +1227,13 @@ QString ExpressionBar::pointFaultTolerance(const QString &text) .replace(QString::fromUtf8(")"), ")") .replace(QString::fromUtf8("——"), QString::fromUtf8("-")) .replace(QString::fromUtf8("%"), "%"); - QStringList list = reformatStr.split(QRegExp("[+-×÷(]")); //20200717去掉),否则下方)小数点容错无法进入; //此处可不考虑多符号问题 + QStringList list = reformatStr.split(QRegularExpression("[+-×÷(]")); //20200717去掉),否则下方)小数点容错无法进入; //此处可不考虑多符号问题 QStringList symbollist; for (int i = 0; i < reformatStr.size(); ++i) { - if (QRegExp("[+-×÷(]").exactMatch(reformatStr.at(i))) + QRegularExpression re("[+-×÷(]"); + if (re.match(reformatStr.at(i)).hasMatch()) { symbollist << reformatStr.at(i); + } } reformatStr.clear(); for (int i = 0; i < list.size(); ++i) { @@ -1252,7 +1259,7 @@ QString ExpressionBar::pointFaultTolerance(const QString &text) ++firstPoint; // oldText.replace(list[i], item); } else { - if (item.at(firstPoint - 1) == ")" || item.at(firstPoint - 1) == "%") { + if (item.at(firstPoint - 1) == QChar(')') || item.at(firstPoint - 1) == QChar('%')) { item.remove(firstPoint, 1); item.insert(firstPoint, "0."); //20200717)及%后小数点补0;与小数点输入处理一致 } @@ -1268,7 +1275,7 @@ QString ExpressionBar::pointFaultTolerance(const QString &text) } } for (int i = 0; i < reformatStr.size(); ++i) { - if (reformatStr[i] == "." && (i == 0 || !reformatStr[i - 1].isNumber())) { + if (reformatStr[i] == QChar('.') && (i == 0 || !reformatStr[i - 1].isNumber())) { reformatStr.insert(i, "0"); //补0操作,例:1+.2->1+0.2 ++i; } @@ -1286,7 +1293,7 @@ void ExpressionBar::expressionCheck() int sum = 0; for (int i = 0; i < exp.size(); ++i) { - if (exp[i] == ",") { + if (exp[i] == QChar(',')) { exp.remove(i, 1); --i; if (i + 1 < cur) { @@ -1299,7 +1306,7 @@ void ExpressionBar::expressionCheck() int a = 0; while (exp[i].isNumber()) { // fix for delete 0 behind "." - if (exp[i] == "0" && exp[i + 1] != "." && (i == 0 || exp[i - 1] != ".") && + if (exp[i] == QChar('0') && exp[i + 1] != QChar('.') && (i == 0 || exp[i - 1] != QChar('.')) && (i == 0 || !exp[i - 1].isNumber()) && (exp.size() == 1 || exp[i + 1].isNumber())) { exp.remove(i, 1); --i; @@ -1314,7 +1321,7 @@ void ExpressionBar::expressionCheck() num++;//光标后每个数字段长度 } sum = sum + ((num - 1) / 3 - (num - a - 1) / 3) + a; - if (exp[i] == "." && (i == 0 || !exp[i - 1].isNumber())) { + if (exp[i] == QChar('.') && (i == 0 || !exp[i - 1].isNumber())) { exp.insert(i, "0"); ++i; if (i < cur) @@ -1460,11 +1467,11 @@ void ExpressionBar::replaceSelection(QString text) selcurPos <= selection.curpos + selection.selected.size()) selcurPos = selection.curpos; // 20200313选中部分光标置位问题修复 - if (seloldtext.mid(0, selcurPos).remove(QRegExp("[,]")).length() == - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[,]")).length()) + if (seloldtext.mid(0, selcurPos).remove(QRegularExpression("[,]")).length() == + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[,]")).length()) m_inputEdit->setCursorPosition(selcurPos); - else if (seloldtext.mid(0, selcurPos).remove(QRegExp("[,]")).length() > - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[,]")).length()) + else if (seloldtext.mid(0, selcurPos).remove(QRegularExpression("[,]")).length() > + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[,]")).length()) m_inputEdit->setCursorPosition(selcurPos + 1); else m_inputEdit->setCursorPosition(selcurPos - 1); @@ -1481,8 +1488,8 @@ bool ExpressionBar::cancelLink(int index) if (m_hisLink[i].linkedItem == m_hisRevision) { QString exp = m_inputEdit->text(); exp = exp.replace(",", ""); - QStringList list = exp.split(QRegExp("[+-×÷()]")); - if (exp[0] == "-") + QStringList list = exp.split(QRegularExpression("[+-×÷()]")); + if (exp[0] == QChar(QStringLiteral("-").at(0))) list[0] = "-" + list[1]; QString linkvalue = m_hisLink[i].linkageValue; linkvalue = linkvalue.replace(",", ""); diff --git a/src/widgets/inputedit.cpp b/src/widgets/inputedit.cpp index 92eb6b5a..d6f157c8 100644 --- a/src/widgets/inputedit.cpp +++ b/src/widgets/inputedit.cpp @@ -121,7 +121,7 @@ QString InputEdit::expressionText() */ if (ans.length() > 17) { for (int i = 17; i < ans.length(); i++) { - if (ans.at(i) != "0") { + if (ans.at(i) != QChar('0')) { longnumber = true; break; } @@ -161,7 +161,7 @@ void InputEdit::setAnswer(const QString &str, const Quantity &ans) .replace('-', QString::fromUtf8("-")) .replace('*', QString::fromUtf8("×")) .replace(QString::fromUtf8("("), "(") - .replace(QString::fromUtf8(")"), ")"); + .replace(QString::fromUtf8(")"), QChar(')')); } /** @@ -417,7 +417,7 @@ void InputEdit::valueChangeFromProSyskeypad(const QString num) while (numend < text.length() && isNumber(text.at(numend))) { numend++; } - if (numstart > 0 && !isNumber(text.at(numstart - 1)) && number.at(0) == "-") { + if (numstart > 0 && !isNumber(text.at(numstart - 1)) && number.at(0) == QChar('-')) { text.remove(numstart, numend - numstart).insert(numstart, "(" + number); numstart += 2; } else @@ -427,7 +427,7 @@ void InputEdit::valueChangeFromProSyskeypad(const QString num) //重新找到numend numend = numstart < 0 ? 0 : numstart; while (numend < this->text().length() && (isNumber(this->text().at(numend)) || - ((numend == 0) && isNumber(text.at(1)) && text.at(0) == "-"))) { + ((numend == 0) && isNumber(text.at(1)) && text.at(0) == QChar('-')))) { numend++; } this->setCursorPosition(numend); @@ -486,12 +486,12 @@ void InputEdit::handleTextChanged(const QString &text) // .replace('x', QString::fromUtf8("×")) .replace('X', QString::fromUtf8("×")) .replace(QString::fromUtf8("("), "(") - .replace(QString::fromUtf8(")"), ")") + .replace(QString::fromUtf8(")"), QChar(')')) .replace(QString::fromUtf8("——"), QString::fromUtf8("-")) - .replace(QString::fromUtf8("%"), "%"); + .replace(QString::fromUtf8("%"), QChar('%')); multipleArithmetic(reformatStr); -// reformatStr.remove(QRegExp("[^0-9+-×÷,.%()e]")); +// reformatStr.remove(QRegularExpression("[^0-9+-×÷,.%()e]")); // reformatStr = pointFaultTolerance(reformatStr); // reformatStr = symbolFaultTolerance(reformatStr); setText(reformatStr); @@ -612,7 +612,7 @@ QString InputEdit::scanAndExec(int baseori, int basedest) } QString newtext = QString(); for (int i = 0; i < m_textorder.length(); i++) { - if (m_textorder.at(i) == "0") { + if (m_textorder.at(i) == QChar('0')) { newtext.append(m_numvec.first()); m_numvec.pop_front(); } else { @@ -630,7 +630,7 @@ QString InputEdit::pointFaultTolerance(const QString &text) { QString exp = text; QString oldText = text; - QStringList list = exp.split(QRegExp("[+-×÷/()]")); + QStringList list = exp.split(QRegularExpression("[+-×÷/()]")); for (int i = 0; i < list.size(); ++i) { QString item = list[i]; int firstPoint = item.indexOf("."); @@ -641,7 +641,7 @@ QString InputEdit::pointFaultTolerance(const QString &text) ++firstPoint; // oldText.replace(list[i], item); } else { - if (item.at(firstPoint - 1) == ")" || item.at(firstPoint - 1) == "%") { + if (item.at(firstPoint - 1) == QChar(')') || item.at(firstPoint - 1) == QChar('%')) { item.remove(firstPoint, 1); //原定义)及%右侧不能添加.,现在小数点输入事件中进行过补0操作,不会进入此判断 oldText.replace(list[i], item); } @@ -713,8 +713,8 @@ QString InputEdit::symbolFaultTolerance(const QString &text) if (expPos > 0) { //e后非+/- if (newText.length() > expPos + 1 && newText.at(expPos + 1) != QString::fromUtf8("-") && newText.at(expPos + 1) != QString::fromUtf8("+") - && newText.at(expPos + 1) != "-" && newText.at(expPos + 1) != "+") { - while (newText.length() > expPos + 1 && (newText.at(expPos + 1) == "(" || newText.at(expPos + 1) == ")")) { + && newText.at(expPos + 1) != QChar('-') && newText.at(expPos + 1) != QChar('+')) { + while (newText.length() > expPos + 1 && (newText.at(expPos + 1) == QChar('(') || newText.at(expPos + 1) == QChar(')'))) { newText.remove(expPos + 1, 1); //避免e后可输入()情况 } return newText; @@ -724,9 +724,9 @@ QString InputEdit::symbolFaultTolerance(const QString &text) while (newText.length() > expPos + 2 && newText.at(expPos + 2).isNumber() == false) { newText.remove(expPos + 2, 1); //e+/e-和数字间不可以插入非数字 } - int nextsymbolpos = newText.indexOf(QRegExp("[+-×÷/()]"), expPos + 2); //e+/e-右侧第一个符号 + int nextsymbolpos = newText.indexOf(QRegularExpression("[+-×÷/()]"), expPos + 2); //e+/e-右侧第一个符号 for (int i = expPos; i < (nextsymbolpos == -1 ? newText.length() : nextsymbolpos); i++) { - if (newText.at(i) == "." || newText.at(i) == QString::fromUtf8("。")) + if (newText.at(i) == QChar('.') || newText.at(i) == QString::fromUtf8("。")) newText.remove(i, 1); //去除从e到下一个运算符中的小数点 } } @@ -789,15 +789,15 @@ void InputEdit::BracketCompletion(QKeyEvent *e) int curs = this->cursorPosition(); int right = oldText.length() - curs; int leftLeftParen = oldText.left(curs).count("("); - int leftRightParen = oldText.left(curs).count(")"); + int leftRightParen = oldText.left(curs).count(QChar(')')); int rightLeftParen = oldText.right(right).count("("); - int rightrightParen = oldText.right(right).count(")"); + int rightrightParen = oldText.right(right).count(QChar(')')); //左右括号总数是否相等 - if (oldText.count("(") != oldText.count(")")) { + if (oldText.count("(") != oldText.count(QChar(')'))) { //光标左侧左括号大于右括号 if (leftLeftParen > leftRightParen) { if (leftLeftParen - leftRightParen + (rightLeftParen - rightrightParen) > 0) { - oldText.insert(curs, ")"); + oldText.insert(curs, QChar(')')); } else if (leftLeftParen - leftRightParen + (rightLeftParen - rightrightParen) < 0) { oldText.insert(curs, "("); } else { @@ -832,7 +832,7 @@ void InputEdit::multipleArithmetic(QString &text) index = text.indexOf("\n", i); if (index == 0) continue; - if (text.at(index - 1) == ")" || text.at(index - 1) == "%") + if (text.at(index - 1) == QChar(')') || text.at(index - 1) == QChar('%')) text.replace(index, 1, "×"); } } @@ -1046,7 +1046,7 @@ QPair InputEdit::getMemoryAnswer() QPair pair; QString expression; expression = symbolComplement(expressionText()).replace(QString::fromUtf8("+"), "+") - .replace(QString::fromUtf8("-"), "-") + .replace(QString::fromUtf8("-"), QChar('-')) .replace(QString::fromUtf8("×"), "*") .replace(QString::fromUtf8("÷"), "/") .replace(QString::fromUtf8(","), ""); @@ -1105,14 +1105,14 @@ QString InputEdit::symbolComplement(const QString exp) ++index; index = text.indexOf("(", index); } - index = text.indexOf(")", 0); + index = text.indexOf(QChar(')'), 0); while (index != -1) { if (index < text.length() - 1 && text.at(index + 1).isNumber()) { text.insert(index + 1, "×"); ++index; } ++index; - index = text.indexOf(")", index); + index = text.indexOf(QChar(')'), index); } return text; } @@ -1219,7 +1219,7 @@ void InputEdit::getCurrentCursorPositionNumber(const int pos) bool InputEdit::isNumber(QChar a) { - if (a.isDigit() || a == " " || a == "," || AtoF.contains(a)) + if (a.isDigit() || a == QChar(' ') || a == QChar(',') || AtoF.contains(a)) return true; else return false; @@ -1252,12 +1252,12 @@ QString InputEdit::formatExpression(const int &probase, const QString &text) { QString formattext = text; formattext.replace(QString::fromUtf8("+"), "+") - .replace(QString::fromUtf8("-"), "-") + .replace(QString::fromUtf8("-"), QChar('-')) .replace(QString::fromUtf8("×"), "*") .replace(QString::fromUtf8("÷"), "/") .replace(QString::fromUtf8(","), "") .replace(QString::fromUtf8(" "), "") - .replace("%", "mod"); + .replace(QChar('%'), "mod"); QString base = QString(); switch (probase) { diff --git a/src/widgets/memhiswidget.cpp b/src/widgets/memhiswidget.cpp index 65933b04..19b76a57 100644 --- a/src/widgets/memhiswidget.cpp +++ b/src/widgets/memhiswidget.cpp @@ -66,13 +66,12 @@ MemHisWidget::MemHisWidget(QWidget *parent) m_isshowM = true; m_Hlayout->addWidget(clearwidget); m_Hlayout->addSpacing(10); - m_Hlayout->setMargin(0); + m_Hlayout->setContentsMargins(0, 0, 0, 0); hwidget->setLayout(m_Hlayout); hwidget->setFixedHeight(48); m_Vlayout->addWidget(m_stackWidget); m_Vlayout->addWidget(hwidget); m_Vlayout->setSpacing(0); - m_Vlayout->setMargin(0); m_Vlayout->setContentsMargins(0, 0, 0, 0); this->setLayout(m_Vlayout); this->setContentsMargins(0, 0, 0, 0); diff --git a/src/widgets/probitwidget.cpp b/src/widgets/probitwidget.cpp index 86332c4c..55e4724e 100644 --- a/src/widgets/probitwidget.cpp +++ b/src/widgets/probitwidget.cpp @@ -30,7 +30,6 @@ ProBitWidget::ProBitWidget(QWidget *parent) hlay->addSpacing(1); hlay->addWidget(m_fourthbtn); hlay->addSpacing(1); - hlay->setMargin(0); hlay->setSpacing(4); hlay->setContentsMargins(0, 0, 0, 0); m_label->setFixedSize(70, 20); @@ -42,7 +41,6 @@ ProBitWidget::ProBitWidget(QWidget *parent) m_label->setFont(font); vlay->addLayout(hlay); vlay->addWidget(m_label); - vlay->setMargin(0); vlay->setSpacing(0); vlay->setContentsMargins(0, 0, 0, 0); } diff --git a/src/widgets/proexpressionbar.cpp b/src/widgets/proexpressionbar.cpp index e2a20afe..90fe1854 100644 --- a/src/widgets/proexpressionbar.cpp +++ b/src/widgets/proexpressionbar.cpp @@ -11,6 +11,7 @@ #include #include #include +#include const int LIST_HEIGHT = 35; //输入栏上方表达式的高度 const int INPUTEDIT_HEIGHT = 55; @@ -47,7 +48,7 @@ ProExpressionBar::ProExpressionBar(QWidget *parent) QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(m_listView); layout->addWidget(m_inputEdit); - layout->setMargin(0); + layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); initConnect(); @@ -84,15 +85,16 @@ bool ProExpressionBar::isnumber(QChar a) bool ProExpressionBar::judgeinput() { QString sRegNum = "[a-z]"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); SSelection selection = m_inputEdit->getSelection(); if (selection.selected != "") { //光标不在开头且光标左侧是字母或者光标右侧是字母 if ((selection.curpos > 0 && - rx.exactMatch(m_inputEdit->text().at(selection.curpos - 1))) - || (selection.curpos + selection.selected.size() < m_inputEdit->text().size() && rx.exactMatch(m_inputEdit->text().at(selection.curpos + selection.selected.size())))) { + rx.match(m_inputEdit->text().at(selection.curpos - 1)).hasMatch()) // 使用 match() 替代 exactMatch() + || (selection.curpos + selection.selected.size() < m_inputEdit->text().size() && + rx.match(m_inputEdit->text().at(selection.curpos + selection.selected.size())).hasMatch())) { // 使用 match() 替代 exactMatch() int funpos = -1; int rightfunpos = -1; for (int i = 0; i < m_funclist.size(); i++) { @@ -111,7 +113,7 @@ bool ProExpressionBar::judgeinput() } return true; } else { - if (m_inputEdit->cursorPosition() > 0 && rx.exactMatch(m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1))) { + if (m_inputEdit->cursorPosition() > 0 && rx.match(m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1)).hasMatch()) { // 使用 match() 替代 exactMatch() for (int i = 0; i < m_funclist.size(); i++) { //记录光标左侧离光标最近的函数位 int funpos = m_inputEdit->text().lastIndexOf(m_funclist[i], m_inputEdit->cursorPosition() - 1); @@ -123,6 +125,7 @@ bool ProExpressionBar::judgeinput() } } + void ProExpressionBar::enterNumberEvent(const QString &text) { if (!judgeinput()) @@ -214,12 +217,13 @@ void ProExpressionBar::enterSymbolEvent(const QString &text) // 2020316修复添加符号后光标问题 //添加符号后左侧数字不会多分隔符,只需考虑添加符号后输入框光标前的数字与添加前是否一致 - if (exp.mid(0, curPos).remove(QRegExp("[+-×÷/,%()\\s]")) == - m_inputEdit->text().mid(0, curPos).remove(QRegExp("[+-×÷/,%()\\s]"))) { + if (exp.mid(0, curPos).remove(QRegularExpression("[+-×÷/,%()\\s]")) == + m_inputEdit->text().mid(0, curPos).remove(QRegularExpression("[+-×÷/,%()\\s]"))) { QString sRegNum = "[+-×÷/]"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); - rx.exactMatch(m_inputEdit->text().at(curPos)) + QRegularExpressionMatch match = rx.match(m_inputEdit->text().at(curPos)); + match.hasMatch() ? m_inputEdit->setCursorPosition(curPos + 1) : m_inputEdit->setCursorPosition(curPos); qDebug() << curPos; @@ -237,9 +241,10 @@ void ProExpressionBar::enterSymbolEvent(const QString &text) void ProExpressionBar::enterBackspaceEvent() { - QString sRegNum = "[a-z]"; //20200811去除大写字母,否则E将被看作函数 - QRegExp rx; + QString sRegNum = "[a-zA-Z0-9]"; // 匹配字母和数字 + QRegularExpression rx; rx.setPattern(sRegNum); + SSelection selection = m_inputEdit->getSelection(); if (selection.selected != "") { selectedPartDelete(rx); @@ -249,39 +254,22 @@ void ProExpressionBar::enterBackspaceEvent() int funpos = -1; int i; int Sepold = text.count(",") + text.count(" "); - if (text.size() > 0 && cur > 1 && (text[cur - 1] == "," || (text[cur - 1] == " " && !text[cur - 2].isLower()))) { + + if (text.size() > 0 && cur > 1 && (text[cur - 1] == QChar(',') || (text[cur - 1] == QChar(' ') && !text[cur - 2].isLower()))) { text.remove(cur - 2, 2); m_inputEdit->setText(text); - // 20200401 symbolFaultTolerance m_inputEdit->setText(symbolFaultTolerance(m_inputEdit->text())); m_inputEdit->setCursorPosition(cur - 2); } else { - //退函数 - //光标不在开头且光标左侧是字母 - if (m_inputEdit->cursorPosition() > 0 && rx.exactMatch(m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1))) { + // 退函数 + if (m_inputEdit->cursorPosition() > 0 && rx.match(m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1)).hasMatch()) { for (i = 0; i < m_funclist.size(); i++) { - //记录光标左侧离光标最近的函数位 funpos = m_inputEdit->text().lastIndexOf(m_funclist[i], m_inputEdit->cursorPosition() - 1); - if (funpos != -1 && (funpos <= m_inputEdit->cursorPosition()) - && (m_inputEdit->cursorPosition() <= funpos + m_funclist[i].length())) - break; //光标在函数开头和函数结尾之间 - else - funpos = -1; - } - if (funpos != -1) { - m_inputEdit->setText(m_inputEdit->text().remove(funpos, m_funclist[i].length())); - int Sepnew = m_inputEdit->text().count(",") + m_inputEdit->text().count(" "); - m_inputEdit->setCursorPosition(funpos + Sepnew - Sepold + 1); - } - } else if (m_inputEdit->cursorPosition() > 1 && (text[cur - 1] == " " && text[cur - 2].isLower())) { - for (i = 0; i < m_funclist.size(); i++) { - //记录光标左侧离光标最近的函数位 - funpos = m_inputEdit->text().lastIndexOf(m_funclist[i], m_inputEdit->cursorPosition() - 2); - if (funpos != -1 && (funpos <= m_inputEdit->cursorPosition()) - && (m_inputEdit->cursorPosition() <= funpos + m_funclist[i].length() + 1)) - break; //光标在函数开头和函数结尾之间 - else + if (funpos != -1 && (funpos <= m_inputEdit->cursorPosition()) && (m_inputEdit->cursorPosition() <= funpos + m_funclist[i].length())) { + break; + } else { funpos = -1; + } } if (funpos != -1) { m_inputEdit->setText(m_inputEdit->text().remove(funpos, m_funclist[i].length())); @@ -292,19 +280,19 @@ void ProExpressionBar::enterBackspaceEvent() int proNumber = text.count(",") + text.count(" "); m_inputEdit->backspace(); int separator = proNumber - m_inputEdit->text().count(",") - m_inputEdit->text().count(" "); - // 20200401 symbolFaultTolerance m_inputEdit->setText(symbolFaultTolerance(m_inputEdit->text())); int newPro = m_inputEdit->text().count(",") + m_inputEdit->text().count(" "); if (cur > 0) { QString sRegNum1 = "[0-9A-F]+"; - QRegExp rx1; + QRegularExpression rx1; rx1.setPattern(sRegNum1); - //退数字 - if (rx1.exactMatch(text.at(cur - 1)) && proNumber > newPro) { + QRegularExpressionMatch match = rx1.match(text.at(cur - 1)); + if (match.hasMatch() && proNumber > newPro) { if (text.mid(cur, text.length() - cur) == m_inputEdit->text().mid(m_inputEdit->text().length() - (text.length() - cur), text.length() - cur)) { m_inputEdit->setCursorPosition(cur - 2); - } else + } else { m_inputEdit->setCursorPosition(cur - 1); + } } else { if (separator < 0) { m_inputEdit->setCursorPosition(cur - 1 - separator); @@ -316,6 +304,7 @@ void ProExpressionBar::enterBackspaceEvent() } } } + if (m_inputEdit->text().isEmpty() && m_listModel->rowCount(QModelIndex()) != 0) { emit clearStateChanged(true); m_isAllClear = true; @@ -330,6 +319,7 @@ void ProExpressionBar::enterBackspaceEvent() addUndo(); } + void ProExpressionBar::enterClearEvent() { Settings::instance()->proRotateCarry = "00"; @@ -375,7 +365,7 @@ void ProExpressionBar::enterEqualEvent() // 20200407 超过16位小数未科学计数 qDebug() << "m_evaluator->error()" << m_evaluator->error(); qDebug() << "ans" << m_inputEdit->expressionText(); - if (m_evaluator->error().isEmpty() && (exp.indexOf(QRegExp("[a-z+-×÷,%()\\s]")) != -1)) { + if (m_evaluator->error().isEmpty() && (exp.indexOf(QRegularExpression("[a-z+-×÷,%()\\s]")) != -1)) { if (ans.isNan() && !m_evaluator->isUserFunctionAssign()) { m_expression = exp + "=" + tr("Expression error"); m_listModel->updataList(m_expression, -1, true); @@ -478,9 +468,10 @@ void ProExpressionBar::enterNotEvent() // start edit for task-13519 // QString sRegNum1 = "[^0-9,.×÷)]"; QString sRegNum1 = "[^A-F^0-9,\\s)]"; - QRegExp rx1; + QRegularExpression rx1; rx1.setPattern(sRegNum1); - if (rx1.exactMatch(exp.at(curPos - 1))) + QRegularExpressionMatch match = rx1.match(exp.at(curPos - 1)); + if (match.hasMatch()) m_inputEdit->setText(oldText); else { QString newtext = m_inputEdit->text(); @@ -492,8 +483,8 @@ void ProExpressionBar::enterNotEvent() * 1. 负号在表达式的开头,如-1,-120等,视为一个整体的负数 * 2. 负号在左括号的后一位,如(-1, (-121等,也视为一个整体的负数 * 其中,当出现(-12)时,光标在右括号右侧时则会优先取到 ")",只有在右括号左侧才满足条件2*/ - if ((operatorpos == 0 && newtext.at(0) == "-") - || (operatorpos > 0 && newtext.at(operatorpos) == "-" && newtext.at(operatorpos - 1) == "(")) + if ((operatorpos == 0 && newtext.at(0) == QChar(QStringLiteral("-").at(0))) + || (operatorpos > 0 && newtext.at(operatorpos) == QChar(QStringLiteral("-").at(0)) && newtext.at(operatorpos - 1) == QChar('('))) operatorpos--; bool nooperator = false; @@ -518,9 +509,10 @@ void ProExpressionBar::enterNotEvent() //匹配到的(不在开头且(左侧是字母 QString sRegNum2 = "[a-z]"; - QRegExp latterrx; + QRegularExpression latterrx; latterrx.setPattern(sRegNum2); - if (operatorpos > 0 && latterrx.exactMatch(m_inputEdit->text().at(operatorpos - 1))) { + QRegularExpressionMatch match = latterrx.match(m_inputEdit->text().at(operatorpos - 1)); + if (operatorpos > 0 && match.hasMatch()) { int funpos = -1; //记录函数位 int i; for (i = 0; i < m_funclist.size(); i++) { @@ -589,9 +581,10 @@ void ProExpressionBar::enterOperatorEvent(const QString &text) */ int diff = 0; //补数字后光标位移的距离 QString sRegNum = "[+-×÷(]"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); - if (curpos == 0 || rx.exactMatch(exp.at(curpos - 1))) { + QRegularExpressionMatch match = rx.match(exp.at(curpos - 1)); + if (curpos == 0 || match.hasMatch()) { m_inputEdit->insert(zerotext); diff = 1; } else if (exp.at(curpos - 1).isLower()) { @@ -601,14 +594,14 @@ void ProExpressionBar::enterOperatorEvent(const QString &text) exp.replace(curpos - diff, diff, text); m_inputEdit->setText(exp); isreplace = true; - } else if (curpos > 1 && exp.at(curpos - 1) == " " && exp.at(curpos - 2).isLower()) { + } else if (curpos > 1 && exp.at(curpos - 1) == QChar(' ') && exp.at(curpos - 2).isLower()) { while (diff <= curpos - 2 && exp.at(curpos - 2 - diff).isLower()) { diff++; } exp.replace(curpos - 1 - diff, diff, text); m_inputEdit->setText(exp); isreplace = true; - } else if (exp.at(curpos - 1) == "%") { + } else if (exp.at(curpos - 1) == QChar('%')) { diff = 1; exp.replace(curpos - diff, diff, text); m_inputEdit->setText(exp); @@ -632,7 +625,7 @@ void ProExpressionBar::enterOperatorEvent(const QString &text) int newPro = m_inputEdit->text().left(curpos + length).count(",") + m_inputEdit->text().left(curpos + length).count(" "); diff += newPro - proNumber; if (!isAtEnd) { - if (newPro < proNumber && exp.at(curpos) != "," && exp.at(curpos) != " ") { + if (newPro < proNumber && exp.at(curpos) != QChar(',') && exp.at(curpos) != QChar(' ')) { m_inputEdit->setCursorPosition(curpos + length - 1 + diff); } else { m_inputEdit->setCursorPosition(curpos + length + diff); @@ -676,14 +669,16 @@ void ProExpressionBar::enterOppositeEvent() // QString sRegNum1 = "[^0-9,.×÷)]"; QString sRegNum1 = "[^A-F^0-9,\\s)]"; QString sRegNum2 = "[A-F0-9,]"; - QRegExp rx1, rx2; + QRegularExpression rx1, rx2; rx1.setPattern(sRegNum1); rx2.setPattern(sRegNum2); - if (rx1.exactMatch(exp.at(curPos - 1))) + QRegularExpressionMatch match1 = rx1.match(exp.at(curPos - 1)); + QRegularExpressionMatch match2 = rx2.match(exp.at(curPos - 2)); + if (match1.hasMatch()) return; - else if (exp.at(curPos - 1) == "0" && (curPos <= 1 || !rx2.exactMatch(exp.at(curPos - 2)))) { + else if (exp.at(curPos - 1) == QChar('0') && (curPos <= 1 || !match2.hasMatch())) { return; - } else if (curPos > 1 && exp.at(curPos - 1) == " " && exp.at(curPos - 2).isLower()) { + } else if (curPos > 1 && exp.at(curPos - 1) == QChar(' ')&& exp.at(curPos - 2).isLower()) { return; } else { QString newtext = m_inputEdit->text(); @@ -713,9 +708,10 @@ void ProExpressionBar::enterOppositeEvent() //匹配到的(不在开头且(左侧是字母 QString sRegNum3 = "[a-z]"; - QRegExp latterrx; + QRegularExpression latterrx; latterrx.setPattern(sRegNum3); - if (operatorpos > 0 && latterrx.exactMatch(m_inputEdit->text().at(operatorpos - 1))) { + QRegularExpressionMatch match = latterrx.match(m_inputEdit->text().at(operatorpos - 1)); + if (operatorpos > 0 && match.hasMatch()) { int funpos = -1; //记录函数位 int i; for (i = 0; i < m_funclist.size(); i++) { @@ -806,7 +802,7 @@ void ProExpressionBar::enterLeftBracketsEvent() addUndo(); if (!isAtEnd) { - if (newPro < proNumber && exp.at(curpos) != "," && exp.at(curpos) != " ") { + if (newPro < proNumber && exp.at(curpos) != QChar(',') && exp.at(curpos) != QChar('.')) { m_inputEdit->setCursorPosition(curpos); } else { m_inputEdit->setCursorPosition(curpos + 1); @@ -836,7 +832,7 @@ void ProExpressionBar::enterRightBracketsEvent() addUndo(); if (!isAtEnd) { - if (newPro < proNumber && exp.at(curpos) != "," && exp.at(curpos) != " ") { + if (newPro < proNumber && exp.at(curpos) != QChar(',') && exp.at(curpos) != QChar(' ')) { m_inputEdit->setCursorPosition(curpos); } else { m_inputEdit->setCursorPosition(curpos + 1); @@ -846,56 +842,85 @@ void ProExpressionBar::enterRightBracketsEvent() void ProExpressionBar::moveLeft() { + // 定义正则表达式,只匹配小写字母 QString sRegNum = "[a-z]"; - QRegExp rx; - rx.setPattern(sRegNum); - if (m_inputEdit->cursorPosition() > 0 && rx.exactMatch(m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1))) { - int funpos = -1; - int i; - for (i = 0; i < m_funclist.size(); i++) { - funpos = m_inputEdit->text().lastIndexOf(m_funclist[i], m_inputEdit->cursorPosition() - 1); - if (funpos != -1 && funpos + m_funclist[i].length() == m_inputEdit->cursorPosition()) - break; - else - funpos = -1; - } - if (funpos != -1) { - m_inputEdit->setCursorPosition(m_inputEdit->cursorPosition() - m_funclist[i].length()); + QRegularExpression rx(sRegNum); + + // 检查光标是否在文本的起始位置,避免越界 + if (m_inputEdit->cursorPosition() > 0) { + // 获取光标前一个字符 + QChar prevChar = m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1); + + // 使用正则表达式匹配光标前一个字符 + if (rx.match(prevChar).hasMatch()) { + int funpos = -1; + int i; + + // 遍历 m_funclist 查找是否为函数名 + for (i = 0; i < m_funclist.size(); i++) { + funpos = m_inputEdit->text().lastIndexOf(m_funclist[i], m_inputEdit->cursorPosition() - 1); + if (funpos != -1 && funpos + m_funclist[i].length() == m_inputEdit->cursorPosition()) { + // 找到函数,移动光标到函数的起始位置 + m_inputEdit->setCursorPosition(m_inputEdit->cursorPosition() - m_funclist[i].length()); + break; + } else { + funpos = -1; + } + } + + // 如果没有找到函数名,则光标向左移动一个字符 + if (funpos == -1) { + m_inputEdit->setCursorPosition(m_inputEdit->cursorPosition() - 1); + } } else { + // 如果前一个字符不符合正则,则光标直接向左移动一个字符 m_inputEdit->setCursorPosition(m_inputEdit->cursorPosition() - 1); } - } else { - m_inputEdit->setCursorPosition(m_inputEdit->cursorPosition() - 1); } m_inputEdit->setFocus(); } void ProExpressionBar::moveRight() { + // 定义正则表达式,只匹配小写字母 QString sRegNum = "[a-z]"; - QRegExp rx; - rx.setPattern(sRegNum); - if (!cursorPosAtEnd() && rx.exactMatch(m_inputEdit->text().at(m_inputEdit->cursorPosition()))) { - int funpos = -1; - int i; - for (i = 0; i < m_funclist.size(); i++) { - funpos = m_inputEdit->text().indexOf(m_funclist[i], m_inputEdit->cursorPosition()); - if (funpos != -1 && funpos == m_inputEdit->cursorPosition()) - break; - else - funpos = -1; - } - if (funpos != -1) { - m_inputEdit->setCursorPosition(m_inputEdit->cursorPosition() + m_funclist[i].length()); + QRegularExpression rx(sRegNum); + + // 检查光标是否已在文本的结尾位置,避免越界 + if (!cursorPosAtEnd()) { + // 获取当前光标位置的字符 + QChar currChar = m_inputEdit->text().at(m_inputEdit->cursorPosition()); + + // 使用正则表达式匹配当前字符 + if (rx.match(currChar).hasMatch()) { + int funpos = -1; + int i; + + // 遍历 m_funclist 查找是否为函数名 + for (i = 0; i < m_funclist.size(); i++) { + funpos = m_inputEdit->text().indexOf(m_funclist[i], m_inputEdit->cursorPosition()); + if (funpos != -1 && funpos == m_inputEdit->cursorPosition()) { + // 找到函数,光标向右移动函数的长度 + m_inputEdit->setCursorPosition(m_inputEdit->cursorPosition() + m_funclist[i].length()); + break; + } else { + funpos = -1; + } + } + + // 如果没有找到函数名,则光标向右移动一个字符 + if (funpos == -1) { + m_inputEdit->setCursorPosition(m_inputEdit->cursorPosition() + 1); + } } else { + // 如果当前字符不符合正则,则光标向右移动一个字符 m_inputEdit->setCursorPosition(m_inputEdit->cursorPosition() + 1); } - } else { - m_inputEdit->setCursorPosition(m_inputEdit->cursorPosition() + 1); } m_inputEdit->setFocus(); } + void ProExpressionBar::initTheme(int type) { m_listDelegate->setThemeType(type); @@ -968,19 +993,19 @@ void ProExpressionBar::copyClipboard2Result() //根据不同进制,对应不同的筛选规则 switch (Settings::instance()->programmerBase) { case 16: - text.remove(QRegExp("[G-Z]")); - list = text.split(QRegExp("[A-F0-9+-×÷()%\\s]")); + text.remove(QRegularExpression("[G-Z]")); + list = text.split(QRegularExpression("[A-F0-9+-×÷()%\\s]")); break; case 8: - text.remove(QRegExp("[8-9]")); - list = text.split(QRegExp("[0-7+-×÷()%\\s]")); + text.remove(QRegularExpression("[8-9]")); + list = text.split(QRegularExpression("[0-7+-×÷()%\\s]")); break; case 2: - text.remove(QRegExp("[2-9]")); - list = text.split(QRegExp("[0-1+-×÷()%\\s]")); + text.remove(QRegularExpression("[2-9]")); + list = text.split(QRegularExpression("[0-1+-×÷()%\\s]")); break; default: - list = text.split(QRegExp("[0-9+-×÷(),%\\s]")); + list = text.split(QRegularExpression("[0-9+-×÷(),%\\s]")); break; } for (int i = 0; i < list.size(); i++) { @@ -1035,7 +1060,7 @@ void ProExpressionBar::allElection() void ProExpressionBar::shear() { QString sRegNum = "[a-z]"; //20200811去除大写字母,否则E将被看作函数 - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); QString text = m_inputEdit->text(); QString selectText = m_inputEdit->selectedText(); @@ -1059,7 +1084,7 @@ void ProExpressionBar::shear() void ProExpressionBar::deleteText() { QString sRegNum = "[a-z]"; //20200811去除大写字母,否则E将被看作函数 - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); selectedPartDelete(rx); @@ -1165,11 +1190,11 @@ void ProExpressionBar::replaceSelection(QString text) selcurPos <= selection.curpos + selection.selected.size()) selcurPos = selection.curpos; // 20200313选中部分光标置位问题修复 - if (seloldtext.mid(0, selcurPos).remove(QRegExp("[,\\s]")).length() == - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[,\\s]")).length()) + if (seloldtext.mid(0, selcurPos).remove(QRegularExpression("[,\\s]")).length() == + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[,\\s]")).length()) m_inputEdit->setCursorPosition(selcurPos); - else if (seloldtext.mid(0, selcurPos).remove(QRegExp("[,\\s]")).length() > - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[,\\s]")).length()) + else if (seloldtext.mid(0, selcurPos).remove(QRegularExpression("[,\\s]")).length() > + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[,\\s]")).length()) m_inputEdit->setCursorPosition(selcurPos + 1); else m_inputEdit->setCursorPosition(selcurPos - 1); @@ -1250,7 +1275,7 @@ void ProExpressionBar::expressionCheck() int separator = 0; for (int i = 0; i < exp.size(); ++i) { - if (exp[i] == ",") { + if (exp[i] == QChar(',')) { exp.remove(i, 1); --i; if (i + 1 < cur) { @@ -1261,7 +1286,7 @@ void ProExpressionBar::expressionCheck() } for (int i = 0; i < exp.size(); ++i) { while (isnumber(exp[i])) { - if (exp[i] == "0" && (i == 0 || !isnumber(exp[i - 1])) && (exp.size() == 1 || isnumber(exp[i + 1]))) { + if (exp[i] == QChar('0') && (i == 0 || !isnumber(exp[i - 1])) && (exp.size() == 1 || isnumber(exp[i + 1]))) { exp.remove(i, 1); --i; if (i + 1 < cur) @@ -1425,16 +1450,20 @@ bool ProExpressionBar::isNumberOutOfRange(const QString &text) * @brief ProExpressionBar::selectedPartDelete * 选中部分的删除处理 */ -void ProExpressionBar::selectedPartDelete(const QRegExp &rx) +void ProExpressionBar::selectedPartDelete(const QRegularExpression &rx) { SSelection selection = m_inputEdit->getSelection(); int removepos = 0; //被删除位置 QString text = m_inputEdit->text(); QString seloldtext = text; + + // 预处理文本,减少对 exactMatch 的调用 + QString leftChar = (selection.curpos > 0) ? text.at(selection.curpos - 1) : QString(); + QString rightChar = (selection.curpos + selection.selected.size() < text.size()) ? text.at(selection.curpos + selection.selected.size()) : QString(); + //光标不在开头且光标左侧是字母或者光标右侧是字母 - if ((selection.curpos > 0 && - rx.exactMatch(m_inputEdit->text().at(selection.curpos - 1))) - || (selection.curpos + selection.selected.size() < m_inputEdit->text().size() && rx.exactMatch(m_inputEdit->text().at(selection.curpos + selection.selected.size())))) { + if ((selection.curpos > 0 && rx.match(leftChar).hasMatch()) || + (selection.curpos + selection.selected.size() < text.size() && rx.match(rightChar).hasMatch())) { int selleftfunlen = 0; //选中左侧一部分函数长度 int funpos = -1; int rightfunpos = -1; @@ -1481,7 +1510,7 @@ void ProExpressionBar::selectedPartDelete(const QRegExp &rx) m_inputEdit->setText(symbolFaultTolerance(m_inputEdit->text())); // 20200316选中部分光标置位问题修复 // 选中部分删除后重新设置光标位置,用删除前的光标位置左侧(分隔符新增的只会影响左侧的数字)的数字部分去除分隔符后减去删除后的光标位置左侧数字部分的长度,为光标位置的差。 - int diff = (seloldtext.mid(0, removepos).remove(QRegExp("[+-×÷/,%()\\s]")).length()) - m_inputEdit->text().mid(0, removepos).remove(QRegExp("[+-×÷/,%()\\s]")).length(); + int diff = (seloldtext.mid(0, removepos).remove(QRegularExpression("[+-×÷/,%()\\s]")).length()) - m_inputEdit->text().mid(0, removepos).remove(QRegularExpression("[+-×÷/,%()\\s]")).length(); if (0 == diff) { m_inputEdit->setCursorPosition(removepos); } else { @@ -1501,7 +1530,7 @@ bool ProExpressionBar::curposInNumber(int curpos) } if (isnumber(m_inputEdit->text().at(curpos))) { if (curpos < m_inputEdit->text().length() - 1 - && m_inputEdit->text().at(curpos) == " " && !isnumber(m_inputEdit->text().at(curpos + 1))) { + && m_inputEdit->text().at(curpos) == QChar(' ') && !isnumber(m_inputEdit->text().at(curpos + 1))) { return false; } else { return true; diff --git a/src/widgets/proexpressionbar.h b/src/widgets/proexpressionbar.h index ab527cc0..7e5103df 100644 --- a/src/widgets/proexpressionbar.h +++ b/src/widgets/proexpressionbar.h @@ -14,6 +14,7 @@ #include #include #include +#include DWIDGET_USE_NAMESPACE @@ -72,7 +73,7 @@ public slots: void expressionCheck(); QString symbolFaultTolerance(const QString &text); bool isNumberOutOfRange(const QString &text); - void selectedPartDelete(const QRegExp &rx); + void selectedPartDelete(const QRegularExpression &rx); bool curposInNumber(int curpos); void onSeparateChange();//数字将位数发生改变 diff --git a/src/widgets/programmodule.cpp b/src/widgets/programmodule.cpp index 7373d0f8..3cee10ba 100644 --- a/src/widgets/programmodule.cpp +++ b/src/widgets/programmodule.cpp @@ -51,7 +51,7 @@ ProgramModule::ProgramModule(QWidget *parent) vlay->addWidget(m_checkBtnKeypad); vlay->addWidget(m_stackWidget); vlay->setSpacing(0); - vlay->setMargin(0); + // vlay->setMargin(0); vlay->setContentsMargins(0, 0, 0, 0); //主题变换事件 @@ -391,9 +391,9 @@ void ProgramModule::handleKeypadButtonPress(int key) int right = 0; QString text = m_proExpressionBar->getInputEdit()->text(); for (int i = 0; i < text.length(); i++) { - if (text[i] == "(") + if (text[i] == QChar('(')) left ++; - else if (text[i] == ")") { + else if (text[i] == QChar(')')) { if (left > 0) left--; else @@ -609,9 +609,9 @@ void ProgramModule::handleKeypadButtonPressByspace(int key) int right = 0; QString text = m_proExpressionBar->getInputEdit()->text(); for (int i = 0; i < text.length(); i++) { - if (text[i] == "(") + if (text[i] == QChar('(')) left ++; - else if (text[i] == ")") { + else if (text[i] == QChar(')')) { if (left > 0) left--; else @@ -890,7 +890,7 @@ void ProgramModule::initArrowRectangle() m_shiftArrowListWidget->setAttribute(Qt::WA_TranslucentBackground, true); QFont font; font.setPixelSize(14); - font.setWeight(2); + font.setWeight(QFont::Light); QFontMetrics fm(font); int width = fm.boundingRect(tr("Rotate through carry circular shift")).width(); int itemwidth = width > 170 ? (width + 80) : 250; @@ -1341,9 +1341,9 @@ void ProgramModule::handleEditKeyPress(QKeyEvent *e) int right = 0; QString text = m_proExpressionBar->getInputEdit()->text(); for (int i = 0; i < text.length(); i++) { - if (text[i] == "(") + if (text[i] == QChar('(')) left ++; - else if (text[i] == ")") { + else if (text[i] == QChar(')')) { if (left > 0) left--; else @@ -1387,7 +1387,7 @@ void ProgramModule::resetArrowWidth() { QFont font; font.setPixelSize(14); - font.setWeight(2); + font.setWeight(QFont::Light); QFontMetrics fm(font); int width = fm.boundingRect(tr("Rotate through carry circular shift")).width(); int itemwidth = width > 170 ? (width + 90) : 250; diff --git a/src/widgets/scientificmodule.cpp b/src/widgets/scientificmodule.cpp index 6ffde408..22444d32 100644 --- a/src/widgets/scientificmodule.cpp +++ b/src/widgets/scientificmodule.cpp @@ -466,9 +466,9 @@ void scientificModule::handleEditKeyPress(QKeyEvent *e) int right = 0; QString text = m_sciexpressionBar->getInputEdit()->text(); for (int i = 0; i < text.length(); i++) { - if (text[i] == "(") + if (text[i] == QChar('(')) left ++; - else if (text[i] == ")") { + else if (text[i] == QChar(')')) { if (left > 0) left--; else @@ -694,9 +694,9 @@ void scientificModule::handleKeypadButtonPress(int key) int right = 0; QString text = m_sciexpressionBar->getInputEdit()->text(); for (int i = 0; i < text.length(); i++) { - if (text[i] == "(") + if (text[i] == QChar('(')) left ++; - else if (text[i] == ")") { + else if (text[i] == QChar(')')) { if (left > 0) left--; else @@ -922,9 +922,9 @@ void scientificModule::handleKeypadButtonPressByspace(int key) int right = 0; QString text = m_sciexpressionBar->getInputEdit()->text(); for (int i = 0; i < text.length(); i++) { - if (text[i] == "(") + if (text[i] == QChar('(')) left ++; - else if (text[i] == ")") { + else if (text[i] == QChar(')')) { if (left > 0) left--; else diff --git a/src/widgets/sciexpressionbar.cpp b/src/widgets/sciexpressionbar.cpp index b425def4..dcfa42fd 100644 --- a/src/widgets/sciexpressionbar.cpp +++ b/src/widgets/sciexpressionbar.cpp @@ -52,7 +52,7 @@ SciExpressionBar::SciExpressionBar(QWidget *parent) layout->addWidget(m_listView); layout->addWidget(m_inputEdit); - layout->setMargin(0); + //layout->setMargin(0); layout->setSpacing(0); initConnect(); @@ -115,9 +115,10 @@ void SciExpressionBar::enterNumberEvent(const QString &text) QString exp = m_inputEdit->text(); int curpos = m_inputEdit->cursorPosition(); QString sRegNum1 = "[πe]"; - QRegExp rx1; + QRegularExpression rx1; rx1.setPattern(sRegNum1); - if (curpos > 0 && rx1.exactMatch(exp.at(curpos - 1))) { + QRegularExpressionMatch match = rx1.match(exp.at(curpos - 1)); + if (curpos > 0 && match.hasMatch()) { m_inputEdit->insert("×" + text); } else m_inputEdit->insert(text); @@ -177,12 +178,13 @@ void SciExpressionBar::enterSymbolEvent(const QString &text) // 2020316修复添加符号后光标问题 //添加符号后左侧数字不会多分隔符,只需考虑添加符号后输入框光标前的数字与添加前是否一致 - if (exp.mid(0, curPos).remove(QRegExp("[+-×÷/,.%()E]")) == - m_inputEdit->text().mid(0, curPos).remove(QRegExp("[+-×÷/,.%()E]"))) { + if (exp.mid(0, curPos).remove(QRegularExpression("[+-×÷/,.%()E]")) == + m_inputEdit->text().mid(0, curPos).remove(QRegularExpression("[+-×÷/,.%()E]"))) { QString sRegNum = "[+-×÷/]"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); - rx.exactMatch(m_inputEdit->text().at(curPos)) + QRegularExpressionMatch match = rx.match(m_inputEdit->text().at(curPos)); + match.hasMatch() ? m_inputEdit->setCursorPosition(curPos + 1) : m_inputEdit->setCursorPosition(curPos); } else @@ -217,9 +219,10 @@ void SciExpressionBar::enterPercentEvent() */ int diff = 0; //补数字后光标位移的距离 QString sRegNum = "[+-×÷/(^!%E]"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); - if (curpos == 0 || rx.exactMatch(exp.at(curpos - 1))) { + QRegularExpressionMatch match = rx.match(exp.at(curpos - 1)); + if (curpos == 0 ||match.hasMatch()) { m_inputEdit->insert(""); diff = -1; } else @@ -232,7 +235,7 @@ void SciExpressionBar::enterPercentEvent() addUndo(); if (!isAtEnd) { - if (newPro < proNumber && exp.at(curpos) != ",") { + if (newPro < proNumber && exp.at(curpos) != QChar(',')) { m_inputEdit->setCursorPosition(curpos + diff); } else { m_inputEdit->setCursorPosition(curpos + 1 + diff); @@ -252,13 +255,14 @@ void SciExpressionBar::enterPointEvent() if (curpos == 0) { m_inputEdit->insert("0."); } else { - if (exp.at(curpos - 1) == ".") + if (exp.at(curpos - 1) == QChar('.')) return; QString sRegNum = "[0-9,]+"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); - if (rx.exactMatch(exp.at(curpos - 1))) { - int index = exp.indexOf(QRegExp("[^0-9,]"), curpos); + QRegularExpressionMatch match = rx.match(exp.at(curpos - 1)); + if (match.hasMatch()) { + int index = exp.indexOf(QRegularExpression("[^0-9,]"), curpos); QString cut = exp.mid(curpos, index - curpos); int aftercurpos = cut.count(","); int before = exp.count(","); @@ -284,17 +288,20 @@ void SciExpressionBar::enterPointEvent() void SciExpressionBar::enterBackspaceEvent() { QString sRegNum = "[a-z]"; //20200811去除大写字母,否则E将被看作函数 - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); SSelection selection = m_inputEdit->getSelection(); if (!selection.selected.isEmpty()) { QString text = m_inputEdit->text(); QString seloldtext = text; int removepos = 0; //被删除位置 + QRegularExpressionMatch match1 = rx.match(m_inputEdit->text().at(selection.curpos - 1)); + QRegularExpressionMatch match2 = rx.match(m_inputEdit->text().at(selection.curpos + selection.selected.size())); + //光标不在开头且光标左侧是字母或者光标右侧是字母 if ((selection.curpos > 0 && - rx.exactMatch(m_inputEdit->text().at(selection.curpos - 1))) - || (selection.curpos + selection.selected.size() < m_inputEdit->text().size() && rx.exactMatch(m_inputEdit->text().at(selection.curpos + selection.selected.size())))) { + match1.hasMatch()) + || (selection.curpos + selection.selected.size() < m_inputEdit->text().size() && match2.hasMatch())) { int selleftfunlen = 0; //选中左侧一部分函数长度 int funpos = -1; int rightfunpos = -1; @@ -340,11 +347,11 @@ void SciExpressionBar::enterBackspaceEvent() // 20200401 symbolFaultTolerance m_inputEdit->setText(m_inputEdit->symbolFaultTolerance(m_inputEdit->text())); // 20200316选中部分光标置位问题修复 - if ((seloldtext.mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) == - m_inputEdit->text().mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) + if ((seloldtext.mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) == + m_inputEdit->text().mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) m_inputEdit->setCursorPosition(removepos); - else if ((seloldtext.mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) > - m_inputEdit->text().mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) + else if ((seloldtext.mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) > + m_inputEdit->text().mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) m_inputEdit->setCursorPosition(removepos + 1); else m_inputEdit->setCursorPosition(removepos - 1); @@ -353,7 +360,7 @@ void SciExpressionBar::enterBackspaceEvent() int cur = m_inputEdit->cursorPosition(); int funpos = -1; int i; - if (text.size() > 0 && cur > 0 && text[cur - 1] == ",") { + if (text.size() > 0 && cur > 0 && text[cur - 1] == QChar(',')) { text.remove(cur - 2, 2); m_inputEdit->setText(text); // 20200401 symbolFaultTolerance @@ -362,7 +369,8 @@ void SciExpressionBar::enterBackspaceEvent() } else { //退函数 //光标不在开头且光标左侧是字母 - if (m_inputEdit->cursorPosition() > 0 && rx.exactMatch(m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1))) { + QRegularExpressionMatch match = rx.match(m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1)); + if (m_inputEdit->cursorPosition() > 0 && match.hasMatch()) { for (i = 0; i < m_funclist.size(); i++) { //记录光标左侧离光标最近的函数位 funpos = m_inputEdit->text().lastIndexOf(m_funclist[i], m_inputEdit->cursorPosition() - 1); @@ -385,10 +393,11 @@ void SciExpressionBar::enterBackspaceEvent() int newPro = m_inputEdit->text().count(","); if (cur > 0) { QString sRegNum1 = "[0-9]+"; - QRegExp rx1; + QRegularExpression rx1; rx1.setPattern(sRegNum1); //退数字 - if (rx1.exactMatch(text.at(cur - 1)) && proNumber > newPro) { + QRegularExpressionMatch match = rx1.match(text.at(cur - 1)); + if (match.hasMatch() && proNumber > newPro) { if (text.mid(cur, text.length() - cur) == m_inputEdit->text().mid(m_inputEdit->text().length() - (text.length() - cur), text.length() - cur)) { m_inputEdit->setCursorPosition(cur - 2); } else @@ -401,7 +410,7 @@ void SciExpressionBar::enterBackspaceEvent() } } //退小数点 - if (text.at(cur - 1) == ".") { + if (text.at(cur - 1) == QChar('.')) { if (text.mid(0, cur).count(",") != m_inputEdit->text().mid(0, cur).count(",")) m_inputEdit->setCursorPosition(cur); else @@ -471,7 +480,7 @@ void SciExpressionBar::enterEqualEvent() // 20200407 超过16位小数未科学计数 qDebug() << "m_evaluator->error()" << m_evaluator->error(); qDebug() << "ans" << m_inputEdit->expressionText(); - if (m_evaluator->error().isEmpty() && (exp.indexOf(QRegExp("[a-z+-×÷/.,%()πe^!]")) != -1)) { + if (m_evaluator->error().isEmpty() && (exp.indexOf(QRegularExpression("[a-z+-×÷/.,%()πe^!]")) != -1)) { if (ans.isNan() && !m_evaluator->isUserFunctionAssign()) { m_pair.first = false; m_expression = exp + "=" + tr("Expression error"); @@ -613,13 +622,14 @@ void SciExpressionBar::enterOperatorEvent(const QString &text) */ int diff = 0; //补数字后光标位移的距离 QString sRegNum = "[+-×÷/(^]"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); + QRegularExpressionMatch match = rx.match(exp.at(curpos - 1)); if (curpos == 0) { m_inputEdit->insert(zerotext); diff = 1; - } else if (rx.exactMatch(exp.at(curpos - 1))) { - if (exp.at(curpos - 1) == "^") { + } else if (match.hasMatch()) { + if (exp.at(curpos - 1) == QChar('^')) { m_inputEdit->insert(brackettext); diff = 2; } else { @@ -635,7 +645,7 @@ void SciExpressionBar::enterOperatorEvent(const QString &text) m_isUndo = false; if (!isAtEnd) { - if (newPro < proNumber && exp.at(curpos) != ",") { + if (newPro < proNumber && exp.at(curpos) != QChar(',')) { m_inputEdit->setCursorPosition(curpos + length - 1 + diff); } else { m_inputEdit->setCursorPosition(curpos + length + diff); @@ -671,7 +681,7 @@ void SciExpressionBar::enterFunctionEvent(const QString &text) addUndo(); if (!isAtEnd) { - if (newPro < proNumber && exp.at(curpos) != ",") { + if (newPro < proNumber && exp.at(curpos) != QChar(',')) { m_inputEdit->setCursorPosition(curpos + length); } else { m_inputEdit->setCursorPosition(curpos + length + 1); @@ -711,7 +721,7 @@ void SciExpressionBar::enterConstantEvent(const QString &text) addUndo(); if (!isAtEnd) { - if (newPro < proNumber && exp.at(curpos) != ",") { + if (newPro < proNumber && exp.at(curpos) != QChar(',')) { m_inputEdit->setCursorPosition(curpos + length - 1 + multi); } else { m_inputEdit->setCursorPosition(curpos + length + multi); @@ -749,7 +759,7 @@ void SciExpressionBar::enterBracketEvent(const int &type) addUndo(); if (!isAtEnd) { - if (newPro < proNumber && exp.at(curpos) != ",") { + if (newPro < proNumber && exp.at(curpos) != QChar(',')) { m_inputEdit->setCursorPosition(curpos); } else { m_inputEdit->setCursorPosition(curpos + 1); @@ -840,7 +850,7 @@ void SciExpressionBar::copyClipboard2Result() .replace(" ", ""); //对粘贴板中的内容进行英替中 //匹配函数方法 - QStringList list = text.split(QRegExp("[0-9+-×÷/()%^!E.,。]")); //正则表达式中为科学模式下可存在的非字母;函数中;无法被复制 + QStringList list = text.split(QRegularExpression("[0-9+-×÷/()%^!E.,。]")); //正则表达式中为科学模式下可存在的非字母;函数中;无法被复制 for (int i = 0; i < list.size(); i++) { QString item = list[i]; for (int j = 0; j < m_funclist.size(); j++) { @@ -892,7 +902,7 @@ void SciExpressionBar::allElection() void SciExpressionBar::shear() { QString sRegNum = "[a-z]"; //20200811去除大写字母,否则E将被看作函数 - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); SSelection selection = m_inputEdit->getSelection(); QString selectText = m_inputEdit->selectedText(); @@ -901,10 +911,17 @@ void SciExpressionBar::shear() int removepos = 0; //被删除位置 QString text = m_inputEdit->text(); QString seloldtext = text; + + // 光标不在开头且光标左侧是字母或者光标右侧是字母 + QChar leftChar = m_inputEdit->text().at(selection.curpos - 1); + QChar rightChar = m_inputEdit->text().at(selection.curpos + selection.selected.size()); + + QRegularExpressionMatch leftMatch = rx.match(QString(leftChar)); + QRegularExpressionMatch rightMatch = rx.match(QString(rightChar)); + //光标不在开头且光标左侧是字母或者光标右侧是字母 - if ((selection.curpos > 0 && - rx.exactMatch(m_inputEdit->text().at(selection.curpos - 1))) - || (selection.curpos + selection.selected.size() < m_inputEdit->text().size() && rx.exactMatch(m_inputEdit->text().at(selection.curpos + selection.selected.size())))) { + if ((selection.curpos > 0 && leftMatch.hasMatch()) || + (selection.curpos + selection.selected.size() < m_inputEdit->text().size() && rightMatch.hasMatch())) { int selleftfunlen = 0; //选中左侧一部分函数长度 int funpos = -1; int rightfunpos = -1; @@ -950,11 +967,11 @@ void SciExpressionBar::shear() // 20200401 symbolFaultTolerance m_inputEdit->setText(m_inputEdit->symbolFaultTolerance(m_inputEdit->text())); // 20200316选中部分光标置位问题修复 - if ((seloldtext.mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) == - m_inputEdit->text().mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) + if ((seloldtext.mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) == + m_inputEdit->text().mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) m_inputEdit->setCursorPosition(removepos); - else if ((seloldtext.mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) > - m_inputEdit->text().mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) + else if ((seloldtext.mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) > + m_inputEdit->text().mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) m_inputEdit->setCursorPosition(removepos + 1); else m_inputEdit->setCursorPosition(removepos - 1); @@ -982,16 +999,23 @@ void SciExpressionBar::shear() void SciExpressionBar::deleteText() { QString sRegNum = "[a-z]"; //20200811去除大写字母,否则E将被看作函数 - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); SSelection selection = m_inputEdit->getSelection(); int removepos = 0; //被删除位置 QString text = m_inputEdit->text(); QString seloldtext = text; + //光标不在开头且光标左侧是字母或者光标右侧是字母 - if ((selection.curpos > 0 && - rx.exactMatch(m_inputEdit->text().at(selection.curpos - 1))) - || (selection.curpos + selection.selected.size() < m_inputEdit->text().size() && rx.exactMatch(m_inputEdit->text().at(selection.curpos + selection.selected.size())))) { + QChar leftChar = m_inputEdit->text().at(selection.curpos - 1); + QChar rightChar = m_inputEdit->text().at(selection.curpos + selection.selected.size()); + + // 将 QChar 转换为 QString 进行匹配 + QRegularExpressionMatch leftMatch = rx.match(QString(leftChar)); + QRegularExpressionMatch rightMatch = rx.match(QString(rightChar)); + + if ((selection.curpos > 0 && leftMatch.hasMatch()) || + (selection.curpos + selection.selected.size() < m_inputEdit->text().size() && rightMatch.hasMatch())) { int selleftfunlen = 0; //选中左侧一部分函数长度 int funpos = -1; int rightfunpos = -1; @@ -1037,11 +1061,11 @@ void SciExpressionBar::deleteText() // 20200401 symbolFaultTolerance m_inputEdit->setText(m_inputEdit->symbolFaultTolerance(m_inputEdit->text())); // 20200316选中部分光标置位问题修复 - if ((seloldtext.mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) == - m_inputEdit->text().mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) + if ((seloldtext.mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) == + m_inputEdit->text().mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) m_inputEdit->setCursorPosition(removepos); - else if ((seloldtext.mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) > - m_inputEdit->text().mid(0, removepos).remove(QRegExp("[+-×÷/,.%()E]")).length()) + else if ((seloldtext.mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) > + m_inputEdit->text().mid(0, removepos).remove(QRegularExpression("[+-×÷/,.%()E]")).length()) m_inputEdit->setCursorPosition(removepos + 1); else m_inputEdit->setCursorPosition(removepos - 1); @@ -1141,45 +1165,63 @@ void SciExpressionBar::hisRevisionResults(const QModelIndex &index, Quantity ans bool SciExpressionBar::judgeinput() { - QString sRegNum = "[A-Za-z]"; - QRegExp rx; + QString sRegNum = "[A-Za-z]"; // 匹配字母(大小写字母) + QRegularExpression rx; rx.setPattern(sRegNum); SSelection selection = m_inputEdit->getSelection(); if (selection.selected != "") { - //光标不在开头且光标左侧是字母或者光标右侧是字母 - if ((selection.curpos > 0 && - rx.exactMatch(m_inputEdit->text().at(selection.curpos - 1))) - || (selection.curpos + selection.selected.size() < m_inputEdit->text().size() && rx.exactMatch(m_inputEdit->text().at(selection.curpos + selection.selected.size())))) { + // 光标不在开头且光标左侧是字母或者光标右侧是字母 + QChar leftChar = m_inputEdit->text().at(selection.curpos - 1); + QChar rightChar = m_inputEdit->text().at(selection.curpos + selection.selected.size()); + + // 使用 match() 和 hasMatch() 替代 exactMatch + QRegularExpressionMatch leftMatch = rx.match(QString(leftChar)); + QRegularExpressionMatch rightMatch = rx.match(QString(rightChar)); + + if ((selection.curpos > 0 && leftMatch.hasMatch()) || + (selection.curpos + selection.selected.size() < m_inputEdit->text().size() && rightMatch.hasMatch())) { + int funpos = -1; int rightfunpos = -1; + + // 检查左侧是否在函数中 for (int i = 0; i < m_funclist.size(); i++) { - //记录光标左侧离光标最近的函数位 funpos = m_inputEdit->text().lastIndexOf(m_funclist[i], selection.curpos - 1); if (funpos != -1 && (funpos <= selection.curpos) && (selection.curpos < funpos + m_funclist[i].length())) { - return false; //选中左侧在函数中 + return false; // 选中左侧在函数中 } } + + // 检查右侧是否在函数中 for (int j = 0; j < m_funclist.size(); j++) { - //记录离光标最近的右侧函数位 rightfunpos = m_inputEdit->text().lastIndexOf(m_funclist[j], selection.curpos + selection.selected.size() - 1); - if (rightfunpos != -1 && (rightfunpos + m_funclist[j].length() > selection.curpos + selection.selected.size())) - return false; //选中右侧在函数中 + if (rightfunpos != -1 && (rightfunpos + m_funclist[j].length() > selection.curpos + selection.selected.size())) { + return false; // 选中右侧在函数中 + } } } return true; } else { - if (m_inputEdit->cursorPosition() > 0 && rx.exactMatch(m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1))) { - for (int i = 0; i < m_funclist.size(); i++) { - //记录光标左侧离光标最近的函数位 - int funpos = m_inputEdit->text().lastIndexOf(m_funclist[i], m_inputEdit->cursorPosition() - 1); - if (funpos != -1 && (funpos < m_inputEdit->cursorPosition()) && (m_inputEdit->cursorPosition() < funpos + m_funclist[i].length())) - return false; //光标在函数中 + if (m_inputEdit->cursorPosition() > 0) { + QChar leftChar = m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1); + QRegularExpressionMatch leftMatch = rx.match(QString(leftChar)); + + if (leftMatch.hasMatch()) { + for (int i = 0; i < m_funclist.size(); i++) { + // 检查光标左侧是否在函数中 + int funpos = m_inputEdit->text().lastIndexOf(m_funclist[i], m_inputEdit->cursorPosition() - 1); + if (funpos != -1 && (funpos < m_inputEdit->cursorPosition()) && (m_inputEdit->cursorPosition() < funpos + m_funclist[i].length())) { + return false; // 光标在函数中 + } + } } } return true; } } + + void SciExpressionBar::initConnect() { connect(m_listView, &SimpleListView::obtainingHistoricalSimple, this, @@ -1259,12 +1301,19 @@ QString SciExpressionBar::pointFaultTolerance(const QString &text) .replace(CN_Underscore, CN_MIN) .replace(CN_Percent, EN_Percent) /*.replace('/', QString::fromUtf8("÷"))*/; //对内容进行英替中 - QStringList list = reformatStr.split(QRegExp("[+-×÷/(]")); //20200717去掉),否则下方)小数点容错无法进入 + QStringList list = reformatStr.split(QRegularExpression("[+-×÷/(]")); //20200717去掉),否则下方)小数点容错无法进入 QStringList symbollist; - for (int i = 0; i < reformatStr.size(); ++i) { - if (QRegExp("[+-×÷/(]").exactMatch(reformatStr.at(i))) + + // 预先定义正则表达式,避免在循环中重复创建 + QRegularExpression re("[+-×÷/(]"); + + // 遍历字符串并匹配符号 + for (int i = 0; i < reformatStr.length(); ++i) { + if (re.match(reformatStr[i]).hasMatch()) { symbollist << reformatStr.at(i); + } } + reformatStr.clear(); for (int i = 0; i < list.size(); ++i) { QString item = list[i]; @@ -1289,7 +1338,7 @@ QString SciExpressionBar::pointFaultTolerance(const QString &text) ++firstPoint; // oldText.replace(list[i], item); } else { - if (item.at(firstPoint - 1) == ")" || item.at(firstPoint - 1) == "%") { + if (item.at(firstPoint - 1) == QChar(')') || item.at(firstPoint - 1) == QChar('%')) { item.remove(firstPoint, 1); item.insert(firstPoint, "0."); //20200717)及%后小数点补0;与小数点输入处理一致 } @@ -1306,7 +1355,7 @@ QString SciExpressionBar::pointFaultTolerance(const QString &text) } for (int i = 0; i < reformatStr.size(); ++i) { //20200811避免e,π后的小数点补0 - if (reformatStr[i] == "." && (i == 0 || (!reformatStr[i - 1].isNumber() && reformatStr[i - 1] != "e" && reformatStr[i - 1] != "π"))) { + if (reformatStr[i] == QChar('.') && (i == 0 || (!reformatStr[i - 1].isNumber() && reformatStr[i - 1] != QChar('e') && reformatStr[i - 1] != QString("π")))) { reformatStr.insert(i, "0"); //补0操作,例:1+.2->1+0.2 ++i; } @@ -1323,7 +1372,7 @@ void SciExpressionBar::expressionCheck() int separator = 0; for (int i = 0; i < exp.size(); ++i) { - if (exp[i] == ",") { + if (exp[i] == QChar(',')) { exp.remove(i, 1); --i; if (i + 1 < cur) { @@ -1335,7 +1384,7 @@ void SciExpressionBar::expressionCheck() for (int i = 0; i < exp.size(); ++i) { while (exp[i].isNumber()) { // fix for delete 0 behind "." - if (exp[i] == "0" && exp[i + 1] != "." && (i == 0 || exp[i - 1] != ".") && + if (exp[i] == QChar('0') && exp[i + 1] != QChar('.') && (i == 0 || exp[i - 1] != QChar('.')) && (i == 0 || !exp[i - 1].isNumber()) && (exp.size() == 1 || exp[i + 1].isNumber())) { exp.remove(i, 1); --i; @@ -1344,7 +1393,7 @@ void SciExpressionBar::expressionCheck() } ++i; } - if (exp[i] == "." && (i == 0 || !exp[i - 1].isNumber())) { + if (exp[i] == QChar('.') && (i == 0 || !exp[i - 1].isNumber())) { exp.insert(i, "0"); ++i; if (i < cur) @@ -1357,7 +1406,7 @@ void SciExpressionBar::expressionCheck() bool SciExpressionBar::isnumber(QChar a) { - if (a.isDigit() || a == "." || a == "," || a == EN_PI || a == EN_Eular) + if (a.isDigit() || a == QChar(' ') || a == QChar(',') || a == EN_PI || a == EN_Eular) return true; else return false; @@ -1372,12 +1421,12 @@ bool SciExpressionBar::expressionInFunc(QString &text) int curPos = m_inputEdit->cursorPosition(); int epos = m_inputEdit->text().indexOf("E"); QString sRegNum = "[0-9,.E]"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); if (curPos == 0) { return false; } - if (m_inputEdit->text().length() > curPos && rx.exactMatch(m_inputEdit->text().at(curPos))) { + if (m_inputEdit->text().length() > curPos && rx.match(QString(m_inputEdit->text().at(curPos))).hasMatch()) { return false; } if (epos > -1 && epos == curPos - 1) { @@ -1385,9 +1434,11 @@ bool SciExpressionBar::expressionInFunc(QString &text) } // start edit for task-13519 QString sRegNum1 = "[^0-9,.)πe]"; - QRegExp rx1; + QRegularExpression rx1; rx1.setPattern(sRegNum1); - if (rx1.exactMatch(oldText.at(curPos - 1))) + QRegularExpressionMatch match = rx1.match(oldText.at(curPos - 1)); + + if (match.hasMatch()) return false; else { QString newtext = m_inputEdit->text(); @@ -1396,7 +1447,7 @@ bool SciExpressionBar::expressionInFunc(QString &text) newtext.lastIndexOf(QRegularExpression(QStringLiteral("[^0-9,.eπE]")), percentpos - 1); bool nooperator = false; - if (operatorpos > 0 && newtext.at(operatorpos - 1) == "E") + if (operatorpos > 0 && newtext.at(operatorpos - 1) == QChar('E')) operatorpos = newtext.mid(0, operatorpos - 1) .lastIndexOf(QRegularExpression(QStringLiteral("[^0-9,.eπE]")), percentpos - 1); @@ -1404,8 +1455,8 @@ bool SciExpressionBar::expressionInFunc(QString &text) * 1. 负号在表达式的开头,如-1,-120等,视为一个整体的负数 * 2. 负号在左括号的后一位,如(-1, (-121等,也视为一个整体的负数 * 其中,当出现(-12)时,光标在右括号右侧时则会优先取到 ")",只有在右括号左侧才满足条件2*/ - if ((operatorpos == 0 && newtext.at(0) == "-") - || (operatorpos > 0 && newtext.at(operatorpos) == "-" && newtext.at(operatorpos - 1) == "(")) + if ((operatorpos == 0 && newtext.at(0) == QChar(QStringLiteral("-").at(0))) + || (operatorpos > 0 && newtext.at(operatorpos) == QChar(QStringLiteral("-").at(0)) && newtext.at(operatorpos - 1) == QChar('('))) operatorpos--; if (operatorpos < 0) { @@ -1426,9 +1477,12 @@ bool SciExpressionBar::expressionInFunc(QString &text) //匹配到的(不在开头且(左侧是字母 QString sRegNum2 = "[A-Za-z]"; - QRegExp latterrx; - latterrx.setPattern(sRegNum2); - if (operatorpos > 0 && latterrx.exactMatch(m_inputEdit->text().at(operatorpos - 1))) { + // QRegularExpression latterrx; + // latterrx.setPattern(sRegNum2); + QRegularExpression latterrx(sRegNum2); + QRegularExpressionMatch match = latterrx.match(m_inputEdit->text().at(operatorpos - 1)); + + if (operatorpos > 0 && match.hasMatch()) { int funpos = -1; //记录函数位 int i; for (i = 0; i < m_funclist.size(); i++) { @@ -1549,11 +1603,11 @@ void SciExpressionBar::replaceSelection(QString text) selcurPos <= selection.curpos + selection.selected.size()) selcurPos = selection.curpos; // 20200313选中部分光标置位问题修复 - if (seloldtext.mid(0, selcurPos).remove(QRegExp("[,]")).length() == - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[,]")).length()) + if (seloldtext.mid(0, selcurPos).remove(QRegularExpression("[,]")).length() == + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[,]")).length()) m_inputEdit->setCursorPosition(selcurPos); - else if (seloldtext.mid(0, selcurPos).remove(QRegExp("[,]")).length() > - m_inputEdit->text().mid(0, selcurPos).remove(QRegExp("[,]")).length()) + else if (seloldtext.mid(0, selcurPos).remove(QRegularExpression("[,]")).length() > + m_inputEdit->text().mid(0, selcurPos).remove(QRegularExpression("[,]")).length()) m_inputEdit->setCursorPosition(selcurPos + 1); else m_inputEdit->setCursorPosition(selcurPos - 1); @@ -1576,9 +1630,10 @@ bool SciExpressionBar::isOperator(const QString &text) void SciExpressionBar::moveLeft() { QString sRegNum = "[A-Za-z]"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); - if (m_inputEdit->cursorPosition() > 0 && rx.exactMatch(m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1))) { + QRegularExpressionMatch match = rx.match(m_inputEdit->text().at(m_inputEdit->cursorPosition() - 1)); + if (m_inputEdit->cursorPosition() > 0 && match.hasMatch()) { int funpos = -1; int i; for (i = 0; i < m_funclist.size(); i++) { @@ -1602,9 +1657,10 @@ void SciExpressionBar::moveLeft() void SciExpressionBar::moveRight() { QString sRegNum = "[A-Za-z]"; - QRegExp rx; + QRegularExpression rx; rx.setPattern(sRegNum); - if (!cursorPosAtEnd() && rx.exactMatch(m_inputEdit->text().at(m_inputEdit->cursorPosition()))) { + QRegularExpressionMatch match = rx.match(m_inputEdit->text().at(m_inputEdit->cursorPosition())); + if (!cursorPosAtEnd() && match.hasMatch()) { int funpos = -1; int i; for (i = 0; i < m_funclist.size(); i++) {