Implement 'Pin books' function

This commit is contained in:
Nicolas Mailloux 2022-07-18 02:40:32 -04:00
parent 54633f1dc9
commit 13b3bda215
12 changed files with 509 additions and 70 deletions

View file

@ -99,7 +99,6 @@ bookInfoDialog::bookInfoDialog(QWidget *parent) :
bookInfo.append("<b>Path:</b> " + path + "<br>"); bookInfo.append("<b>Path:</b> " + path + "<br>");
} }
log(bookInfo, className);
global::text::textBrowserContents = bookInfo; global::text::textBrowserContents = bookInfo;
textwidget * textwidgetWindow = new textwidget(this); textwidget * textwidgetWindow = new textwidget(this);
ui->stackedWidget->insertWidget(1, textwidgetWindow); ui->stackedWidget->insertWidget(1, textwidgetWindow);

View file

@ -9,10 +9,29 @@ bookOptionsDialog::bookOptionsDialog(QWidget *parent) :
{ {
ui->setupUi(this); ui->setupUi(this);
ui->pinBtn->setProperty("type", "borderless"); ui->pinBtn->setProperty("type", "borderless");
if(global::localLibrary::bookOptionsDialog::deleteOption == false) {
global::localLibrary::bookOptionsDialog::deleteOption = true;
ui->deleteBtn->hide();
ui->line_2->hide();
ui->deleteBtn->deleteLater();
ui->line_2->deleteLater();
this->adjustSize();
}
else {
ui->deleteBtn->setProperty("type", "borderless"); ui->deleteBtn->setProperty("type", "borderless");
}
ui->infoBtn->setProperty("type", "borderless"); ui->infoBtn->setProperty("type", "borderless");
global::localLibrary::bookOptionsDialog::bookPinAction = false;
bookPath = getBookMetadata(global::localLibrary::bookOptionsDialog::bookID)["BookPath"].toString(); bookPath = getBookMetadata(global::localLibrary::bookOptionsDialog::bookID)["BookPath"].toString();
if(isBookPinned(global::localLibrary::bookOptionsDialog::bookID)) {
bookPinned = true;
ui->pinBtn->setText("Unpin");
}
else {
bookPinned = false;
}
} }
bookOptionsDialog::~bookOptionsDialog() bookOptionsDialog::~bookOptionsDialog()
@ -22,8 +41,12 @@ bookOptionsDialog::~bookOptionsDialog()
void bookOptionsDialog::on_pinBtn_clicked() void bookOptionsDialog::on_pinBtn_clicked()
{ {
// TODO if(bookPinned == false) {
log("Pinned book with ID " + QString::number(global::localLibrary::bookOptionsDialog::bookID), className); pinBook(global::localLibrary::bookOptionsDialog::bookID);
}
else {
unpinBook(global::localLibrary::bookOptionsDialog::bookID);
}
} }
void bookOptionsDialog::on_deleteBtn_clicked() void bookOptionsDialog::on_deleteBtn_clicked()
@ -44,3 +67,139 @@ void bookOptionsDialog::on_infoBtn_clicked()
{ {
emit openLocalBookInfoDialog(); emit openLocalBookInfoDialog();
} }
void bookOptionsDialog::pinBook(int bookID) {
QJsonObject pinnedBooksObject;
if(QFile::exists(global::localLibrary::pinnedBooksDatabasePath)) {
QString function = __func__; log(function + ": Reading pinned books database", className);
QFile database(global::localLibrary::pinnedBooksDatabasePath);
QByteArray data;
if(database.open(QIODevice::ReadOnly)) {
data = database.readAll();
database.close();
}
else {
QString function = __func__; log(function + ": Failed to open pinned books library database file for reading at '" + database.fileName() + "'", className);
}
pinnedBooksObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(data))).object();
bool bookIsAlreadyPinned = false;
bool foundSpaceForBook = false;
for(int i = 1; i <= global::homePageWidget::pinnedBooksNumber; i++) {
QString pinnedBookPath = getBookMetadata(bookID)["BookPath"].toString();
if(pinnedBooksObject["Book" + QString::number(i)].toObject().value("BookPath").toString() == pinnedBookPath) {
bookIsAlreadyPinned = true;
}
else {
if(pinnedBooksObject["Book" + QString::number(i)].toObject().value("BookPath").toString().isEmpty() && foundSpaceForBook == false) {
foundSpaceForBook = true;
QJsonObject jsonObject;
jsonObject.insert("BookPath", pinnedBookPath);
pinnedBooksObject["Book" + QString::number(i)] = jsonObject;
QString function = __func__; log(function + ": Pinned book with ID " + QString::number(global::localLibrary::bookOptionsDialog::bookID), className);
global::toast::delay = 3000;
emit showToast("Book pinned successfully");
global::localLibrary::bookOptionsDialog::bookPinAction = true;
}
}
}
if(foundSpaceForBook == false && bookIsAlreadyPinned == false) {
global::localLibrary::bookOptionsDialog::bookPinAction = false;
emit showToast("No space left for pinning book");
}
else if(bookIsAlreadyPinned == true) {
global::localLibrary::bookOptionsDialog::bookPinAction = false;
emit showToast("Book is already pinned");
}
}
else {
QJsonObject mainJsonObject;
QJsonObject firstJsonObject;
firstJsonObject.insert("BookPath", QJsonValue(getBookMetadata(bookID)["BookPath"].toString()));
mainJsonObject["Book1"] = firstJsonObject;
for(int i = 2; i <= global::homePageWidget::pinnedBooksNumber; i++) {
QJsonObject jsonObject;
jsonObject.insert("BookPath", QJsonValue(""));
mainJsonObject["Book" + QString::number(i)] = jsonObject;
}
pinnedBooksObject = mainJsonObject;
}
// Writing database to file
QFile::remove(global::localLibrary::pinnedBooksDatabasePath);
writeFile(global::localLibrary::pinnedBooksDatabasePath, qCompress(QJsonDocument(pinnedBooksObject).toJson()).toBase64());
}
void bookOptionsDialog::unpinBook(int bookID) {
QJsonObject pinnedBooksObject;
QString function = __func__; log(function + ": Reading pinned books database", className);
QFile database(global::localLibrary::pinnedBooksDatabasePath);
QByteArray data;
if(database.open(QIODevice::ReadOnly)) {
data = database.readAll();
database.close();
}
else {
QString function = __func__; log(function + ": Failed to open pinned books library database file for reading at '" + database.fileName() + "'", className);
}
pinnedBooksObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(data))).object();
QJsonObject mainJsonObject;
// Removing pinned book associated to requested ID from database
int bookToUnpin;
for(int i = 1; i <= global::homePageWidget::pinnedBooksNumber; i++) {
if(pinnedBooksObject["Book" + QString::number(i)].toObject().value("BookPath").toString() == getBookMetadata(bookID)["BookPath"].toString()) {
bookToUnpin = i;
}
}
// Recreating pinned books database without previously pinned book
QString pinnedBookPath;
int recreationIndex = 1;
for(int i = 1; i <= global::homePageWidget::pinnedBooksNumber; i++) {
pinnedBookPath = pinnedBooksObject["Book" + QString::number(i)].toObject().value("BookPath").toString();
if(i != bookToUnpin) {
QJsonObject jsonObject;
jsonObject.insert("BookPath", QJsonValue(pinnedBookPath));
mainJsonObject["Book" + QString::number(recreationIndex)] = jsonObject;
recreationIndex++;
}
}
log(function + ": Unpinned book with ID " + bookID, className);
emit showToast("Book unpinned successfully");
global::localLibrary::bookOptionsDialog::bookPinAction = true;
// Writing database to file
pinnedBooksObject = mainJsonObject;
QFile::remove(global::localLibrary::pinnedBooksDatabasePath);
writeFile(global::localLibrary::pinnedBooksDatabasePath, qCompress(QJsonDocument(pinnedBooksObject).toJson()).toBase64());
}
bool bookOptionsDialog::isBookPinned(int bookID) {
QJsonObject pinnedBooksObject;
if(QFile::exists(global::localLibrary::pinnedBooksDatabasePath)) {
QString function = __func__; log(function + ": Reading pinned books database", className);
QFile database(global::localLibrary::pinnedBooksDatabasePath);
QByteArray data;
if(database.open(QIODevice::ReadOnly)) {
data = database.readAll();
database.close();
}
else {
QString function = __func__; log(function + ": Failed to open pinned books library database file for reading at '" + database.fileName() + "'", className);
}
pinnedBooksObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(data))).object();
}
QString pinnedBookPath = getBookMetadata(bookID)["BookPath"].toString();
for(int i = 1; i <= global::homePageWidget::pinnedBooksNumber; i++) {
if(pinnedBooksObject["Book" + QString::number(i)].toObject().value("BookPath").toString() == pinnedBookPath) {
bookPinned = true;
break;
}
else {
bookPinned = false;
}
}
return bookPinned;
}

View file

@ -17,11 +17,15 @@ public:
explicit bookOptionsDialog(QWidget *parent = nullptr); explicit bookOptionsDialog(QWidget *parent = nullptr);
~bookOptionsDialog(); ~bookOptionsDialog();
QString bookPath; QString bookPath;
bool bookPinned;
private slots: private slots:
void on_pinBtn_clicked(); void on_pinBtn_clicked();
void on_deleteBtn_clicked(); void on_deleteBtn_clicked();
void on_infoBtn_clicked(); void on_infoBtn_clicked();
void pinBook(int bookID);
void unpinBook(int bookID);
bool isBookPinned(int bookID);
signals: signals:
void openLocalBookInfoDialog(); void openLocalBookInfoDialog();

View file

@ -138,7 +138,9 @@ namespace global {
inline bool headless; inline bool headless;
namespace bookOptionsDialog { namespace bookOptionsDialog {
inline int bookID; inline int bookID;
inline bool deleteOption = true;
inline bool bookDeleted; inline bool bookDeleted;
inline bool bookPinAction;
} }
} }
namespace localStorage { namespace localStorage {
@ -158,6 +160,9 @@ namespace global {
static inline int recentBooksNumber = 8; static inline int recentBooksNumber = 8;
static inline int recentBooksNumberPerRow = 4; static inline int recentBooksNumberPerRow = 4;
static inline int recentBooksRowNumber = global::homePageWidget::recentBooksNumber / global::homePageWidget::recentBooksNumberPerRow; static inline int recentBooksRowNumber = global::homePageWidget::recentBooksNumber / global::homePageWidget::recentBooksNumberPerRow;
static inline int pinnedBooksNumber = 4;
static inline int pinnedBooksNumberPerRow = 4;
static inline int pinnedBooksRowNumber = global::homePageWidget::pinnedBooksNumber / global::homePageWidget::pinnedBooksNumberPerRow;
} }
inline QString systemInfoText; inline QString systemInfoText;
inline bool forbidOpenSearchDialog; inline bool forbidOpenSearchDialog;
@ -602,7 +607,7 @@ namespace {
global::systemInfoText = "<b>InkBox OS version "; global::systemInfoText = "<b>InkBox OS version ";
string_checkconfig_ro("/external_root/opt/isa/version"); string_checkconfig_ro("/external_root/opt/isa/version");
global::systemInfoText.append(checkconfig_str_val); global::systemInfoText.append(checkconfig_str_val);
global::systemInfoText.append("</b>"); global::systemInfoText.append("</b><br>Copyright <font face='Inter'>©</font> 2021-2022 Nicolas Mailloux and contributors");
global::systemInfoText.append("<br><b>Git:</b> "); global::systemInfoText.append("<br><b>Git:</b> ");
global::systemInfoText.append(GIT_VERSION); global::systemInfoText.append(GIT_VERSION);
global::systemInfoText.append("<br><b>Device UID:</b> "); global::systemInfoText.append("<br><b>Device UID:</b> ");
@ -988,7 +993,6 @@ namespace {
} }
QJsonObject getBookMetadata(int bookID) { QJsonObject getBookMetadata(int bookID) {
// Read library database from file // Read library database from file
log("Reading database", "functions");
QFile database(global::localLibrary::databasePath); QFile database(global::localLibrary::databasePath);
QByteArray data; QByteArray data;
if(database.open(QIODevice::ReadOnly)) { if(database.open(QIODevice::ReadOnly)) {

View file

@ -15,6 +15,10 @@ homePageWidget::homePageWidget(QWidget *parent) :
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);
pinnedBooksHorizontalLayoutArray.resize(global::homePageWidget::pinnedBooksRowNumber);
pinnedBooksVerticalLayoutArray.resize(global::homePageWidget::pinnedBooksNumber);
pinnedBooksBtnArray.resize(global::homePageWidget::pinnedBooksNumber);
pinnedBooksTitleArray.resize(global::homePageWidget::pinnedBooksNumber);
if(global::deviceID == "n705\n") { if(global::deviceID == "n705\n") {
bookTitleTruncateThreshold = 20; bookTitleTruncateThreshold = 20;
@ -106,6 +110,7 @@ void homePageWidget::setupDisplay(bool databaseGenerated) {
QString function = __func__; log(function + ": Failed to open local library database file for reading at '" + database.fileName() + "'", className); QString function = __func__; log(function + ": Failed to open local library database file for reading at '" + database.fileName() + "'", className);
} }
/* Recent books */
log("Reading recent books database", className); log("Reading recent books database", className);
QFile recentBooksDatabase(global::localLibrary::recentBooksDatabasePath); QFile recentBooksDatabase(global::localLibrary::recentBooksDatabasePath);
QByteArray recentBooksData; QByteArray recentBooksData;
@ -150,6 +155,7 @@ void homePageWidget::setupDisplay(bool databaseGenerated) {
// Book icon button // Book icon button
QObject::connect(bookBtnArray[i], &QClickableLabel::bookPath, this, &homePageWidget::openBook); QObject::connect(bookBtnArray[i], &QClickableLabel::bookPath, this, &homePageWidget::openBook);
QObject::connect(bookBtnArray[i], &QClickableLabel::longPressString, this, &homePageWidget::openBookOptionsDialog);
bookBtnArray[i]->setAlignment(Qt::AlignCenter); bookBtnArray[i]->setAlignment(Qt::AlignCenter);
bookBtnArray[i]->setFont(QFont("u001")); bookBtnArray[i]->setFont(QFont("u001"));
bookBtnArray[i]->setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px"); bookBtnArray[i]->setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px");
@ -203,7 +209,7 @@ void homePageWidget::setupDisplay(bool databaseGenerated) {
} }
// Hiding books that don't exist/are not in the database // Hiding books that don't exist/are not in the database
// If you think this is not an efficient way to do it, try to teach the code above not create the button if the book does not exist and arrange layouts so that it looks linear // If you think this is not an efficient way to do it, try to teach the code above not to create the button if the book does not exist and arrange layouts so that it looks linear
for(int i = 1; i <= global::homePageWidget::recentBooksNumber; i++) { for(int i = 1; i <= global::homePageWidget::recentBooksNumber; i++) {
QString bookPath = recentBooksJsonObject["Book" + QString::number(i)].toObject().value("BookPath").toString(); QString bookPath = recentBooksJsonObject["Book" + QString::number(i)].toObject().value("BookPath").toString();
if(bookPath.isEmpty()) { if(bookPath.isEmpty()) {
@ -216,11 +222,6 @@ void homePageWidget::setupDisplay(bool databaseGenerated) {
} }
else { else {
log("No books found in recent books database", className); log("No books found in recent books database", className);
QFrame * line = new QFrame(this);
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Plain);
line->setLineWidth(1);
ui->booksVerticalLayout->addWidget(line);
QLabel * noRecentBooksLabel = new QLabel(this); QLabel * noRecentBooksLabel = new QLabel(this);
noRecentBooksLabel->setFont(QFont("Chivo")); noRecentBooksLabel->setFont(QFont("Chivo"));
noRecentBooksLabel->setAlignment(Qt::AlignCenter); noRecentBooksLabel->setAlignment(Qt::AlignCenter);
@ -228,6 +229,110 @@ void homePageWidget::setupDisplay(bool databaseGenerated) {
noRecentBooksLabel->setText("Recently read books will appear here"); noRecentBooksLabel->setText("Recently read books will appear here");
ui->booksVerticalLayout->addWidget(noRecentBooksLabel); ui->booksVerticalLayout->addWidget(noRecentBooksLabel);
} }
/* Pinned books */
if(QFile::exists(global::localLibrary::pinnedBooksDatabasePath)) {
log("Reading pinned books database", className);
QFile pinnedBooksDatabase(global::localLibrary::pinnedBooksDatabasePath);
QByteArray pinnedBooksData;
if(pinnedBooksDatabase.open(QIODevice::ReadOnly)) {
pinnedBooksData = pinnedBooksDatabase.readAll();
pinnedBooksDatabase.close();
}
else {
QString function = __func__; log(function + ": Failed to open pinned books database file for reading at '" + pinnedBooksDatabase.fileName() + "'", className);
}
QJsonObject pinnedBooksJsonObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(pinnedBooksData))).object();
for(int i = 1; i <= global::homePageWidget::pinnedBooksNumber; i++) {
if(!pinnedBooksJsonObject["Book" + QString::number(i)].toObject().value("BookPath").toString().isEmpty()) {
pinnedBooksDatabaseEmpty = false;
break;
}
}
if(pinnedBooksDatabaseEmpty == false) {
QHBoxLayout * pinnedBooksHorizontalLayoutArray = new QHBoxLayout();
for(int i = 1; i <= global::homePageWidget::pinnedBooksNumber; i++) {
pinnedBooksBtnArray[i] = new QClickableLabel(this);
pinnedBooksTitleArray[i] = new QToolTipLabel(this);
pinnedBooksVerticalLayoutArray[i] = new QVBoxLayout();
// Book icon button
QObject::connect(pinnedBooksBtnArray[i], &QClickableLabel::bookPath, this, &homePageWidget::openBook);
QObject::connect(pinnedBooksBtnArray[i], &QClickableLabel::longPressString, this, &homePageWidget::openBookOptionsDialog);
pinnedBooksBtnArray[i]->setAlignment(Qt::AlignCenter);
pinnedBooksBtnArray[i]->setFont(QFont("u001"));
pinnedBooksBtnArray[i]->setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px");
// Book title label
pinnedBooksTitleArray[i]->setWordWrap(true);
pinnedBooksTitleArray[i]->setAlignment(Qt::AlignCenter);
pinnedBooksTitleArray[i]->setFont(QFont("u001"));
pinnedBooksTitleArray[i]->setStyleSheet("font-size: 7pt");
// Iterate until we find a book matching the pinned book's "BookPath" key/value pair
for(int in = 1; in <= databaseBooksNumber; in++) {
QJsonObject bookJsonObject = databaseJsonArrayList.at(in - 1).toObject();
QString bookPath = pinnedBooksJsonObject["Book" + QString::number(i)].toObject().value("BookPath").toString();
if(bookJsonObject["BookPath"].toString() == bookPath) {
QByteArray data = qCompress(QJsonDocument(bookJsonObject).toJson()).toBase64();
QString dataString = QString(data);
pinnedBooksBtnArray[i]->setObjectName(dataString);
}
}
QJsonObject uncompressedJsonObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(pinnedBooksBtnArray[i]->objectName().toUtf8()))).object();
QString bookTitle = uncompressedJsonObject["Title"].toString();
pinnedBooksTitleArray[i]->setObjectName(bookTitle);
int localBookTitleTruncateThreshold;
if(!bookTitle.contains(" ")) {
localBookTitleTruncateThreshold = bookTitleTruncateThreshold - 10;
}
else {
localBookTitleTruncateThreshold = bookTitleTruncateThreshold;
}
if(bookTitle.length() > localBookTitleTruncateThreshold) {
pinnedBooksTitleArray[i]->setProperty("showToolTip", "true");
bookTitle.truncate(localBookTitleTruncateThreshold);
bookTitle.append("...");
}
pinnedBooksTitleArray[i]->setText(bookTitle);
QString bookIcon = uncompressedJsonObject["CoverPath"].toString();
if(QFile::exists(bookIcon)) {
pinnedBooksBtnArray[i]->setPixmap(QPixmap(bookIcon).scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
}
else {
pinnedBooksBtnArray[i]->setPixmap(QPixmap(":/resources/cover_unavailable").scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
}
pinnedBooksVerticalLayoutArray[i]->addWidget(pinnedBooksBtnArray[i]);
pinnedBooksVerticalLayoutArray[i]->addWidget(pinnedBooksTitleArray[i]);
pinnedBooksHorizontalLayoutArray->addLayout(pinnedBooksVerticalLayoutArray[i]);
}
ui->pinnedBooksVerticalLayout->addLayout(pinnedBooksHorizontalLayoutArray);
// Hiding books that don't exist/are not in the database
for(int i = 1; i <= global::homePageWidget::pinnedBooksNumber; i++) {
QString bookPath = pinnedBooksJsonObject["Book" + QString::number(i)].toObject().value("BookPath").toString();
if(bookPath.isEmpty()) {
pinnedBooksBtnArray[i]->hide();
pinnedBooksTitleArray[i]->hide();
pinnedBooksBtnArray[i]->deleteLater();
pinnedBooksTitleArray[i]->deleteLater();
}
}
}
}
if(pinnedBooksDatabaseEmpty == true) {
log("No books found in pinned books database", className);
QLabel * noPinnedBooksLabel = new QLabel(this);
noPinnedBooksLabel->setFont(QFont("Chivo"));
noPinnedBooksLabel->setAlignment(Qt::AlignCenter);
noPinnedBooksLabel->setStyleSheet("padding: 30px");
noPinnedBooksLabel->setText("Pinned books will appear here");
ui->pinnedBooksVerticalLayout->addWidget(noPinnedBooksLabel);
}
QTimer::singleShot(500, this, SLOT(refreshScreenNative())); QTimer::singleShot(500, this, SLOT(refreshScreenNative()));
} }
@ -249,3 +354,56 @@ void homePageWidget::showToast(QString messageToDisplay) {
toastWindow->setAttribute(Qt::WA_DeleteOnClose); toastWindow->setAttribute(Qt::WA_DeleteOnClose);
toastWindow->show(); toastWindow->show();
} }
void homePageWidget::openBookOptionsDialog(QString bookPath) {
int bookID;
// Finding bookID from bookPath
log("Reading database", className);
QFile database(global::localLibrary::databasePath);
QByteArray data;
if(database.open(QIODevice::ReadOnly)) {
data = database.readAll();
database.close();
}
else {
QString function = __func__; log(function + ": Failed to open local library database file for reading at '" + database.fileName() + "'", className);
}
QJsonObject databaseJsonObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(data))).object();
QJsonArray databaseJsonArrayList = databaseJsonObject["database"].toArray();
int databaseBooksNumber = databaseJsonArrayList.size();
for(int in = 1; in <= databaseBooksNumber; in++) {
QJsonObject bookJsonObject = databaseJsonArrayList.at(in - 1).toObject();
if(bookJsonObject["BookPath"].toString() == bookPath) {
// Because 'bookID = bookJsonObject["BookID"].toInt();' always returns value 0 ;)
QString bookIDStr = bookJsonObject["BookID"].toString();
bookID = bookIDStr.toInt();
}
}
log("Opening book options dialog for book with path '" + bookPath + "', ID " + QString::number(bookID), className);
global::localLibrary::bookOptionsDialog::bookID = bookID;
global::localLibrary::bookOptionsDialog::deleteOption = false;
bookOptionsDialog * bookOptionsDialogWindow = new bookOptionsDialog(this);
QObject::connect(bookOptionsDialogWindow, &bookOptionsDialog::openLocalBookInfoDialog, this, &homePageWidget::openLocalBookInfoDialog);
QObject::connect(bookOptionsDialogWindow, &bookOptionsDialog::showToast, this, &homePageWidget::showToast);
QObject::connect(bookOptionsDialogWindow, &bookOptionsDialog::destroyed, this, &homePageWidget::handlePossibleBookPin);
bookOptionsDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
bookOptionsDialogWindow->setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
bookOptionsDialogWindow->show();
}
void homePageWidget::openLocalBookInfoDialog() {
global::bookInfoDialog::localInfoDialog = true;
bookInfoDialog * bookInfoDialogWindow = new bookInfoDialog(this);
bookInfoDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
bookInfoDialogWindow->show();
}
void homePageWidget::handlePossibleBookPin() {
if(global::localLibrary::bookOptionsDialog::bookPinAction == true) {
QTimer::singleShot(3100, this, [&]() {
emit relaunchHomePageWidget();
});
}
}

View file

@ -7,6 +7,8 @@
#include "qtooltiplabel.h" #include "qtooltiplabel.h"
#include "locallibrarywidget.h" #include "locallibrarywidget.h"
#include "toast.h" #include "toast.h"
#include "bookoptionsdialog.h"
#include "bookinfodialog.h"
namespace Ui { namespace Ui {
class homePageWidget; class homePageWidget;
@ -27,10 +29,12 @@ public:
float stdIconWidthDivider; float stdIconWidthDivider;
float stdIconHeightDivider; float stdIconHeightDivider;
int bookTitleTruncateThreshold; int bookTitleTruncateThreshold;
bool pinnedBooksDatabaseEmpty = true;
signals: signals:
void openBookSignal(QString bookPath, bool relativePath); void openBookSignal(QString bookPath, bool relativePath);
void refreshScreen(); void refreshScreen();
void relaunchHomePageWidget();
private slots: private slots:
void openBook(QString bookPath); void openBook(QString bookPath);
@ -39,15 +43,24 @@ private slots:
void setupDisplaySlot(); void setupDisplaySlot();
void setupDisplayWithDatabase(); void setupDisplayWithDatabase();
void showToast(QString messageToDisplay); void showToast(QString messageToDisplay);
void openBookOptionsDialog(QString bookPath);
void openLocalBookInfoDialog();
void handlePossibleBookPin();
private: private:
Ui::homePageWidget *ui; Ui::homePageWidget *ui;
localLibraryWidget * localLibraryWidgetWindow; localLibraryWidget * localLibraryWidgetWindow;
toast * toastWindow; toast * toastWindow;
bookOptionsDialog * bookOptionsDialogWindow;
bookInfoDialog * bookInfoDialogWindow;
QVector<QToolTipLabel*> bookTitleArray; QVector<QToolTipLabel*> bookTitleArray;
QVector<QHBoxLayout*> horizontalLayoutArray; QVector<QHBoxLayout*> horizontalLayoutArray;
QVector<QVBoxLayout*> verticalLayoutArray; QVector<QVBoxLayout*> verticalLayoutArray;
QVector<QClickableLabel*> bookBtnArray; QVector<QClickableLabel*> bookBtnArray;
QVector<QToolTipLabel*> pinnedBooksTitleArray;
QVector<QHBoxLayout*> pinnedBooksHorizontalLayoutArray;
QVector<QVBoxLayout*> pinnedBooksVerticalLayoutArray;
QVector<QClickableLabel*> pinnedBooksBtnArray;
}; };
#endif // HOMEPAGEWIDGET_H #endif // HOMEPAGEWIDGET_H

View file

@ -14,6 +14,42 @@
<string>Form</string> <string>Form</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin"> <property name="leftMargin">
<number>0</number> <number>0</number>
</property> </property>
@ -49,6 +85,19 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="Line" name="line_2">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>4</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item> <item>
<layout class="QVBoxLayout" name="booksVerticalLayout"> <layout class="QVBoxLayout" name="booksVerticalLayout">
<property name="sizeConstraint"> <property name="sizeConstraint">
@ -64,11 +113,43 @@
<property name="frameShadow"> <property name="frameShadow">
<enum>QFrame::Plain</enum> <enum>QFrame::Plain</enum>
</property> </property>
<property name="lineWidth">
<number>4</number>
</property>
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Pinned books</string>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line_3">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>4</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="pinnedBooksVerticalLayout"/>
</item>
<item> <item>
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">
@ -84,6 +165,10 @@
</item> </item>
</layout> </layout>
</widget> </widget>
</widget>
</item>
</layout>
</widget>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>

View file

@ -94,7 +94,7 @@ localLibraryWidget::localLibraryWidget(QWidget *parent) :
bookBtnArray[i] = new QClickableLabel(this); bookBtnArray[i] = new QClickableLabel(this);
bookBtnArray[i]->setObjectName(QString::number(i)); bookBtnArray[i]->setObjectName(QString::number(i));
QObject::connect(bookBtnArray[i], &QClickableLabel::bookID, this, &localLibraryWidget::btnOpenBook); QObject::connect(bookBtnArray[i], &QClickableLabel::bookID, this, &localLibraryWidget::btnOpenBook);
QObject::connect(bookBtnArray[i], &QClickableLabel::longPress, this, &localLibraryWidget::openBookOptionsDialog); QObject::connect(bookBtnArray[i], &QClickableLabel::longPressInt, this, &localLibraryWidget::openBookOptionsDialog);
bookBtnArray[i]->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); bookBtnArray[i]->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
bookBtnArray[i]->setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px"); bookBtnArray[i]->setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px");
bookBtnArray[i]->setFont(QFont("u001")); bookBtnArray[i]->setFont(QFont("u001"));
@ -341,8 +341,14 @@ void localLibraryWidget::refreshScreenNative() {
void localLibraryWidget::setupDisplay() { void localLibraryWidget::setupDisplay() {
setupDatabase(); setupDatabase();
if(noBooksInDatabase == false) { if(noBooksInDatabase == false) {
// Prevent segmentation fault if a book was the last of its page
if(currentPageNumber > pagesNumber) {
setupBooksList(currentPageNumber - 1);
}
else {
setupBooksList(currentPageNumber); setupBooksList(currentPageNumber);
} }
}
else { else {
ui->previousPageBtn->setEnabled(false); ui->previousPageBtn->setEnabled(false);
ui->nextPageBtn->setEnabled(false); ui->nextPageBtn->setEnabled(false);

View file

@ -551,7 +551,13 @@ void MainWindow::on_brightnessBtn_clicked()
void MainWindow::on_homeBtn_clicked() void MainWindow::on_homeBtn_clicked()
{ {
log("Showing home screen", className); log("Showing home screen", className);
if(global::localLibrary::bookOptionsDialog::bookPinAction == true) {
global::localLibrary::bookOptionsDialog::bookPinAction = false;
global::mainwindow::tabSwitcher::repaint = false;
}
else {
global::mainwindow::tabSwitcher::repaint = true; global::mainwindow::tabSwitcher::repaint = true;
}
resetFullWindowException = true; resetFullWindowException = true;
resetWindow(true); resetWindow(true);
} }
@ -1027,6 +1033,7 @@ void MainWindow::setupHomePageWidget() {
homePageWidgetWindow = new homePageWidget(this); homePageWidgetWindow = new homePageWidget(this);
QObject::connect(homePageWidgetWindow, &homePageWidget::openBookSignal, this, &MainWindow::openBookFile); QObject::connect(homePageWidgetWindow, &homePageWidget::openBookSignal, this, &MainWindow::openBookFile);
QObject::connect(homePageWidgetWindow, &homePageWidget::refreshScreen, this, &MainWindow::refreshScreen); QObject::connect(homePageWidgetWindow, &homePageWidget::refreshScreen, this, &MainWindow::refreshScreen);
QObject::connect(homePageWidgetWindow, &homePageWidget::relaunchHomePageWidget, this, &MainWindow::on_homeBtn_clicked);
homePageWidgetWindow->setAttribute(Qt::WA_DeleteOnClose); homePageWidgetWindow->setAttribute(Qt::WA_DeleteOnClose);
ui->homeStackedWidget->insertWidget(2, homePageWidgetWindow); ui->homeStackedWidget->insertWidget(2, homePageWidgetWindow);
ui->homeStackedWidget->setCurrentIndex(2); ui->homeStackedWidget->setCurrentIndex(2);

View file

@ -4,9 +4,7 @@
#include "qclickablelabel.h" #include "qclickablelabel.h"
QClickableLabel::QClickableLabel(QWidget* parent, Qt::WindowFlags f) QClickableLabel::QClickableLabel(QWidget* parent, Qt::WindowFlags f)
: QLabel(parent) { : QLabel(parent) {}
}
QClickableLabel::~QClickableLabel() {} QClickableLabel::~QClickableLabel() {}
@ -23,13 +21,18 @@ void QClickableLabel::mousePressEvent(QMouseEvent * event) {
void QClickableLabel::mouseReleaseEvent(QMouseEvent * event) { void QClickableLabel::mouseReleaseEvent(QMouseEvent * event) {
if(QDateTime::currentMSecsSinceEpoch() >= timeAtClick + 500) { if(QDateTime::currentMSecsSinceEpoch() >= timeAtClick + 500) {
emit longPress(objectName().toInt()); if(objectName().toInt()) {
emit longPressInt(objectName().toInt());
}
else {
emit longPressString(QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(objectName().toUtf8()))).object()["BookPath"].toString());
}
} }
else { else {
emit unclicked(); emit unclicked();
emit bookID(objectName().toInt()); emit bookID(objectName().toInt());
}
emit bookPath(QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(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("border-radius: 10px; padding-left: 10px; padding-right: 10px"); QClickableLabel::setStyleSheet("border-radius: 10px; padding-left: 10px; padding-right: 10px");
} }

View file

@ -17,7 +17,8 @@ signals:
void unclicked(); void unclicked();
void bookID(int id); void bookID(int id);
void bookPath(QString path); void bookPath(QString path);
void longPress(int id); void longPressInt(int id);
void longPressString(QString bookPath);
protected: protected:
void mousePressEvent(QMouseEvent * event); void mousePressEvent(QMouseEvent * event);