From 5c50911642f5c71eae4f498777a92d90fa979412 Mon Sep 17 00:00:00 2001 From: Nicolas Mailloux Date: Wed, 29 Jun 2022 13:11:11 -0400 Subject: [PATCH] Home page: Implement QToolTipLabel and book titles --- homepagewidget.cpp | 34 ++++++++++++++++++++++++++++++++++ homepagewidget.h | 4 +++- homepagewidget.ui | 12 +++++++++++- inkbox.pro | 2 ++ mainwindow.ui | 3 +++ qclickablelabel.h | 1 - qtooltiplabel.cpp | 18 ++++++++++++++++++ qtooltiplabel.h | 20 ++++++++++++++++++++ 8 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 qtooltiplabel.cpp create mode 100644 qtooltiplabel.h diff --git a/homepagewidget.cpp b/homepagewidget.cpp index 606dff1..ceb8ee5 100644 --- a/homepagewidget.cpp +++ b/homepagewidget.cpp @@ -16,6 +16,16 @@ homePageWidget::homePageWidget(QWidget *parent) : bookBtnArray.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 sW = QGuiApplication::screens()[0]->size().width(); sH = QGuiApplication::screens()[0]->size().height(); @@ -77,6 +87,7 @@ homePageWidget::homePageWidget(QWidget *parent) : QJsonObject jsonObject = recentBooksJsonObject[objectName].toObject(); QString bookPath = jsonObject.value("BookPath").toString(); 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 for(int in = i; in <= databaseBooksNumber; in++) { @@ -88,10 +99,32 @@ homePageWidget::homePageWidget(QWidget *parent) : verticalLayoutArray[i] = new QVBoxLayout(); + // Book icon button 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"); + // 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(); if(QFile::exists(bookIcon)) { @@ -102,6 +135,7 @@ homePageWidget::homePageWidget(QWidget *parent) : } verticalLayoutArray[i]->addWidget(bookBtnArray[i]); + verticalLayoutArray[i]->addWidget(bookTitleArray[i]); } if(newRow == true) { diff --git a/homepagewidget.h b/homepagewidget.h index 5aa71d3..d03891b 100644 --- a/homepagewidget.h +++ b/homepagewidget.h @@ -4,6 +4,7 @@ #include #include #include "qclickablelabel.h" +#include "qtooltiplabel.h" namespace Ui { class homePageWidget; @@ -23,6 +24,7 @@ public: int stdIconHeight; float stdIconWidthDivider; float stdIconHeightDivider; + int bookTitleTruncateThreshold; signals: void openBookSignal(QString bookPath, bool relativePath); @@ -34,7 +36,7 @@ private slots: private: Ui::homePageWidget *ui; - QVector bookTitleArray; + QVector bookTitleArray; QVector horizontalLayoutArray; QVector verticalLayoutArray; QVector bookBtnArray; diff --git a/homepagewidget.ui b/homepagewidget.ui index bd60184..5d69b97 100644 --- a/homepagewidget.ui +++ b/homepagewidget.ui @@ -55,10 +55,20 @@ QLayout::SetNoConstraint - 25 + 0 + + + + QFrame::Plain + + + Qt::Horizontal + + + diff --git a/inkbox.pro b/inkbox.pro index c0950c5..4451b2e 100644 --- a/inkbox.pro +++ b/inkbox.pro @@ -42,6 +42,7 @@ SOURCES += \ mainwindow.cpp \ otamanager.cpp \ qclickablelabel.cpp \ + qtooltiplabel.cpp \ quit.cpp \ reader.cpp \ savedwords.cpp \ @@ -75,6 +76,7 @@ HEADERS += \ mainwindow.h \ otamanager.h \ qclickablelabel.h \ + qtooltiplabel.h \ quit.h \ reader.h \ savedwords.h \ diff --git a/mainwindow.ui b/mainwindow.ui index e193f2c..d0c5a67 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -448,6 +448,9 @@ QFrame::Plain + + 4 + Qt::Horizontal diff --git a/qclickablelabel.h b/qclickablelabel.h index 8c0384c..213884c 100644 --- a/qclickablelabel.h +++ b/qclickablelabel.h @@ -3,7 +3,6 @@ #include #include -#include class QClickableLabel : public QLabel { Q_OBJECT diff --git a/qtooltiplabel.cpp b/qtooltiplabel.cpp new file mode 100644 index 0000000..057dd31 --- /dev/null +++ b/qtooltiplabel.cpp @@ -0,0 +1,18 @@ +#include + +#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)), "" + objectName() + ""); +} + +void QToolTipLabel::mouseReleaseEvent(QMouseEvent * event) { + +} diff --git a/qtooltiplabel.h b/qtooltiplabel.h new file mode 100644 index 0000000..1c016a9 --- /dev/null +++ b/qtooltiplabel.h @@ -0,0 +1,20 @@ +#ifndef QTOOLTIPLABEL_H +#define QTOOLTIPLABEL_H + +#include +#include + +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