From d94b17f0d5b901138b667ea63e77f76caeb175b6 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Wed, 18 Jan 2023 15:57:31 -0600 Subject: [PATCH] the new slidemodel and slideobject classes --- src/cpp/slidemodel.cpp | 573 ++++++++++++++++++++++++++++++++++++++++ src/cpp/slidemodel.h | 123 +++++++++ src/cpp/slideobject.cpp | 230 ++++++++++++++++ src/cpp/slideobject.h | 50 ++++ 4 files changed, 976 insertions(+) create mode 100644 src/cpp/slidemodel.cpp create mode 100644 src/cpp/slidemodel.h create mode 100644 src/cpp/slideobject.cpp create mode 100644 src/cpp/slideobject.h diff --git a/src/cpp/slidemodel.cpp b/src/cpp/slidemodel.cpp new file mode 100644 index 0000000..f18ce2f --- /dev/null +++ b/src/cpp/slidemodel.cpp @@ -0,0 +1,573 @@ +#include "slidemodel.h" +#include "slide.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +SlideModel::SlideModel(QObject *parent) + : QAbstractListModel(parent) { + if (!loadLastSaved()) { + addItem(new Slide("10,000 Reasons", "song", + "file:/home/chris/nextcloud/tfc/openlp/CMG - Nature King 21.jpg", + "image", QStringList("Yip Yip"), + "file:/home/chris/nextcloud/tfc/openlp/music/Eden-Phil Wickham [lyrics].mp3")); + addItem(new Slide("Marvelous Light", "song", + "file:/home/chris/nextcloud/tfc/openlp/Fire Embers_Loop.mp4", + "video", QStringList("Hallelujah!"))); + addItem(new Slide("BP Text", "video", + "file:/home/chris/nextcloud/tfc/openlp/videos/test.mp4", + "video", QStringList())); + } +} + +int SlideModel::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_items.size(); +} + +QVariant SlideModel::data(const QModelIndex &index, int role) const { + if (!index.isValid()) + return QVariant(); + + Slide *item = m_items[index.row()]; + switch (role) { + case NameRole: + return item->name(); + case TypeRole: + return item->type(); + case BackgroundRole: + return item->background(); + case BackgroundTypeRole: + return item->backgroundType(); + case TextRole: + return item->text(); + case AudioRole: + return item->audio(); + case FontRole: + return item->font(); + case FontSizeRole: + return item->fontSize(); + case ServiceItemIdRole: + return item->serviceItemId(); + case HorizontalTextAlignmentRole: + return item->horizontalTextAlignment(); + case VerticalTextAlignmentRole: + return item->verticalTextAlignment(); + case ActiveRole: + return item->active(); + case SelectedRole: + return item->selected(); + default: + return QVariant(); + } +} + +QHash SlideModel::roleNames() const { + static QHash mapping{ + {NameRole, "name"}, + {TypeRole, "type"}, + {BackgroundRole, "background"}, + {BackgroundTypeRole, "backgroundType"}, + {TextRole, "text"}, + {AudioRole, "audio"}, + {FontRole, "font"}, + {FontSizeRole, "fontSize"}, + {ServiceItemIdRole, "serviceItemId"}, + {HorizontalTextAlignmentRole, "horizontalTextAlignment"}, + {VerticalTextAlignmentRole, "verticalTextAlignment"}, + {ActiveRole, "active"}, + {SelectedRole, "selected"} + }; + + return mapping; +} + +bool SlideModel::setData(const QModelIndex &index, const QVariant &value, + int role) { + + Slide *item = m_items[index.row()]; + bool somethingChanged = false; + + switch (role) { + case TypeRole: + if (item->type() != value.toString()) { + item->setType(value.toString()); + somethingChanged = true; + } + break; + case BackgroundRole: + if (item->background() != value.toString()) { + item->setBackground(value.toString()); + somethingChanged = true; + } + break; + case BackgroundTypeRole: + if (item->backgroundType() != value.toString()) { + item->setBackgroundType(value.toString()); + somethingChanged = true; + } + break; + case TextRole: + if (item->text() != value.toString()) { + item->setText(value.toString()); + somethingChanged = true; + } + break; + case AudioRole: + if (item->audio() != value.toString()) { + item->setAudio(value.toString()); + somethingChanged = true; + } + break; + case FontRole: + if (item->font() != value.toString()) { + item->setFont(value.toString()); + somethingChanged = true; + } + break; + case FontSizeRole: + if (item->fontSize() != value.toInt()) { + item->setFontSize(value.toInt()); + somethingChanged = true; + } + break; + case ServiceItemIdRole: + if (item->serviceItemId() != value.toInt()) { + item->setServiceItemId(value.toInt()); + somethingChanged = true; + } + break; + case HorizontalTextAlignmentRole: + if (item->horizontalTextAlignment() != value.toString()) { + item->setHorizontalTextAlignment(value.toString()); + somethingChanged = true; + } + break; + case VerticalTextAlignmentRole: + if (item->verticalTextAlignment() != value.toString()) { + item->setVerticalTextAlignment(value.toString()); + somethingChanged = true; + } + break; + case ActiveRole: + if (item->active() != value.toBool()) { + item->setActive(value.toBool()); + somethingChanged = true; + } + break; + case SelectedRole: + if (item->selected() != value.toBool()) { + item->setSelected(value.toBool()); + somethingChanged = true; + } + break; + if (somethingChanged) { + emit dataChanged(index, index, QVector() << role); + return true; + } + } + + return false; +} + +Qt::ItemFlags SlideModel::flags(const QModelIndex &index) const { + if (!index.isValid()) + return Qt::NoItemFlags; + + return Qt::ItemIsEditable; // FIXME: Implement me! +} + +// int SlideModel::index(int row, int column, const QModelIndex &parent) { +// if (!hasIndex(row, column, parent)) +// return QModelIndex(); + +// Slide *parentItem; + +// if (!parent.isValid()) +// parentItem = rootItem; +// else +// parentItem = static_cast(parent.internalPointer()); + +// Slide *childItem = parentItem->child(row); +// if (childItem) +// return createIndex(row, column, childItem); +// return QModelIndex(); +// } + +void SlideModel::addItem(Slide *item) { + const int index = m_items.size(); + qDebug() << index; + // foreach (item, m_items) { + // qDebug() << item; + // } + beginInsertRows(QModelIndex(), index, index); + m_items.append(item); + endInsertRows(); +} + +void SlideModel::insertItem(const int &index, Slide *item) { + beginInsertRows(this->index(index).parent(), index, index); + m_items.insert(index, item); + endInsertRows(); + qDebug() << "Success"; +} + +void SlideModel::addItem(const QString &text, const QString &type) { + Slide *item = new Slide(name, type); + item->setSelected(false); + item->setActive(false); + addItem(item); +} + +void SlideModel::addItem(const QString &name, const QString &type, + const QString &imageBackground, const QString &videoBackground) { + Slide *item = new Slide(name, type, imageBackground, videoBackground); + item->setSelected(false); + item->setActive(false); + addItem(item); +} + +void SlideModel::addItem(const QString &name, const QString &type, + const QString &imageBackground, const QString &videoBackground, + const QStringList &text) { + Slide *item = new Slide(name, type, imageBackground, videoBackground, text); + item->setSelected(false); + item->setActive(false); + addItem(item); + qDebug() << name << type << imageBackground; +} + +void SlideModel::addItem(const QString &name, const QString &type, + const QString &imageBackground, const QString &videoBackground, + const QStringList &text, const QString &audio) { + Slide *item = new Slide(name, type, imageBackground, videoBackground, + text, audio); + item->setSelected(false); + item->setActive(false); + addItem(item); + qDebug() << name << type << imageBackground; +} + +void SlideModel::addItem(const QString &name, const QString &type, + const QString &imageBackground, const QString &videoBackground, + const QStringList &text, const QString &audio, + const QString &font, const int &fontSize) { + Slide *item = new Slide(name, type, imageBackground, videoBackground, + text, audio, font, fontSize); + item->setSelected(false); + item->setActive(false); + addItem(item); + qDebug() << "#################################"; + qDebug() << name << type << font << fontSize; + qDebug() << "#################################"; +} + +void SlideModel::addItem(const QString &name, const QString &type, + const QString &imageBackground, const QString &videoBackground, + const QStringList &text, const QString &audio, + const QString &font, const int &fontSize, + const QString &horizontalTextAlignment, + const QString &verticalTextAlignment) { + Slide *item = new Slide(name, type, imageBackground, videoBackground, + text, audio, font, fontSize, horizontalTextAlignment, + verticalTextAlignment); + item->setSelected(false); + item->setActive(false); + addItem(item); + qDebug() << "#################################"; + qDebug() << name << type << font << fontSize; + qDebug() << "#################################"; +} + +void SlideModel::addItem(const QString &name, const QString &type, + const QString &imageBackground, const QString &videoBackground, + const QStringList &text, const QString &audio, + const QString &font, const int &fontSize, + const QString &horizontalTextAlignment, + const QString &verticalTextAlignment, + const int &serviceItemId) { + Slide *item = new Slide(name, type, imageBackground, videoBackground, + text, audio, font, fontSize, horizontalTextAlignment, + verticalTextAlignment); + item->setSelected(false); + item->setActive(false); + item->setServiceItemId(serviceItemId); + addItem(item); + qDebug() << "#################################"; + qDebug() << name << type << font << fontSize << serviceItemId; + qDebug() << "#################################"; +} + +void SlideModel::insertItem(const int &index, const QString &name, const QString &type) { + Slide *item = new Slide(name, type); + item->setSelected(false); + item->setActive(false); + insertItem(index, item); + qDebug() << name << type; +} + +void SlideModel::insertItem(const int &index, const QString &name, const QString &type, + const QString &imageBackground, const QString &videoBackground) { + Slide *item = new Slide(name, type, imageBackground, videoBackground); + item->setSelected(false); + item->setActive(false); + insertItem(index, item); + qDebug() << name << type << imageBackground; +} + +void SlideModel::insertItem(const int &index, const QString &name, const QString &type, + const QString &imageBackground, const QString &videoBackground, + const QStringList &text) { + Slide *item = new Slide(name, type, imageBackground, videoBackground, text); + insertItem(index, item); + qDebug() << name << type << imageBackground << text; +} + +void SlideModel::insertItem(const int &index, const QString &name, + const QString &type,const QString &imageBackground, + const QString &videoBackground,const QStringList &text, + const QString &audio) { + Slide *item = new Slide(name, type, imageBackground, videoBackground, + text, audio); + item->setSelected(false); + item->setActive(false); + insertItem(index, item); + qDebug() << name << type << imageBackground << text; +} + +void SlideModel::insertItem(const int &index, const QString &name, + const QString &type,const QString &imageBackground, + const QString &videoBackground,const QStringList &text, + const QString &audio, const QString &font, + const int &fontSize) { + Slide *item = new Slide(name, type, imageBackground, videoBackground, + text, audio, font, fontSize); + item->setSelected(false); + item->setActive(false); + insertItem(index, item); + qDebug() << "#################################"; + qDebug() << name << type << font << fontSize; + qDebug() << "#################################"; +} + +void SlideModel::insertItem(const int &index, const QString &name, + const QString &type,const QString &imageBackground, + const QString &videoBackground,const QStringList &text, + const QString &audio, const QString &font, + const int &fontSize, const int &slideNumber) { + Slide *item = new Slide(name, type, imageBackground, videoBackground, + text, audio, font, fontSize, slideNumber); + item->setSelected(false); + item->setActive(false); + insertItem(index, item); + qDebug() << "#################################"; + qDebug() << name << type << font << fontSize << slideNumber; + qDebug() << "#################################"; +} + +void SlideModel::removeItem(int index) { + beginRemoveRows(QModelIndex(), index, index); + m_items.removeAt(index); + endRemoveRows(); +} + +bool SlideModel::moveRows(int sourceIndex, int destIndex, int count) { + qDebug() << index(sourceIndex).row(); + qDebug() << index(destIndex).row(); + + const int lastIndex = rowCount() - 1; + + if (sourceIndex == destIndex + || (sourceIndex < 0 || sourceIndex > lastIndex) + || (destIndex < 0 || destIndex > lastIndex)) { + return false; + } + + const QModelIndex parent = index(sourceIndex).parent(); + const bool isMoveDown = destIndex > sourceIndex; + + if (!beginMoveRows(parent, sourceIndex, sourceIndex + count - 1, + parent, isMoveDown ? destIndex + 2 : destIndex)) { + qDebug() << "Can't move rows"; + return false; + } + + qDebug() << "starting move: " << "source: " << sourceIndex << "dest: " << destIndex; + + m_items.move(sourceIndex, isMoveDown ? destIndex + 1 : destIndex); + + endMoveRows(); + return true; +} + +bool SlideModel::moveDown(int id) { + qDebug() << index(id).row(); + qDebug() << index(id + 1).row(); + QModelIndex parent = index(id).parent(); + + bool begsuc = beginMoveRows(parent, id, + id, parent, id + 2); + if (begsuc) { + int dest = id + 1; + if (dest >= m_items.size()) + { + qDebug() << "dest too big, moving to end"; + m_items.move(id, m_items.size() - 1); + } + else + m_items.move(id, dest); + endMoveRows(); + return true; + } + return false; +} + +bool SlideModel::moveUp(int id) { + qDebug() << index(id).row(); + qDebug() << index(id - 1).row(); + QModelIndex parent = index(id).parent(); + + bool begsuc = beginMoveRows(parent, id, + id, parent, id - 1); + if (begsuc) { + int dest = id - 1; + if (dest <= -1) + { + qDebug() << "dest too big, moving to beginning"; + m_items.move(id, 0); + } + else + m_items.move(id, dest); + endMoveRows(); + return true; + } + + return false; +} + +QVariantMap SlideModel::getItem(int index) const { + QVariantMap data; + const QModelIndex idx = this->index(index,0); + // qDebug() << idx; + if( !idx.isValid() ) + return data; + const QHash rn = roleNames(); + // qDebug() << rn; + QHashIterator it(rn); + while (it.hasNext()) { + it.next(); + qDebug() << it.key() << ":" << it.value(); + data[it.value()] = idx.data(it.key()); + } + return data; +} + +QVariantList SlideModel::getItems() { + QVariantList data; + Slide * item; + foreach (item, m_items) { + qDebug() << item->serviceItemId(); + QVariantMap itm; + itm["type"] = item->type(); + itm["imageBackground"] = item->imageBackground(); + itm["videoBackground"] = item->videoBackground(); + itm["text"] = item->text(); + itm["audio"] = item->audio(); + itm["font"] = item->font(); + itm["fontSize"] = item->fontSize(); + itm["horizontalTextAlignment"] = item->horizontalTextAlignment(); + itm["verticalTextAlignment"] = item->verticalTextAlignment(); + itm["serviceItemId"] = item->seviceItemId(); + itm["selected"] = item->selected(); + itm["active"] = item->active(); + data.append(itm); + } + qDebug() << "$$$$$$$$$$$$$$$$$$$$$$$$$$$"; + qDebug() << data; + qDebug() << "$$$$$$$$$$$$$$$$$$$$$$$$$$$"; + return data; +} + +bool SlideModel::select(int id) { + for (int i = 0; i < m_items.length(); i++) { + QModelIndex idx = index(i); + Slide *item = m_items[idx.row()]; + if (item->selected()) { + item->setSelected(false); + qDebug() << "################"; + qDebug() << "deselected" << item->name(); + qDebug() << "################"; + emit dataChanged(idx, idx, QVector() << SelectedRole); + } + } + QModelIndex idx = index(id); + Slide *item = m_items[idx.row()]; + item->setSelected(true); + qDebug() << "################"; + qDebug() << "selected" << item->name(); + qDebug() << "################"; + emit dataChanged(idx, idx, QVector() << SelectedRole); + return true; +} + +bool SlideModel::activate(int id) { + QModelIndex idx = index(id); + Slide *item = m_items[idx.row()]; + + for (int i = 0; i < m_items.length(); i++) { + QModelIndex idx = index(i); + Slide *itm = m_items[idx.row()]; + if (itm->active()) { + itm->setActive(false); + qDebug() << "################"; + qDebug() << "deactivated" << itm->name(); + qDebug() << "################"; + emit dataChanged(idx, idx, QVector() << ActiveRole); + } + } + + item->setActive(true); + qDebug() << "################"; + qDebug() << "activated" << item->name(); + qDebug() << "################"; + emit dataChanged(idx, idx, QVector() << ActiveRole); + return true; +} + +bool SlideModel::deactivate(int id) { + QModelIndex idx = index(id); + Slide *item = m_items[idx.row()]; + + item->setActive(false); + qDebug() << "################"; + qDebug() << "deactivated" << item->name(); + qDebug() << "################"; + emit dataChanged(idx, idx, QVector() << ActiveRole); + return true; +} + +void SlideModel::clearAll() { + for (int i = m_items.size(); i >= 0; i--) { + removeItem(i); + } +} diff --git a/src/cpp/slidemodel.h b/src/cpp/slidemodel.h new file mode 100644 index 0000000..c10fb3e --- /dev/null +++ b/src/cpp/slidemodel.h @@ -0,0 +1,123 @@ +#ifndef SLIDEMODEL_H +#define SLIDEMODEL_H + +#include "slide.h" +#include +#include +#include +#include +#include + +class SlideModel : public QAbstractListModel { + Q_OBJECT + +public: + explicit SlideModel(QObject *parent = nullptr); + + enum Roles { + TypeRole = Qt::UserRole, + TextRole, + AudioRole, + ImageBackgroundRole, + VideoBackgroundRole, + HorizontalTextAlignmentRole, + VerticalTextAlignmentRole, + FontRole, + FontSizeRole, + ServiceItemIdRole, + ActiveRole, + SelectedRole + }; + + // Basic functionality: + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + // int columnCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, + int role = Qt::DisplayRole) const override; + QHash roleNames() const override; + + // Q_INVOKABLE int index(int row, int column, + // const QModelIndex &parent = QModelIndex()) const override; + // Q_INVOKABLE QModelIndex parent(const QModelIndex &index) const override; + + // Editable: + bool setData(const QModelIndex &index, const QVariant &value, + int role = Qt::EditRole) override; + Qt::ItemFlags flags(const QModelIndex &index) const override; + + // Helper methods + void addItem(Slide *item); + void insertItem(const int &index, Slide *item); + Q_INVOKABLE void addItem(const QString &name, const QString &type); + Q_INVOKABLE void addItem(const QString &text, const QString &type, + const QString &imageBackground, + const QString &videoBackground); + Q_INVOKABLE void addItem(const QString &text, const QString &type, + const QString &imageBackground, + const QString &videoBackground, + const QString &audio); + Q_INVOKABLE void addItem(const QString &text, const QString &type, + const QString &imageBackground, + const QString &videoBackground, + const QString &audio, + const QString &font, const int &fontSize); + Q_INVOKABLE void addItem(const QString &text, const QString &type, + const QString &imageBackground, + const QString &videoBackground, + const QString &audio, + const QString &font, const int &fontSize, + const QString &horizontalTextAlignment, + const QString &verticalTextAlignment); + Q_INVOKABLE void addItem(const QString &text, const QString &type, + const QString &imageBackground, + const QString &videoBackground, + const QString &audio, + const QString &font, const int &fontSize, + const QString &horizontalTextAlignment, + const QString &verticalTextAlignment, + const int &serviceItemId); + Q_INVOKABLE void insertItem(const int &index, const QString &text, + const QString &type); + Q_INVOKABLE void insertItem(const int &index, const QString &text, + const QString &type, const QString &imageBackground, + const QString &videoBackground); + Q_INVOKABLE void insertItem(const int &index, const QString &text, + const QString &type, const QString &imageBackground, + const QString &videoBackground, + const QString &audio); + Q_INVOKABLE void insertItem(const int &index, const QString &text, + const QString &type, const QString &imageBackground, + const QString &videoBackground, + const QString &audio, const QString &font, + const int &fontSize); + Q_INVOKABLE void insertItem(const int &index, const QString &text, + const QString &type, const QString &imageBackground, + const QString &videoBackground, + const QString &audio, const QString &font, + const int &fontSize, + const QString &horizontalTextAlignment, + const QString &verticalTextAlignment); + Q_INVOKABLE void insertItem(const int &index, const QString &text, + const QString &type, const QString &imageBackground, + const QString &videoBackground, + const QString &audio, const QString &font, + const int &fontSize, + const QString &horizontalTextAlignment, + const QString &verticalTextAlignment, + const int &serviceItemId); + Q_INVOKABLE void removeItem(int index); + Q_INVOKABLE bool moveRows(int sourceIndex, int destIndex, int count); + Q_INVOKABLE bool moveDown(int index); + Q_INVOKABLE bool moveUp(int index); + Q_INVOKABLE bool select(int id); + Q_INVOKABLE bool activate(int id); + Q_INVOKABLE bool deactivate(int id); + Q_INVOKABLE QVariantMap getItem(int index) const; + Q_INVOKABLE QVariantList getItems(); + Q_INVOKABLE void clearAll(); + +private: + QList m_items; +}; + +#endif // SLIDEMODEL_H diff --git a/src/cpp/slideobject.cpp b/src/cpp/slideobject.cpp new file mode 100644 index 0000000..264b16d --- /dev/null +++ b/src/cpp/slideobject.cpp @@ -0,0 +1,230 @@ +#include "slideobject.h" +#include "serviceitemmodel.h" + +#include +#include + +using namespace PoDoFo; +SlideObject::SlideObject(QObject *parent) + : Slide{parent} +{ + qDebug() << "Initializing slide"; +} + +SlideObject::SlideObject(const QString &text, const QString &audio, + const QString &imageBackground, + const QString &videoBackground, + const QString &horizontalTextAlignment, + const QString &verticalTextAlignment, + const QString &font, + const int &fontSize, + const int &imageCount, + const bool &isPlaying, + const QString &type, + QObject *parent) +: Slide(parent), + m_isPlaying(isPlaying), + m_slideIndex(0) +{ + setText(text); + setAudio(audio), + setImageBackground(imageBackground), + setVideoBackground(videoBackground), + setVerticalTextAlignment(verticalTextAlignment), + setHorizontalTextAlignment(horizontalTextAlignment), + setFont(font), + setFontSize(fontSize), + setImageCount(imageCount), + setType(type), + qDebug() << "Initializing slide with defaults"; +} + +bool SlideObject::isPlaying() const +{ + return m_isPlaying; +} + +int SlideObject::slideIndex() const +{ + return m_slideIndex; +} + +int SlideObject::slideSize() const +{ + return m_slideSize; +} + +void SlideObject::changeSlide(QVariantMap item) +{ + setServiceItem(item); + setType(serviceItemId().value("type").toString()); + qDebug() << "#$% SLIDE TYPE: " << type() << " %$#"; + + // First let's clear the text and then set + // the size and index of a basic slide + // then we'll build the rest + setText(""); + m_slideSize = 1; + m_slideIndex = 1; + + qDebug() << serviceItemId().value("backgroundType").toString(); + if (serviceItemId().value("backgroundType") == "image") { + setImageBackground(serviceItemId().value("background").toString()); + setVideoBackground(""); + } else { + setVideoBackground(serviceItemId().value("background").toString()); + setImageBackground(""); + } + + setFont(serviceItemId().value("font").toString()); + setFontSize(serviceItemId().value("fontSize").toInt()); + setAudio(""); + + if (type() == "presentation") { + qDebug() << "#$#$#$#$ THIS PDF $#$#$#$#"; + int pageCount; + QString str = imageBackground().remove(0,6); + qDebug() << str; + std::string file = str.toStdString(); + // qDebug() << file; + const char * doc = file.c_str(); + qDebug() << doc; + try { + PdfMemDocument pdf = PdfMemDocument(doc); + pageCount = pdf.GetPageCount(); + } catch ( const PdfError & eCode ) { + eCode.PrintErrorMsg(); + eCode.GetError(); + return; + } + setImageCount(pageCount); + qDebug() << imageCount(); + m_slideSize = imageCount(); + } + + QStringList text = serviceItemId().value("text").toStringList(); + if (type() == "song") { + qDebug() << "TEXT LENGTH: " << text.length(); + m_slideSize = text.length(); + m_slideIndex = 1; + setText(text[0]); + setAudio(serviceItemId().value("audio").toString()); + } + + qDebug() << "MAP: " << serviceItemId().value("text"); + emit slideIndexChanged(m_slideIndex); + emit slideSizeChanged(m_slideSize); +} + +bool SlideObject::next(QVariantMap nextItem) +{ + qDebug() << "Starting to go to next item."; + qDebug() << "SlideObject Index: " << slideIndex() << " SlideObject Size: " << slideSize(); + QStringList text = serviceItemId().value("text").toStringList(); + if (slideIndex() == slideSize()) { + // changeSlideObject(nextItem); + return true; + } + + qDebug() << type(); + // since the string list is 0 indexed m_slideIndex actually + // maps to the next item. + if (type() == "song") { + int nextTextIndex = slideIndex(); + qDebug() << nextTextIndex; + qDebug() << text[nextTextIndex]; + setText(text[nextTextIndex]); + m_slideSize++; + emit slideIndexChanged(m_slideIndex); + } + + if (type() == "presentation") { + qDebug() << "prev slide index: " << slideIndex(); + m_slideIndex++; + emit slideIndexChanged(m_slideIndex); + qDebug() << "new slide index: " << slideIndex(); + } + + return false; +} + +bool SlideObject::previous(QVariantMap prevItem) +{ + qDebug() << "Starting to go to previous item."; + qDebug() << "SlideObject Index: " << slideIndex() << " SlideObject Size: " << slideSize(); + QStringList text = serviceItemId().value("text").toStringList(); + if (slideIndex() == 1) { + // changeSlideObject(prevItem); + return true; + } + + // since the string list is 0 indexed m_slideIndex actually + // maps to the next item. So the prev text is minus 2 + if (type() == "song") { + int prevTextIndex = slideIndex() - 2; + qDebug() << prevTextIndex; + qDebug() << text[prevTextIndex]; + setText(text[prevTextIndex]); + m_slideIndex--; + emit slideIndexChanged(m_slideIndex); + } + + if (type() == "presentation") { + qDebug() << "prev slide index: " << slideIndex(); + m_slideIndex--; + emit slideIndexChanged(m_slideIndex); + qDebug() << "new slide index: " << slideIndex(); + } + + return false; +} + +bool SlideObject::changeSlideIndex(int index) +{ + qDebug() << "Starting to change slide index."; + qDebug() << "SlideObject Index: " << slideIndex() << " SlideObject Size: " << slideSize(); + QStringList text = serviceItemId().value("text").toStringList(); + if (index > slideSize() - 1 || index < 0) { + qDebug() << "index is invalid: " << index; + return false; + } + + // since the string list is 0 indexed m_slideIndex actually + // maps to the next item. So the prev text is minus 2 + if (type() == "song") { + int textIndex = index; + qDebug() << textIndex; + qDebug() << text[textIndex]; + setText(text[textIndex]); + m_slideIndex = index; + emit slideIndexChanged(m_slideIndex); + return true; + } + + if (type() == "presentation") { + qDebug() << "prev slide index: " << slideIndex(); + m_slideIndex = index; + qDebug() << "new slide index: " << slideIndex(); + emit slideIndexChanged(m_slideIndex); + return true; + } + return false; +} + +void SlideObject::play() +{ + m_isPlaying = true; + emit isPlayingChanged(m_isPlaying); +} + +void SlideObject::pause() +{ + m_isPlaying = false; + emit isPlayingChanged(m_isPlaying); +} + +void SlideObject::playPause() +{ + m_isPlaying = !m_isPlaying; + emit isPlayingChanged(m_isPlaying); +} diff --git a/src/cpp/slideobject.h b/src/cpp/slideobject.h new file mode 100644 index 0000000..874c057 --- /dev/null +++ b/src/cpp/slideobject.h @@ -0,0 +1,50 @@ +#ifndef SLIDEOBJECT_H +#define SLIDEOBJECT_H + +#include "slide.h" +#include +#include +#include +#include + +class SlideObject : public Slide +{ + Q_OBJECT + Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY isPlayingChanged) + Q_PROPERTY(int slideIndex READ slideIndex NOTIFY slideIndexChanged) + Q_PROPERTY(int slideSize READ slideSize NOTIFY slideSizeChanged) + // QML_ELEMENT + +public: + explicit SlideObject(QObject *parent = nullptr); + SlideObject(const QString &text, const QString &audio, + const QString &imageBackground, const QString &videoBackground, + const QString &horizontalTextAlignment, const QString &verticalTextAlignment, + const QString &font, const int &fontSize, const int &imageCount, + const bool &isPlaying, const QString &type, + QObject * parent = nullptr); + + bool isPlaying() const; + int slideIndex() const; + int slideSize() const; + + Q_INVOKABLE void changeSlide(QVariantMap item); + Q_INVOKABLE void play(); + Q_INVOKABLE void pause(); + Q_INVOKABLE void playPause(); + Q_INVOKABLE bool next(QVariantMap nextItem); + Q_INVOKABLE bool previous(QVariantMap prevItem); + Q_INVOKABLE bool changeSlideIndex(int index); + +signals: + Q_INVOKABLE void isPlayingChanged(bool isPlaying); + Q_INVOKABLE void slideIndexChanged(int slideIndex); + Q_INVOKABLE void slideSizeChanged(int slideSize); + +private: + bool m_isPlaying; + int m_slideIndex; + int m_slideSize; +}; + +#endif //SLIDEOBJECT_H