Local library: Switch to QVector for widget arrays; improvements

Thanks to @shermp for the help provided on QVectors, which I now use here instead of C-style arrays which could only have a fixed size.
This commit is contained in:
Nicolas Mailloux 2022-06-26 11:51:52 -04:00
parent 2014500317
commit 6192927b98
5 changed files with 45 additions and 39 deletions

View file

@ -30,9 +30,21 @@ localLibraryWidget::localLibraryWidget(QWidget *parent) :
buttonsNumber = 5; buttonsNumber = 5;
} }
else { else {
buttonsNumber = 3; buttonsNumber = 4;
} }
if(global::deviceID == "n873\n") {
bookTitleTruncateThreshold = 50;
}
else {
bookTitleTruncateThreshold = 40;
}
bookIconArray.resize(buttonsNumber);
bookBtnArray.resize(buttonsNumber);
horizontalLayoutArray.resize(buttonsNumber);
lineArray.resize(buttonsNumber);
// 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();
@ -97,6 +109,7 @@ void localLibraryWidget::setupDatabase() {
QString localLibraryDatabasePath = "/mnt/onboard/onboard/.inkbox/LocalLibrary.db"; QString localLibraryDatabasePath = "/mnt/onboard/onboard/.inkbox/LocalLibrary.db";
setDefaultWorkDir(); setDefaultWorkDir();
if(!QFile::exists(localLibraryDatabasePath)) { if(!QFile::exists(localLibraryDatabasePath)) {
log("Generating database", className);
QStringList booksList; QStringList booksList;
QDir dir("/mnt/onboard/onboard"); QDir dir("/mnt/onboard/onboard");
@ -126,6 +139,7 @@ void localLibraryWidget::setupDatabase() {
} }
// Read library database from file // Read library database from file
log("Reading database", className);
QFile database(localLibraryDatabasePath); QFile database(localLibraryDatabasePath);
QByteArray data; QByteArray data;
if(database.open(QIODevice::ReadOnly)) { if(database.open(QIODevice::ReadOnly)) {
@ -144,16 +158,21 @@ void localLibraryWidget::setupDatabase() {
// Determine maximum page number // Determine maximum page number
booksNumber = databaseJsonArrayList.size(); booksNumber = databaseJsonArrayList.size();
pagesNumber = std::ceil((double)booksNumber / buttonsNumber); pagesNumber = std::ceil((double)booksNumber / buttonsNumber);
if(pagesNumber == 1) {
ui->nextPageBtn->setEnabled(false);
}
} }
void localLibraryWidget::setupBooksList(int pageNumber) { void localLibraryWidget::setupBooksList(int pageNumber) {
idList->clear(); idList.clear();
int in = 1; int in = 1;
for(int i = (1 * pageNumber * buttonsNumber) - (buttonsNumber - 1); i <= (1 * pageNumber * buttonsNumber); i++) { for(int i = (1 * pageNumber * buttonsNumber) - (buttonsNumber - 1); i <= (1 * pageNumber * buttonsNumber); i++) {
// Read database info for each book and display the corresponding information on each button // Read database info for each book and display the corresponding information on each button
bookIconArray[in]->show(); bookIconArray[in]->show();
bookBtnArray[in]->show(); bookBtnArray[in]->show();
lineArray[(in - 1)]->show(); if(buttonsNumber - in > 0) {
lineArray[in]->show();
}
QJsonObject jsonObject = databaseJsonArrayList.at(i - 1).toObject(); QJsonObject jsonObject = databaseJsonArrayList.at(i - 1).toObject();
QString bookTitle = jsonObject["Title"].toString(); QString bookTitle = jsonObject["Title"].toString();
@ -170,8 +189,12 @@ void localLibraryWidget::setupBooksList(int pageNumber) {
bookIconArray[in]->setPixmap(pixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio)); bookIconArray[in]->setPixmap(pixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
} }
// Display book title // Display book title
idList->append(bookID.toInt()); idList.append(bookID.toInt());
bookBtnArray[in]->setText("<b>" + bookTitle + "</b>" + "<br>" + bookAuthor); if(bookTitle.length() > bookTitleTruncateThreshold) {
bookTitle.truncate(bookTitleTruncateThreshold);
bookTitle.append("...");
}
bookBtnArray[in]->setText("<font face='Inter'><b>" + bookTitle + "</b></font>" + "<br>" + bookAuthor);
if(!bookID.isEmpty()) { if(!bookID.isEmpty()) {
in++; in++;
} }
@ -180,12 +203,14 @@ void localLibraryWidget::setupBooksList(int pageNumber) {
for(int i = in; i <= buttonsNumber; i++) { for(int i = in; i <= buttonsNumber; i++) {
bookIconArray[i]->hide(); bookIconArray[i]->hide();
bookBtnArray[i]->hide(); bookBtnArray[i]->hide();
if(i - 1 <= buttonsNumber) { if(i - 1 < buttonsNumber) {
lineArray[(i - 1)]->hide(); lineArray[(i - 1)]->hide();
} }
} }
} }
ui->pageNumberLabel->setText("Page " + QString::number(pageNumber) + " <i>of</i> " + QString::number(pagesNumber)); ui->pageNumberLabel->setText("Page " + QString::number(pageNumber) + " <i>of</i> " + QString::number(pagesNumber));
// NOTICE: Memory leak?
ui->verticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
} }
void localLibraryWidget::on_previousPageBtn_clicked() void localLibraryWidget::on_previousPageBtn_clicked()
@ -217,7 +242,7 @@ void localLibraryWidget::openBook(int bookID) {
} }
void localLibraryWidget::btnOpenBook(int buttonNumber) { void localLibraryWidget::btnOpenBook(int buttonNumber) {
int id = idList->at(buttonNumber - 1); int id = idList.at(buttonNumber - 1);
openBook(id); openBook(id);
localLibraryWidget::close(); localLibraryWidget::close();
} }

View file

@ -31,7 +31,8 @@ public:
int currentPageNumber = 1; int currentPageNumber = 1;
int pagesNumber; int pagesNumber;
int booksNumber; int booksNumber;
QList<int> idList[4]; int bookTitleTruncateThreshold;
QVector<int> idList;
private slots: private slots:
void setupDatabase(); void setupDatabase();
@ -43,10 +44,10 @@ private slots:
private: private:
Ui::localLibraryWidget * ui; Ui::localLibraryWidget * ui;
QHBoxLayout * horizontalLayoutArray[4]; QVector<QHBoxLayout*> horizontalLayoutArray;
QLabel * bookIconArray[4]; QVector<QLabel*> bookIconArray;
QClickableLabel * bookBtnArray[4]; QVector<QClickableLabel*> bookBtnArray;
QFrame * lineArray[3]; QVector<QFrame*> lineArray;
signals: signals:
void openBookSignal(QString bookFile, bool relativePath); void openBookSignal(QString bookFile, bool relativePath);

View file

@ -10,6 +10,12 @@
<height>300</height> <height>300</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font"> <property name="font">
<font> <font>
<weight>75</weight> <weight>75</weight>
@ -80,23 +86,10 @@
<item> <item>
<layout class="QVBoxLayout" name="booksVerticalLayout"> <layout class="QVBoxLayout" name="booksVerticalLayout">
<property name="sizeConstraint"> <property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum> <enum>QLayout::SetNoConstraint</enum>
</property> </property>
</layout> </layout>
</item> </item>
<item>
<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>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>

View file

@ -962,18 +962,6 @@ void MainWindow::resetFullWindow() {
} }
} }
void MainWindow::setRecentBooksLabelsTruncateThreshold() {
if(global::deviceID == "n705\n" or global::deviceID == "n905\n" or global::deviceID == "kt\n") {
truncateThreshold = 12;
}
else if(global::deviceID == "n613\n" or global::deviceID == "n873\n" or global::deviceID == "n236\n" or global::deviceID == "n437\n" or global::deviceID == "n306\n"){
truncateThreshold = 20;
}
else {
truncateThreshold = 12;
}
}
void MainWindow::checkForOtaUpdate() { void MainWindow::checkForOtaUpdate() {
if(global::network::isConnected == true) { if(global::network::isConnected == true) {
string_checkconfig_ro("/external_root/opt/storage/update/last_sync"); string_checkconfig_ro("/external_root/opt/storage/update/last_sync");

View file

@ -94,7 +94,6 @@ private slots:
void on_libraryButton_clicked(); void on_libraryButton_clicked();
void resetWindow(bool resetStackedWidget); void resetWindow(bool resetStackedWidget);
void resetFullWindow(); void resetFullWindow();
void setRecentBooksLabelsTruncateThreshold();
void resetWifiIconClickedWhileReconnecting(); void resetWifiIconClickedWhileReconnecting();
void setupLocalLibraryWidget(); void setupLocalLibraryWidget();