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;
}
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
sW = QGuiApplication::screens()[0]->size().width();
sH = QGuiApplication::screens()[0]->size().height();
@ -97,6 +109,7 @@ void localLibraryWidget::setupDatabase() {
QString localLibraryDatabasePath = "/mnt/onboard/onboard/.inkbox/LocalLibrary.db";
setDefaultWorkDir();
if(!QFile::exists(localLibraryDatabasePath)) {
log("Generating database", className);
QStringList booksList;
QDir dir("/mnt/onboard/onboard");
@ -126,6 +139,7 @@ void localLibraryWidget::setupDatabase() {
}
// Read library database from file
log("Reading database", className);
QFile database(localLibraryDatabasePath);
QByteArray data;
if(database.open(QIODevice::ReadOnly)) {
@ -144,16 +158,21 @@ void localLibraryWidget::setupDatabase() {
// Determine maximum page number
booksNumber = databaseJsonArrayList.size();
pagesNumber = std::ceil((double)booksNumber / buttonsNumber);
if(pagesNumber == 1) {
ui->nextPageBtn->setEnabled(false);
}
}
void localLibraryWidget::setupBooksList(int pageNumber) {
idList->clear();
idList.clear();
int in = 1;
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
bookIconArray[in]->show();
bookBtnArray[in]->show();
lineArray[(in - 1)]->show();
if(buttonsNumber - in > 0) {
lineArray[in]->show();
}
QJsonObject jsonObject = databaseJsonArrayList.at(i - 1).toObject();
QString bookTitle = jsonObject["Title"].toString();
@ -170,8 +189,12 @@ void localLibraryWidget::setupBooksList(int pageNumber) {
bookIconArray[in]->setPixmap(pixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio));
}
// Display book title
idList->append(bookID.toInt());
bookBtnArray[in]->setText("<b>" + bookTitle + "</b>" + "<br>" + bookAuthor);
idList.append(bookID.toInt());
if(bookTitle.length() > bookTitleTruncateThreshold) {
bookTitle.truncate(bookTitleTruncateThreshold);
bookTitle.append("...");
}
bookBtnArray[in]->setText("<font face='Inter'><b>" + bookTitle + "</b></font>" + "<br>" + bookAuthor);
if(!bookID.isEmpty()) {
in++;
}
@ -180,12 +203,14 @@ void localLibraryWidget::setupBooksList(int pageNumber) {
for(int i = in; i <= buttonsNumber; i++) {
bookIconArray[i]->hide();
bookBtnArray[i]->hide();
if(i - 1 <= buttonsNumber) {
if(i - 1 < buttonsNumber) {
lineArray[(i - 1)]->hide();
}
}
}
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()
@ -217,7 +242,7 @@ void localLibraryWidget::openBook(int bookID) {
}
void localLibraryWidget::btnOpenBook(int buttonNumber) {
int id = idList->at(buttonNumber - 1);
int id = idList.at(buttonNumber - 1);
openBook(id);
localLibraryWidget::close();
}

View file

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

View file

@ -10,6 +10,12 @@
<height>300</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<weight>75</weight>
@ -80,23 +86,10 @@
<item>
<layout class="QVBoxLayout" name="booksVerticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum>
<enum>QLayout::SetNoConstraint</enum>
</property>
</layout>
</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>
</widget>
<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() {
if(global::network::isConnected == true) {
string_checkconfig_ro("/external_root/opt/storage/update/last_sync");

View file

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