From f6b6fa89118ac98a7832067ddd9134c81b40808c Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Fri, 13 Jan 2023 11:21:54 -0600 Subject: [PATCH] adding pdf pagecount to presentationmodel --- src/cpp/presentationsqlmodel.cpp | 39 +++++++++++++++++++++++++++++++- src/cpp/presentationsqlmodel.h | 8 ++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/cpp/presentationsqlmodel.cpp b/src/cpp/presentationsqlmodel.cpp index c645caf..bf41fa3 100644 --- a/src/cpp/presentationsqlmodel.cpp +++ b/src/cpp/presentationsqlmodel.cpp @@ -33,6 +33,7 @@ static void createPresentationTable() " 'id' INTEGER NOT NULL," " 'title' TEXT NOT NULL," " 'filePath' TEXT NOT NULL," + " 'pageCount' INTEGER," " PRIMARY KEY(id))")) { qFatal("Failed to query database: %s", qPrintable(query.lastError().text())); @@ -74,10 +75,11 @@ QHash PresentationSqlModel::roleNames() const names[Qt::UserRole] = "id"; names[Qt::UserRole + 1] = "title"; names[Qt::UserRole + 2] = "filePath"; + names[Qt::UserRole + 3] = "pageCount"; return names; } -void PresentationSqlModel::newPresentation(const QUrl &filePath) { +void PresentationSqlModel::newPresentation(const QUrl &filePath, int pageCount) { qDebug() << "adding new presentation"; int rows = rowCount(); @@ -87,6 +89,7 @@ void PresentationSqlModel::newPresentation(const QUrl &filePath) { QString title = fileInfo.baseName(); recordData.setValue("title", title); recordData.setValue("filePath", filePath); + recordData.setValue("pageCount", pageCount); if (insertRecord(rows, recordData)) { submitAll(); @@ -176,6 +179,40 @@ void PresentationSqlModel::updateFilePath(const int &row, const QUrl &filePath) emit filePathChanged(); } +int PresentationSqlModel::pageCount() const { + return m_pageCount; +} + +void PresentationSqlModel::setPageCount(const int &pageCount) { + if (pageCount == m_pageCount) + return; + + m_pageCount = pageCount; + + select(); + emit pageCountChanged(); +} + +// This function is for updating the pageCount from outside the delegate +void PresentationSqlModel::updatePageCount(const int &row, const int &pageCount) { + qDebug() << "Row is " << row; + QSqlQuery query("select id from presentations"); + QList ids; + while (query.next()) { + ids.append(query.value(0).toInt()); + // qDebug() << ids; + } + int id = ids.indexOf(row,0); + + QSqlRecord rowdata = record(id); + qDebug() << rowdata; + rowdata.setValue("pageCount", pageCount); + setRecord(id, rowdata); + qDebug() << rowdata; + submitAll(); + emit pageCountChanged(); +} + QVariantMap PresentationSqlModel::getPresentation(const int &row) { // qDebug() << "Row we are getting is " << row; // QUrl presentation; diff --git a/src/cpp/presentationsqlmodel.h b/src/cpp/presentationsqlmodel.h index 39fe00a..06e297a 100644 --- a/src/cpp/presentationsqlmodel.h +++ b/src/cpp/presentationsqlmodel.h @@ -14,6 +14,7 @@ class PresentationSqlModel : public QSqlTableModel Q_PROPERTY(int id READ id) Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) Q_PROPERTY(QUrl filePath READ filePath WRITE setFilePath NOTIFY filePathChanged) + Q_PROPERTY(int pageCount READ pageCount WRITE setPageCount NOTIFY pageCountChanged) QML_ELEMENT public: @@ -22,14 +23,17 @@ public: int id() const; QString title() const; QUrl filePath() const; + int pageCount() const; void setTitle(const QString &title); void setFilePath(const QUrl &filePath); + void setPageCount(const int &pageCount); Q_INVOKABLE void updateTitle(const int &row, const QString &title); Q_INVOKABLE void updateFilePath(const int &row, const QUrl &filePath); + Q_INVOKABLE void updatePageCount(const int &row, const int &pageCount); - Q_INVOKABLE void newPresentation(const QUrl &filePath); + Q_INVOKABLE void newPresentation(const QUrl &filePath, int pageCount); Q_INVOKABLE void deletePresentation(const int &row); Q_INVOKABLE QVariantMap getPresentation(const int &row); @@ -39,11 +43,13 @@ public: signals: void titleChanged(); void filePathChanged(); + void pageCountChanged(); private: int m_id; QString m_title; QUrl m_filePath; + int m_pageCount; }; #endif //PRESENTATIONSQLMODEL_H