diff --git a/plugins/dockers/storyboarddocker/storyboardDelegate.cpp b/plugins/dockers/storyboarddocker/storyboardDelegate.cpp index 470b074228..8b6a3f483f 100644 --- a/plugins/dockers/storyboarddocker/storyboardDelegate.cpp +++ b/plugins/dockers/storyboarddocker/storyboardDelegate.cpp @@ -1,382 +1,384 @@ /* * 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 "storyboardDelegate.h" #include #include #include #include #include #include #include #include #include #include #include #include "storyboardModel.h" StoryboardDelegate::StoryboardDelegate(QObject *parent) : QStyledItemDelegate(parent) { } StoryboardDelegate::~StoryboardDelegate() { } void StoryboardDelegate::paint(QPainter *p, const QStyleOptionViewItem &option, const QModelIndex &index) const { p->save(); { QStyle *style = option.widget ? option.widget->style() : QApplication::style(); style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, p, option.widget); p->setFont(option.font); if (!index.isValid()){ p->restore(); return; } if (!index.parent().isValid()){ QRect parentRect = option.rect; p->drawRect(parentRect); parentRect.setTopLeft(parentRect.topLeft() + QPoint(4, 4)); parentRect.setBottomRight(parentRect.bottomRight() - QPoint(4, 4)); //TODO: change highlight color and the area that is highlighted if (option.state & QStyle::State_Selected){ p->fillRect(option.rect, option.palette.foreground()); } else { p->fillRect(option.rect, option.palette.background()); } p->eraseRect(parentRect); } else{ //paint Child index (the indices that hold data) QModelIndex parent = index.parent(); //draw the child items int rowNum = index.model()->rowCount(parent); int childNum = index.row(); QString data = index.model()->data(index, Qt::DisplayRole).toString(); switch (childNum) { case 0: { QRect frameNumRect = option.rect; frameNumRect.setHeight(m_view->fontMetrics().height()+3); frameNumRect.setWidth(3 * m_view->fontMetrics().width("0")+2); frameNumRect.moveBottom(option.rect.top()-1); p->setPen(QPen(option.palette.dark(), 2)); p->drawRect(frameNumRect); p->setPen(QPen(option.palette.text(), 1)); p->drawText(frameNumRect, Qt::AlignHCenter | Qt::AlignVCenter, data); QIcon icon = KisIconUtils::loadIcon("krita-base"); icon.paint(p, option.rect); p->setPen(QPen(option.palette.dark(), 2)); p->drawRect(option.rect); if (option.state & QStyle::State_MouseOver){ QRect buttonsRect = option.rect; buttonsRect.setTop(option.rect.bottom() - 22); p->fillRect(buttonsRect, option.palette.background()); buttonsRect.setWidth(22); buttonsRect.moveBottomLeft(option.rect.bottomLeft()); QIcon addIcon = KisIconUtils::loadIcon("list-add"); addIcon.paint(p, buttonsRect); buttonsRect.moveBottomRight(option.rect.bottomRight()); QIcon deleteIcon = KisIconUtils::loadIcon("trash-empty"); deleteIcon.paint(p, buttonsRect); } break; } case 1: { QRect itemNameRect = option.rect; itemNameRect.setLeft(option.rect.left() + 5); p->setPen(QPen(option.palette.text(), 1)); p->drawText(itemNameRect, Qt::AlignLeft | Qt::AlignVCenter, data); p->setPen(QPen(option.palette.dark(), 2)); p->drawRect(option.rect); break; } case 2: //time duration case 3: //frame duration { drawSpinBox(p, option, data); break; } default: { - p->setPen(QPen(option.palette.dark(), 2)); - //p->drawRect(option.rect); - drawComment(p, option, data, index); + const StoryboardModel* model = dynamic_cast(index.model()); + if (model->getComment(index.row() - 4).visibility){ + p->setPen(QPen(option.palette.dark(), 2)); + drawComment(p, option, data, index); + } break; } } } } p->restore(); } void StoryboardDelegate::drawSpinBox(QPainter *p, const QStyleOptionViewItem &option, QString data) const { QStyle *style = option.widget ? option.widget->style() : QApplication::style(); QStyleOptionSpinBox spinBoxOption; spinBoxOption.stepEnabled = QAbstractSpinBox::StepDownEnabled | QAbstractSpinBox::StepUpEnabled; spinBoxOption.subControls = QStyle::SC_SpinBoxUp | QStyle::SC_SpinBoxDown; spinBoxOption.rect = option.rect; p->setPen(QPen(option.palette.dark(), 2)); p->drawRect(option.rect); style->drawComplexControl(QStyle::CC_SpinBox, &spinBoxOption, p, option.widget); QRect rect = style->subControlRect(QStyle::CC_SpinBox, &spinBoxOption, QStyle::QStyle::SC_SpinBoxEditField); rect.moveTopLeft(option.rect.topLeft()); p->setPen(QPen(option.palette.text(), 1)); p->drawText(rect, Qt::AlignHCenter | Qt::AlignVCenter, data); } void StoryboardDelegate::drawComment(QPainter *p, const QStyleOptionViewItem &option, QString data, const QModelIndex &index) const { QStyle *style = option.widget ? option.widget->style() : QApplication::style(); const StoryboardModel* model = dynamic_cast(index.model()); QRect titleRect = option.rect; titleRect.setHeight(option.fontMetrics.height() + 3); p->setPen(QPen(option.palette.text(), 1)); p->drawText(titleRect, Qt::AlignLeft | Qt::AlignVCenter, model->getComment(index.row() - 4).name); QRect contentRect = option.rect; contentRect.setTop(option.rect.top() + option.fontMetrics.height() + 3); p->setPen(QPen(option.palette.dark(), 2)); p->drawRect(contentRect); contentRect.setTopLeft(contentRect.topLeft() + QPoint(5, 5)); contentRect.setBottomRight(contentRect.bottomRight() - QPoint(5, 5)); p->setPen(QPen(option.palette.text(), 1)); p->drawText(contentRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, data); } QSize StoryboardDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { if (!index.parent().isValid()){ int width = option.widget->width() - 17; const StoryboardModel* model = dynamic_cast(index.model()); - int numComments = model->commentCount(); + int numComments = model->visibleCommentCount(); int numItem = width/250; if(numItem <=0){ return QSize(0, 0); } return QSize(width / numItem, 120 + option.fontMetrics.height() + 3 + numComments*100 + 10); } else { return option.rect.size(); } return QSize(0,0); } QWidget *StoryboardDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option , const QModelIndex &index) const { //only create editor for children if (index.parent().isValid()){ int row = index.row(); switch (row) { case 0: //frame thumbnail is uneditable return nullptr; case 1: { QLineEdit *editor = new QLineEdit(parent); return editor; } case 2: //second and frame spin box case 3: { QSpinBox *spinbox = new QSpinBox(parent); spinbox->setRange(0, 999); return spinbox; } default: // for itemName and comments { QTextEdit *editor = new QTextEdit(parent); return editor; } } } return nullptr; } bool StoryboardDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { if ((event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick) && (index.flags() & Qt::ItemIsEnabled)) { QMouseEvent *mouseEvent = static_cast(event); //handle the duration edit event if (index.parent().isValid() && (index.row() == 2 || index.row() == 3)){ QRect upButton = spinBoxUpButton(option); QRect downButton = spinBoxDownButton(option); bool upButtonClicked = upButton.isValid() && upButton.contains(mouseEvent->pos()); bool downButtonClicked = downButton.isValid() && downButton.contains(mouseEvent->pos()); const bool leftButton = mouseEvent->buttons() & Qt::LeftButton; if (leftButton && upButtonClicked){ model->setData(index, index.data().toInt() + 1); return true; } else if (leftButton && downButtonClicked){ model->setData(index, std::max(0,index.data().toInt() - 1)); return true; } } } return false; } //set the existing data in the editor void StoryboardDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QVariant value = index.data(); if (index.parent().isValid()){ int row = index.row(); switch (row) { case 0: //frame thumbnail is uneditable return; case 1: { QLineEdit *lineEdit = static_cast(editor); lineEdit->setText(value.toString()); return; } case 2: //second and frame spin box case 3: { QSpinBox *spinbox = static_cast(editor); spinbox->setValue(value.toInt()); return; } default: // for itemName and comments { QTextEdit *textEdit = static_cast(editor); textEdit->setText(value.toString()); return; } } } } void StoryboardDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QVariant value = index.data(); if (index.parent().isValid()){ int row = index.row(); switch (row) { case 0: //frame thumbnail is uneditable return; case 1: // for itemName { QLineEdit *lineEdit = static_cast(editor); QString value = lineEdit->text(); model->setData(index, value, Qt::EditRole); return; } case 2: //second and frame spin box case 3: { QSpinBox *spinbox = static_cast(editor); int value = spinbox->value(); model->setData(index, value, Qt::EditRole); return; } default: // for comments { QTextEdit *textEdit = static_cast(editor); QString value = textEdit->toPlainText(); model->setData(index, value, Qt::EditRole); return; } } } } void StoryboardDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.row() < 4){ editor->setGeometry(option.rect); } else { //for comment textedits QRect commentRect = option.rect; commentRect.setTop(option.rect.top() + option.fontMetrics.height() + 3); editor->setGeometry(commentRect); } } void StoryboardDelegate::setView(QListView *view) { m_view = view; } QRect StoryboardDelegate::spinBoxUpButton(const QStyleOptionViewItem &option) { QStyle *style = option.widget ? option.widget->style() : QApplication::style(); QStyleOptionSpinBox spinOption; spinOption.rect = option.rect; QRect rect = style->subControlRect(QStyle::CC_SpinBox, &spinOption, QStyle::QStyle::SC_SpinBoxUp); rect.moveTopRight(option.rect.topRight()); return rect; } QRect StoryboardDelegate::spinBoxDownButton(const QStyleOptionViewItem &option) { QStyle *style = option.widget ? option.widget->style() : QApplication::style(); QStyleOptionSpinBox spinOption; spinOption.rect = option.rect; QRect rect = style->subControlRect(QStyle::CC_SpinBox, &spinOption, QStyle::QStyle::SC_SpinBoxDown); rect.moveBottomRight(option.rect.bottomRight()); return rect; } QRect StoryboardDelegate::spinBoxEditField(const QStyleOptionViewItem &option) { QStyle *style = option.widget ? option.widget->style() : QApplication::style(); QStyleOptionSpinBox spinOption; spinOption.rect = option.rect; QRect rect = style->subControlRect(QStyle::CC_SpinBox, &spinOption, QStyle::QStyle::SC_SpinBoxEditField); rect.moveTopLeft(option.rect.topLeft()); return rect; } \ No newline at end of file diff --git a/plugins/dockers/storyboarddocker/storyboardModel.cpp b/plugins/dockers/storyboarddocker/storyboardModel.cpp index 7fd6b57310..651576872c 100644 --- a/plugins/dockers/storyboarddocker/storyboardModel.cpp +++ b/plugins/dockers/storyboarddocker/storyboardModel.cpp @@ -1,317 +1,335 @@ /* * 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 StoryboardModel::StoryboardModel(QObject *parent) : QAbstractItemModel(parent) , m_commentCount(0) {} QModelIndex StoryboardModel::index(int row, int column, const QModelIndex &parent) const { if (!hasIndex(row, column, parent)) return QModelIndex(); if (row < 0 || row >= rowCount()) return QModelIndex(); if (column !=0) return QModelIndex(); //1st level node has invalid parent if (!parent.isValid()){ return createIndex(row, column, m_items.at(row)); } else if (!parent.parent().isValid()){ StoryboardItem *parentItem = static_cast(parent.internalPointer()); StoryboardChild *childItem = parentItem->child(row); if (childItem) return createIndex(row, column, childItem); } return QModelIndex(); } QModelIndex StoryboardModel::parent(const QModelIndex &index) const { if (!index.isValid()) return QModelIndex(); { //no parent for 1st level node StoryboardItem *childItem = static_cast(index.internalPointer()); if (m_items.contains(childItem)){ return QModelIndex(); } } //return parent only for 2nd level nodes StoryboardChild *childItem = static_cast(index.internalPointer()); StoryboardItem *parentItem = childItem->parent(); int indexOfParent = m_items.indexOf(const_cast(parentItem)); return createIndex(indexOfParent, 0, parentItem); } int StoryboardModel::rowCount(const QModelIndex &parent) const { if (!parent.isValid()) return m_items.count(); else if (!parent.parent().isValid()){ StoryboardItem *parentItem = static_cast(parent.internalPointer()); return parentItem->childCount(); } return 0; //2nd level nodes have no child } int StoryboardModel::columnCount(const QModelIndex &parent) const { if (!parent.isValid()){ return 1; } //1st level nodes have 1 column if (!parent.parent().isValid()){ return 1; } //end level nodes have no child return 0; } QVariant StoryboardModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() >= m_items.size()) return QVariant(); if(role == Qt::SizeHintRole){ return QSize(200,200); } //return data only for the storyboardChild i.e. 2nd level nodes if (!index.parent().isValid()){ return QVariant(); } if (role == Qt::DisplayRole || role == Qt::EditRole) { StoryboardChild *child = static_cast(index.internalPointer()); return child->data(); } return QVariant(); } bool StoryboardModel::setData(const QModelIndex & index, const QVariant & value, int role) { if (index.isValid() && (role == Qt::EditRole || role == Qt::DisplayRole)) { if (!index.parent().isValid()) return false; StoryboardChild *child = static_cast(index.internalPointer()); if (child){ int fps = 24; if(index.row() == 3 && value.toInt() >= fps){ //TODO : set fps QModelIndex secondIndex = index.siblingAtRow(2); setData(secondIndex, secondIndex.data().toInt() + value.toInt() / fps, role); child->setData(value.toInt() % fps); } else { child->setData(value); } emit dataChanged(index, index); return true; } } return false; } Qt::ItemFlags StoryboardModel::flags(const QModelIndex & index) const { if(!index.isValid()) return Qt::ItemIsDropEnabled; //1st level nodes if (!index.parent().isValid()) return Qt::ItemIsDragEnabled | Qt::ItemIsSelectable | Qt::ItemIsEnabled; //2nd level nodes return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemNeverHasChildren; } bool StoryboardModel::insertRows(int position, int rows, const QModelIndex &parent) { 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; } //insert 2nd level nodes if (!parent.parent().isValid()){ StoryboardItem *item = static_cast(parent.internalPointer()); beginInsertRows(QModelIndex(), position, position+rows-1); for (int row = 0; row < rows; ++row) { item->insertChild(position, QVariant()); } endInsertRows(); return true; } //we can't insert to 2nd level nodes as they are leaf nodes return false; } bool StoryboardModel::removeRows(int position, int rows, const QModelIndex &parent) { //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; } //remove 2nd level nodes if (!parent.parent().isValid()){ StoryboardItem *item = static_cast(parent.internalPointer()); if (m_items.contains(item)){ beginRemoveRows(QModelIndex(), position, position+rows-1); for (int row = 0; row < rows; ++row) { item->removeChild(position); } endRemoveRows(); return true; } } //2nd level node has no child return false; } bool StoryboardModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) { if (sourceParent != destinationParent){ return false; } if (destinationChild == sourceRow || destinationChild == sourceRow + 1){ return false; } if (destinationChild > sourceRow + count - 1){ //we adjust for the upward shift, see qt doc for why this is needed beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild + count - 1); destinationChild = destinationChild - count; } else { beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild); } //only implementing for moves within the 1st level nodes for comment nodes if (sourceParent == destinationParent && !sourceParent.parent().isValid()){ const QModelIndex parent = sourceParent; for (int row = 0; row < count; row++){ if (sourceRow < 4 || sourceRow >= rowCount(parent)) return false; if (destinationChild + row < 4 || destinationChild + row >= rowCount(parent)) return false; StoryboardItem *item = static_cast(parent.internalPointer()); item->moveChild(sourceRow, destinationChild + row); } endMoveRows(); return true; } else { return false; } } Qt::DropActions StoryboardModel::supportedDropActions() const { return Qt::CopyAction | Qt::MoveAction; } Qt::DropActions StoryboardModel::supportedDragActions() const { return Qt::CopyAction | Qt::MoveAction; } -int StoryboardModel::commentCount() const +int StoryboardModel::visibleCommentCount() const { - return m_commentList.count(); + int visibleComments = 0; + foreach(Comment comment, m_commentList){ + if (comment.visibility){ + visibleComments++; + } + } + return visibleComments; +} + +int StoryboardModel::visibleCommentsUpto(QModelIndex index) const +{ + int commentRow = index.row() - 4; + int visibleComments = 0; + for (int row = 0; row < commentRow; row++){ + if (m_commentList.at(row).visibility){ + visibleComments++; + } + } + return visibleComments; } void StoryboardModel::setCommentModel(CommentModel *commentModel) { m_commentModel = commentModel; connect(m_commentModel, SIGNAL(dataChanged(const QModelIndex ,const QModelIndex)), this, SLOT(slotCommentDataChanged())); connect(m_commentModel, SIGNAL(rowsRemoved(const QModelIndex ,int, int)), this, SLOT(slotCommentRowRemoved(const QModelIndex ,int, int))); connect(m_commentModel, SIGNAL(rowsInserted(const QModelIndex, int, int)), this, SLOT(slotCommentRowInserted(const QModelIndex, int, int))); connect(m_commentModel, SIGNAL(rowsMoved(const QModelIndex, int, int, const QModelIndex, int)), this, SLOT(slotCommentRowMoved(const QModelIndex, int, int, const QModelIndex, int))); } Comment StoryboardModel::getComment(int row) const { return m_commentList.at(row); } void StoryboardModel::slotCommentDataChanged() { m_commentList = m_commentModel->m_commentList; emit(dataChanged(QModelIndex(), QModelIndex())); } void StoryboardModel::slotCommentRowInserted(const QModelIndex parent, int first, int last) { int numItems = rowCount(); for(int row = 0; row < numItems; row++){ QModelIndex parentIndex = index(row, 0); insertRows(4 + first, last - first + 1, parentIndex); //four indices are already there } slotCommentDataChanged(); } void StoryboardModel::slotCommentRowRemoved(const QModelIndex parent, int first, int last) { int numItems = rowCount(); for(int row = 0; row < numItems; row++){ QModelIndex parentIndex = index(row, 0); removeRows(4 + first, last - first + 1, parentIndex); } slotCommentDataChanged(); } void StoryboardModel::slotCommentRowMoved(const QModelIndex &sourceParent, int start, int end, const QModelIndex &destinationParent, int destinationRow) { int numItems = rowCount(); for(int row = 0; row < numItems; row++){ QModelIndex parentIndex = index(row, 0); moveRows(parentIndex, start + 4, end - start + 1, parentIndex, destinationRow + 4); } slotCommentDataChanged(); } \ No newline at end of file diff --git a/plugins/dockers/storyboarddocker/storyboardModel.h b/plugins/dockers/storyboarddocker/storyboardModel.h index d934ae9380..ec304631b4 100644 --- a/plugins/dockers/storyboarddocker/storyboardModel.h +++ b/plugins/dockers/storyboarddocker/storyboardModel.h @@ -1,80 +1,81 @@ /* * 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" #include "commentModel.h" /* The main storyboard model. */ class StoryboardModel : public QAbstractItemModel { Q_OBJECT public: //if we don't need this constructor change it StoryboardModel(QObject *parent = nullptr); QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex 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; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; //QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; Qt::ItemFlags flags(const QModelIndex &index) const 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()); bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild); //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() const; + int visibleCommentCount() const; + int visibleCommentsUpto(QModelIndex index) const; void setCommentModel(CommentModel *commentModel); Comment getComment(int row) const; private Q_SLOTS: void slotCommentDataChanged(); void slotCommentRowInserted(const QModelIndex, int, int); void slotCommentRowRemoved(const QModelIndex, int, int); void slotCommentRowMoved(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild); private: QVector m_items; int m_commentCount = 0; QVector m_commentList; CommentModel *m_commentModel; }; #endif \ No newline at end of file diff --git a/plugins/dockers/storyboarddocker/storyboardView.cpp b/plugins/dockers/storyboarddocker/storyboardView.cpp index 9572e21ce0..26bd62c883 100644 --- a/plugins/dockers/storyboarddocker/storyboardView.cpp +++ b/plugins/dockers/storyboarddocker/storyboardView.cpp @@ -1,167 +1,168 @@ /* Copyright (c) 2020 Saurabh Kumar This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include "storyboardView.h" #include "storyboardModel.h" /** * This view draws the children of every index in the first column of * the model inside the parent * * */ StoryboardView::StoryboardView(QWidget *parent) :QListView(parent) { setWrapping(true); setFlow(QListView::LeftToRight); setResizeMode(QListView::Adjust); setUniformItemSizes(true); setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); QWidget::setMouseTracking(true); viewport()->setAttribute( Qt::WA_Hover ); //make drag and drop work as expected /* setDragEnabled(true); setAcceptDrops(true); setDropIndicatorShown(true); setDragDropMode(QAbstractItemView::InternalMove); */ } StoryboardView::~StoryboardView() {} void StoryboardView::paintEvent(QPaintEvent *event) { event->accept(); QListView::paintEvent(event); //ask delegate to draw the child nodes too QPainter painter(viewport()); int itemNum = model()->rowCount(); for (int row = 0; row < itemNum; row++){ QModelIndex index = model()->index(row, 0); int childNum = model()->rowCount(index); for (int childRow = 0; childRow < childNum; childRow++){ QModelIndex childIndex = model()->index(childRow, 0, index); QStyleOptionViewItem option; //TO DO: check if the childIndex is in focus //TO DO: set proper options for spinbox type indices if (selectionModel()->isSelected(childIndex)) { option.state |= QStyle::State_Selected; } if (childIndex == selectionModel()->currentIndex()) { option.state |= QStyle::State_HasFocus; } if (childIndex == m_hoverIndex){ option.state |= QStyle::State_MouseOver; } option.rect = visualRect(childIndex); itemDelegate()->paint(&painter, option, childIndex); } } } QRect StoryboardView::visualRect(const QModelIndex &index) const { if (!index.isValid() || !index.parent().isValid()) { return QListView::visualRect(index); } else { QRect parentRect = visualRect(index.parent()); parentRect.setTopLeft(parentRect.topLeft() + QPoint(5, 5)); parentRect.setBottomRight(parentRect.bottomRight() - QPoint(5, 5)); int fontHeight = fontMetrics().height() + 3; int numericFontWidth = fontMetrics().width("0"); int height = parentRect.height(); int width = parentRect.width(); int childRow = index.row(); switch (childRow) { case 0: { //the frame thumbnail rect parentRect.setSize(QSize(width, 120)); parentRect.translate(0, fontHeight); return parentRect; } case 1: { QRect itemNameRect = parentRect; itemNameRect.setSize(QSize(width - (10 * numericFontWidth + 22), fontHeight)); itemNameRect.moveLeft(parentRect.left() + 3*numericFontWidth + 2); return itemNameRect; } case 2: { QRect secondRect = parentRect; secondRect.setSize(QSize(4 * numericFontWidth + 10, fontHeight)); secondRect.moveRight(parentRect.right() - 3*numericFontWidth -10); return secondRect; } case 3: { QRect frameRect = parentRect; frameRect.setSize(QSize(3 * numericFontWidth + 10, fontHeight)); frameRect.moveRight(parentRect.right()); return frameRect; } default: { //comment rect - parentRect.setTop(parentRect.top() + 120 + fontHeight + (index.row() - 4) * 100); + const StoryboardModel* Model = dynamic_cast(model()); + parentRect.setTop(parentRect.top() + 120 + fontHeight + Model->visibleCommentsUpto(index) * 100); parentRect.setHeight(100); return parentRect; } } } return QRect(); } QModelIndex StoryboardView::indexAt(const QPoint &point) const { QModelIndex index = QListView::indexAt(point); if (index.isValid()) { //look for the index in children of the current index int numChild = model()->rowCount(index); for (int row = 0; row < numChild; row++) { QRect childRect = visualRect(model()->index(row, 0, index)); if (childRect.contains(point)) { return model()->index(row, 0, index); } } } return index; } void StoryboardView::mouseMoveEvent(QMouseEvent *event) { event->accept(); QListView::mouseMoveEvent(event); m_hoverIndex = indexAt(event->pos()); }