Home screen: Implement two-row recent books

This commit is contained in:
Nicolas Mailloux 2022-06-29 11:42:05 -04:00
parent 7822f65e02
commit ebcfd349f2
6 changed files with 74 additions and 27 deletions

View file

@ -145,7 +145,9 @@ namespace global {
inline bool launchApp; inline bool launchApp;
} }
namespace homePageWidget { namespace homePageWidget {
inline int recentBooksNumber = 4; inline int recentBooksNumber = 8;
inline int recentBooksNumberPerRow = 4;
inline int recentBooksRowNumber = global::homePageWidget::recentBooksNumber / global::homePageWidget::recentBooksNumberPerRow;
} }
inline QString systemInfoText; inline QString systemInfoText;
inline bool forbidOpenSearchDialog; inline bool forbidOpenSearchDialog;

View file

@ -11,6 +11,7 @@ homePageWidget::homePageWidget(QWidget *parent) :
{ {
ui->setupUi(this); ui->setupUi(this);
horizontalLayoutArray.resize(global::homePageWidget::recentBooksRowNumber);
verticalLayoutArray.resize(global::homePageWidget::recentBooksNumber); verticalLayoutArray.resize(global::homePageWidget::recentBooksNumber);
bookBtnArray.resize(global::homePageWidget::recentBooksNumber); bookBtnArray.resize(global::homePageWidget::recentBooksNumber);
bookTitleArray.resize(global::homePageWidget::recentBooksNumber); bookTitleArray.resize(global::homePageWidget::recentBooksNumber);
@ -68,37 +69,53 @@ homePageWidget::homePageWidget(QWidget *parent) :
log("Setting up home page", className); log("Setting up home page", className);
for(int i = 1; i <= global::homePageWidget::recentBooksNumber; i++) { int in = 0;
QString objectName = "Book" + QString::number(i); bool newRow = false;
QJsonObject jsonObject = recentBooksJsonObject[objectName].toObject(); for(int i = 1; i <= (global::homePageWidget::recentBooksNumber + 1); i++) {
QString bookPath = jsonObject.value("BookPath").toString(); if(in < global::homePageWidget::recentBooksRowNumber) {
bookBtnArray[i] = new QClickableLabel(this); QString objectName = "Book" + QString::number(i);
QJsonObject jsonObject = recentBooksJsonObject[objectName].toObject();
QString bookPath = jsonObject.value("BookPath").toString();
bookBtnArray[i] = new QClickableLabel(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 = i; in <= databaseBooksNumber; in++) {
QJsonObject bookJsonObject = databaseJsonArrayList.at(in - 1).toObject(); QJsonObject bookJsonObject = databaseJsonArrayList.at(in - 1).toObject();
if(bookJsonObject["BookPath"] == bookPath) { if(bookJsonObject["BookPath"] == bookPath) {
bookBtnArray[i]->setObjectName(QJsonDocument(bookJsonObject).toJson()); bookBtnArray[i]->setObjectName(QJsonDocument(bookJsonObject).toJson());
}
} }
verticalLayoutArray[i] = new QVBoxLayout();
QObject::connect(bookBtnArray[i], &QClickableLabel::bookPath, this, &homePageWidget::openBook);
bookBtnArray[i]->setAlignment(Qt::AlignCenter);
bookBtnArray[i]->setFont(QFont("u001"));
bookBtnArray[i]->setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px");
QString bookIcon = QJsonDocument::fromJson(bookBtnArray[i]->objectName().toUtf8()).object()["CoverPath"].toString();
if(QFile::exists(bookIcon)) {
bookBtnArray[i]->setPixmap(QPixmap(bookIcon).scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
}
else {
bookBtnArray[i]->setPixmap(QPixmap(":/resources/cover_unavailable").scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
}
verticalLayoutArray[i]->addWidget(bookBtnArray[i]);
} }
verticalLayoutArray[i] = new QVBoxLayout(this); if(newRow == true) {
newRow = false;
QObject::connect(bookBtnArray[i], &QClickableLabel::bookPath, this, &homePageWidget::openBook); horizontalLayoutArray[in] = new QHBoxLayout();
bookBtnArray[i]->setAlignment(Qt::AlignCenter); for(int n = ((i - 1) - (global::homePageWidget::recentBooksNumberPerRow - 1)); n < (((i - 1) - (global::homePageWidget::recentBooksNumberPerRow - 1)) + global::homePageWidget::recentBooksNumberPerRow); n++) {
bookBtnArray[i]->setFont(QFont("u001")); horizontalLayoutArray[in]->addLayout(verticalLayoutArray[n]);
bookBtnArray[i]->setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px"); }
ui->booksVerticalLayout->addLayout(horizontalLayoutArray[in]);
QString bookIcon = QJsonDocument::fromJson(bookBtnArray[i]->objectName().toUtf8()).object()["CoverPath"].toString();
if(QFile::exists(bookIcon)) {
bookBtnArray[i]->setPixmap(QPixmap(bookIcon).scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
} }
else { if(!(i % global::homePageWidget::recentBooksNumberPerRow)) {
bookBtnArray[i]->setPixmap(QPixmap(":/resources/cover_unavailable").scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio)); newRow = true;
in++;
} }
verticalLayoutArray[i]->addWidget(bookBtnArray[i]);
ui->horizontalLayout->addLayout(verticalLayoutArray[i]);
} }
QTimer::singleShot(500, this, SLOT(refreshScreenNative())); QTimer::singleShot(500, this, SLOT(refreshScreenNative()));
} }

View file

@ -35,6 +35,7 @@ private slots:
private: private:
Ui::homePageWidget *ui; Ui::homePageWidget *ui;
QVector<QLabel*> bookTitleArray; QVector<QLabel*> bookTitleArray;
QVector<QHBoxLayout*> horizontalLayoutArray;
QVector<QVBoxLayout*> verticalLayoutArray; QVector<QVBoxLayout*> verticalLayoutArray;
QVector<QClickableLabel*> bookBtnArray; QVector<QClickableLabel*> bookBtnArray;
}; };

View file

@ -50,7 +50,10 @@
</widget> </widget>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QVBoxLayout" name="booksVerticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetNoConstraint</enum>
</property>
<property name="topMargin"> <property name="topMargin">
<number>25</number> <number>25</number>
</property> </property>

View file

@ -11,6 +11,7 @@ localLibraryWidget::localLibraryWidget(QWidget *parent) :
{ {
ui->setupUi(this); ui->setupUi(this);
this->setFont(QFont("u001")); this->setFont(QFont("u001"));
QObject::connect(ui->pageNumberLabel, &QClickableLabel::clicked, this, &localLibraryWidget::openGoToPageDialog);
ui->previousPageBtn->setProperty("type", "borderless"); ui->previousPageBtn->setProperty("type", "borderless");
ui->previousPageBtn->setEnabled(false); ui->previousPageBtn->setEnabled(false);
@ -308,6 +309,25 @@ void localLibraryWidget::btnOpenBook(int buttonNumber) {
localLibraryWidget::close(); localLibraryWidget::close();
} }
void localLibraryWidget::openGoToPageDialog() {
log("Showing 'Go to page' dialog", className);
global::keyboard::keypadDialog = true;
generalDialogWindow = new generalDialog();
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
QObject::connect(generalDialogWindow, &generalDialog::gotoPageSelected, this, &localLibraryWidget::goToPage);
}
void localLibraryWidget::goToPage(int page) {
if(page > pagesNumber or page <= 0) {
log("Page number specified (" + QString::number(page) + ") is out of range", className);
}
else {
log("Going to page " + QString::number(page), className);
setupBooksList(page);
emit refreshScreen();
}
}
void localLibraryWidget::refreshScreenNative() { void localLibraryWidget::refreshScreenNative() {
emit refreshScreen(); emit refreshScreen();
} }

View file

@ -7,6 +7,7 @@
#include "functions.h" #include "functions.h"
#include "qclickablelabel.h" #include "qclickablelabel.h"
#include "generaldialog.h"
namespace Ui { namespace Ui {
class localLibraryWidget; class localLibraryWidget;
@ -46,9 +47,12 @@ private slots:
void openBook(int id); void openBook(int id);
void btnOpenBook(int buttonNumber); void btnOpenBook(int buttonNumber);
void refreshScreenNative(); void refreshScreenNative();
void openGoToPageDialog();
void goToPage(int page);
private: private:
Ui::localLibraryWidget * ui; Ui::localLibraryWidget * ui;
generalDialog * generalDialogWindow;
QVector<QHBoxLayout*> horizontalLayoutArray; QVector<QHBoxLayout*> horizontalLayoutArray;
QVector<QLabel*> bookIconArray; QVector<QLabel*> bookIconArray;
QVector<QClickableLabel*> bookBtnArray; QVector<QClickableLabel*> bookBtnArray;