aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/TextEdit.cpp
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2023-02-04 06:40:17 +0000
committersaturneric <[email protected]>2023-02-04 06:50:30 +0000
commitf52b5f91bec98c7d45422d77cc09a0419d59b81b (patch)
tree391802daf890abe85ccd63a42257ef1619249e35 /src/ui/widgets/TextEdit.cpp
parentfix: solve open file issue in menu bar (diff)
downloadGpgFrontend-f52b5f91bec98c7d45422d77cc09a0419d59b81b.tar.gz
GpgFrontend-f52b5f91bec98c7d45422d77cc09a0419d59b81b.zip
feat: add cache recovery function
Diffstat (limited to '')
-rw-r--r--src/ui/widgets/TextEdit.cpp59
1 files changed, 58 insertions, 1 deletions
diff --git a/src/ui/widgets/TextEdit.cpp b/src/ui/widgets/TextEdit.cpp
index d8c4605d..5d10ece9 100644
--- a/src/ui/widgets/TextEdit.cpp
+++ b/src/ui/widgets/TextEdit.cpp
@@ -29,6 +29,11 @@
#include "ui/widgets/TextEdit.h"
#include <boost/format.hpp>
+#include <string>
+#include <tuple>
+#include <vector>
+
+#include "spdlog/spdlog.h"
namespace GpgFrontend::UI {
@@ -61,6 +66,8 @@ void TextEdit::SlotNewTab() {
page->GetTextPage()->setFocus();
connect(page->GetTextPage()->document(), &QTextDocument::modificationChanged,
this, &TextEdit::SlotShowModified);
+ connect(page->GetTextPage()->document(), &QTextDocument::contentsChanged,
+ this, &TextEdit::slot_save_status_to_cache_for_revovery);
}
void TextEdit::slotNewHelpTab(const QString& title, const QString& path) const {
@@ -88,6 +95,9 @@ void TextEdit::SlotOpenFile(const QString& path) {
connect(page->GetTextPage()->document(),
&QTextDocument::modificationChanged, this,
&TextEdit::SlotShowModified);
+ // connect to cache recovery fucntion
+ connect(page->GetTextPage()->document(), &QTextDocument::contentsChanged,
+ this, &TextEdit::slot_save_status_to_cache_for_revovery);
QApplication::setOverrideCursor(Qt::WaitCursor);
auto index = tab_widget_->addTab(page, stripped_name(path));
@@ -449,9 +459,17 @@ void TextEdit::SlotPrint() {
#endif
}
-void TextEdit::SlotShowModified() const {
+void TextEdit::SlotShowModified(bool changed) const {
+ // get current tab
int index = tab_widget_->currentIndex();
QString title = tab_widget_->tabText(index);
+
+ // if changed
+ if (!changed) {
+ tab_widget_->setTabText(index, title.remove(0, 2));
+ return;
+ }
+
// if doc is modified now, add leading * to title,
// otherwise remove the leading * from the title
if (CurTextPage()->GetTextPage()->document()->isModified()) {
@@ -580,4 +598,43 @@ void TextEdit::slot_file_page_path_changed(const QString& path) const {
tab_widget_->setTabText(index, mPath);
}
+void TextEdit::slot_save_status_to_cache_for_revovery() {
+ SPDLOG_DEBUG("catch text page modified event, count: {}",
+ text_page_data_modified_count_);
+ if (this->text_page_data_modified_count_++ % 3 != 0) return;
+
+ int tab_count = tab_widget_->count();
+ SPDLOG_DEBUG("current tabs count {}", tab_count);
+
+ std::vector<std::pair<int, std::string>> saved_pages;
+ std::vector<std::tuple<int, std::string, std::string>> unsaved_pages;
+
+ for (int i = 0; i < tab_count; i++) {
+ auto* target_page =
+ qobject_cast<PlainTextEditorPage*>(tab_widget_->widget(i));
+
+ // if this page is no textedit, there should be nothing to save
+ if (target_page == nullptr) {
+ continue;
+ }
+
+ auto* document = target_page->GetTextPage()->document();
+ auto tab_title = tab_widget_->tabText(i).toStdString();
+ if (!target_page->ReadDone() || !target_page->isEnabled() ||
+ !document->isModified()) {
+ auto file_path = target_page->GetFilePath().toStdString();
+ SPDLOG_DEBUG("saved page index: {}, tab title: {} tab file path: {}", i,
+ tab_title, file_path);
+
+ saved_pages.push_back({i, file_path});
+ continue;
+ }
+
+ auto raw_text = document->toRawText().toStdString();
+ SPDLOG_DEBUG("unsaved page index: {}, tab title: {} tab content: {}", i,
+ tab_title, raw_text);
+ unsaved_pages.push_back({i, tab_title, raw_text});
+ }
+}
+
} // namespace GpgFrontend::UI