songsqlmodel working

This commit is contained in:
Chris Cochrun 2022-02-22 09:23:42 -06:00
parent d647f4442f
commit 641b884901
13 changed files with 342 additions and 33 deletions

View file

@ -4,6 +4,7 @@ target_sources(presenter
PRIVATE
main.cpp resources.qrc
songlistmodel.cpp songlistmodel.h
songsqlmodel.cpp songsqlmodel.h
mpv/mpvobject.h mpv/mpvobject.cpp
mpv/qthelper.hpp mpv/mpvhelpers.h
)

View file

@ -8,6 +8,9 @@
#include <KLocalizedString>
#include <iostream>
#include <QQmlEngine>
#include <QtSql>
#include <QSqlDatabase>
#include <QSqlTableModel>
#include <QObject>
#include <QtGlobal>
@ -18,16 +21,50 @@
#include <QtQuick/QQuickWindow>
#include <QtQuick/QQuickView>
#include <qdir.h>
#include <qglobal.h>
#include <qqml.h>
#include <qsqldatabase.h>
#include <qsqlquery.h>
#include "songlistmodel.h"
#include "mpv/mpvobject.h"
#include "songsqlmodel.h"
static void connectToDatabase() {
// let's setup our sql database
QSqlDatabase db = QSqlDatabase::database();
if (!db.isValid()){
db = QSqlDatabase::addDatabase("QSQLITE");
if (!db.isValid())
qFatal("Cannot add database: %s", qPrintable(db.lastError().text()));
}
const QDir writeDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
if (!writeDir.mkpath(".")) {
qFatal("Failed to create writable location at %s", qPrintable(writeDir.absolutePath()));
}
const QString dbName = writeDir.absolutePath() + "/library-db.sqlite3";
db.setHostName("localhost");
db.setDatabaseName(dbName);
db.setUserName("presenter");
db.setPassword("i393jkf782djyr98302j");
if (!db.open()) {
qFatal("Cannot open database: %s", qPrintable(db.lastError().text()));
QFile::remove(dbName);
}
}
int main(int argc, char *argv[])
{
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
KLocalizedString::setApplicationDomain("presenter");
QCoreApplication::setOrganizationName(QStringLiteral("TFC"));
QCoreApplication::setOrganizationName(QStringLiteral("presenter"));
QCoreApplication::setOrganizationDomain(QStringLiteral("tfcconnection.org"));
QCoreApplication::setApplicationName(QStringLiteral("Church Presenter"));
@ -35,18 +72,22 @@ int main(int argc, char *argv[])
std::setlocale(LC_NUMERIC, "C");
qmlRegisterType<MpvObject>("mpv", 1, 0, "MpvObject");
//register our song model from sql
qmlRegisterType<SongSqlModel>("org.presenter", 1, 0, "SongSqlModel");
SongListModel songListModel;
// path = QQmlEngine::importPathList()
qDebug() << "Hello World!";
connectToDatabase();
QQmlApplicationEngine engine;
engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
engine.rootContext()->setContextProperty("_songListModel", &songListModel);
engine.load(QUrl(QStringLiteral("qrc:qml/main.qml")));
// QQuickView *view = new QQuickView;
// view->setSource(QUrl(QStringLiteral("qrc:qml/main.qml")));
// view->show();
#ifdef STATIC_KIRIGAMI
KirigamiPlugin::getInstance().registerTypes();

View file

@ -14,7 +14,7 @@ Kirigami.ApplicationWindow {
property bool libraryOpen: true
property bool presenting: false
property bool presentMode: true
property var secondScreen: null
property var screens
pageStack.initialPage: mainPage
header: Presenter.Header {}
@ -30,10 +30,16 @@ Kirigami.ApplicationWindow {
}
Component.onCompleted: {
print("checking screens")
print("Present Mode is " + presentMode)
secondScreen = Qt.application.screens[1]
print(secondScreen)
print("checking screens");
print("Present Mode is " + presentMode);
screens = Qt.application.screens;
for (let i = 0; i < screens.length; i++) {
print(screens[i].name);
print("width of screen: " + (screens[i].width * screens[i].devicePixelRatio));
print("height of screen: " + (screens[i].height * screens[i].devicePixelRatio));
print("pixeldensity of screen: " + screens[i].pixelDensity);
print("pixelratio of screen: " + screens[i].devicePixelRatio);
}
}
}

View file

@ -4,6 +4,7 @@ import QtQuick.Controls 2.0 as Controls
import QtQuick.Layouts 1.2
import org.kde.kirigami 2.13 as Kirigami
import "./" as Presenter
import org.presenter 1.0
Item {
id: root
@ -25,10 +26,20 @@ Item {
color: Kirigami.Theme.backgroundColor
Controls.Label {
id: songlable
anchors.centerIn: parent
text: "Songs"
}
Controls.Label {
anchors {left: songlable.right
verticalCenter: songlable.verticalCenter
leftMargin: 15}
text: songsqlmodel.rowCount()
font.pixelSize: 15
color: Kirigami.Theme.disabledTextColor
}
MouseArea {
anchors.fill: parent
onClicked: {
@ -41,15 +52,19 @@ Item {
}
}
SongSqlModel {
id: songsqlmodel
}
ListView {
Layout.fillHeight: true
Layout.fillWidth: true
id: songLibraryList
model: _songListModel
model: songsqlmodel
delegate: itemDelegate
state: "selected"
Component.onCompleted: print(selectedLibrary)
/* Component.onCompleted: print(selectedLibrary) */
states: [
State {

139
src/songsqlmodel.cpp Normal file
View file

@ -0,0 +1,139 @@
#include "songsqlmodel.h"
#include <QDateTime>
#include <QDebug>
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QSql>
#include <QSqlDatabase>
static const char *songsTableName = "songs";
static void createTable()
{
if(QSqlDatabase::database().tables().contains(songsTableName)) {
return;
}
QSqlQuery query;
if (!query.exec(
"CREATE TABLE IF NOT EXISTS 'songs' ("
" 'title' TEXT NOT NULL,"
" 'lyrics' TEXT,"
" 'author' TEXT,"
" 'ccli' TEXT,"
" 'audio' TEXT,"
" PRIMARY KEY(title))"
)) {
qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
}
query.exec("INSERT INTO songs VALUES ('10,000 Reasons', '10,000 reasons for my heart to sing', 'Matt Redman', '13470183', '')");
query.exec("INSERT INTO songs VALUES ('River', 'Im going down to the river', 'Jordan Feliz', '13470183', '')");
query.exec("INSERT INTO songs VALUES ('Marvelous Light', 'Into marvelous light Im running', 'Chris Tomlin', '13470183', '')");
query.exec("select * from songs");
qDebug() << query.lastQuery();
}
SongSqlModel::SongSqlModel(QObject *parent)
: QSqlTableModel(parent)
{
createTable();
setTable(songsTableName);
setEditStrategy(QSqlTableModel::OnFieldChange);
select();
}
QVariant SongSqlModel::data(const QModelIndex &index, int role) const {
if (role < Qt::UserRole) {
qDebug() << role;
return QSqlTableModel::data(index, role);
}
qDebug() << role;
const QSqlRecord sqlRecord = record(index.row());
return sqlRecord.value(role - Qt::UserRole);
}
QHash<int, QByteArray> SongSqlModel::roleNames() const
{
QHash<int, QByteArray> names;
names[Qt::UserRole] = "title";
names[Qt::UserRole + 1] = "lyrics";
names[Qt::UserRole + 2] = "author";
names[Qt::UserRole + 3] = "ccli";
names[Qt::UserRole + 4] = "audio";
return names;
}
QString SongSqlModel::title() const {
return m_title;
}
void SongSqlModel::setTitle(const QString &title) {
if (title == m_title)
return;
m_title = title;
select();
emit titleChanged();
}
QString SongSqlModel::author() const {
return m_author;
}
void SongSqlModel::setAuthor(const QString &author) {
if (author == m_author)
return;
m_author = author;
select();
emit authorChanged();
}
QString SongSqlModel::lyrics() const {
return m_lyrics;
}
void SongSqlModel::setLyrics(const QString &lyrics) {
if (lyrics == m_lyrics)
return;
m_lyrics = lyrics;
select();
emit lyricsChanged();
}
QString SongSqlModel::ccli() const {
return m_ccli;
}
void SongSqlModel::setCcli(const QString &ccli) {
if (ccli == m_ccli)
return;
m_ccli = ccli;
select();
emit ccliChanged();
}
QString SongSqlModel::audio() const {
return m_audio;
}
void SongSqlModel::setAudio(const QString &audio) {
if (audio == m_audio)
return;
m_audio = audio;
select();
emit audioChanged();
}

50
src/songsqlmodel.h Normal file
View file

@ -0,0 +1,50 @@
#ifndef SONGSQLMODEL_H
#define SONGSQLMODEL_H
#include <QSqlTableModel>
#include <qqml.h>
class SongSqlModel : public QSqlTableModel
{
Q_OBJECT
Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
Q_PROPERTY(QString lyrics READ lyrics WRITE setLyrics NOTIFY lyricsChanged)
Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
Q_PROPERTY(QString ccli READ ccli WRITE setCcli NOTIFY ccliChanged)
Q_PROPERTY(QString audio READ audio WRITE setAudio NOTIFY audioChanged)
QML_ELEMENT
public:
SongSqlModel(QObject *parent = 0);
QString title() const;
QString lyrics() const;
QString author() const;
QString ccli() const;
QString audio() const;
void setTitle(const QString &title);
void setLyrics(const QString &lyrics);
void setAuthor(const QString &author);
void setCcli(const QString &ccli);
void setAudio(const QString &audio);
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
signals:
void titleChanged();
void lyricsChanged();
void authorChanged();
void ccliChanged();
void audioChanged();
private:
QString m_title;
QString m_lyrics;
QString m_author;
QString m_ccli;
QString m_audio;
};
#endif //SONGSQLMODEL_H