1 /*
2 SPDX-License-Identifier: GPL-2.0-or-later
3 SPDX-FileCopyrightText: 2016-2020 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
4 */
5
6 #include "diagramsmodel.h"
7
8 // app includes
9 #include "umlscene.h"
10 #include "umlview.h"
11 #include "uml.h"
12 #include "umldoc.h"
13 #include "folder.h"
14
15 // kde includes
16 #include <KLocalizedString>
17
18 // qt includes
19 #include <QtDebug>
20
DiagramsModel()21 DiagramsModel::DiagramsModel()
22 : m_count(0)
23 {
24 }
25
rowCount(const QModelIndex & parent) const26 int DiagramsModel::rowCount(const QModelIndex &parent) const
27 {
28 Q_UNUSED(parent);
29
30 int count = m_views.size();
31 return count;
32 }
33
columnCount(const QModelIndex & parent) const34 int DiagramsModel::columnCount(const QModelIndex &parent) const
35 {
36 Q_UNUSED(parent);
37
38 return 4;
39 }
40
headerData(int section,Qt::Orientation orientation,int role) const41 QVariant DiagramsModel::headerData(int section, Qt::Orientation orientation, int role) const
42 {
43 if (section < 0)
44 return QVariant();
45
46 if (role != Qt::DisplayRole)
47 return QVariant();
48
49 if (orientation == Qt::Vertical)
50 return section + 1;
51 if (section == 0)
52 return QVariant(i18n("Name"));
53 else if (section == 1)
54 return QVariant(i18n("Type"));
55 else if (section == 2)
56 return QVariant(i18n("Folder"));
57 else if (section == 3)
58 return QVariant(i18n("Widgets/Associations"));
59 else return QVariant();
60 }
61
data(const QModelIndex & index,int role) const62 QVariant DiagramsModel::data(const QModelIndex & index, int role) const
63 {
64 if (role == Qt::UserRole && index.column() == 0) {
65 QVariant v;
66 v.setValue(m_views.at(index.row()).data());
67 return v;
68 }
69 else if (role == Qt::DecorationRole && index.column() == 0) {
70 UMLView *v = m_views.at(index.row());
71 return QVariant(Icon_Utils::smallIcon(v->umlScene()->type()));
72 }
73 else if (role != Qt::DisplayRole)
74 return QVariant();
75
76 int cCount = columnCount(index);
77 if (index.column() >= cCount)
78 return QVariant();
79
80 UMLView *v = m_views.at(index.row());
81 if (index.column() == 0)
82 return v->umlScene()->name();
83 else if (index.column() == 1)
84 return Uml::DiagramType::toStringI18n(v->umlScene()->type());
85 else if (index.column() == 2)
86 return v->umlScene()->folder()->name();
87 else
88 return QVariant(QString::number(v->umlScene()->widgetList().size())
89 + QLatin1String("/")
90 + QString::number(v->umlScene()->associationList().size()));
91 }
92
addDiagram(UMLView * view)93 bool DiagramsModel::addDiagram(UMLView *view)
94 {
95 if (m_views.contains(view))
96 return false;
97 int index = m_views.size();
98 beginInsertRows(QModelIndex(), index, index);
99 m_views.append(view);
100 endInsertRows();
101 return true;
102 }
103
removeDiagram(UMLView * view)104 bool DiagramsModel::removeDiagram(UMLView *view)
105 {
106 if (!m_views.contains(view))
107 return false;
108 int index = m_views.indexOf(view);
109 beginRemoveRows(QModelIndex(), index, index);
110 m_views.removeAll(view);
111 endRemoveRows();
112 return true;
113 }
114
removeAllDiagrams()115 bool DiagramsModel::removeAllDiagrams()
116 {
117 if (!m_views.count())
118 return false;
119 beginRemoveRows(QModelIndex(), 0, m_views.count() - 1);
120 m_views.clear();
121 endRemoveRows();
122 return true;
123 }
124
emitDataChanged(const QModelIndex & index)125 void DiagramsModel::emitDataChanged(const QModelIndex &index)
126 {
127 emit dataChanged(index, index);
128 }
129
emitDataChanged(int index)130 void DiagramsModel::emitDataChanged(int index)
131 {
132 QModelIndex mi = createIndex(index,0);
133 emit dataChanged(mi, mi);
134 }
135
emitDataChanged(UMLView * view)136 void DiagramsModel::emitDataChanged(UMLView *view)
137 {
138 #if QT_VERSION < 0x050000
139 emit layoutAboutToBeChanged();
140 #endif
141 int index = m_views.indexOf(view);
142 emitDataChanged(index);
143 #if QT_VERSION < 0x050000
144 emit layoutChanged();
145 #endif
146 }
147