mirror of
https://github.com/Quill-OS/quill.git
synced 2024-12-26 07:37:21 -08:00
Implement 'Pin books' function
This commit is contained in:
parent
54633f1dc9
commit
13b3bda215
12 changed files with 509 additions and 70 deletions
|
@ -99,7 +99,6 @@ bookInfoDialog::bookInfoDialog(QWidget *parent) :
|
|||
bookInfo.append("<b>Path:</b> " + path + "<br>");
|
||||
}
|
||||
|
||||
log(bookInfo, className);
|
||||
global::text::textBrowserContents = bookInfo;
|
||||
textwidget * textwidgetWindow = new textwidget(this);
|
||||
ui->stackedWidget->insertWidget(1, textwidgetWindow);
|
||||
|
|
|
@ -9,10 +9,29 @@ bookOptionsDialog::bookOptionsDialog(QWidget *parent) :
|
|||
{
|
||||
ui->setupUi(this);
|
||||
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->infoBtn->setProperty("type", "borderless");
|
||||
|
||||
global::localLibrary::bookOptionsDialog::bookPinAction = false;
|
||||
|
||||
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()
|
||||
|
@ -22,8 +41,12 @@ bookOptionsDialog::~bookOptionsDialog()
|
|||
|
||||
void bookOptionsDialog::on_pinBtn_clicked()
|
||||
{
|
||||
// TODO
|
||||
log("Pinned book with ID " + QString::number(global::localLibrary::bookOptionsDialog::bookID), className);
|
||||
if(bookPinned == false) {
|
||||
pinBook(global::localLibrary::bookOptionsDialog::bookID);
|
||||
}
|
||||
else {
|
||||
unpinBook(global::localLibrary::bookOptionsDialog::bookID);
|
||||
}
|
||||
}
|
||||
|
||||
void bookOptionsDialog::on_deleteBtn_clicked()
|
||||
|
@ -44,3 +67,139 @@ void bookOptionsDialog::on_infoBtn_clicked()
|
|||
{
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -17,11 +17,15 @@ public:
|
|||
explicit bookOptionsDialog(QWidget *parent = nullptr);
|
||||
~bookOptionsDialog();
|
||||
QString bookPath;
|
||||
bool bookPinned;
|
||||
|
||||
private slots:
|
||||
void on_pinBtn_clicked();
|
||||
void on_deleteBtn_clicked();
|
||||
void on_infoBtn_clicked();
|
||||
void pinBook(int bookID);
|
||||
void unpinBook(int bookID);
|
||||
bool isBookPinned(int bookID);
|
||||
|
||||
signals:
|
||||
void openLocalBookInfoDialog();
|
||||
|
|
|
@ -138,7 +138,9 @@ namespace global {
|
|||
inline bool headless;
|
||||
namespace bookOptionsDialog {
|
||||
inline int bookID;
|
||||
inline bool deleteOption = true;
|
||||
inline bool bookDeleted;
|
||||
inline bool bookPinAction;
|
||||
}
|
||||
}
|
||||
namespace localStorage {
|
||||
|
@ -158,6 +160,9 @@ namespace global {
|
|||
static inline int recentBooksNumber = 8;
|
||||
static inline int recentBooksNumberPerRow = 4;
|
||||
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 bool forbidOpenSearchDialog;
|
||||
|
@ -602,7 +607,7 @@ namespace {
|
|||
global::systemInfoText = "<b>InkBox OS version ";
|
||||
string_checkconfig_ro("/external_root/opt/isa/version");
|
||||
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(GIT_VERSION);
|
||||
global::systemInfoText.append("<br><b>Device UID:</b> ");
|
||||
|
@ -988,7 +993,6 @@ namespace {
|
|||
}
|
||||
QJsonObject getBookMetadata(int bookID) {
|
||||
// Read library database from file
|
||||
log("Reading database", "functions");
|
||||
QFile database(global::localLibrary::databasePath);
|
||||
QByteArray data;
|
||||
if(database.open(QIODevice::ReadOnly)) {
|
||||
|
|
|
@ -15,6 +15,10 @@ homePageWidget::homePageWidget(QWidget *parent) :
|
|||
verticalLayoutArray.resize(global::homePageWidget::recentBooksNumber);
|
||||
bookBtnArray.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") {
|
||||
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);
|
||||
}
|
||||
|
||||
/* Recent books */
|
||||
log("Reading recent books database", className);
|
||||
QFile recentBooksDatabase(global::localLibrary::recentBooksDatabasePath);
|
||||
QByteArray recentBooksData;
|
||||
|
@ -150,6 +155,7 @@ void homePageWidget::setupDisplay(bool databaseGenerated) {
|
|||
|
||||
// Book icon button
|
||||
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]->setFont(QFont("u001"));
|
||||
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
|
||||
// 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++) {
|
||||
QString bookPath = recentBooksJsonObject["Book" + QString::number(i)].toObject().value("BookPath").toString();
|
||||
if(bookPath.isEmpty()) {
|
||||
|
@ -216,11 +222,6 @@ void homePageWidget::setupDisplay(bool databaseGenerated) {
|
|||
}
|
||||
else {
|
||||
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);
|
||||
noRecentBooksLabel->setFont(QFont("Chivo"));
|
||||
noRecentBooksLabel->setAlignment(Qt::AlignCenter);
|
||||
|
@ -228,6 +229,110 @@ void homePageWidget::setupDisplay(bool databaseGenerated) {
|
|||
noRecentBooksLabel->setText("Recently read books will appear here");
|
||||
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()));
|
||||
}
|
||||
|
||||
|
@ -249,3 +354,56 @@ void homePageWidget::showToast(QString messageToDisplay) {
|
|||
toastWindow->setAttribute(Qt::WA_DeleteOnClose);
|
||||
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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
#include "qtooltiplabel.h"
|
||||
#include "locallibrarywidget.h"
|
||||
#include "toast.h"
|
||||
#include "bookoptionsdialog.h"
|
||||
#include "bookinfodialog.h"
|
||||
|
||||
namespace Ui {
|
||||
class homePageWidget;
|
||||
|
@ -27,10 +29,12 @@ public:
|
|||
float stdIconWidthDivider;
|
||||
float stdIconHeightDivider;
|
||||
int bookTitleTruncateThreshold;
|
||||
bool pinnedBooksDatabaseEmpty = true;
|
||||
|
||||
signals:
|
||||
void openBookSignal(QString bookPath, bool relativePath);
|
||||
void refreshScreen();
|
||||
void relaunchHomePageWidget();
|
||||
|
||||
private slots:
|
||||
void openBook(QString bookPath);
|
||||
|
@ -39,15 +43,24 @@ private slots:
|
|||
void setupDisplaySlot();
|
||||
void setupDisplayWithDatabase();
|
||||
void showToast(QString messageToDisplay);
|
||||
void openBookOptionsDialog(QString bookPath);
|
||||
void openLocalBookInfoDialog();
|
||||
void handlePossibleBookPin();
|
||||
|
||||
private:
|
||||
Ui::homePageWidget *ui;
|
||||
localLibraryWidget * localLibraryWidgetWindow;
|
||||
toast * toastWindow;
|
||||
bookOptionsDialog * bookOptionsDialogWindow;
|
||||
bookInfoDialog * bookInfoDialogWindow;
|
||||
QVector<QToolTipLabel*> bookTitleArray;
|
||||
QVector<QHBoxLayout*> horizontalLayoutArray;
|
||||
QVector<QVBoxLayout*> verticalLayoutArray;
|
||||
QVector<QClickableLabel*> bookBtnArray;
|
||||
QVector<QToolTipLabel*> pinnedBooksTitleArray;
|
||||
QVector<QHBoxLayout*> pinnedBooksHorizontalLayoutArray;
|
||||
QVector<QVBoxLayout*> pinnedBooksVerticalLayoutArray;
|
||||
QVector<QClickableLabel*> pinnedBooksBtnArray;
|
||||
};
|
||||
|
||||
#endif // HOMEPAGEWIDGET_H
|
||||
|
|
|
@ -14,6 +14,42 @@
|
|||
<string>Form</string>
|
||||
</property>
|
||||
<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">
|
||||
<number>0</number>
|
||||
</property>
|
||||
|
@ -49,6 +85,19 @@
|
|||
</property>
|
||||
</widget>
|
||||
</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>
|
||||
<layout class="QVBoxLayout" name="booksVerticalLayout">
|
||||
<property name="sizeConstraint">
|
||||
|
@ -64,11 +113,43 @@
|
|||
<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>
|
||||
<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>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
|
@ -84,6 +165,10 @@
|
|||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
|
|
|
@ -94,7 +94,7 @@ localLibraryWidget::localLibraryWidget(QWidget *parent) :
|
|||
bookBtnArray[i] = new QClickableLabel(this);
|
||||
bookBtnArray[i]->setObjectName(QString::number(i));
|
||||
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]->setStyleSheet("color: black; background-color: white; border-radius: 10px; padding: 10px");
|
||||
bookBtnArray[i]->setFont(QFont("u001"));
|
||||
|
@ -341,8 +341,14 @@ void localLibraryWidget::refreshScreenNative() {
|
|||
void localLibraryWidget::setupDisplay() {
|
||||
setupDatabase();
|
||||
if(noBooksInDatabase == false) {
|
||||
// Prevent segmentation fault if a book was the last of its page
|
||||
if(currentPageNumber > pagesNumber) {
|
||||
setupBooksList(currentPageNumber - 1);
|
||||
}
|
||||
else {
|
||||
setupBooksList(currentPageNumber);
|
||||
}
|
||||
}
|
||||
else {
|
||||
ui->previousPageBtn->setEnabled(false);
|
||||
ui->nextPageBtn->setEnabled(false);
|
||||
|
|
|
@ -551,7 +551,13 @@ void MainWindow::on_brightnessBtn_clicked()
|
|||
void MainWindow::on_homeBtn_clicked()
|
||||
{
|
||||
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;
|
||||
}
|
||||
resetFullWindowException = true;
|
||||
resetWindow(true);
|
||||
}
|
||||
|
@ -1027,6 +1033,7 @@ void MainWindow::setupHomePageWidget() {
|
|||
homePageWidgetWindow = new homePageWidget(this);
|
||||
QObject::connect(homePageWidgetWindow, &homePageWidget::openBookSignal, this, &MainWindow::openBookFile);
|
||||
QObject::connect(homePageWidgetWindow, &homePageWidget::refreshScreen, this, &MainWindow::refreshScreen);
|
||||
QObject::connect(homePageWidgetWindow, &homePageWidget::relaunchHomePageWidget, this, &MainWindow::on_homeBtn_clicked);
|
||||
homePageWidgetWindow->setAttribute(Qt::WA_DeleteOnClose);
|
||||
ui->homeStackedWidget->insertWidget(2, homePageWidgetWindow);
|
||||
ui->homeStackedWidget->setCurrentIndex(2);
|
||||
|
|
|
@ -4,9 +4,7 @@
|
|||
#include "qclickablelabel.h"
|
||||
|
||||
QClickableLabel::QClickableLabel(QWidget* parent, Qt::WindowFlags f)
|
||||
: QLabel(parent) {
|
||||
|
||||
}
|
||||
: QLabel(parent) {}
|
||||
|
||||
QClickableLabel::~QClickableLabel() {}
|
||||
|
||||
|
@ -23,13 +21,18 @@ void QClickableLabel::mousePressEvent(QMouseEvent * event) {
|
|||
|
||||
void QClickableLabel::mouseReleaseEvent(QMouseEvent * event) {
|
||||
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 {
|
||||
emit unclicked();
|
||||
emit bookID(objectName().toInt());
|
||||
}
|
||||
emit bookPath(QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(objectName().toUtf8()))).object()["BookPath"].toString());
|
||||
}
|
||||
if(objectName() == "pageNumberLabel") {
|
||||
QClickableLabel::setStyleSheet("border-radius: 10px; padding-left: 10px; padding-right: 10px");
|
||||
}
|
||||
|
|
|
@ -17,7 +17,8 @@ signals:
|
|||
void unclicked();
|
||||
void bookID(int id);
|
||||
void bookPath(QString path);
|
||||
void longPress(int id);
|
||||
void longPressInt(int id);
|
||||
void longPressString(QString bookPath);
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent * event);
|
||||
|
|
Loading…
Reference in a new issue