Implement 'Generating database' toast; various improvements

This commit is contained in:
Nicolas Mailloux 2022-06-30 09:52:48 -04:00
parent 5c50911642
commit 3be9608a10
7 changed files with 114 additions and 40 deletions

View file

@ -130,6 +130,7 @@ namespace global {
static inline QString rawDatabasePath = "/inkbox/LocalLibrary.db.raw"; static inline QString rawDatabasePath = "/inkbox/LocalLibrary.db.raw";
static inline QString databasePath = "/mnt/onboard/onboard/.inkbox/LocalLibrary.db"; static inline QString databasePath = "/mnt/onboard/onboard/.inkbox/LocalLibrary.db";
static inline QString recentBooksDatabasePath = "/mnt/onboard/onboard/.inkbox/RecentBooks.db"; static inline QString recentBooksDatabasePath = "/mnt/onboard/onboard/.inkbox/RecentBooks.db";
inline bool headless;
} }
namespace localStorage { namespace localStorage {
inline QStringList searchResultsPaths; inline QStringList searchResultsPaths;
@ -145,9 +146,9 @@ namespace global {
inline bool launchApp; inline bool launchApp;
} }
namespace homePageWidget { namespace homePageWidget {
inline int recentBooksNumber = 8; static inline int recentBooksNumber = 8;
inline int recentBooksNumberPerRow = 4; static inline int recentBooksNumberPerRow = 4;
inline int recentBooksRowNumber = global::homePageWidget::recentBooksNumber / global::homePageWidget::recentBooksNumberPerRow; static inline int recentBooksRowNumber = global::homePageWidget::recentBooksNumber / global::homePageWidget::recentBooksNumberPerRow;
} }
inline QString systemInfoText; inline QString systemInfoText;
inline bool forbidOpenSearchDialog; inline bool forbidOpenSearchDialog;

View file

@ -61,6 +61,35 @@ homePageWidget::homePageWidget(QWidget *parent) :
stdIconHeight = sH / stdIconHeightDivider; 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); log("Reading database", className);
QFile database(global::localLibrary::databasePath); QFile database(global::localLibrary::databasePath);
QByteArray data; QByteArray data;
@ -90,10 +119,12 @@ homePageWidget::homePageWidget(QWidget *parent) :
bookTitleArray[i] = new QToolTipLabel(this); bookTitleArray[i] = new QToolTipLabel(this);
// Iterate until we find a book matching the recently opened book's "BookPath" key/value pair // 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(); QJsonObject bookJsonObject = databaseJsonArrayList.at(in - 1).toObject();
if(bookJsonObject["BookPath"] == bookPath) { if(bookJsonObject["BookPath"].toString() == bookPath) {
bookBtnArray[i]->setObjectName(QJsonDocument(bookJsonObject).toJson()); 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]->setFont(QFont("u001"));
bookTitleArray[i]->setStyleSheet("font-size: 7pt"); 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); bookTitleArray[i]->setObjectName(bookTitle);
int localBookTitleTruncateThreshold; int localBookTitleTruncateThreshold;
@ -126,7 +158,7 @@ homePageWidget::homePageWidget(QWidget *parent) :
} }
bookTitleArray[i]->setText(bookTitle); bookTitleArray[i]->setText(bookTitle);
QString bookIcon = QJsonDocument::fromJson(bookBtnArray[i]->objectName().toUtf8()).object()["CoverPath"].toString(); QString bookIcon = uncompressedJsonObject["CoverPath"].toString();
if(QFile::exists(bookIcon)) { if(QFile::exists(bookIcon)) {
bookBtnArray[i]->setPixmap(QPixmap(bookIcon).scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio)); bookBtnArray[i]->setPixmap(QPixmap(bookIcon).scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
} }
@ -154,15 +186,21 @@ homePageWidget::homePageWidget(QWidget *parent) :
QTimer::singleShot(500, this, SLOT(refreshScreenNative())); QTimer::singleShot(500, this, SLOT(refreshScreenNative()));
} }
homePageWidget::~homePageWidget() void homePageWidget::setupDisplaySlot() {
{ setupDisplay(true);
delete ui;
} }
void homePageWidget::openBook(QString bookPath) { void homePageWidget::setupDisplayWithDatabase() {
emit openBookSignal(bookPath, false); 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() { void homePageWidget::showToast(QString messageToDisplay) {
emit refreshScreen(); global::toast::message = messageToDisplay;
toastWindow = new toast(this);
toastWindow->setAttribute(Qt::WA_DeleteOnClose);
toastWindow->show();
} }

View file

@ -5,6 +5,8 @@
#include <QVBoxLayout> #include <QVBoxLayout>
#include "qclickablelabel.h" #include "qclickablelabel.h"
#include "qtooltiplabel.h" #include "qtooltiplabel.h"
#include "locallibrarywidget.h"
#include "toast.h"
namespace Ui { namespace Ui {
class homePageWidget; class homePageWidget;
@ -33,9 +35,15 @@ signals:
private slots: private slots:
void openBook(QString bookPath); void openBook(QString bookPath);
void refreshScreenNative(); void refreshScreenNative();
void setupDisplay(bool databaseGenerated);
void setupDisplaySlot();
void setupDisplayWithDatabase();
void showToast(QString messageToDisplay);
private: private:
Ui::homePageWidget *ui; Ui::homePageWidget *ui;
localLibraryWidget * localLibraryWidgetWindow;
toast * toastWindow;
QVector<QToolTipLabel*> bookTitleArray; QVector<QToolTipLabel*> bookTitleArray;
QVector<QHBoxLayout*> horizontalLayoutArray; QVector<QHBoxLayout*> horizontalLayoutArray;
QVector<QVBoxLayout*> verticalLayoutArray; QVector<QVBoxLayout*> verticalLayoutArray;

View file

@ -70,8 +70,8 @@ localLibraryWidget::localLibraryWidget(QWidget *parent) :
stdIconHeight = sH / stdIconHeightDivider; stdIconHeight = sH / stdIconHeightDivider;
} }
else if(global::deviceID == "n873\n") { else if(global::deviceID == "n873\n") {
stdIconWidthDivider = 7.5; stdIconWidthDivider = 9.7;
stdIconHeightDivider = 7.5; stdIconHeightDivider = 9.7;
stdIconWidth = sW / stdIconWidthDivider; stdIconWidth = sW / stdIconWidthDivider;
stdIconHeight = sH / stdIconHeightDivider; stdIconHeight = sH / stdIconHeightDivider;
} }
@ -111,16 +111,13 @@ localLibraryWidget::localLibraryWidget(QWidget *parent) :
ui->booksVerticalLayout->addWidget(lineArray[i]); ui->booksVerticalLayout->addWidget(lineArray[i]);
} }
} }
setupDatabase();
if(noBooksInDatabase == false) { if(!QFile::exists(global::localLibrary::databasePath)) {
setupBooksList(currentPageNumber); global::toast::modalToast = true;
} global::toast::indefiniteToast = true;
else { showToast("Generating database");
ui->previousPageBtn->setEnabled(false);
ui->nextPageBtn->setEnabled(false);
ui->pageNumberLabel->setText("1 <i>of</i> 1");
ui->stackedWidget->setCurrentIndex(1);
} }
QTimer::singleShot(100, this, SLOT(setupDisplay()));
} }
localLibraryWidget::~localLibraryWidget() 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; 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(); QProcess *proc = new QProcess();
proc->start(prog, args); proc->start(prog, args);
proc->waitForFinished(); proc->waitForFinished(-1);
QJsonDocument jsonDocument = QJsonDocument::fromJson(readFile(global::localLibrary::rawDatabasePath).toUtf8()); QJsonDocument jsonDocument = QJsonDocument::fromJson(readFile(global::localLibrary::rawDatabasePath).toUtf8());
QFile::remove(global::localLibrary::rawDatabasePath); QFile::remove(global::localLibrary::rawDatabasePath);
proc->deleteLater(); proc->deleteLater();
// Write database in compressed form, encoded in Base64 format // Write database in compressed form, encoded in Base64 format
writeFile(global::localLibrary::databasePath, qCompress(jsonDocument.toJson()).toBase64()); writeFile(global::localLibrary::databasePath, qCompress(jsonDocument.toJson()).toBase64());
toastWindow->close();
} }
// Read library database from file // Read library database from file
@ -190,6 +188,11 @@ void localLibraryWidget::setupDatabase() {
ui->nextPageBtn->setEnabled(false); ui->nextPageBtn->setEnabled(false);
} }
} }
if(global::localLibrary::headless == true) {
global::localLibrary::headless = false;
localLibraryWidget::close();
}
} }
void localLibraryWidget::setupBooksList(int pageNumber) { void localLibraryWidget::setupBooksList(int pageNumber) {
@ -253,19 +256,26 @@ void localLibraryWidget::setupBooksList(int pageNumber) {
for(int i = 0; i <= 1; i++) { for(int i = 0; i <= 1; i++) {
ui->verticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding)); ui->verticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
} }
}
void localLibraryWidget::on_previousPageBtn_clicked() // Set boundaries for 'Previous'/'Next' page turn buttons
{ currentPageNumber = pageNumber;
currentPageNumber--;
if(currentPageNumber - 1 < 1) { if(currentPageNumber - 1 < 1) {
ui->previousPageBtn->setEnabled(false); ui->previousPageBtn->setEnabled(false);
ui->nextPageBtn->setEnabled(true); ui->nextPageBtn->setEnabled(true);
} }
else if(currentPageNumber + 1 > pagesNumber) {
ui->previousPageBtn->setEnabled(true);
ui->nextPageBtn->setEnabled(false);
}
else { else {
ui->previousPageBtn->setEnabled(true); ui->previousPageBtn->setEnabled(true);
ui->nextPageBtn->setEnabled(true); ui->nextPageBtn->setEnabled(true);
} }
}
void localLibraryWidget::on_previousPageBtn_clicked()
{
currentPageNumber--;
setupBooksList(currentPageNumber); setupBooksList(currentPageNumber);
pagesTurned = pagesTurned + 1; pagesTurned = pagesTurned + 1;
@ -279,14 +289,6 @@ void localLibraryWidget::on_previousPageBtn_clicked()
void localLibraryWidget::on_nextPageBtn_clicked() void localLibraryWidget::on_nextPageBtn_clicked()
{ {
currentPageNumber++; currentPageNumber++;
if(currentPageNumber + 1 > pagesNumber) {
ui->previousPageBtn->setEnabled(true);
ui->nextPageBtn->setEnabled(false);
}
else {
ui->previousPageBtn->setEnabled(true);
ui->nextPageBtn->setEnabled(true);
}
setupBooksList(currentPageNumber); setupBooksList(currentPageNumber);
pagesTurned = pagesTurned + 1; pagesTurned = pagesTurned + 1;
@ -331,3 +333,23 @@ void localLibraryWidget::goToPage(int page) {
void localLibraryWidget::refreshScreenNative() { void localLibraryWidget::refreshScreenNative() {
emit refreshScreen(); emit refreshScreen();
} }
void localLibraryWidget::setupDisplay() {
setupDatabase();
if(noBooksInDatabase == false) {
setupBooksList(currentPageNumber);
}
else {
ui->previousPageBtn->setEnabled(false);
ui->nextPageBtn->setEnabled(false);
ui->pageNumberLabel->setText("1 <i>of</i> 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();
}

View file

@ -8,6 +8,7 @@
#include "functions.h" #include "functions.h"
#include "qclickablelabel.h" #include "qclickablelabel.h"
#include "generaldialog.h" #include "generaldialog.h"
#include "toast.h"
namespace Ui { namespace Ui {
class localLibraryWidget; class localLibraryWidget;
@ -49,10 +50,13 @@ private slots:
void refreshScreenNative(); void refreshScreenNative();
void openGoToPageDialog(); void openGoToPageDialog();
void goToPage(int page); void goToPage(int page);
void setupDisplay();
void showToast(QString messageToDisplay);
private: private:
Ui::localLibraryWidget * ui; Ui::localLibraryWidget * ui;
generalDialog * generalDialogWindow; generalDialog * generalDialogWindow;
toast * toastWindow;
QVector<QHBoxLayout*> horizontalLayoutArray; QVector<QHBoxLayout*> horizontalLayoutArray;
QVector<QLabel*> bookIconArray; QVector<QLabel*> bookIconArray;
QVector<QClickableLabel*> bookBtnArray; QVector<QClickableLabel*> bookBtnArray;

View file

@ -23,7 +23,7 @@ void QClickableLabel::mousePressEvent(QMouseEvent * event) {
void QClickableLabel::mouseReleaseEvent(QMouseEvent * event) { void QClickableLabel::mouseReleaseEvent(QMouseEvent * event) {
emit unclicked(); emit unclicked();
emit bookID(objectName().toInt()); 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") { if(objectName() == "pageNumberLabel") {
QClickableLabel::setStyleSheet("color: black; background-color: white; border-radius: 10px; padding-left: 10px; padding-right: 10px"); QClickableLabel::setStyleSheet("color: black; background-color: white; border-radius: 10px; padding-left: 10px; padding-right: 10px");
} }

View file

@ -622,6 +622,7 @@ reader::reader(QWidget *parent) :
// Way to tell shell scripts that we're in the Reader framework // Way to tell shell scripts that we're in the Reader framework
string_writeconfig("/tmp/inkboxReading", "true"); string_writeconfig("/tmp/inkboxReading", "true");
// Maintain a 'Recent books' list
QJsonObject recentBooksObject; QJsonObject recentBooksObject;
if(QFile::exists(global::localLibrary::recentBooksDatabasePath)) { if(QFile::exists(global::localLibrary::recentBooksDatabasePath)) {
QJsonObject mainJsonObject = QJsonDocument::fromJson(readFile(global::localLibrary::recentBooksDatabasePath).toUtf8()).object(); QJsonObject mainJsonObject = QJsonDocument::fromJson(readFile(global::localLibrary::recentBooksDatabasePath).toUtf8()).object();