fix: 修复和增强编辑器相关问题;

This commit is contained in:
寂静的羽夏 2025-05-09 12:18:17 +08:00
parent b49523e952
commit 1be1f7322f
11 changed files with 130 additions and 33 deletions

View File

@ -374,7 +374,6 @@ QString QConsoleWidget::currentCommandLine() const {
int QConsoleWidget::currentHeaderPos() const { return inpos_; } int QConsoleWidget::currentHeaderPos() const { return inpos_; }
void QConsoleWidget::write(const QString &message, const QTextCharFormat &fmt) { void QConsoleWidget::write(const QString &message, const QTextCharFormat &fmt) {
QTextCharFormat currfmt = currentCharFormat();
QTextCursor tc = textCursor(); QTextCursor tc = textCursor();
if (mode() == Input) { if (mode() == Input) {
@ -400,8 +399,8 @@ void QConsoleWidget::write(const QString &message, const QTextCharFormat &fmt) {
inpos_ = tc.position() - inpos_; inpos_ = tc.position() - inpos_;
// restore the edit pos // restore the edit pos
tc.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, editpos); tc.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, editpos);
tc.setCharFormat({});
setTextCursor(tc); setTextCursor(tc);
setCurrentCharFormat(currfmt);
} else { } else {
// in output mode messages are ed // in output mode messages are ed
QTextCursor tc1 = tc; QTextCursor tc1 = tc;
@ -416,6 +415,8 @@ void QConsoleWidget::write(const QString &message, const QTextCharFormat &fmt) {
textCursor().insertText(message, fmt); textCursor().insertText(message, fmt);
ensureCursorVisible(); ensureCursorVisible();
tc.setCharFormat({});
// restore cursor if needed // restore cursor if needed
if (needsRestore) if (needsRestore)
setTextCursor(tc); setTextCursor(tc);

View File

@ -707,7 +707,6 @@ void QHexRenderer::drawHex(QPainter *painter, const QRect &linerect,
if (!dis) if (!dis)
this->applyMetadata(textcursor, line, Hex); this->applyMetadata(textcursor, line, Hex);
this->applyBookMark(textcursor, line, Hex);
this->applySelection(textcursor, line, Hex); this->applySelection(textcursor, line, Hex);
this->applyCursorHex(textcursor, line); this->applyCursorHex(textcursor, line);
@ -718,24 +717,76 @@ void QHexRenderer::drawHex(QPainter *painter, const QRect &linerect,
ctx.palette.setColor(QPalette::Text, m_bytesColor); ctx.palette.setColor(QPalette::Text, m_bytesColor);
textdocument.documentLayout()->draw(painter, ctx); textdocument.documentLayout()->draw(painter, ctx);
this->applyBookMark(painter, textcursor, line, Hex);
painter->restore(); painter->restore();
} }
void QHexRenderer::applyBookMark(QTextCursor &textcursor, qsizetype line, void QHexRenderer::applyBookMark(QPainter *painter, QTextCursor &textcursor,
Factor factor) { qsizetype line, Factor factor) {
if (!m_document->lineHasBookMark(line)) if (!m_document->lineHasBookMark(line))
return; return;
painter->save();
auto pos = m_document->getLineBookmarksPos(line); auto pos = m_document->getLineBookmarksPos(line);
for (auto &item : pos) { 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(); auto charformat = textcursor.charFormat();
textcursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, auto textOutline = charformat.textOutline();
factor - 1);
charformat.setFontWeight(QFont::Bold); constexpr auto ALPHA = 180;
textcursor.setCharFormat(charformat);
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, 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); ctx.palette.setColor(QPalette::Text, m_bytesColor);
textdocument.documentLayout()->draw(painter, ctx); textdocument.documentLayout()->draw(painter, ctx);
this->applyBookMark(painter, textcursor, line, String);
painter->restore(); painter->restore();
} }

View File

@ -144,8 +144,10 @@ private:
qsizetype lineStart, qsizetype lineEnd, Factor factor, qsizetype lineStart, qsizetype lineEnd, Factor factor,
bool strikeOut, bool hasSelection) const; bool strikeOut, bool hasSelection) const;
void applyBookMark(QTextCursor &textcursor, qsizetype line, // added by wingsummer
Factor factor); // added by wingsummer void applyBookMark(QPainter *painter, QTextCursor &textcursor,
qsizetype line, Factor factor);
void applyCursorAscii(QTextCursor &textcursor, qsizetype line) const; void applyCursorAscii(QTextCursor &textcursor, qsizetype line) const;
void applyCursorHex(QTextCursor &textcursor, qsizetype line) const; void applyCursorHex(QTextCursor &textcursor, qsizetype line) const;
void drawAddress(QPainter *painter, const QRect &linerect, qsizetype line); void drawAddress(QPainter *painter, const QRect &linerect, qsizetype line);

View File

@ -537,10 +537,11 @@ bool QHexView::copy(bool hex) {
qreal QHexView::fontSize() const { return m_fontSize; } qreal QHexView::fontSize() const { return m_fontSize; }
void QHexView::setScaleRate(qreal rate) { void QHexView::setScaleRate(qreal rate) {
if (m_scaleRate > 0) { if (rate >= 0.2 && rate < 2.01) {
m_scaleRate = rate; m_scaleRate = rate;
setFontSize(fontSize()); setFontSize(fontSize());
emit scaleRateChanged(); emit scaleRateChanged();
update();
} }
} }
@ -709,7 +710,7 @@ void QHexView::focusOutEvent(QFocusEvent *e) {
void QHexView::wheelEvent(QWheelEvent *e) { void QHexView::wheelEvent(QWheelEvent *e) {
if (qApp->keyboardModifiers() == Qt::ControlModifier) { if (qApp->keyboardModifiers() == Qt::ControlModifier) {
auto dela = e->angleDelta().y() / 1200.0 / 2; auto dela = e->angleDelta().y() / 1200.0;
setScaleRate(scaleRate() + dela); setScaleRate(scaleRate() + dela);
return; return;
} }

View File

@ -90,14 +90,14 @@ void asDebugger::lineCallback(asIScriptContext *ctx) {
ctx->GetUserData(AsUserDataType::UserData_Timer)); ctx->GetUserData(AsUserDataType::UserData_Timer));
auto timeOutTime = reinterpret_cast<asPWORD>( auto timeOutTime = reinterpret_cast<asPWORD>(
ctx->GetUserData(AsUserDataType::UserData_TimeOut)); ctx->GetUserData(AsUserDataType::UserData_TimeOut));
auto mode = ScriptMachine::ConsoleMode(reinterpret_cast<asPWORD>( auto mode = reinterpret_cast<asPWORD>(
ctx->GetUserData(AsUserDataType::UserData_ContextMode))); ctx->GetUserData(AsUserDataType::UserData_ContextMode));
bool timeOut = false; bool timeOut = false;
if (timer < 0) { if (timer < 0) {
timeOut = true; timeOut = true;
} else { } else {
if (mode == ScriptMachine::DefineEvaluator) { if (mode == 0) {
timeOut = (now - timer) > 3000; // 3 s timeOut = (now - timer) > 3000; // 3 s
} else { } else {
if (timeOutTime) { if (timeOutTime) {
@ -106,11 +106,11 @@ void asDebugger::lineCallback(asIScriptContext *ctx) {
} }
} }
if (timeOut) { if (timeOut && mode) {
auto timeOut = tr("ScriptTimedOut"); auto timeOut = tr("ScriptTimedOut");
ScriptMachine::MessageInfo info; ScriptMachine::MessageInfo info;
info.message = timeOut; info.message = timeOut;
info.mode = mode; info.mode = ScriptMachine::ConsoleMode(mode);
info.type = ScriptMachine::MessageType::Error; info.type = ScriptMachine::MessageType::Error;
ScriptMachine::instance().outputMessage(info); ScriptMachine::instance().outputMessage(info);
ctx->Abort(); ctx->Abort();

View File

@ -180,6 +180,8 @@ void CodeEdit::applyEditorSetStyle() {
SearchReplaceWidget *CodeEdit::searchWidget() const { return m_searchWidget; } SearchReplaceWidget *CodeEdit::searchWidget() const { return m_searchWidget; }
void CodeEdit::setContentModified(bool b) { emit contentModified(b); }
void CodeEdit::resizeEvent(QResizeEvent *event) { void CodeEdit::resizeEvent(QResizeEvent *event) {
if (event) if (event)
WingCodeEdit::resizeEvent(event); WingCodeEdit::resizeEvent(event);

View File

@ -35,6 +35,9 @@ public:
SearchReplaceWidget *searchWidget() const; SearchReplaceWidget *searchWidget() const;
public:
void setContentModified(bool b);
signals: signals:
void contentModified(bool b); void contentModified(bool b);

View File

@ -288,9 +288,8 @@ ErrFile EditorView::newFile(size_t index) {
if (isCloneFile()) { if (isCloneFile()) {
return ErrFile::ClonedFile; return ErrFile::ClonedFile;
} }
if (!m_fileName.isEmpty()) {
_watcher.removePath(m_fileName); removeMonitorPaths();
}
auto istr = QString::number(index); auto istr = QString::number(index);
m_fileName = tr("Untitled") + istr; m_fileName = tr("Untitled") + istr;
this->setWindowTitle(m_fileName); this->setWindowTitle(m_fileName);
@ -327,9 +326,7 @@ ErrFile EditorView::openFile(const QString &filename) {
return ErrFile::Permission; return ErrFile::Permission;
} }
if (!m_fileName.isEmpty()) { removeMonitorPaths();
_watcher.removePath(m_fileName);
}
m_hex->setDocument(QSharedPointer<QHexDocument>(p)); m_hex->setDocument(QSharedPointer<QHexDocument>(p));
m_hex->setLockedFile(readonly); m_hex->setLockedFile(readonly);
@ -347,7 +344,7 @@ ErrFile EditorView::openFile(const QString &filename) {
tab->setIcon(Utilities::getIconFromFile(style(), m_fileName)); tab->setIcon(Utilities::getIconFromFile(style(), m_fileName));
tab->setToolTip(m_fileName); tab->setToolTip(m_fileName);
_watcher.addPath(m_fileName); addMonitorPath();
} }
return ErrFile::Success; return ErrFile::Success;
@ -385,9 +382,7 @@ ErrFile EditorView::openExtFile(const QString &ext, const QString &file) {
return ErrFile::Error; return ErrFile::Error;
} }
if (!m_fileName.isEmpty()) { removeMonitorPaths();
_watcher.removePath(m_fileName);
}
m_hex->setDocument(QSharedPointer<QHexDocument>(p)); m_hex->setDocument(QSharedPointer<QHexDocument>(p));
m_hex->setLockedFile(readonly); m_hex->setLockedFile(readonly);
@ -575,19 +570,20 @@ ErrFile EditorView::save(const QString &workSpaceName, const QString &path,
return ErrFile::Permission; return ErrFile::Permission;
} }
removeMonitorPaths();
if (doc->saveTo(&file, !isExport)) { if (doc->saveTo(&file, !isExport)) {
file.close(); file.close();
if (!isExport) { if (!isExport) {
if (!m_fileName.isEmpty()) {
_watcher.removePath(m_fileName);
}
m_fileName = QFileInfo(fileName).absoluteFilePath(); m_fileName = QFileInfo(fileName).absoluteFilePath();
m_isNewFile = false; m_isNewFile = false;
m_docType = DocumentType::File; m_docType = DocumentType::File;
doc->setDocSaved(); doc->setDocSaved();
_watcher.addPath(m_fileName);
} }
addMonitorPath();
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
adjustPermission(); adjustPermission();
#endif #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; } BookMarksModel *EditorView::bookmarksModel() const { return m_bookmarks; }
MetaDataModel *EditorView::metadataModel() const { return m_metadata; } MetaDataModel *EditorView::metadataModel() const { return m_metadata; }

View File

@ -536,7 +536,10 @@ private:
parent->addAction(a); parent->addAction(a);
} }
private:
void connectDocSavedFlag(EditorView *editor); void connectDocSavedFlag(EditorView *editor);
void removeMonitorPaths();
void addMonitorPath();
signals: signals:
void sigOnCutFile(); void sigOnCutFile();

View File

@ -57,6 +57,9 @@ ScriptEditor::ScriptEditor(QWidget *parent)
connect(m_editor, &CodeEdit::contentModified, this, connect(m_editor, &CodeEdit::contentModified, this,
[this]() { processTitle(); }); [this]() { processTitle(); });
connect(&_watcher, &QFileSystemWatcher::fileChanged, this,
&ScriptEditor::need2Reload);
this->setWidget(m_editor); this->setWidget(m_editor);
} }
@ -75,12 +78,30 @@ bool ScriptEditor::openFile(const QString &filename) {
} }
m_editor->setPlainText(QString::fromUtf8(f.readAll())); m_editor->setPlainText(QString::fromUtf8(f.readAll()));
f.close(); f.close();
if (!m_fileName.isEmpty()) {
_watcher.removePath(m_fileName);
}
m_fileName = filename; m_fileName = filename;
_watcher.addPath(m_fileName);
processTitle(); processTitle();
return true; return true;
} }
bool ScriptEditor::save(const QString &path) { 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 #ifdef Q_OS_LINUX
auto needAdjustFile = !QFile::exists(path); auto needAdjustFile = !QFile::exists(path);
#endif #endif

View File

@ -21,6 +21,8 @@
#include "Qt-Advanced-Docking-System/src/DockWidget.h" #include "Qt-Advanced-Docking-System/src/DockWidget.h"
#include "control/codeedit.h" #include "control/codeedit.h"
#include <QFileSystemWatcher>
class asIScriptEngine; class asIScriptEngine;
class ScriptEditor : public ads::CDockWidget { class ScriptEditor : public ads::CDockWidget {
@ -40,6 +42,8 @@ signals:
void onToggleMark(int line); void onToggleMark(int line);
void onFunctionTip(const QString &tip); void onFunctionTip(const QString &tip);
void need2Reload();
public slots: public slots:
void setReadOnly(bool b); void setReadOnly(bool b);
bool openFile(const QString &filename); bool openFile(const QString &filename);
@ -57,6 +61,8 @@ private:
private: private:
CodeEdit *m_editor = nullptr; CodeEdit *m_editor = nullptr;
QString m_fileName; QString m_fileName;
QFileSystemWatcher _watcher;
}; };
#endif // SCRIPTEDITOR_H #endif // SCRIPTEDITOR_H