From 1be1f7322f95de4cefb217fbf1c9b0936ad20616 Mon Sep 17 00:00:00 2001 From: wingsummer <1326224942@qq.com> Date: Fri, 9 May 2025 12:18:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=92=8C=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E7=BC=96=E8=BE=91=E5=99=A8=E7=9B=B8=E5=85=B3=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdparty/QConsoleWidget/QConsoleWidget.cpp | 5 +- 3rdparty/QHexView/document/qhexrenderer.cpp | 68 ++++++++++++++++++--- 3rdparty/QHexView/document/qhexrenderer.h | 6 +- 3rdparty/QHexView/qhexview.cpp | 5 +- src/class/asdebugger.cpp | 10 +-- src/control/codeedit.cpp | 2 + src/control/codeedit.h | 3 + src/control/editorview.cpp | 34 ++++++----- src/control/editorview.h | 3 + src/control/scripteditor.cpp | 21 +++++++ src/control/scripteditor.h | 6 ++ 11 files changed, 130 insertions(+), 33 deletions(-) diff --git a/3rdparty/QConsoleWidget/QConsoleWidget.cpp b/3rdparty/QConsoleWidget/QConsoleWidget.cpp index 4d2f0bf..50e6be5 100644 --- a/3rdparty/QConsoleWidget/QConsoleWidget.cpp +++ b/3rdparty/QConsoleWidget/QConsoleWidget.cpp @@ -374,7 +374,6 @@ QString QConsoleWidget::currentCommandLine() const { int QConsoleWidget::currentHeaderPos() const { return inpos_; } void QConsoleWidget::write(const QString &message, const QTextCharFormat &fmt) { - QTextCharFormat currfmt = currentCharFormat(); QTextCursor tc = textCursor(); if (mode() == Input) { @@ -400,8 +399,8 @@ void QConsoleWidget::write(const QString &message, const QTextCharFormat &fmt) { inpos_ = tc.position() - inpos_; // restore the edit pos tc.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, editpos); + tc.setCharFormat({}); setTextCursor(tc); - setCurrentCharFormat(currfmt); } else { // in output mode messages are ed QTextCursor tc1 = tc; @@ -416,6 +415,8 @@ void QConsoleWidget::write(const QString &message, const QTextCharFormat &fmt) { textCursor().insertText(message, fmt); ensureCursorVisible(); + tc.setCharFormat({}); + // restore cursor if needed if (needsRestore) setTextCursor(tc); diff --git a/3rdparty/QHexView/document/qhexrenderer.cpp b/3rdparty/QHexView/document/qhexrenderer.cpp index 80b07e0..acfc1a9 100644 --- a/3rdparty/QHexView/document/qhexrenderer.cpp +++ b/3rdparty/QHexView/document/qhexrenderer.cpp @@ -707,7 +707,6 @@ void QHexRenderer::drawHex(QPainter *painter, const QRect &linerect, if (!dis) this->applyMetadata(textcursor, line, Hex); - this->applyBookMark(textcursor, line, Hex); this->applySelection(textcursor, line, Hex); this->applyCursorHex(textcursor, line); @@ -718,24 +717,76 @@ void QHexRenderer::drawHex(QPainter *painter, const QRect &linerect, ctx.palette.setColor(QPalette::Text, m_bytesColor); textdocument.documentLayout()->draw(painter, ctx); + this->applyBookMark(painter, textcursor, line, Hex); painter->restore(); } -void QHexRenderer::applyBookMark(QTextCursor &textcursor, qsizetype line, - Factor factor) { +void QHexRenderer::applyBookMark(QPainter *painter, QTextCursor &textcursor, + qsizetype line, Factor factor) { if (!m_document->lineHasBookMark(line)) return; + painter->save(); + auto pos = m_document->getLineBookmarksPos(line); for (auto &item : pos) { - textcursor.setPosition(int((item % hexLineWidth()) * factor) + 2); + auto off = item % hexLineWidth(); + + qreal begin, width; + auto height = lineHeight(); + + // add some paddings + if (factor == Hex) { + begin = getCellWidth() * off * 3 + 1; + begin += getCellWidth() / 2; + width = getCellWidth() * 2 + 2; + textcursor.setPosition(off * factor); + } else { + begin = getCellWidth() * off + 1; + width = getCellWidth(); + textcursor.setPosition(m_cursor->currentColumn()); + } + auto charformat = textcursor.charFormat(); - textcursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, - factor - 1); - charformat.setFontWeight(QFont::Bold); - textcursor.setCharFormat(charformat); + auto textOutline = charformat.textOutline(); + + constexpr auto ALPHA = 180; + + if (textOutline.style() != Qt::NoPen) { + auto outColor = textOutline.color(); + outColor.setAlpha(ALPHA); + QPen pen(outColor, 1, Qt::DotLine); + painter->setPen(pen); + } else { + if (m_cursor->currentLine() == line && + m_cursor->currentColumn() == off) { + auto color = m_bytesColor; + color.setAlpha(ALPHA); + QPen pen(color, 1, Qt::DotLine); + painter->setPen(pen); + } else { + auto foreground = charformat.foreground(); + if (foreground.style() != Qt::NoBrush) { + auto textColor = foreground.color(); + textColor.setAlpha(ALPHA); + QPen pen(textColor, 1, Qt::DotLine); + painter->setPen(pen); + } else { + auto color = m_bytesColor; + color.setAlpha(ALPHA); + QPen pen(color, 1, Qt::DotLine); + painter->setPen(pen); + } + } + } + + painter->setBrush(Qt::transparent); + painter->setBackground(Qt::transparent); + painter->drawRect(begin, 0, width, height); } + + painter->restore(); } void QHexRenderer::drawString(QPainter *painter, const QRect &linerect, @@ -770,6 +821,7 @@ void QHexRenderer::drawString(QPainter *painter, const QRect &linerect, ctx.palette.setColor(QPalette::Text, m_bytesColor); textdocument.documentLayout()->draw(painter, ctx); + this->applyBookMark(painter, textcursor, line, String); painter->restore(); } diff --git a/3rdparty/QHexView/document/qhexrenderer.h b/3rdparty/QHexView/document/qhexrenderer.h index cc6bf9b..c1fa211 100644 --- a/3rdparty/QHexView/document/qhexrenderer.h +++ b/3rdparty/QHexView/document/qhexrenderer.h @@ -144,8 +144,10 @@ private: qsizetype lineStart, qsizetype lineEnd, Factor factor, bool strikeOut, bool hasSelection) const; - void applyBookMark(QTextCursor &textcursor, qsizetype line, - Factor factor); // added by wingsummer + // added by wingsummer + void applyBookMark(QPainter *painter, QTextCursor &textcursor, + qsizetype line, Factor factor); + void applyCursorAscii(QTextCursor &textcursor, qsizetype line) const; void applyCursorHex(QTextCursor &textcursor, qsizetype line) const; void drawAddress(QPainter *painter, const QRect &linerect, qsizetype line); diff --git a/3rdparty/QHexView/qhexview.cpp b/3rdparty/QHexView/qhexview.cpp index e8e95e1..50bd494 100644 --- a/3rdparty/QHexView/qhexview.cpp +++ b/3rdparty/QHexView/qhexview.cpp @@ -537,10 +537,11 @@ bool QHexView::copy(bool hex) { qreal QHexView::fontSize() const { return m_fontSize; } void QHexView::setScaleRate(qreal rate) { - if (m_scaleRate > 0) { + if (rate >= 0.2 && rate < 2.01) { m_scaleRate = rate; setFontSize(fontSize()); emit scaleRateChanged(); + update(); } } @@ -709,7 +710,7 @@ void QHexView::focusOutEvent(QFocusEvent *e) { void QHexView::wheelEvent(QWheelEvent *e) { if (qApp->keyboardModifiers() == Qt::ControlModifier) { - auto dela = e->angleDelta().y() / 1200.0 / 2; + auto dela = e->angleDelta().y() / 1200.0; setScaleRate(scaleRate() + dela); return; } diff --git a/src/class/asdebugger.cpp b/src/class/asdebugger.cpp index cd8dede..8fb203b 100644 --- a/src/class/asdebugger.cpp +++ b/src/class/asdebugger.cpp @@ -90,14 +90,14 @@ void asDebugger::lineCallback(asIScriptContext *ctx) { ctx->GetUserData(AsUserDataType::UserData_Timer)); auto timeOutTime = reinterpret_cast( ctx->GetUserData(AsUserDataType::UserData_TimeOut)); - auto mode = ScriptMachine::ConsoleMode(reinterpret_cast( - ctx->GetUserData(AsUserDataType::UserData_ContextMode))); + auto mode = reinterpret_cast( + ctx->GetUserData(AsUserDataType::UserData_ContextMode)); bool timeOut = false; if (timer < 0) { timeOut = true; } else { - if (mode == ScriptMachine::DefineEvaluator) { + if (mode == 0) { timeOut = (now - timer) > 3000; // 3 s } else { if (timeOutTime) { @@ -106,11 +106,11 @@ void asDebugger::lineCallback(asIScriptContext *ctx) { } } - if (timeOut) { + if (timeOut && mode) { auto timeOut = tr("ScriptTimedOut"); ScriptMachine::MessageInfo info; info.message = timeOut; - info.mode = mode; + info.mode = ScriptMachine::ConsoleMode(mode); info.type = ScriptMachine::MessageType::Error; ScriptMachine::instance().outputMessage(info); ctx->Abort(); diff --git a/src/control/codeedit.cpp b/src/control/codeedit.cpp index ae0f169..faff1f4 100644 --- a/src/control/codeedit.cpp +++ b/src/control/codeedit.cpp @@ -180,6 +180,8 @@ void CodeEdit::applyEditorSetStyle() { SearchReplaceWidget *CodeEdit::searchWidget() const { return m_searchWidget; } +void CodeEdit::setContentModified(bool b) { emit contentModified(b); } + void CodeEdit::resizeEvent(QResizeEvent *event) { if (event) WingCodeEdit::resizeEvent(event); diff --git a/src/control/codeedit.h b/src/control/codeedit.h index 490dc02..406abbe 100644 --- a/src/control/codeedit.h +++ b/src/control/codeedit.h @@ -35,6 +35,9 @@ public: SearchReplaceWidget *searchWidget() const; +public: + void setContentModified(bool b); + signals: void contentModified(bool b); diff --git a/src/control/editorview.cpp b/src/control/editorview.cpp index 9a50e29..3abbda0 100644 --- a/src/control/editorview.cpp +++ b/src/control/editorview.cpp @@ -288,9 +288,8 @@ ErrFile EditorView::newFile(size_t index) { if (isCloneFile()) { return ErrFile::ClonedFile; } - if (!m_fileName.isEmpty()) { - _watcher.removePath(m_fileName); - } + + removeMonitorPaths(); auto istr = QString::number(index); m_fileName = tr("Untitled") + istr; this->setWindowTitle(m_fileName); @@ -327,9 +326,7 @@ ErrFile EditorView::openFile(const QString &filename) { return ErrFile::Permission; } - if (!m_fileName.isEmpty()) { - _watcher.removePath(m_fileName); - } + removeMonitorPaths(); m_hex->setDocument(QSharedPointer(p)); m_hex->setLockedFile(readonly); @@ -347,7 +344,7 @@ ErrFile EditorView::openFile(const QString &filename) { tab->setIcon(Utilities::getIconFromFile(style(), m_fileName)); tab->setToolTip(m_fileName); - _watcher.addPath(m_fileName); + addMonitorPath(); } return ErrFile::Success; @@ -385,9 +382,7 @@ ErrFile EditorView::openExtFile(const QString &ext, const QString &file) { return ErrFile::Error; } - if (!m_fileName.isEmpty()) { - _watcher.removePath(m_fileName); - } + removeMonitorPaths(); m_hex->setDocument(QSharedPointer(p)); m_hex->setLockedFile(readonly); @@ -575,19 +570,20 @@ ErrFile EditorView::save(const QString &workSpaceName, const QString &path, return ErrFile::Permission; } + removeMonitorPaths(); + if (doc->saveTo(&file, !isExport)) { file.close(); if (!isExport) { - if (!m_fileName.isEmpty()) { - _watcher.removePath(m_fileName); - } m_fileName = QFileInfo(fileName).absoluteFilePath(); m_isNewFile = false; m_docType = DocumentType::File; doc->setDocSaved(); - _watcher.addPath(m_fileName); } + + addMonitorPath(); + #ifdef Q_OS_LINUX adjustPermission(); #endif @@ -738,6 +734,16 @@ void EditorView::connectDocSavedFlag(EditorView *editor) { }); } +void EditorView::removeMonitorPaths() { + auto files = _watcher.files(); + if (files.isEmpty()) { + return; + } + _watcher.removePaths(files); +} + +void EditorView::addMonitorPath() { _watcher.addPath(m_fileName); } + BookMarksModel *EditorView::bookmarksModel() const { return m_bookmarks; } MetaDataModel *EditorView::metadataModel() const { return m_metadata; } diff --git a/src/control/editorview.h b/src/control/editorview.h index 84a6be1..c4c0240 100644 --- a/src/control/editorview.h +++ b/src/control/editorview.h @@ -536,7 +536,10 @@ private: parent->addAction(a); } +private: void connectDocSavedFlag(EditorView *editor); + void removeMonitorPaths(); + void addMonitorPath(); signals: void sigOnCutFile(); diff --git a/src/control/scripteditor.cpp b/src/control/scripteditor.cpp index 4c01a7c..cab33e8 100644 --- a/src/control/scripteditor.cpp +++ b/src/control/scripteditor.cpp @@ -57,6 +57,9 @@ ScriptEditor::ScriptEditor(QWidget *parent) connect(m_editor, &CodeEdit::contentModified, this, [this]() { processTitle(); }); + connect(&_watcher, &QFileSystemWatcher::fileChanged, this, + &ScriptEditor::need2Reload); + this->setWidget(m_editor); } @@ -75,12 +78,30 @@ bool ScriptEditor::openFile(const QString &filename) { } m_editor->setPlainText(QString::fromUtf8(f.readAll())); f.close(); + + if (!m_fileName.isEmpty()) { + _watcher.removePath(m_fileName); + } + m_fileName = filename; + _watcher.addPath(m_fileName); + processTitle(); return true; } bool ScriptEditor::save(const QString &path) { + if (!m_fileName.isEmpty()) { + _watcher.removePath(m_fileName); + } + QScopeGuard guard([this, path]() { + if (path.isEmpty()) { + _watcher.addPath(m_fileName); + } else { + _watcher.addPath(path); + } + }); + #ifdef Q_OS_LINUX auto needAdjustFile = !QFile::exists(path); #endif diff --git a/src/control/scripteditor.h b/src/control/scripteditor.h index 16226fd..fe16e56 100644 --- a/src/control/scripteditor.h +++ b/src/control/scripteditor.h @@ -21,6 +21,8 @@ #include "Qt-Advanced-Docking-System/src/DockWidget.h" #include "control/codeedit.h" +#include + class asIScriptEngine; class ScriptEditor : public ads::CDockWidget { @@ -40,6 +42,8 @@ signals: void onToggleMark(int line); void onFunctionTip(const QString &tip); + void need2Reload(); + public slots: void setReadOnly(bool b); bool openFile(const QString &filename); @@ -57,6 +61,8 @@ private: private: CodeEdit *m_editor = nullptr; QString m_fileName; + + QFileSystemWatcher _watcher; }; #endif // SCRIPTEDITOR_H