diff --git a/plugins/dockers/storyboarddocker/storyboardItem.cpp b/plugins/dockers/storyboarddocker/storyboardItem.cpp new file mode 100644 index 0000000000..4b0ebc816f --- /dev/null +++ b/plugins/dockers/storyboarddocker/storyboardItem.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2020 Saurabh Kumar + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "storyboardItem.h" + +//create 3 rows by default +StoryboardItem::StoryboardItem() +{} + +StoryboardItem::~StoryboardItem() +{ + qDeleteAll(m_childData); +} + +void StoryboardItem::appendChild(QVariant &data) +{ + StoryboardChild* child = new StoryboardChild(data, this); + m_childData.append(child); +} + +void StoryboardItem::insertChild(int row, QVariant &data) +{ + StoryboardChild* child = new StoryboardChild(data, this); + m_childData.insert(row, child); +} + +void StoryboardItem::removeChild(int row) +{ + m_childData.removeAt(row); +} + +int StoryboardItem::childCount() const +{ + return m_childData.count(); +} + +StoryboardChild* StoryboardItem::child(int row) +{ + if (row < 0 || row >= m_childData.size()) + return nullptr; + return m_childData.at(row); +} diff --git a/plugins/dockers/storyboarddocker/storyboardItem.h b/plugins/dockers/storyboarddocker/storyboardItem.h new file mode 100644 index 0000000000..348c58892e --- /dev/null +++ b/plugins/dockers/storyboarddocker/storyboardItem.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2020 Saurabh Kumar + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef STORYBOARD_ITEM +#define STORYBOARD_ITEM + +#include +#include + +//each storyboardItem contains pointer to child data +class StoryboardItem; + +class StoryboardChild +{ +public: + StoryboardChild(QVariant &data, StoryboardItem *parent) + : m_data(data) + , m_parentItem(parent) + {} + StoryboardItem parent(){ return *m_parentItem;} + QVariant data(){ return m_data;} + //returns the row number of this child relative to its parent + int row() const{ + if (m_parentItem) + return m_parentItem->m_childData.indexOf(const_cast(this)); + + return 0; + } + void setData(QVariant &value){ + m_data = value; + } + +private: + StoryboardItem *m_parentItem; + QVariant m_data; +}; + +class StoryboardItem +{ +public: + //see later if the constructor needs data + explicit StoryboardItem(/*const QVector &data*/); + ~StoryboardItem(); + + void appendChild(int row, QVariant &data); + void insertChild(int row, QVariant &data); + void removeChild(int row); + void childCount() const; + StoryboardChild *child(int row); + QPointer parent(){ + return nullptr; + } + +private: + QVector m_childData; +}; diff --git a/plugins/dockers/storyboarddocker/storyboardModel.cpp b/plugins/dockers/storyboarddocker/storyboardModel.cpp index 7032b8c802..a6f3890336 100644 --- a/plugins/dockers/storyboarddocker/storyboardModel.cpp +++ b/plugins/dockers/storyboarddocker/storyboardModel.cpp @@ -1,189 +1,221 @@ /* * Copyright (c) 2020 Saurabh Kumar * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "storyboardModel.h" #include #include -struct CommentHeader -{ - QString name; - bool visiblity; -}; +StoryboardModel::StoryboardModel(QObject *parent) + : QAbstractTableModel(parent) +{} -struct StoryboardItem -{ - int startFrame; - QString name; - int duration; - QStringList comments; - - StoryboardItem(int _startFrame = 0, QString _name = "", int _duration = 0) - : startFrame(_startFrame) - , name(_name) - , duration(std::max(0, _duration) -}; - -struct StoryboardModel::Private +QModelIndex StoryboardModel::index(int row, int column, const QModelIndex &parent) const { - Private() - //initialize - {} - - //KisImageWSP image; + if (!hasIndex(row, column, parent)) + return QModelIndex(); + if (row < 0 || row >= rowCount()) + return QModelIndex(); + if (column !=0) + return QModelIndex(); - QVector items; - QVector commentHeader; - - //functions -}; + //top level node has invalid parent + if (!parent.isValid) + return createIndex(row, column, m_items.at(row)); + else + StoryboardItem *parentItem = static_cast(parent.internalPointer()); + + TreeItem *childItem = parentItem->child(row); + if (childItem) + return createIndex(row, column, childItem); + return QModelIndex(); +} -StoryboardModel::StoryboardModel(const QStringList &strings, QObject *parent) - : QAbstractTableModel(parent) - , m_d(new Private()) +QModelIndex StoryboardModel::parent(const QModelIndex &index) const { - //initialize variables + if (!index.isValid()) + return QModelIndex(); + + //return parent only for 2nd level nodes + StoryboardChild *childItem = dynamic_cast(index.internalPointer()); + if(childItem){ + StoryboardItem *parentItem = childItem->parent(); + int indexOfParent = m_items.indexOf(const_cast(parentItem)); + return createIndex(indexOfParent, 0, parentItem); + } + return QModelIndex(); } int StoryboardModel::rowCount(const QModelIndex &parent) const { - return m_d->items.count(); + if (!parent.isValid()) + return m_items.count(); + else if (dynamic_cast(parent.internalPointer())){ + StoryboardItem parentItem = dynamic_cast(parent.internalPointer()); + return parentItem->childCount(); + } + return 0; //2nd level nodes have no child } -int StoryboardModel::rowCount(const QModelIndex &parent) const +int StoryboardModel::columnCount(const QModelIndex &parent) const { - return m_d->commentHeader.count() + 3; + //2nd level nodes have no child + if (dynamic_cast(parent.internalPointer())){ + return 0; + } + return 1; } QVariant StoryboardModel::data(const QModelIndex &index, int role) const { - /* - if(!index.isValid()) + + if (!index.isValid()) return QVariant(); - if(index.row() >= m_d->item.size()) + if (index.row() >= m_items.size()) return QVariant(); - if (role == Qt::DisplayRole || role == Qt::EditRole) - { - return m_d->items.at(index.row()); - } - */ - return QVariant(); - -} + //return data only for the storyboardChild i.e. 2nd level nodes + if (dynamic_cast(index.internalPointer())) + return QVariant(); -QVariant KisMetaDataModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (orientation == Qt::Horizontal && - (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::DecorationRole) + if (role == Qt::DisplayRole || role == Qt::EditRole) { - if(section > 3) { - switch (role) { - case Qt::DecorationRole: - if (m_d->commentHeader[section-3].visibility){ - //return no-visible icon - } - else{ - //return visible icon - } - case (Qt::DisplayRole||Qt::EditRole): - return m_d->commentHeader[section-3].name; - } + StoryboardChild *child = static_cast(index.internalPointer()); + child->data().toS + if (child){ + switch (index.row()): + case 0: + //frame number + return child->data().toInt(); + case 1: + //item name + return child->data().toString(); + case 2: + //duration + return child->data().toInt(); + default: + //comment(would it be string or a custom struct) + return child->data().toInt(); } } return QVariant(); } bool StoryboardModel::setData(const QModelIndex & index, const QVariant & value, int role) { - /* - qDebug()<<"attempting data set"<(index.internalPointer())) + return false; if (index.isValid() && (role == Qt::EditRole || role == Qt::DisplayRole)) { - if( value.toInt) - stringList.replace(index.row(), value.toString()); - emit dataChanged(index, index); - return true; - } - */ - return false; -} - -bool KisMetaDataModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) -{ - if (orientation == Qt::Horizontal && - (role == Qt::DisplayRole || role == Qt::EditRole) - { - if(section > 3) { - m_d->commentHeader.replace(section -3, (CommentHeader)value); + StoryboardChild *child = static_cast(index.internalPointer()); + if (child){ + child->setData(value); + emit dataChanged(index, index); return true; } } - return false + return false; } Qt::ItemFlags StoryboardModel::flags(const QModelIndex & index) const { //qDebug()<<"flags requested"; if(!index.isValid()) return Qt::ItemIsDropEnabled; - return Qt::ItemIsDragEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled| - Qt::ItemIsEditable | Qt::ItemIsEnabled ; + //1st level nodes + if (dynamic_cast(index.internalPointer())) + return Qt::ItemIsDragEnabled | Qt::ItemIsSelectable ; + + //2nd level nodes + return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled; } bool StoryboardModel::insertRows(int position, int rows, const QModelIndex &parent) { - qDebug()<<"row inserted"; - beginInsertRows(QModelIndex(), position, position+rows-1); - - for (int row = 0; row < rows; ++row) { - StoryboardItem item() - - stringList.insert(position, ""); //maybe set a default name like comment 1 + //we can't insert to 2nd level nodes as they are leaf nodes + if (dynamic_cast(parent.internalPointer())){ + return false; + } + //insert 1st level nodes + if (!parent.isValid()){ + beginInsertRows(QModelIndex(), position, position+rows-1); + for (int row = 0; row < rows; ++row) { + StoryboardItem newItem = new StoryboardItem(); + m_items.insert(position, newItem); + } + endInsertRows(); + return true; } - endInsertRows(); - return true; + //insert 2nd level nodes + StoryboardItem *item = dynamic_cast(index.internalPointer()); + if (item){ + beginInsertRows(QModelIndex(), position, position+rows-1); + for (int row = 0; row < rows; ++row) { + item->insertChild(position, QVariant()); + } + endInsertRows(); + return true; + } + return false; } bool StoryboardModel::removeRows(int position, int rows, const QModelIndex &parent) { qDebug()<<"row removed"; - beginRemoveRows(QModelIndex(), position, position+rows-1); - - for (int row = 0; row < rows; ++row) { - stringList.removeAt(position); + //2nd level node has no child + if (dynamic_cast(parent.internalPointer())){ + return false; + } + //remove 1st level nodes + if (!parent.isValid()){ + beginRemoveRows(QModelIndex(), position, position+rows-1); + for (int row = 0; row < rows; ++row) { + delete m_items.at(row); + m_items.removeAt(position); + } + endRemoveRows(); + return true; } - endRemoveRows(); - return true; + //remove 2nd level nodes + StoryboardItem *item = dynamic_cast(index.internalPointer()); + if (item){ + beginRemoveRows(QModelIndex(), position, position+rows-1); + for (int row = 0; row < rows; ++row) { + item->removeChild(position); + } + endRemoveRows(); + return true; + } + return false; } Qt::DropActions StoryboardModel::supportedDropActions() const { return Qt::CopyAction | Qt::MoveAction; } Qt::DropActions StoryboardModel::supportedDragActions() const { return Qt::CopyAction | Qt::MoveAction; } diff --git a/plugins/dockers/storyboarddocker/storyboardModel.h b/plugins/dockers/storyboarddocker/storyboardModel.h index e677dfd7f0..43ffa71eef 100644 --- a/plugins/dockers/storyboarddocker/storyboardModel.h +++ b/plugins/dockers/storyboarddocker/storyboardModel.h @@ -1,65 +1,64 @@ /* * Copyright (c) 2020 Saurabh Kumar * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef STORYBOARD_MODEL #define STORYBOARD_MODEL #include #include +#include "storyboardItem.h" + /* The main storyboard model. */ -class StoryboardModel : public QAbstractListModel +class StoryboardModel : public QAbstractItemModel { - Q_OBJECT public: //if we don't need this constructor change it - StoryboardModel(QObject *parent = 0); + StoryboardModel(QObject *parent = nullptr); + + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; + QModelIndex StoryboardModel::parent(const QModelIndex &index) const override; 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; - QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Qt::ItemFlags flags(const QModelIndex &index) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) override; //for removing and inserting rows bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()); bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex()); - //for removing and inserting column - bool insertColumns(int position, int Columns, const QModelIndex &index = QModelIndex()); - bool removeColumns(int position, int Columns, const QModelIndex &index = QModelIndex()); - //for drag and drop Qt::DropActions supportedDropActions() const override; Qt::DropActions supportedDragActions() const override; + + //this function accesses the value from the comment model int commentCount(); private: - struct Private; - const QScopedPointer m_d; - + QVector m_items; + int commentCount = 0; }; #endif \ No newline at end of file