From 2857ce6c2bff73883e5352b06bc95524fbfcf091 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Sun, 27 Mar 2022 15:06:44 -0500 Subject: [PATCH] got a working lyriclist, now adding serviceitem --- src/CMakeLists.txt | 1 + src/serviceitem.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++ src/serviceitem.h | 54 +++++++++++++++++++++++++++ src/songsqlmodel.cpp | 80 ++++++++++++++++++++-------------------- 4 files changed, 183 insertions(+), 40 deletions(-) create mode 100644 src/serviceitem.cpp create mode 100644 src/serviceitem.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a5d35c0..6d72383 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources(presenter PRIVATE main.cpp resources.qrc songsqlmodel.cpp songsqlmodel.h + serviceitem.cpp serviceitem.h videosqlmodel.cpp videosqlmodel.h mpv/mpvobject.h mpv/mpvobject.cpp mpv/qthelper.hpp mpv/mpvhelpers.h diff --git a/src/serviceitem.cpp b/src/serviceitem.cpp new file mode 100644 index 0000000..e465381 --- /dev/null +++ b/src/serviceitem.cpp @@ -0,0 +1,88 @@ +#include "serviceitem.h" +#include + +ServiceItem::ServiceItem(QObject *parent) + : QAbstractListModel(parent) +{ +} + +int ServiceItem::rowCount(const QModelIndex &parent) const +{ + // For list models only the root node (an invalid parent) should return the list's size. For all + // other (valid) parents, rowCount() should return 0 so that it does not become a tree model. + if (parent.isValid()) + return 0; + + // FIXME: Implement me! + return m_data.count(); +} + +QVariant ServiceItem::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + const Data &data = m_data.at(index.row()); + switch (role) { + case NameRole: + return data.name; + case TypeRole: + return data.type; + case BackgroundRole: + return data.background; + case BackgroundTypeRole: + return data.backgroundType; + case TextRole: + return data.text; + default: + return QVariant(); + } +} + +QHash ServiceItem::roleNames() const { + static QHash mapping { + {NameRole, "name"}, + {TypeRole, "type"}, + {BackgroundRole, "background"}, + {BackgroundTypeRole, "backgroundType"}, + {TextRole, "text"} + }; + + return mapping; +} + +bool ServiceItem::setData(const QModelIndex &index, + const QVariant &value, + int role) { + const Data &oldData = m_data.at(index.row()); + const QVariant newData = data(index, role); + if (newData != value) { + // FIXME: Implement me! + switch (role) { + case NameRole: + m_data.at(index.row()).name = newData.toString(); + case TypeRole: + return data.type; + case BackgroundRole: + return data.background; + case BackgroundTypeRole: + return data.backgroundType; + case TextRole: + return data.text; + default: + return QVariant(); + } + oldData = newData; + emit dataChanged(index, index, QVector() << role); + return true; + } + return false; +} + +Qt::ItemFlags ServiceItem::flags(const QModelIndex &index) const +{ + if (!index.isValid()) + return Qt::NoItemFlags; + + return Qt::ItemIsEditable; // FIXME: Implement me! +} diff --git a/src/serviceitem.h b/src/serviceitem.h new file mode 100644 index 0000000..cc893cc --- /dev/null +++ b/src/serviceitem.h @@ -0,0 +1,54 @@ +#ifndef SERVICEITEM_H +#define SERVICEITEM_H + +#include +#include + +struct Data { + Data() {} + Data( const QString& name, + const QString& type, + const QString& background, + const QString& backgroundType, + const QStringList& text) + : name(name), type(type), background(background), + backgroundType(backgroundType), text(text) {} + QString name; + QString type; + QString background; + QString backgroundType; + QStringList text; +}; + +class ServiceItem : public QAbstractListModel +{ + Q_OBJECT + +public: + explicit ServiceItem(QObject *parent = nullptr); + + enum Roles { + NameRole = Qt::UserRole, + TypeRole, + BackgroundRole, + BackgroundTypeRole, + TextRole, + SlidesRole + }; + + // Basic functionality: + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + QHash roleNames() const override; + + // Editable: + bool setData(const QModelIndex &index, const QVariant &value, + int role = Qt::EditRole) override; + + Qt::ItemFlags flags(const QModelIndex& index) const override; + +private: + QVector m_data; +}; + +#endif // SERVICEITEM_H diff --git a/src/songsqlmodel.cpp b/src/songsqlmodel.cpp index ccba9c9..218fa31 100644 --- a/src/songsqlmodel.cpp +++ b/src/songsqlmodel.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -142,61 +143,60 @@ QStringList SongSqlModel::getLyricList(const int &row) { } QStringList rawLyrics = recordData.value("lyrics").toString().split("\n"); - qDebug() << rawLyrics; - QStringList lyrics; - // qDebug() << lyrics; QStringList vorder = recordData.value("vorder").toString().split(" "); qDebug() << vorder; - int endIndex; - QString line; QStringList keywords = {"Verse 1", "Verse 2", "Verse 3", "Verse 4", "Verse 5", "Verse 6", "Verse 7", "Verse 8", "Chorus 1", "Chorus 2", "Chorus 3", "Chorus 4", "Bridge 1", "Bridge 2", "Bridge 3", "Bridge 4", - "Intro 1", "Outro 2", "Ending 1", "Other 1"}; + "Intro 1", "Intro 2", "Ending 1", "Ending 2", + "Other 1", "Other 2", "Other 3", "Other 4"}; - foreach (const QString &vstr, vorder) { - int startIndex; - QString verse; - qDebug() << vstr; - foreach (line, rawLyrics) { + bool recordVerse; + bool firstItem = true; + QString verse; + QString vtitle; + QString line; + QMap verses; + + // This first function pulls out each verse into our verses map + foreach (line, rawLyrics) { + if (firstItem) { if (keywords.contains(line)) { - if (line.startsWith(vstr.at(0)) && line.endsWith(vstr.at(1))) { - qDebug() << vstr << line; - startIndex = rawLyrics.indexOf(line); - continue; - } - qDebug() << "Break out"; - break; + recordVerse = true; + firstItem = false; + vtitle = line; + continue; } - qDebug() << line; - // verse.append(line + "\n"); - // qDebug() << verse; + } else if (keywords.contains(line)) { + verses.insert(vtitle, verse); + verse.clear(); + vtitle = line; + recordVerse = false; + } else if (rawLyrics.endsWith(line)) { + verses.insert(vtitle, verse); + break; } - // lyrics.append(verse); - // qDebug() << verse; + if (recordVerse) { + verse.append(line.trimmed() + "\n"); + } + recordVerse = true; } - // foreach (line, rawLyrics) { - // if (line.trimmed() == "Chorus 1") { - // startIndex = rawLyrics.indexOf(line) + 1; - // // qDebug() << line; - // // qDebug() << startIndex; - // } - // if (line.trimmed() == "Verse 1") { - // endIndex = rawLyrics.indexOf(line); - // // qDebug() << endIndex; - // break; - // } - // if (rawLyrics.indexOf(line) == startIndex - 1) { - // continue; - // } - // verse.append(line + "\n"); - // // qDebug() << verse; - // } + qDebug() << verses; + // this function appends the verse that matches the verse order from the map + foreach (const QString &vstr, vorder) { + foreach (line, rawLyrics) { + if (line.startsWith(vstr.at(0)) && line.endsWith(vstr.at(1))) { + lyrics.append(verses[line]); + } + } + } + + qDebug() << lyrics; return lyrics; }