File indexing completed on 2024-11-17 04:49:34
0001 /* 0002 * Copyright (c) 2019 Alexander Potashev <aspotashev@gmail.com> 0003 * 0004 * This program is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU General Public License as 0006 * published by the Free Software Foundation; either version 2 of 0007 * the License or (at your option) version 3 or any later version 0008 * accepted by the membership of KDE e.V. (or its successor approved 0009 * by the membership of KDE e.V.), which shall act as a proxy 0010 * defined in Section 14 of version 3 of the license. 0011 * 0012 * This program is distributed in the hope that it will be useful, 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0015 * GNU General Public License for more details. 0016 * 0017 * You should have received a copy of the GNU General Public License 0018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0019 */ 0020 0021 #include "tasksmodelitem.h" 0022 0023 #include "tasksmodel.h" 0024 0025 TasksModelItem *TasksModelItem::takeChild(int index) 0026 { 0027 if (index < 0 || index >= m_children.count()) { 0028 return nullptr; 0029 } 0030 0031 m_model->beginRemoveRows(m_model->index(this, 0), index, index); 0032 0033 TasksModelItem *item = m_children.takeAt(index); 0034 item->m_parent = nullptr; 0035 0036 m_model->endRemoveRows(); 0037 return item; 0038 } 0039 0040 int TasksModelItem::indexOfChild(TasksModelItem *child) const 0041 { 0042 return m_children.indexOf(child); 0043 } 0044 0045 void TasksModelItem::insertChild(int index, TasksModelItem *child) 0046 { 0047 if (index < 0 || index > m_children.count() || child == nullptr) { 0048 return; 0049 } 0050 0051 if (m_model) { 0052 if (m_model->m_rootItem == this) { 0053 child->m_parent = nullptr; 0054 } else { 0055 child->m_parent = this; 0056 } 0057 0058 m_model->beginInsertRows(child->m_parent ? m_model->index(child->m_parent, 0) : QModelIndex(), index, index); 0059 m_children.insert(index, child); 0060 m_model->endInsertRows(); 0061 } else { 0062 child->m_parent = this; 0063 m_children.insert(index, child); 0064 } 0065 } 0066 0067 QVariant TasksModelItem::data(int /*unused*/, int /*unused*/) const 0068 { 0069 return {}; 0070 } 0071 0072 TasksModelItem *TasksModelItem::parent() const 0073 { 0074 return m_parent; 0075 } 0076 0077 TasksModelItem *TasksModelItem::child(int index) const 0078 { 0079 if (index < 0 || index >= m_children.size()) { 0080 return nullptr; 0081 } 0082 return m_children.at(index); 0083 } 0084 0085 int TasksModelItem::childCount() const 0086 { 0087 return m_children.count(); 0088 } 0089 0090 TasksModelItem::TasksModelItem(TasksModel *model, TasksModelItem *parent) 0091 : m_model(model) 0092 , m_parent(parent) 0093 { 0094 } 0095 0096 void TasksModelItem::addChild(TasksModelItem *child) 0097 { 0098 if (child) { 0099 insertChild(m_children.count(), child); 0100 } 0101 } 0102 0103 void TasksModelItem::disconnectFromParent() 0104 { 0105 TasksModelItem *parent = m_parent ? m_parent : m_model->m_rootItem; 0106 0107 int i = parent->m_children.indexOf(this); 0108 if (i >= 0) { 0109 m_model->beginRemoveRows(m_model->index(parent, 0), i, i); 0110 0111 // users _could_ do changes when connected to rowsAboutToBeRemoved, 0112 // so we check again to make sure 'i' is valid 0113 if (!parent->m_children.isEmpty() && parent->m_children.at(i) == this) { 0114 parent->m_children.takeAt(i); 0115 } 0116 0117 m_model->endRemoveRows(); 0118 } 0119 } 0120 0121 void TasksModelItem::invalidateRunningState() 0122 { 0123 // invalidate icon in column "Session Time" 0124 QModelIndex index = m_model->index(this, 1); 0125 Q_EMIT m_model->dataChanged(index, index, QVector<int>{Qt::DecorationRole}); 0126 } 0127 0128 void TasksModelItem::invalidateCompletedState() 0129 { 0130 // invalidate icon in column "Task Name" 0131 QModelIndex index = m_model->index(this, 0); 0132 Q_EMIT m_model->dataChanged(index, index, QVector<int>{Qt::DecorationRole}); 0133 }