adding a presentation model and including it's ui in library

This commit is contained in:
Chris Cochrun 2022-09-22 05:59:38 -05:00
parent a8943b86cf
commit a9286eeb1b
5 changed files with 360 additions and 0 deletions

View file

@ -38,6 +38,7 @@
#include "songsqlmodel.h" #include "songsqlmodel.h"
#include "videosqlmodel.h" #include "videosqlmodel.h"
#include "imagesqlmodel.h" #include "imagesqlmodel.h"
#include "pressqlmodel.h"
#include "slide.h" #include "slide.h"
static void connectToDatabase() { static void connectToDatabase() {
@ -105,6 +106,7 @@ int main(int argc, char *argv[])
qmlRegisterType<SongSqlModel>("org.presenter", 1, 0, "SongSqlModel"); qmlRegisterType<SongSqlModel>("org.presenter", 1, 0, "SongSqlModel");
qmlRegisterType<VideoSqlModel>("org.presenter", 1, 0, "VideoSqlModel"); qmlRegisterType<VideoSqlModel>("org.presenter", 1, 0, "VideoSqlModel");
qmlRegisterType<ImageSqlModel>("org.presenter", 1, 0, "ImageSqlModel"); qmlRegisterType<ImageSqlModel>("org.presenter", 1, 0, "ImageSqlModel");
qmlRegisterType<PresSqlModel>("org.presenter", 1, 0, "PresSqlModel");
qmlRegisterType<ServiceItemModel>("org.presenter", 1, 0, "ServiceItemModel"); qmlRegisterType<ServiceItemModel>("org.presenter", 1, 0, "ServiceItemModel");
qmlRegisterSingletonInstance("org.presenter", 1, 0, "SlideObject", slide.get()); qmlRegisterSingletonInstance("org.presenter", 1, 0, "SlideObject", slide.get());

184
src/pressqlmodel.cpp Normal file
View file

@ -0,0 +1,184 @@
#include "pressqlmodel.h"
#include <QDateTime>
#include <QDebug>
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QSql>
#include <QSqlDatabase>
#include <QFileInfo>
#include <qabstractitemmodel.h>
#include <qdebug.h>
#include <qnamespace.h>
#include <qobject.h>
#include <qobjectdefs.h>
#include <qsqlrecord.h>
#include <qurl.h>
#include <qvariant.h>
static const char *presTableName = "presentation";
static void createPresTable()
{
if(QSqlDatabase::database().tables().contains(presTableName)) {
return;
}
QSqlQuery query;
if (!query.exec("CREATE TABLE IF NOT EXISTS 'presentations' ("
" 'id' INTEGER NOT NULL,"
" 'title' TEXT NOT NULL,"
" 'filePath' TEXT NOT NULL,"
" PRIMARY KEY(id))")) {
qFatal("Failed to query database: %s",
qPrintable(query.lastError().text()));
}
qDebug() << query.lastQuery();
qDebug() << "inserting into presentations";
query.exec("INSERT INTO presentations (title, filePath) VALUES ('Dec 180', 'file:///home/chris/nextcloud/tfc/openlp/5 slides-2.pdf')");
qDebug() << query.lastQuery();
query.exec("INSERT INTO presentations (title, filePath) VALUES ('No TFC', "
"'file:///home/chris/nextcloud/tfc/openlp/5 slides-1.pdf')");
query.exec("select * from presentations");
qDebug() << query.lastQuery();
}
PresSqlModel::PresSqlModel(QObject *parent) : QSqlTableModel(parent) {
qDebug() << "creating pres table";
createPresTable();
setTable(pressTableName);
setEditStrategy(QSqlTableModel::OnManualSubmit);
// make sure to call select else the model won't fill
select();
}
QVariant PresSqlModel::data(const QModelIndex &index, int role) const {
if (role < Qt::UserRole) {
return QSqlTableModel::data(index, role);
}
// qDebug() << role;
const QSqlRecord sqlRecord = record(index.row());
return sqlRecord.value(role - Qt::UserRole);
}
QHash<int, QByteArray> PresSqlModel::roleNames() const
{
QHash<int, QByteArray> names;
names[Qt::UserRole] = "id";
names[Qt::UserRole + 1] = "title";
names[Qt::UserRole + 2] = "filePath";
return names;
}
void PresSqlModel::newPres(const QUrl &filePath) {
qDebug() << "adding new pres";
int rows = rowCount();
qDebug() << rows;
QSqlRecord recordData = record();
QFileInfo fileInfo = filePath.toString();
QString title = fileInfo.baseName();
recordData.setValue("title", title);
recordData.setValue("filePath", filePath);
if (insertRecord(rows, recordData)) {
submitAll();
} else {
qDebug() << lastError();
};
}
void PresSqlModel::deletePres(const int &row) {
QSqlRecord recordData = record(row);
if (recordData.isEmpty())
return;
removeRow(row);
submitAll();
}
int PresSqlModel::id() const {
return m_id;
}
QString PresSqlModel::title() const {
return m_title;
}
void PresSqlModel::setTitle(const QString &title) {
if (title == m_title)
return;
m_title = title;
select();
emit titleChanged();
}
// This function is for updating the title from outside the delegate
void PresSqlModel::updateTitle(const int &row, const QString &title) {
qDebug() << "Row is " << row;
QSqlRecord rowdata = record(row);
qDebug() << rowdata;
rowdata.setValue("title", title);
setRecord(row, rowdata);
qDebug() << rowdata;
submitAll();
emit titleChanged();
}
QUrl PresSqlModel::filePath() const {
return m_filePath;
}
void PresSqlModel::setFilePath(const QUrl &filePath) {
if (filePath == m_filePath)
return;
m_filePath = filePath;
select();
emit filePathChanged();
}
// This function is for updating the filepath from outside the delegate
void PresSqlModel::updateFilePath(const int &row, const QUrl &filePath) {
qDebug() << "Row is " << row;
QSqlRecord rowdata = record(row);
qDebug() << rowdata;
rowdata.setValue("filePath", filePath);
setRecord(row, rowdata);
qDebug() << rowdata;
submitAll();
emit filePathChanged();
}
QVariantMap PresSqlModel::getPres(const int &row) {
// qDebug() << "Row we are getting is " << row;
// QUrl pres;
// QSqlRecord rec = record(row);
// qDebug() << rec.value("filePath").toUrl();
// // pres.append(rec.value("title"));
// // pres.append(rec.value("filePath"));
// pres = rec.value("filePath").toUrl();
// return pres;
QVariantMap data;
const QModelIndex idx = this->index(row,0);
// qDebug() << idx;
if( !idx.isValid() )
return data;
const QHash<int,QByteArray> rn = roleNames();
// qDebug() << rn;
QHashIterator<int,QByteArray> it(rn);
while (it.hasNext()) {
it.next();
qDebug() << it.key() << ":" << it.value();
data[it.value()] = idx.data(it.key());
}
return data;
}

49
src/pressqlmodel.h Normal file
View file

@ -0,0 +1,49 @@
#ifndef PRESSQLMODEL_H
#define PRESSQLMODEL_H
#include <QSqlTableModel>
#include <qobject.h>
#include <qobjectdefs.h>
#include <qqml.h>
#include <qurl.h>
#include <qvariant.h>
class PresSqlModel : public QSqlTableModel
{
Q_OBJECT
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)
QML_ELEMENT
public:
PresSqlModel(QObject *parent = 0);
int id() const;
QString title() const;
QUrl filePath() const;
void setTitle(const QString &title);
void setFilePath(const QUrl &filePath);
Q_INVOKABLE void updateTitle(const int &row, const QString &title);
Q_INVOKABLE void updateFilePath(const int &row, const QUrl &filePath);
Q_INVOKABLE void newPres(const QUrl &filePath);
Q_INVOKABLE void deletePres(const int &row);
Q_INVOKABLE QVariantMap getPres(const int &row);
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
signals:
void titleChanged();
void filePathChanged();
private:
int m_id;
QString m_title;
QUrl m_filePath;
};
#endif //PRESSQLMODEL_H

View file

@ -14,6 +14,7 @@ Item {
property bool overlay: false property bool overlay: false
property var videoexts: ["mp4", "webm", "mkv", "avi", "MP4", "WEBM", "MKV"] property var videoexts: ["mp4", "webm", "mkv", "avi", "MP4", "WEBM", "MKV"]
property var imgexts: ["jpg", "png", "gif", "jpeg", "JPG", "PNG"] property var imgexts: ["jpg", "png", "gif", "jpeg", "JPG", "PNG"]
property var presexts: ["pdf", "PDF", "odp", "pptx"]
Kirigami.Theme.colorSet: Kirigami.Theme.View Kirigami.Theme.colorSet: Kirigami.Theme.View
@ -713,6 +714,9 @@ Item {
Layout.fillWidth: true Layout.fillWidth: true
Layout.alignment: Qt.AlignTop Layout.alignment: Qt.AlignTop
state: "deselected" state: "deselected"
clip: true
model: pressqlmodel
delegate: presDelegate
states: [ states: [
State { State {
@ -738,6 +742,118 @@ Item {
duration: 300 duration: 300
} }
} }
Component {
id: presDelegate
Item{
implicitWidth: ListView.view.width
height: selectedLibrary == "press" ? 50 : 0
Kirigami.BasicListItem {
id: presListItem
property bool rightMenu: false
implicitWidth: presentationLibraryList.width
height: selectedLibrary == "press" ? 50 : 0
clip: true
label: title
/* subtitle: author */
supportsMouseEvents: false
backgroundColor: {
if (parent.ListView.isCurrentItem) {
Kirigami.Theme.highlightColor;
} else if (presDragHandler.containsMouse){
Kirigami.Theme.highlightColor;
} else {
Kirigami.Theme.backgroundColor;
}
}
textColor: {
if (parent.ListView.isCurrentItem || presDragHandler.containsMouse)
activeTextColor;
else
Kirigami.Theme.textColor;
}
Behavior on height {
NumberAnimation {
easing.type: Easing.OutCubic
duration: 300
}
}
Drag.active: presDragHandler.drag.active
Drag.hotSpot.x: width / 2
Drag.hotSpot.y: height / 2
Drag.keys: [ "library" ]
states: State {
name: "dragged"
when: presListItem.Drag.active
PropertyChanges {
target: presListItem
x: x
y: y
width: width
height: height
}
ParentChange {
target: presListItem
parent: rootApp.overlay
}
}
}
MouseArea {
id: presDragHandler
anchors.fill: parent
hoverEnabled: true
drag {
target: presListItem
onActiveChanged: {
if (presDragHandler.drag.active) {
dragItemTitle = title;
dragItemType = "pres";
dragItemText = "";
dragItemBackgroundType = "pres";
dragItemBackground = filePath;
} else {
presListItem.Drag.drop()
}
}
filterChildren: true
threshold: 10
}
MouseArea {
id: presClickHandler
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: {
if(mouse.button == Qt.RightButton)
rightClickPresMenu.popup()
else{
presentationLibraryList.currentIndex = index
const pres = pressqlmodel.getPres(presentationLibraryList.currentIndex);
if (!editMode)
editMode = true;
editType = "pres";
editSwitch(pres);
}
}
}
}
Controls.Menu {
id: rightClickPresMenu
x: presClickHandler.mouseX
y: presClickHandler.mouseY + 10
Kirigami.Action {
text: "delete"
onTriggered: pressqlmodel.deletePres(index)
}
}
}
}
} }
Rectangle { Rectangle {
@ -887,6 +1003,11 @@ Item {
{ {
addImg(file); addImg(file);
} }
if (presexts.includes(extension))
{
showPassiveNotification("it's a presentation!");
return;
}
} }
} }
} }

View file

@ -115,6 +115,10 @@ Controls.Page {
id: imagesqlmodel id: imagesqlmodel
} }
PresSqlModel {
id: pressqlmodel
}
ServiceItemModel { ServiceItemModel {
id: serviceItemModel id: serviceItemModel
} }