aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/widgets
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/widgets')
-rw-r--r--src/ui/widgets/TextEdit.cpp59
-rw-r--r--src/ui/widgets/TextEdit.h11
2 files changed, 68 insertions, 2 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
diff --git a/src/ui/widgets/TextEdit.h b/src/ui/widgets/TextEdit.h
index 6398cf34..2e2f949d 100644
--- a/src/ui/widgets/TextEdit.h
+++ b/src/ui/widgets/TextEdit.h
@@ -172,7 +172,7 @@ class TextEdit : public QWidget {
* @details put a * in front of current tabs title, if current textedit is
* modified
*/
- void SlotShowModified() const;
+ void SlotShowModified(bool) const;
/**
* @details close the current tab and decrease TabWidget->count by \a 1
@@ -193,6 +193,8 @@ class TextEdit : public QWidget {
void SlotSwitchTabDown() const;
private:
+ uint text_page_data_modified_count_ = 0; ///<
+
/**
* @details return just a filename stripped of a whole path
*
@@ -221,6 +223,13 @@ class TextEdit : public QWidget {
*/
void slot_remove_tab(int index);
+ /**
+ * @brief
+ *
+ * @param index
+ */
+ void slot_save_status_to_cache_for_revovery();
+
public slots:
/**