From 3be9608a107eafcfe9bc1ca2a59c987ea488992b Mon Sep 17 00:00:00 2001 From: Nicolas Mailloux Date: Thu, 30 Jun 2022 09:52:48 -0400 Subject: [PATCH] Implement 'Generating database' toast; various improvements --- functions.h | 7 +++-- homepagewidget.cpp | 62 +++++++++++++++++++++++++++++-------- homepagewidget.h | 8 +++++ locallibrarywidget.cpp | 70 +++++++++++++++++++++++++++--------------- locallibrarywidget.h | 4 +++ qclickablelabel.cpp | 2 +- reader.cpp | 1 + 7 files changed, 114 insertions(+), 40 deletions(-) diff --git a/functions.h b/functions.h index 9e8a838..1ddefd7 100644 --- a/functions.h +++ b/functions.h @@ -130,6 +130,7 @@ namespace global { static inline QString rawDatabasePath = "/inkbox/LocalLibrary.db.raw"; static inline QString databasePath = "/mnt/onboard/onboard/.inkbox/LocalLibrary.db"; static inline QString recentBooksDatabasePath = "/mnt/onboard/onboard/.inkbox/RecentBooks.db"; + inline bool headless; } namespace localStorage { inline QStringList searchResultsPaths; @@ -145,9 +146,9 @@ namespace global { inline bool launchApp; } namespace homePageWidget { - inline int recentBooksNumber = 8; - inline int recentBooksNumberPerRow = 4; - inline int recentBooksRowNumber = global::homePageWidget::recentBooksNumber / global::homePageWidget::recentBooksNumberPerRow; + static inline int recentBooksNumber = 8; + static inline int recentBooksNumberPerRow = 4; + static inline int recentBooksRowNumber = global::homePageWidget::recentBooksNumber / global::homePageWidget::recentBooksNumberPerRow; } inline QString systemInfoText; inline bool forbidOpenSearchDialog; diff --git a/homepagewidget.cpp b/homepagewidget.cpp index ceb8ee5..3e13338 100644 --- a/homepagewidget.cpp +++ b/homepagewidget.cpp @@ -61,6 +61,35 @@ homePageWidget::homePageWidget(QWidget *parent) : stdIconHeight = sH / stdIconHeightDivider; } + if(!QFile::exists(global::localLibrary::databasePath)) { + global::toast::modalToast = true; + global::toast::indefiniteToast = true; + showToast("Generating database"); + QTimer::singleShot(100, this, SLOT(setupDisplayWithDatabase())); + } + else { + setupDisplay(false); + } +} + +homePageWidget::~homePageWidget() +{ + delete ui; +} + +void homePageWidget::openBook(QString bookPath) { + emit openBookSignal(bookPath, false); +} + +void homePageWidget::refreshScreenNative() { + emit refreshScreen(); +} + +void homePageWidget::setupDisplay(bool databaseGenerated) { + if(databaseGenerated == true) { + toastWindow->close(); + } + log("Reading database", className); QFile database(global::localLibrary::databasePath); QByteArray data; @@ -90,10 +119,12 @@ homePageWidget::homePageWidget(QWidget *parent) : bookTitleArray[i] = new QToolTipLabel(this); // Iterate until we find a book matching the recently opened book's "BookPath" key/value pair - for(int in = i; in <= databaseBooksNumber; in++) { + for(int in = 1; in <= databaseBooksNumber; in++) { QJsonObject bookJsonObject = databaseJsonArrayList.at(in - 1).toObject(); - if(bookJsonObject["BookPath"] == bookPath) { - bookBtnArray[i]->setObjectName(QJsonDocument(bookJsonObject).toJson()); + if(bookJsonObject["BookPath"].toString() == bookPath) { + QByteArray data = qCompress(QJsonDocument(bookJsonObject).toJson()).toBase64(); + QString dataString = QString(data); + bookBtnArray[i]->setObjectName(dataString); } } @@ -110,7 +141,8 @@ homePageWidget::homePageWidget(QWidget *parent) : bookTitleArray[i]->setFont(QFont("u001")); bookTitleArray[i]->setStyleSheet("font-size: 7pt"); - QString bookTitle = QJsonDocument::fromJson(bookBtnArray[i]->objectName().toUtf8()).object()["Title"].toString(); + QJsonObject uncompressedJsonObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(bookBtnArray[i]->objectName().toUtf8()))).object(); + QString bookTitle = uncompressedJsonObject["Title"].toString(); bookTitleArray[i]->setObjectName(bookTitle); int localBookTitleTruncateThreshold; @@ -126,7 +158,7 @@ homePageWidget::homePageWidget(QWidget *parent) : } bookTitleArray[i]->setText(bookTitle); - QString bookIcon = QJsonDocument::fromJson(bookBtnArray[i]->objectName().toUtf8()).object()["CoverPath"].toString(); + QString bookIcon = uncompressedJsonObject["CoverPath"].toString(); if(QFile::exists(bookIcon)) { bookBtnArray[i]->setPixmap(QPixmap(bookIcon).scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio)); } @@ -154,15 +186,21 @@ homePageWidget::homePageWidget(QWidget *parent) : QTimer::singleShot(500, this, SLOT(refreshScreenNative())); } -homePageWidget::~homePageWidget() -{ - delete ui; +void homePageWidget::setupDisplaySlot() { + setupDisplay(true); } -void homePageWidget::openBook(QString bookPath) { - emit openBookSignal(bookPath, false); +void homePageWidget::setupDisplayWithDatabase() { + global::localLibrary::headless = true; + localLibraryWidget * localLibraryWidgetWindow = new localLibraryWidget(this); + localLibraryWidgetWindow->setAttribute(Qt::WA_DeleteOnClose); + localLibraryWidgetWindow->hide(); + QObject::connect(localLibraryWidgetWindow, &localLibraryWidget::destroyed, this, &homePageWidget::setupDisplaySlot); } -void homePageWidget::refreshScreenNative() { - emit refreshScreen(); +void homePageWidget::showToast(QString messageToDisplay) { + global::toast::message = messageToDisplay; + toastWindow = new toast(this); + toastWindow->setAttribute(Qt::WA_DeleteOnClose); + toastWindow->show(); } diff --git a/homepagewidget.h b/homepagewidget.h index d03891b..99b099f 100644 --- a/homepagewidget.h +++ b/homepagewidget.h @@ -5,6 +5,8 @@ #include #include "qclickablelabel.h" #include "qtooltiplabel.h" +#include "locallibrarywidget.h" +#include "toast.h" namespace Ui { class homePageWidget; @@ -33,9 +35,15 @@ signals: private slots: void openBook(QString bookPath); void refreshScreenNative(); + void setupDisplay(bool databaseGenerated); + void setupDisplaySlot(); + void setupDisplayWithDatabase(); + void showToast(QString messageToDisplay); private: Ui::homePageWidget *ui; + localLibraryWidget * localLibraryWidgetWindow; + toast * toastWindow; QVector bookTitleArray; QVector horizontalLayoutArray; QVector verticalLayoutArray; diff --git a/locallibrarywidget.cpp b/locallibrarywidget.cpp index 483c058..45dc4f6 100644 --- a/locallibrarywidget.cpp +++ b/locallibrarywidget.cpp @@ -70,8 +70,8 @@ localLibraryWidget::localLibraryWidget(QWidget *parent) : stdIconHeight = sH / stdIconHeightDivider; } else if(global::deviceID == "n873\n") { - stdIconWidthDivider = 7.5; - stdIconHeightDivider = 7.5; + stdIconWidthDivider = 9.7; + stdIconHeightDivider = 9.7; stdIconWidth = sW / stdIconWidthDivider; stdIconHeight = sH / stdIconHeightDivider; } @@ -111,16 +111,13 @@ localLibraryWidget::localLibraryWidget(QWidget *parent) : ui->booksVerticalLayout->addWidget(lineArray[i]); } } - setupDatabase(); - if(noBooksInDatabase == false) { - setupBooksList(currentPageNumber); - } - else { - ui->previousPageBtn->setEnabled(false); - ui->nextPageBtn->setEnabled(false); - ui->pageNumberLabel->setText("1 of 1"); - ui->stackedWidget->setCurrentIndex(1); + + if(!QFile::exists(global::localLibrary::databasePath)) { + global::toast::modalToast = true; + global::toast::indefiniteToast = true; + showToast("Generating database"); } + QTimer::singleShot(100, this, SLOT(setupDisplay())); } localLibraryWidget::~localLibraryWidget() @@ -151,13 +148,14 @@ void localLibraryWidget::setupDatabase() { args << "env" << "icon_width_divider=" + QString::number(stdIconWidthDivider - 1.5) << "icon_height_divider=" + QString::number(stdIconHeightDivider - 1.5) << "./explore_local_library.sh" << booksList; QProcess *proc = new QProcess(); proc->start(prog, args); - proc->waitForFinished(); + proc->waitForFinished(-1); QJsonDocument jsonDocument = QJsonDocument::fromJson(readFile(global::localLibrary::rawDatabasePath).toUtf8()); QFile::remove(global::localLibrary::rawDatabasePath); proc->deleteLater(); // Write database in compressed form, encoded in Base64 format writeFile(global::localLibrary::databasePath, qCompress(jsonDocument.toJson()).toBase64()); + toastWindow->close(); } // Read library database from file @@ -190,6 +188,11 @@ void localLibraryWidget::setupDatabase() { ui->nextPageBtn->setEnabled(false); } } + + if(global::localLibrary::headless == true) { + global::localLibrary::headless = false; + localLibraryWidget::close(); + } } void localLibraryWidget::setupBooksList(int pageNumber) { @@ -253,19 +256,26 @@ void localLibraryWidget::setupBooksList(int pageNumber) { for(int i = 0; i <= 1; i++) { ui->verticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding)); } -} -void localLibraryWidget::on_previousPageBtn_clicked() -{ - currentPageNumber--; + // Set boundaries for 'Previous'/'Next' page turn buttons + currentPageNumber = pageNumber; if(currentPageNumber - 1 < 1) { ui->previousPageBtn->setEnabled(false); ui->nextPageBtn->setEnabled(true); } + else if(currentPageNumber + 1 > pagesNumber) { + ui->previousPageBtn->setEnabled(true); + ui->nextPageBtn->setEnabled(false); + } else { ui->previousPageBtn->setEnabled(true); ui->nextPageBtn->setEnabled(true); } +} + +void localLibraryWidget::on_previousPageBtn_clicked() +{ + currentPageNumber--; setupBooksList(currentPageNumber); pagesTurned = pagesTurned + 1; @@ -279,14 +289,6 @@ void localLibraryWidget::on_previousPageBtn_clicked() void localLibraryWidget::on_nextPageBtn_clicked() { currentPageNumber++; - if(currentPageNumber + 1 > pagesNumber) { - ui->previousPageBtn->setEnabled(true); - ui->nextPageBtn->setEnabled(false); - } - else { - ui->previousPageBtn->setEnabled(true); - ui->nextPageBtn->setEnabled(true); - } setupBooksList(currentPageNumber); pagesTurned = pagesTurned + 1; @@ -331,3 +333,23 @@ void localLibraryWidget::goToPage(int page) { void localLibraryWidget::refreshScreenNative() { emit refreshScreen(); } + +void localLibraryWidget::setupDisplay() { + setupDatabase(); + if(noBooksInDatabase == false) { + setupBooksList(currentPageNumber); + } + else { + ui->previousPageBtn->setEnabled(false); + ui->nextPageBtn->setEnabled(false); + ui->pageNumberLabel->setText("1 of 1"); + ui->stackedWidget->setCurrentIndex(1); + } +} + +void localLibraryWidget::showToast(QString messageToDisplay) { + global::toast::message = messageToDisplay; + toastWindow = new toast(this); + toastWindow->setAttribute(Qt::WA_DeleteOnClose); + toastWindow->show(); +} diff --git a/locallibrarywidget.h b/locallibrarywidget.h index eecc96e..fb65128 100644 --- a/locallibrarywidget.h +++ b/locallibrarywidget.h @@ -8,6 +8,7 @@ #include "functions.h" #include "qclickablelabel.h" #include "generaldialog.h" +#include "toast.h" namespace Ui { class localLibraryWidget; @@ -49,10 +50,13 @@ private slots: void refreshScreenNative(); void openGoToPageDialog(); void goToPage(int page); + void setupDisplay(); + void showToast(QString messageToDisplay); private: Ui::localLibraryWidget * ui; generalDialog * generalDialogWindow; + toast * toastWindow; QVector horizontalLayoutArray; QVector bookIconArray; QVector bookBtnArray; diff --git a/qclickablelabel.cpp b/qclickablelabel.cpp index 46a7f03..18c25e6 100644 --- a/qclickablelabel.cpp +++ b/qclickablelabel.cpp @@ -23,7 +23,7 @@ void QClickableLabel::mousePressEvent(QMouseEvent * event) { void QClickableLabel::mouseReleaseEvent(QMouseEvent * event) { emit unclicked(); emit bookID(objectName().toInt()); - emit bookPath(QJsonDocument::fromJson(objectName().toUtf8()).object()["BookPath"].toString()); + emit bookPath(QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(objectName().toUtf8()))).object()["BookPath"].toString()); if(objectName() == "pageNumberLabel") { QClickableLabel::setStyleSheet("color: black; background-color: white; border-radius: 10px; padding-left: 10px; padding-right: 10px"); } diff --git a/reader.cpp b/reader.cpp index 10db435..d0f3ce3 100644 --- a/reader.cpp +++ b/reader.cpp @@ -622,6 +622,7 @@ reader::reader(QWidget *parent) : // Way to tell shell scripts that we're in the Reader framework string_writeconfig("/tmp/inkboxReading", "true"); + // Maintain a 'Recent books' list QJsonObject recentBooksObject; if(QFile::exists(global::localLibrary::recentBooksDatabasePath)) { QJsonObject mainJsonObject = QJsonDocument::fromJson(readFile(global::localLibrary::recentBooksDatabasePath).toUtf8()).object();