Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/editor/dtextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4768,6 +4768,37 @@ void TextEdit::setBookMarkList(QList<int> bookMarkList)
m_listBookmark = bookMarkList;
}

QList<TextEdit::MarkReplaceInfo> TextEdit::getMarkColorInfo()
{
// 将当前标记操作转换为含绝对位置的替换信息,作为持久化载体
return convertMarkToReplace(m_markOperations);
}

void TextEdit::setMarkColorList(const QList<MarkReplaceInfo> &markInfo)
{
// 持久化数据仅含绝对位置,需用当前文档构造有效光标后再交由既有逻辑恢复
QList<QPair<MarkOperation, qint64>> markList;
const int docLen = document()->characterCount();
for (const auto &info : markInfo) {
MarkOperation markOpt;
markOpt.type = info.opt.type;
markOpt.color = info.opt.color;
markOpt.matchText = info.opt.matchText;

// 用当前文档构造有效光标,避免持久化数据中光标无文档导致恢复失败
markOpt.cursor = QTextCursor(document());
// 限制位置在当前文档范围内,文件被外部修改后不致越界
int start = qBound(0, static_cast<int>(info.start), docLen);
int end = qBound(0, static_cast<int>(info.end), docLen);
markOpt.cursor.setPosition(start);
markOpt.cursor.setPosition(end, QTextCursor::KeepAnchor);

markList.append(qMakePair(markOpt, info.time));
}

manualUpdateAllMark(markList);
}

void TextEdit::updateSaveIndex()
{
m_lastSaveIndex = m_pUndoStack->index();
Expand Down
12 changes: 12 additions & 0 deletions src/editor/dtextedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,18 @@ class TextEdit : public DPlainTextEdit
*/
void setBookMarkList(QList<int> bookMarkList);

/**
* @brief getMarkColorInfo 得到当前标记颜色信息(含绝对位置)
* @return 标记颜色信息列表
*/
QList<MarkReplaceInfo> getMarkColorInfo();

/**
* @brief setMarkColorList 从持久化数据恢复标记颜色,重建内存标记并刷新显示
* @param markInfo 标记颜色信息列表(含绝对位置,光标无需有效)
*/
void setMarkColorList(const QList<MarkReplaceInfo> &markInfo);

/**
* 更新上次保存时的撤销回收栈的索引值
*/
Expand Down
6 changes: 6 additions & 0 deletions src/editor/editwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

#include "editwrapper.h"
#include "leftareaoftextedit.h"
#include "drecentmanager.h"

Check warning on line 7 in src/editor/editwrapper.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "drecentmanager.h" not found.
#include "../startmanager.h"

#include <unistd.h>

Check warning on line 10 in src/editor/editwrapper.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <unistd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <QCoreApplication>
#include <QApplication>
Expand Down Expand Up @@ -939,6 +940,11 @@

m_pTextEdit->setTextFinished();

// 文件内容加载完成后,恢复该文件的标记颜色(位置型数据需待内容就绪)
QString markLocalPath = m_pTextEdit->getTruePath();
QList<TextEdit::MarkReplaceInfo> markColorInfo = StartManager::instance()->findMarkColor(markLocalPath);
m_pTextEdit->setMarkColorList(markColorInfo);

QStringList temFileList = Settings::instance()->settings->option("advance.editor.browsing_history_temfile")->value().toStringList();

for (int var = 0; var < temFileList.count(); ++var) {
Expand Down
6 changes: 6 additions & 0 deletions src/resources/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,12 @@
"hide": true,
"reset": false,
"default": ""
},
{
"key": "markcolor",
"hide": true,
"reset": false,
"default": ""
}
]
},
Expand Down
122 changes: 122 additions & 0 deletions src/startmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
#include <DWidgetUtil>
#include <QDebug>
#include <QScreen>
#include <QPropertyAnimation>

Check warning on line 14 in src/startmanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QPropertyAnimation> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DSettingsOption>

Check warning on line 15 in src/startmanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DSettingsOption> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DAboutDialog>

Check warning on line 16 in src/startmanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DAboutDialog> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QJsonArray>

Check warning on line 17 in src/startmanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QJsonArray> not found. Please note: Cppcheck does not need standard library headers to get proper results.
//#include <DSettings>

DWIDGET_USE_NAMESPACE
Expand All @@ -26,6 +27,8 @@

// 用于配置文件书签的标识
static const QString s_bookMarkKey = "advance.editor.bookmark";
// 用于配置文件标记颜色的标识
static const QString s_markColorKey = "advance.editor.markcolor";

StartManager *StartManager::m_instance = nullptr;

Expand Down Expand Up @@ -61,6 +64,8 @@
m_qlistTemFile = Settings::instance()->settings->option("advance.editor.browsing_history_temfile")->value().toStringList();
// 初始化书签信息记录表
initBookmark();
// 初始化标记颜色信息记录表
initMarkColor();

//按时间自动备份(5分钟)
m_pTimer = new QTimer;
Expand Down Expand Up @@ -190,6 +195,10 @@
m_bookmarkTable.remove(localPath);
}

//记录标记颜色
QList<TextEdit::MarkReplaceInfo> markColorList = wrapper->textEditor()->getMarkColorInfo();
recordMarkColor(localPath, markColorList);

//记录活动页
if (m_windows.value(var)->isActiveWindow()) {
if (wrapper == m_windows.value(var)->currentWrapper()) {
Expand Down Expand Up @@ -223,6 +232,8 @@
Settings::instance()->settings->option("advance.editor.browsing_history_temfile")->setValue(m_qlistTemFile);
// 备份书签信息
saveBookmark();
// 备份标记颜色信息
saveMarkColor();
}

int StartManager::recoverFile(Window *window)
Expand Down Expand Up @@ -650,6 +661,8 @@
if (m_windows.isEmpty()) {
// 保存书签信息
saveBookmark();
// 保存标记颜色信息
saveMarkColor();

QDir path = QDir::currentPath();
if (!path.exists()) {
Expand Down Expand Up @@ -832,6 +845,115 @@
return m_bookmarkTable.value(localPath);
}

/**
* @brief 主动更新记录文件 \a localPath 的标记颜色信息,空列表会清除该文件的记录。
* @param localPath 文件路径
* @param markColor 标记颜色信息(含绝对位置)
*/
void StartManager::recordMarkColor(const QString &localPath, const QList<TextEdit::MarkReplaceInfo> &markColor)
{
if (markColor.isEmpty()) {
// 无标记颜色,移除历史记录
m_markColorTable.remove(localPath);
} else {
m_markColorTable.insert(localPath, markColor);
}
}

/**
* @return 返回文件 \a localPath 的标记颜色记录
*/
QList<TextEdit::MarkReplaceInfo> StartManager::findMarkColor(const QString &localPath)
{
return m_markColorTable.value(localPath);
}

/**
* @brief 从配置文件中获取全局的标记颜色信息,包括已关闭的所有文件标记颜色,
* 会遍历每条记录并判断文件是否存在,若文件被删除或移动,则不再记录对应信息。
*/
void StartManager::initMarkColor()
{
// 遍历标记颜色信息列表
QStringList markColorInfoList = Settings::instance()->settings->value(s_markColorKey).toStringList();
for (const QString &markColorInfo : markColorInfoList) {
QJsonParseError readError;
QJsonDocument doc = QJsonDocument::fromJson(markColorInfo.toUtf8(), &readError);
if (QJsonParseError::NoError == readError.error
&& !doc.isNull()) {
QJsonObject obj = doc.object();
QString filePath = obj.value("localPath").toString();

// 判断文件是否仍存在,若不存在,则不保留标记颜色信息
if (!filePath.isEmpty()
&& QFileInfo::exists(filePath)) {
QList<TextEdit::MarkReplaceInfo> markList;
QJsonArray marksArray = obj.value("marks").toArray();
for (const QJsonValue &markValue : marksArray) {
QJsonObject markObj = markValue.toObject();
TextEdit::MarkReplaceInfo info;
info.opt.type = static_cast<TextEdit::MarkOperationType>(markObj.value("type").toInt());
info.opt.color = markObj.value("color").toString();
info.opt.matchText = markObj.value("matchText").toString();
info.start = markObj.value("start").toInt();
info.end = markObj.value("end").toInt();
info.time = static_cast<qint64>(markObj.value("time").toDouble());
markList.append(info);
}

if (!markList.isEmpty()) {
// 文件存在且标记颜色非空,缓存标记颜色信息
m_markColorTable.insert(filePath, markList);
}
}
}
}
}

/**
* @brief 将当前记录的所有文件的标记颜色信息转换为 Json 数据列表记录到配置信息中,
* 被删除或移动文件的标记颜色将被销毁。
*/
void StartManager::saveMarkColor()
{
QStringList recordInfo;
// 遍历记录
for (auto itr = m_markColorTable.begin(); itr != m_markColorTable.end();) {
if (!QFileInfo::exists(itr.key())
|| itr.value().isEmpty()) {
// 文件不存在或无标记颜色则销毁记录
itr = m_markColorTable.erase(itr);
} else {
QJsonObject obj;
obj.insert("localPath", itr.key());

// 遍历标记颜色信息并组合
QJsonArray marksArray;
const auto &markList = itr.value();
for (const TextEdit::MarkReplaceInfo &info : markList) {
QJsonObject markObj;
markObj.insert("type", static_cast<int>(info.opt.type));
markObj.insert("color", info.opt.color);
markObj.insert("matchText", info.opt.matchText);
markObj.insert("start", info.start);
markObj.insert("end", info.end);
// time 为 qint64,超过 int 范围时用 double 保证精度
markObj.insert("time", static_cast<double>(info.time));
marksArray.append(markObj);
}
obj.insert("marks", marksArray);

QJsonDocument doc(obj);
recordInfo.append(doc.toJson(QJsonDocument::Compact));

++itr;
}
}

// 将标记颜色信息保存至配置文件
Settings::instance()->settings->option(s_markColorKey)->setValue(recordInfo);
}

void StartManager::initBlockShutdown()
{
if (m_reply.value().isValid()) {
Expand Down
10 changes: 10 additions & 0 deletions src/startmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class StartManager : public QObject
// 查找文件对应的书签记录
QList<int> findBookmark(const QString &localPath);

// 主动更新记录标记颜色信息
void recordMarkColor(const QString &localPath, const QList<TextEdit::MarkReplaceInfo> &markColor);
// 查找文件对应的标记颜色记录
QList<TextEdit::MarkReplaceInfo> findMarkColor(const QString &localPath);

// 延迟释放堆内存
void delayMallocTrim();

Expand Down Expand Up @@ -105,6 +110,10 @@ public slots:
void initBookmark();
// 保存书签信息
void saveBookmark();
// 初始化标记颜色信息
void initMarkColor();
// 保存标记颜色信息
void saveMarkColor();

private:
static StartManager *m_instance;
Expand All @@ -119,6 +128,7 @@ public slots:
QScopedPointer<Entry> m_pEntry;
QStringList m_qlistTemFile; ///< 备份信息列表
QHash<QString, QList<int>> m_bookmarkTable; ///< 书签标记信息表
QHash<QString, QList<TextEdit::MarkReplaceInfo>> m_markColorTable; ///< 标记颜色信息表
QTimer *m_pTimer;
QBasicTimer m_DelayTimer; ///< 延迟备份定时器
QString m_blankFileDir; ///< 新建文件目录
Expand Down
5 changes: 5 additions & 0 deletions src/widgets/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,11 @@ bool Window::closeTab(const QString &filePath)
StartManager::instance()->recordBookmark(localPath, bookmarkInfo);
}

// 关闭标签页前,记录全局的标记颜色信息(空列表会清除历史记录)
QList<TextEdit::MarkReplaceInfo> markColorInfo = wrapper->textEditor()->getMarkColorInfo();
QString markLocalPath = wrapper->textEditor()->getTruePath();
StartManager::instance()->recordMarkColor(markLocalPath, markColorInfo);

if (isDraftFile) {
if (isModified) {
DDialog *dialog = createDialog(tr("Do you want to save this file?"), "");
Expand Down
Loading