Home page: Implement QToolTipLabel and book titles

This commit is contained in:
Nicolas Mailloux 2022-06-29 13:11:11 -04:00
parent ebcfd349f2
commit 5c50911642
8 changed files with 91 additions and 3 deletions

View file

@ -16,6 +16,16 @@ homePageWidget::homePageWidget(QWidget *parent) :
bookBtnArray.resize(global::homePageWidget::recentBooksNumber); bookBtnArray.resize(global::homePageWidget::recentBooksNumber);
bookTitleArray.resize(global::homePageWidget::recentBooksNumber); bookTitleArray.resize(global::homePageWidget::recentBooksNumber);
if(global::deviceID == "n705\n") {
bookTitleTruncateThreshold = 20;
}
else if(global::deviceID == "n873\n") {
bookTitleTruncateThreshold = 35;
}
else {
bookTitleTruncateThreshold = 25;
}
// Getting the screen's size // Getting the screen's size
sW = QGuiApplication::screens()[0]->size().width(); sW = QGuiApplication::screens()[0]->size().width();
sH = QGuiApplication::screens()[0]->size().height(); sH = QGuiApplication::screens()[0]->size().height();
@ -77,6 +87,7 @@ homePageWidget::homePageWidget(QWidget *parent) :
QJsonObject jsonObject = recentBooksJsonObject[objectName].toObject(); QJsonObject jsonObject = recentBooksJsonObject[objectName].toObject();
QString bookPath = jsonObject.value("BookPath").toString(); QString bookPath = jsonObject.value("BookPath").toString();
bookBtnArray[i] = new QClickableLabel(this); bookBtnArray[i] = new QClickableLabel(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 = i; in <= databaseBooksNumber; in++) {
@ -88,10 +99,32 @@ homePageWidget::homePageWidget(QWidget *parent) :
verticalLayoutArray[i] = new QVBoxLayout(); verticalLayoutArray[i] = new QVBoxLayout();
// Book icon button
QObject::connect(bookBtnArray[i], &QClickableLabel::bookPath, this, &homePageWidget::openBook); QObject::connect(bookBtnArray[i], &QClickableLabel::bookPath, this, &homePageWidget::openBook);
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");
// Book title label
bookTitleArray[i]->setWordWrap(true);
bookTitleArray[i]->setAlignment(Qt::AlignCenter);
bookTitleArray[i]->setFont(QFont("u001"));
bookTitleArray[i]->setStyleSheet("font-size: 7pt");
QString bookTitle = QJsonDocument::fromJson(bookBtnArray[i]->objectName().toUtf8()).object()["Title"].toString();
bookTitleArray[i]->setObjectName(bookTitle);
int localBookTitleTruncateThreshold;
if(!bookTitle.contains(" ")) {
localBookTitleTruncateThreshold = bookTitleTruncateThreshold - 10;
}
else {
localBookTitleTruncateThreshold = bookTitleTruncateThreshold;
}
if(bookTitle.length() > localBookTitleTruncateThreshold) {
bookTitle.truncate(localBookTitleTruncateThreshold);
bookTitle.append("...");
}
bookTitleArray[i]->setText(bookTitle);
QString bookIcon = QJsonDocument::fromJson(bookBtnArray[i]->objectName().toUtf8()).object()["CoverPath"].toString(); QString bookIcon = QJsonDocument::fromJson(bookBtnArray[i]->objectName().toUtf8()).object()["CoverPath"].toString();
if(QFile::exists(bookIcon)) { if(QFile::exists(bookIcon)) {
@ -102,6 +135,7 @@ homePageWidget::homePageWidget(QWidget *parent) :
} }
verticalLayoutArray[i]->addWidget(bookBtnArray[i]); verticalLayoutArray[i]->addWidget(bookBtnArray[i]);
verticalLayoutArray[i]->addWidget(bookTitleArray[i]);
} }
if(newRow == true) { if(newRow == true) {

View file

@ -4,6 +4,7 @@
#include <QWidget> #include <QWidget>
#include <QVBoxLayout> #include <QVBoxLayout>
#include "qclickablelabel.h" #include "qclickablelabel.h"
#include "qtooltiplabel.h"
namespace Ui { namespace Ui {
class homePageWidget; class homePageWidget;
@ -23,6 +24,7 @@ public:
int stdIconHeight; int stdIconHeight;
float stdIconWidthDivider; float stdIconWidthDivider;
float stdIconHeightDivider; float stdIconHeightDivider;
int bookTitleTruncateThreshold;
signals: signals:
void openBookSignal(QString bookPath, bool relativePath); void openBookSignal(QString bookPath, bool relativePath);
@ -34,7 +36,7 @@ private slots:
private: private:
Ui::homePageWidget *ui; Ui::homePageWidget *ui;
QVector<QLabel*> bookTitleArray; QVector<QToolTipLabel*> bookTitleArray;
QVector<QHBoxLayout*> horizontalLayoutArray; QVector<QHBoxLayout*> horizontalLayoutArray;
QVector<QVBoxLayout*> verticalLayoutArray; QVector<QVBoxLayout*> verticalLayoutArray;
QVector<QClickableLabel*> bookBtnArray; QVector<QClickableLabel*> bookBtnArray;

View file

@ -55,10 +55,20 @@
<enum>QLayout::SetNoConstraint</enum> <enum>QLayout::SetNoConstraint</enum>
</property> </property>
<property name="topMargin"> <property name="topMargin">
<number>25</number> <number>0</number>
</property> </property>
</layout> </layout>
</item> </item>
<item>
<widget class="Line" name="line">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item> <item>
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">

View file

@ -42,6 +42,7 @@ SOURCES += \
mainwindow.cpp \ mainwindow.cpp \
otamanager.cpp \ otamanager.cpp \
qclickablelabel.cpp \ qclickablelabel.cpp \
qtooltiplabel.cpp \
quit.cpp \ quit.cpp \
reader.cpp \ reader.cpp \
savedwords.cpp \ savedwords.cpp \
@ -75,6 +76,7 @@ HEADERS += \
mainwindow.h \ mainwindow.h \
otamanager.h \ otamanager.h \
qclickablelabel.h \ qclickablelabel.h \
qtooltiplabel.h \
quit.h \ quit.h \
reader.h \ reader.h \
savedwords.h \ savedwords.h \

View file

@ -448,6 +448,9 @@
<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>

View file

@ -3,7 +3,6 @@
#include <QLabel> #include <QLabel>
#include <QWidget> #include <QWidget>
#include <Qt>
class QClickableLabel : public QLabel { class QClickableLabel : public QLabel {
Q_OBJECT Q_OBJECT

18
qtooltiplabel.cpp Normal file
View file

@ -0,0 +1,18 @@
#include <QToolTip>
#include "qtooltiplabel.h"
QToolTipLabel::QToolTipLabel(QWidget* parent, Qt::WindowFlags f)
: QLabel(parent) {
this->setStyleSheet("QToolTip { font-size: 40pt }");
}
QToolTipLabel::~QToolTipLabel() {}
void QToolTipLabel::mousePressEvent(QMouseEvent * event) {
QToolTip::showText(mapToGlobal(QPoint(0, 0)), "<font face='u001' size=-2>" + objectName() + "</font>");
}
void QToolTipLabel::mouseReleaseEvent(QMouseEvent * event) {
}

20
qtooltiplabel.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef QTOOLTIPLABEL_H
#define QTOOLTIPLABEL_H
#include <QLabel>
#include <QWidget>
class QToolTipLabel : public QLabel {
Q_OBJECT
public:
explicit QToolTipLabel(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
~QToolTipLabel();
protected:
void mousePressEvent(QMouseEvent * event);
void mouseReleaseEvent(QMouseEvent * event);
};
#endif // CLICKABLELABEL_H