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_; }
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);

View File

@ -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 charformat = textcursor.charFormat();
textcursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor,
factor - 1);
charformat.setFontWeight(QFont::Bold);
textcursor.setCharFormat(charformat);
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 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();
}

View File

@ -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);

View File

@ -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;
}

View File

@ -90,14 +90,14 @@ void asDebugger::lineCallback(asIScriptContext *ctx) {
ctx->GetUserData(AsUserDataType::UserData_Timer));
auto timeOutTime = reinterpret_cast<asPWORD>(
ctx->GetUserData(AsUserDataType::UserData_TimeOut));
auto mode = ScriptMachine::ConsoleMode(reinterpret_cast<asPWORD>(
ctx->GetUserData(AsUserDataType::UserData_ContextMode)));
auto mode = reinterpret_cast<asPWORD>(
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();

View File

@ -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);

View File

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

View File

@ -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<QHexDocument>(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<QHexDocument>(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; }

View File

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

View File

@ -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

View File

@ -21,6 +21,8 @@
#include "Qt-Advanced-Docking-System/src/DockWidget.h"
#include "control/codeedit.h"
#include <QFileSystemWatcher>
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