localLibraryWidget: Implement book options dialog (WIP)

This dialog will open if user keeps touch input active on a button from `bookBtnArray` for >= 500 ms.
'Pin' and 'More info' operations are currently not implemented.
This commit is contained in:
Nicolas Mailloux 2022-07-06 14:24:23 -04:00
parent 35fe0ae0e3
commit 80bdeb32ed
12 changed files with 393 additions and 119 deletions

View file

@ -42,125 +42,149 @@
</layout>
</item>
<item>
<layout class="QGridLayout" name="gridLayout">
<property name="horizontalSpacing">
<number>18</number>
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>0</number>
</property>
<item row="0" column="1">
<widget class="QLabel" name="bookCoverLabel">
<property name="text">
<string>Cover</string>
<widget class="QWidget" name="page">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
<property name="topMargin">
<number>0</number>
</property>
</widget>
</item>
<item row="0" column="3">
<layout class="QGridLayout" name="gridLayout_2">
<property name="verticalSpacing">
<number>18</number>
<property name="rightMargin">
<number>0</number>
</property>
<item row="1" column="0">
<widget class="QLabel" name="bookTitleLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QGridLayout" name="gridLayout">
<property name="horizontalSpacing">
<number>18</number>
</property>
<property name="text">
<string>Book title</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="getBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>GET</string>
</property>
</widget>
</item>
<item row="3" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
<item row="0" column="1">
<widget class="QLabel" name="bookCoverLabel">
<property name="text">
<string>Cover</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="0" column="3">
<layout class="QGridLayout" name="gridLayout_2">
<property name="verticalSpacing">
<number>18</number>
</property>
<item row="1" column="0">
<widget class="QLabel" name="bookTitleLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Book title</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="getBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>GET</string>
</property>
</widget>
</item>
<item row="3" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="0" column="4">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
<item row="0" column="4">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2"/>
</widget>
</item>
</layout>
</widget>

43
bookoptionsdialog.cpp Normal file
View file

@ -0,0 +1,43 @@
#include "bookoptionsdialog.h"
#include "ui_bookoptionsdialog.h"
#include "functions.h"
bookOptionsDialog::bookOptionsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::bookOptionsDialog)
{
ui->setupUi(this);
ui->pinBtn->setProperty("type", "borderless");
ui->deleteBtn->setProperty("type", "borderless");
ui->infoBtn->setProperty("type", "borderless");
bookPath = getBookMetadata(global::localLibrary::bookOptionsDialog::bookID)["BookPath"].toString();
}
bookOptionsDialog::~bookOptionsDialog()
{
delete ui;
}
void bookOptionsDialog::on_pinBtn_clicked()
{
// TODO
}
void bookOptionsDialog::on_deleteBtn_clicked()
{
log("Deleting book '" + bookPath + "'", className);
QFile::remove(bookPath);
global::localLibrary::bookOptionsDialog::bookDeleted = true;
QFile::remove(global::localLibrary::databasePath);
ui->deleteBtn->setEnabled(false);
ui->deleteBtn->setStyleSheet("padding: 10px; color: lightGrey");
}
void bookOptionsDialog::on_infoBtn_clicked()
{
// TODO
}

30
bookoptionsdialog.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef BOOKOPTIONSDIALOG_H
#define BOOKOPTIONSDIALOG_H
#include <QDialog>
#include <QJsonObject>
namespace Ui {
class bookOptionsDialog;
}
class bookOptionsDialog : public QDialog
{
Q_OBJECT
public:
QString className = this->metaObject()->className();
explicit bookOptionsDialog(QWidget *parent = nullptr);
~bookOptionsDialog();
QString bookPath;
private slots:
void on_pinBtn_clicked();
void on_deleteBtn_clicked();
void on_infoBtn_clicked();
private:
Ui::bookOptionsDialog *ui;
};
#endif // BOOKOPTIONSDIALOG_H

111
bookoptionsdialog.ui Normal file
View file

@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>bookOptionsDialog</class>
<widget class="QDialog" name="bookOptionsDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>18</number>
</property>
<property name="topMargin">
<number>22</number>
</property>
<property name="rightMargin">
<number>18</number>
</property>
<property name="bottomMargin">
<number>22</number>
</property>
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="0">
<widget class="QPushButton" name="infoBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">padding: 10px</string>
</property>
<property name="text">
<string>More info</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QPushButton" name="pinBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">padding: 10px</string>
</property>
<property name="text">
<string>Pin</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="Line" name="line">
<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 row="2" column="0">
<widget class="QPushButton" name="deleteBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">padding: 10px</string>
</property>
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
<item row="3" column="0">
<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>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View file

@ -128,9 +128,15 @@ namespace global {
}
namespace localLibrary {
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";
static inline QString databaseDirectoryPath = "/mnt/onboard/onboard/.database/";
static inline QString databasePath = databaseDirectoryPath + "LocalLibrary.db";
static inline QString recentBooksDatabasePath = databaseDirectoryPath + "RecentBooks.db";
static inline QString pinnedBooksDatabasePath = databaseDirectoryPath + "PinnedBooks.db";
inline bool headless;
namespace bookOptionsDialog {
inline int bookID;
inline bool bookDeleted;
}
}
namespace localStorage {
inline QStringList searchResultsPaths;
@ -977,6 +983,24 @@ 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)) {
data = database.readAll();
database.close();
}
else {
QString function = __func__; log(function + ": Failed to open local library database file for reading at '" + database.fileName() + "'", "functions");
}
// Parse JSON data
QJsonObject jsonObject = QJsonDocument::fromJson(qUncompress(QByteArray::fromBase64(data))).object();
QJsonArray jsonArrayList = jsonObject["database"].toArray();
return jsonArrayList.at(bookID - 1).toObject();
}
}
#endif // FUNCTIONS_H

View file

@ -78,7 +78,12 @@ homePageWidget::~homePageWidget()
}
void homePageWidget::openBook(QString bookPath) {
emit openBookSignal(bookPath, false);
if(!bookPath.isEmpty()) {
emit openBookSignal(bookPath, false);
}
else {
showToast("Book not found");
}
}
void homePageWidget::refreshScreenNative() {

View file

@ -27,6 +27,7 @@ SOURCES += \
alert.cpp \
apps.cpp \
bookinfodialog.cpp \
bookoptionsdialog.cpp \
brightnessdialog.cpp \
calendarapp.cpp \
dictionarywidget.cpp \
@ -61,6 +62,7 @@ HEADERS += \
alert.h \
apps.h \
bookinfodialog.h \
bookoptionsdialog.h \
brightnessdialog.h \
calendarapp.h \
dictionarywidget.h \
@ -95,6 +97,7 @@ FORMS += \
alert.ui \
apps.ui \
bookinfodialog.ui \
bookoptionsdialog.ui \
brightnessdialog.ui \
calendarapp.ui \
dictionarywidget.ui \

View file

@ -93,7 +93,8 @@ localLibraryWidget::localLibraryWidget(QWidget *parent) :
// Book button
bookBtnArray[i] = new QClickableLabel(this);
bookBtnArray[i]->setObjectName(QString::number(i));
connect(bookBtnArray[i], SIGNAL(bookID(int)), SLOT(btnOpenBook(int)));
QObject::connect(bookBtnArray[i], &QClickableLabel::bookID, this, &localLibraryWidget::btnOpenBook);
QObject::connect(bookBtnArray[i], &QClickableLabel::longPress, 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"));
@ -356,3 +357,23 @@ void localLibraryWidget::showToast(QString messageToDisplay) {
toastWindow->setAttribute(Qt::WA_DeleteOnClose);
toastWindow->show();
}
void localLibraryWidget::openBookOptionsDialog(int bookID) {
log("Opening book options dialog for book with ID " + QString::number(bookID), className);
global::localLibrary::bookOptionsDialog::bookID = bookID;
bookOptionsDialog * bookOptionsDialogWindow = new bookOptionsDialog(this);
QObject::connect(bookOptionsDialogWindow, &bookOptionsDialog::destroyed, this, &localLibraryWidget::handlePossibleBookDeletion);
bookOptionsDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
bookOptionsDialogWindow->setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
bookOptionsDialogWindow->show();
}
void localLibraryWidget::handlePossibleBookDeletion() {
if(global::localLibrary::bookOptionsDialog::bookDeleted == true) {
global::localLibrary::bookOptionsDialog::bookDeleted = false;
global::toast::modalToast = true;
global::toast::indefiniteToast = true;
showToast("Generating database");
QTimer::singleShot(100, this, SLOT(setupDisplay()));
}
}

View file

@ -9,6 +9,7 @@
#include "qclickablelabel.h"
#include "generaldialog.h"
#include "toast.h"
#include "bookoptionsdialog.h"
namespace Ui {
class localLibraryWidget;
@ -52,10 +53,13 @@ private slots:
void goToPage(int page);
void setupDisplay();
void showToast(QString messageToDisplay);
void openBookOptionsDialog(int bookID);
void handlePossibleBookDeletion();
private:
Ui::localLibraryWidget * ui;
generalDialog * generalDialogWindow;
bookOptionsDialog * bookOptionsDialogWindow;
toast * toastWindow;
QVector<QHBoxLayout*> horizontalLayoutArray;
QVector<QLabel*> bookIconArray;

View file

@ -11,6 +11,7 @@ QClickableLabel::QClickableLabel(QWidget* parent, Qt::WindowFlags f)
QClickableLabel::~QClickableLabel() {}
void QClickableLabel::mousePressEvent(QMouseEvent * event) {
timeAtClick = QDateTime::currentMSecsSinceEpoch();
emit clicked();
if(objectName() == "pageNumberLabel") {
QClickableLabel::setStyleSheet("border-radius: 10px; padding-left: 10px; padding-right: 10px");
@ -21,8 +22,13 @@ void QClickableLabel::mousePressEvent(QMouseEvent * event) {
}
void QClickableLabel::mouseReleaseEvent(QMouseEvent * event) {
emit unclicked();
emit bookID(objectName().toInt());
if(QDateTime::currentMSecsSinceEpoch() >= timeAtClick + 500) {
emit longPress(objectName().toInt());
}
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");

View file

@ -10,12 +10,14 @@ class QClickableLabel : public QLabel {
public:
explicit QClickableLabel(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
~QClickableLabel();
long long timeAtClick;
signals:
void clicked();
void unclicked();
void bookID(int id);
void bookPath(QString path);
void longPress(int id);
protected:
void mousePressEvent(QMouseEvent * event);

View file

@ -156,8 +156,9 @@ void userapps::on_launchBtn_clicked()
if(manageRequiredFeatures() == true) {
if(global::userApps::launchApp == true) {
global::userApps::launchApp = false;
QString message = "Launching user application at: ";
QString message = "Launching user application at: '";
message.append("[" + appDir.path() + "]" + execPath.fileName());
message.append("'");
log(message, className);
// Tell the OS that we're not running anymore
string_writeconfig("/tmp/inkbox_running", "false");