From 08747295507ef5e35c1444c2359d01f477a05c48 Mon Sep 17 00:00:00 2001 From: Nicolas Mailloux Date: Tue, 9 Aug 2022 22:31:29 -0400 Subject: [PATCH] Implement highlights list dialog --- inkbox.pro | 3 + src/functions.h | 3 + src/reader/reader.cpp | 22 +++- src/reader/reader.h | 4 + src/reader/reader.ui | 34 ++++-- src/widgets/dialogs/generaldialog.cpp | 2 +- .../dialogs/reader/highlightslistdialog.cpp | 113 ++++++++++++++++++ .../dialogs/reader/highlightslistdialog.h | 34 ++++++ .../dialogs/reader/highlightslistdialog.ui | 102 ++++++++++++++++ 9 files changed, 308 insertions(+), 9 deletions(-) create mode 100644 src/widgets/dialogs/reader/highlightslistdialog.cpp create mode 100644 src/widgets/dialogs/reader/highlightslistdialog.h create mode 100644 src/widgets/dialogs/reader/highlightslistdialog.ui diff --git a/inkbox.pro b/inkbox.pro index b2f4423..0b7c70d 100644 --- a/inkbox.pro +++ b/inkbox.pro @@ -30,6 +30,7 @@ SOURCES += \ src/widgets/dialogs/library/bookoptionsdialog.cpp \ src/widgets/dialogs/brightnessdialog.cpp \ src/apps/calendarapp.cpp \ + src/widgets/dialogs/reader/highlightslistdialog.cpp \ src/widgets/dialogs/reader/textdialog.cpp \ src/widgets/reader/dictionarywidget.cpp \ src/encfs/encryptionmanager.cpp \ @@ -69,6 +70,7 @@ HEADERS += \ src/widgets/dialogs/library/bookoptionsdialog.h \ src/widgets/dialogs/brightnessdialog.h \ src/apps/calendarapp.h \ + src/widgets/dialogs/reader/highlightslistdialog.h \ src/widgets/dialogs/reader/textdialog.h \ src/widgets/reader/dictionarywidget.h \ src/encfs/encryptionmanager.h \ @@ -108,6 +110,7 @@ FORMS += \ src/widgets/dialogs/library/bookoptionsdialog.ui \ src/widgets/dialogs/brightnessdialog.ui \ src/apps/calendarapp.ui \ + src/widgets/dialogs/reader/highlightslistdialog.ui \ src/widgets/dialogs/reader/textdialog.ui \ src/widgets/reader/dictionarywidget.ui \ src/encfs/encryptionmanager.ui \ diff --git a/src/functions.h b/src/functions.h index 9df8dab..248c515 100644 --- a/src/functions.h +++ b/src/functions.h @@ -166,6 +166,9 @@ namespace global { static inline int pinnedBooksNumberPerRow = 4; static inline int pinnedBooksRowNumber = global::homePageWidget::pinnedBooksNumber / global::homePageWidget::pinnedBooksNumberPerRow; } + namespace highlightsListDialog { + inline QString bookPath; + } inline QString systemInfoText; inline bool forbidOpenSearchDialog; inline bool isN705 = false; diff --git a/src/reader/reader.cpp b/src/reader/reader.cpp index aba9ff7..4e1c4fb 100644 --- a/src/reader/reader.cpp +++ b/src/reader/reader.cpp @@ -1449,7 +1449,6 @@ void reader::alignAndHighlightText(int alignment) { // Highlight QString htmlText = ui->text->toHtml(); QJsonObject jsonObject = getHighlightsForBook(book_file); - qDebug() << jsonObject; int keyCount = 1; foreach(const QString& key, jsonObject.keys()) { if(keyCount <= 1) { @@ -2420,3 +2419,24 @@ void reader::unhighlightText() { highlightBookText(selected_text, book_file, true); alignAndHighlightText(textAlignment); } + +void reader::on_viewHighlightsBtn_clicked() +{ + log("Launching highlights list dialog for book '" + book_file + "'", className); + QJsonObject jsonObject = getHighlightsForBook(book_file); + if(jsonObject.isEmpty() or jsonObject.length() <= 1) { + global::toast::delay = 3000; + showToast("No highlights for this book"); + } + else { + global::highlightsListDialog::bookPath = book_file; + highlightsListDialog * highlightsListDialogWindow = new highlightsListDialog(this); + QObject::connect(highlightsListDialogWindow, &highlightsListDialog::destroyed, this, &reader::alignAndHighlightTextSlot); + QObject::connect(highlightsListDialogWindow, &highlightsListDialog::showToast, this, &reader::showToast); + highlightsListDialogWindow->setAttribute(Qt::WA_DeleteOnClose); + } +} + +void reader::alignAndHighlightTextSlot() { + alignAndHighlightText(textAlignment); +} diff --git a/src/reader/reader.h b/src/reader/reader.h index 9976769..ebf784f 100644 --- a/src/reader/reader.h +++ b/src/reader/reader.h @@ -7,6 +7,7 @@ #include "toast.h" #include "quit.h" #include "textdialog.h" +#include "highlightslistdialog.h" #include #include @@ -166,6 +167,8 @@ private slots: void unsetTextDialogLock(); void highlightText(); void unhighlightText(); + void on_viewHighlightsBtn_clicked(); + void alignAndHighlightTextSlot(); signals: void openBookFile(QString book, bool relativePath); @@ -177,6 +180,7 @@ private: toast * toastWindow; quit * quitWindow; textDialog * textDialogWindow; + highlightsListDialog * highlightsListDialogWindow; QGraphicsScene * graphicsScene; }; diff --git a/src/reader/reader.ui b/src/reader/reader.ui index fe4a4ee..3adad3c 100644 --- a/src/reader/reader.ui +++ b/src/reader/reader.ui @@ -102,10 +102,20 @@ 0 - - + + + + 0 + + + + + + + + - x of x + View highlights @@ -116,10 +126,20 @@ - - - - 0 + + + + x of x + + + + + + + QFrame::Plain + + + Qt::Vertical diff --git a/src/widgets/dialogs/generaldialog.cpp b/src/widgets/dialogs/generaldialog.cpp index 15755c7..0a8cd1c 100644 --- a/src/widgets/dialogs/generaldialog.cpp +++ b/src/widgets/dialogs/generaldialog.cpp @@ -780,7 +780,7 @@ void generalDialog::waitForGutenbergSearchDone() { void generalDialog::increaseSize() { - log("Resizing generalDialog", className); + log("Resizing", className); ui->topStackedWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); ui->mainStackedWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding); diff --git a/src/widgets/dialogs/reader/highlightslistdialog.cpp b/src/widgets/dialogs/reader/highlightslistdialog.cpp new file mode 100644 index 0000000..6b81015 --- /dev/null +++ b/src/widgets/dialogs/reader/highlightslistdialog.cpp @@ -0,0 +1,113 @@ +#include "highlightslistdialog.h" +#include "ui_highlightslistdialog.h" + +#include + +highlightsListDialog::highlightsListDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::highlightsListDialog) +{ + // Preventing outside interaction + this->setModal(true); + + ui->setupUi(this); + ui->listWidget->setFont(QFont("u001")); + + ui->okBtn->setProperty("type", "borderless"); + ui->cancelBtn->setProperty("type", "borderless"); + ui->selectAllBtn->setProperty("type", "borderless"); + ui->okBtn->setStyleSheet("font-size: 9pt; padding: 10px; font-weight: bold; background: lightGrey"); + ui->cancelBtn->setStyleSheet("font-size: 9pt; padding: 10px; font-weight: bold; background: lightGrey"); + ui->selectAllBtn->setStyleSheet("font-size: 9pt; padding: 10px"); + ui->listWidget->setStyleSheet("font-size: 10pt"); + ui->listWidget->setWordWrap(true); + + QJsonObject jsonObject = getHighlightsForBook(global::highlightsListDialog::bookPath); + int keyCount = 1; + + foreach(const QString& key, jsonObject.keys()) { + if(keyCount <= 1) { + keyCount++; + continue; + } + else { + QListWidgetItem * item = new QListWidgetItem(); + item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable); + item->setCheckState(Qt::Unchecked); + item->setText(jsonObject.value(key).toString()); + ui->listWidget->addItem(item); + } + } + + QTimer::singleShot(150, this, SLOT(increaseSize())); +} + +highlightsListDialog::~highlightsListDialog() +{ + delete ui; +} + +void highlightsListDialog::increaseSize() +{ + log("Resizing", className); + + ui->listWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + + QRect screenGeometry = QGuiApplication::screens()[0]->geometry(); + { + int wx = screenGeometry.width(); + int x = wx - 25; + int y = this->height() * determineYIncrease(); + this->setFixedWidth(x); + this->setFixedHeight(y); + } + { + this->adjustSize(); + int x = (screenGeometry.width() - this->width()) / 2; + int y = (screenGeometry.height() - this->height()) / 2; + this->move(x, y); + } + + this->show(); +} + +void highlightsListDialog::on_cancelBtn_clicked() +{ + highlightsListDialog::close(); +} + +void highlightsListDialog::on_okBtn_clicked() +{ + int itemsCount = 0; + int range = ui->listWidget->count(); + for(int i = 0; i < range; i++) { + if(ui->listWidget->item(i)->checkState() == Qt::Checked) { + QString highlight = ui->listWidget->item(i)->text(); + // The last argument indicates that we want to remove the specified highlight, not add it + highlightBookText(highlight, global::highlightsListDialog::bookPath, true); + itemsCount++; + } + } + + global::toast::delay = 3000; + if(itemsCount >= 1) { + if(itemsCount >= 2) { + emit showToast("Items deleted successfully"); + } + else { + emit showToast("Item deleted successfully"); + } + highlightsListDialog::close(); + } + else { + emit showToast("Please select items to delete"); + } +} + +void highlightsListDialog::on_selectAllBtn_clicked() +{ + int range = ui->listWidget->count(); + for(int i = 0; i < range; i++) { + ui->listWidget->item(i)->setCheckState(Qt::Checked); + } +} diff --git a/src/widgets/dialogs/reader/highlightslistdialog.h b/src/widgets/dialogs/reader/highlightslistdialog.h new file mode 100644 index 0000000..fb7aaf8 --- /dev/null +++ b/src/widgets/dialogs/reader/highlightslistdialog.h @@ -0,0 +1,34 @@ +#ifndef HIGHLIGHTSLISTDIALOG_H +#define HIGHLIGHTSLISTDIALOG_H + +#include + +#include "functions.h" + +namespace Ui { +class highlightsListDialog; +} + +class highlightsListDialog : public QDialog +{ + Q_OBJECT + +public: + QString className = this->metaObject()->className(); + explicit highlightsListDialog(QWidget *parent = nullptr); + ~highlightsListDialog(); + +private slots: + void increaseSize(); + void on_cancelBtn_clicked(); + void on_okBtn_clicked(); + void on_selectAllBtn_clicked(); + +signals: + void showToast(QString messageToDisplay); + +private: + Ui::highlightsListDialog *ui; +}; + +#endif // HIGHLIGHTSLISTDIALOG_H diff --git a/src/widgets/dialogs/reader/highlightslistdialog.ui b/src/widgets/dialogs/reader/highlightslistdialog.ui new file mode 100644 index 0000000..1a6f601 --- /dev/null +++ b/src/widgets/dialogs/reader/highlightslistdialog.ui @@ -0,0 +1,102 @@ + + + highlightsListDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + + 0 + 0 + + + + + 50 + false + + + + <font face="Inter"><b>Highlights list</b></font><font face="u001"><br>Select items to delete</br></font> + + + Qt::AlignCenter + + + + + + + + + + + + + U001 + + + + Select all + + + + + + + + + QFrame::Plain + + + Qt::Horizontal + + + + + + + + + + 75 + true + + + + OK + + + + + + + + 75 + true + + + + Close + + + + + + + + + +