File indexing completed on 2024-12-15 04:02:45
0001 /* 0002 * SPDX-FileCopyrightText: 2020 Dag Andersen <danders@get2net.dk> 0003 * 0004 * This file is part of the KGantt library. 0005 * 0006 * SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #undef QT_NO_CAST_FROM_ASCII 0010 0011 #include "TestMultiItems.h" 0012 0013 #include "kganttglobal.h" 0014 #include "kganttgraphicsview.h" 0015 #include "kganttgraphicsscene.h" 0016 #include "kganttgraphicsitem.h" 0017 #include "kganttconstraintmodel.h" 0018 #include "kgantttreeviewrowcontroller.h" 0019 #include "kganttlistviewrowcontroller.h" 0020 #include "kganttforwardingproxymodel.h" 0021 #include "kganttitemdelegate.h" 0022 #include "kganttdatetimegrid.h" 0023 #include "kgantttreeviewrowcontroller.h" 0024 0025 #include <QListView> 0026 0027 0028 using namespace KGantt; 0029 0030 void TestMultiItems::init() 0031 { 0032 view = new KGantt::View(); 0033 0034 itemModel = new QStandardItemModel(); 0035 0036 view->setModel(itemModel); 0037 view->setConstraintModel(new KGantt::ConstraintModel()); 0038 QCOMPARE(view->model(), itemModel); 0039 0040 QCOMPARE(itemModel->rowCount(), 0); 0041 } 0042 0043 void TestMultiItems::cleanup() 0044 { 0045 delete view; 0046 delete itemModel; 0047 } 0048 0049 void TestMultiItems::initMultiModel() 0050 { 0051 if (itemModel->columnCount() == 0) { 0052 itemModel->setHorizontalHeaderLabels(QStringList()<< "Title"<<"Type"<<"Start"<<"End"); 0053 } 0054 0055 QList<QStandardItem*> items; 0056 QStandardItem *multi = new QStandardItem("Multi 1"); 0057 items << multi; 0058 QStandardItem *item = new QStandardItem(QString::number((int)KGantt::TypeMulti)); 0059 items << item; 0060 itemModel->appendRow(items); 0061 0062 items.clear(); 0063 item = new QStandardItem("T1"); 0064 items << item; 0065 item = new QStandardItem(QString::number((int)KGantt::TypeTask)); 0066 items << item; 0067 QDateTime now = QDateTime::currentDateTime(); 0068 item = new QStandardItem(now.toString()); 0069 items << item; 0070 item = new QStandardItem(now.addDays(1).toString()); 0071 items << item; 0072 multi->appendRow(items); 0073 0074 items.clear(); 0075 item = new QStandardItem("T2"); 0076 items << item; 0077 item = new QStandardItem(QString::number((int)KGantt::TypeTask)); 0078 items << item; 0079 item = new QStandardItem(now.addDays(1).toString()); 0080 items << item; 0081 item = new QStandardItem(now.addDays(2).toString()); 0082 items << item; 0083 multi->appendRow(items); 0084 0085 items.clear(); 0086 multi = new QStandardItem("Multi 2"); 0087 items << multi; 0088 item = new QStandardItem(QString::number((int)KGantt::TypeMulti)); 0089 items << item; 0090 itemModel->appendRow(items); 0091 0092 items.clear(); 0093 item = new QStandardItem("T1"); 0094 items << item; 0095 item = new QStandardItem(QString::number((int)KGantt::TypeTask)); 0096 items << item; 0097 now = QDateTime::currentDateTime(); 0098 item = new QStandardItem(now.toString()); 0099 items << item; 0100 item = new QStandardItem(now.addDays(1).toString()); 0101 items << item; 0102 multi->appendRow(items); 0103 0104 items.clear(); 0105 item = new QStandardItem("T2"); 0106 items << item; 0107 item = new QStandardItem(QString::number((int)KGantt::TypeTask)); 0108 items << item; 0109 item = new QStandardItem(now.addDays(1).toString()); 0110 items << item; 0111 item = new QStandardItem(now.addDays(2).toString()); 0112 items << item; 0113 multi->appendRow(items); 0114 } 0115 0116 void TestMultiItems::testDefaultView() 0117 { 0118 initMultiModel(); 0119 0120 QCOMPARE(itemModel->rowCount(), 2); 0121 QCOMPARE(view->graphicsView()->scene()->items().count(), 6); 0122 } 0123 0124 void TestMultiItems::testTreeView() 0125 { 0126 QTreeView *treeview = new QTreeView(view); 0127 view->setLeftView(treeview); 0128 view->setModel(itemModel); // must be set again 0129 0130 initMultiModel(); 0131 0132 QCOMPARE(itemModel->rowCount(), 2); 0133 QCOMPARE(view->graphicsView()->scene()->items().count(), 6); 0134 } 0135 0136 void TestMultiItems::testListView() 0137 { 0138 QListView *listview = new QListView(view); 0139 view->setLeftView(listview); 0140 view->setRowController(new KGantt::ListViewRowController(listview, view->ganttProxyModel())); 0141 view->setModel(itemModel); // must be set again 0142 initMultiModel(); 0143 0144 QCOMPARE(itemModel->rowCount(), 2); 0145 QCOMPARE(view->graphicsView()->scene()->items().count(), 6); 0146 } 0147 0148 void TestMultiItems::testConstraints() 0149 { 0150 initMultiModel(); 0151 0152 ConstraintModel *model = view->constraintModel(); 0153 0154 QPersistentModelIndex idx1 = itemModel->index(0, 0); 0155 QPersistentModelIndex idx2 = itemModel->index(1, 0); 0156 0157 model->addConstraint(Constraint(idx1, idx2)); 0158 QCOMPARE(model->constraints().count(), 1); 0159 QVERIFY(model->hasConstraint(Constraint(idx1, idx2))); 0160 0161 QCOMPARE(view->graphicsView()->scene()->items().count(), 7); 0162 0163 QPersistentModelIndex idx11 = itemModel->index(0, 0, idx1); 0164 QPersistentModelIndex idx12 = itemModel->index(0, 0, idx2); 0165 0166 // Hmmm, should this be possible? 0167 model->addConstraint(Constraint(idx11, idx12)); 0168 QCOMPARE(view->graphicsView()->scene()->items().count(), 8); 0169 0170 QVERIFY(itemModel->removeRows(idx1.row(), 1, idx1.parent())); 0171 QCOMPARE(view->graphicsView()->scene()->items().count(), 3); // constraint items also removed 0172 QCOMPARE(model->constraints().count(), 0); // the constraints also removed 0173 0174 itemModel->clear(); 0175 QCOMPARE(view->graphicsView()->scene()->items().count(), 0); 0176 initMultiModel(); 0177 0178 idx1 = itemModel->index(0, 0); 0179 idx2 = itemModel->index(1, 0); 0180 0181 model->addConstraint(Constraint(idx1, idx2)); 0182 QCOMPARE(model->constraints().count(), 1); 0183 QVERIFY(model->hasConstraint(Constraint(idx1, idx2))); 0184 0185 // adding new items shall not affect existing constraints 0186 initMultiModel(); 0187 QCOMPARE(view->graphicsView()->scene()->items().count(), 13); 0188 0189 // removing items wo constraints shall not affect existing constraints 0190 QVERIFY(itemModel->removeRows(2, 1)); 0191 QCOMPARE(view->graphicsView()->scene()->items().count(), 10); 0192 } 0193 0194 QTEST_MAIN(TestMultiItems)