File indexing completed on 2024-04-21 03:56:04

0001 /*
0002     SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "modelcommander.h"
0008 #include <QDebug>
0009 
0010 ModelCommander::ModelCommander(DynamicTreeModel *model, QObject *parent)
0011     : QObject(parent)
0012     , m_model(model)
0013 {
0014 }
0015 
0016 QStringList ModelCommander::execute_testInsertWhenEmpty(const QString &dataTag)
0017 {
0018     ModelInsertCommand *ins = new ModelInsertCommand(m_model, this);
0019     ins->setStartRow(0);
0020 
0021     static const QStringList testData = QStringList() << QStringLiteral("insert01") << QStringLiteral("insert02") << QStringLiteral("insert03");
0022 
0023     if (dataTag.isEmpty()) {
0024         return testData;
0025     }
0026 
0027     if (dataTag == testData.at(0)) {
0028         // Insert a single item at the top.
0029         ins->setEndRow(0);
0030     } else if (dataTag == testData.at(1)) {
0031         // Insert 10 items.
0032         ins->setEndRow(9);
0033     } else if (dataTag == testData.at(2)) {
0034         // Insert 5 items, some of which are parents
0035         ins->interpret(
0036             QLatin1String(" - 1"
0037                           " - - 2"
0038                           " - 3"
0039                           " - 4"
0040                           " - - 5"
0041                           " - - - 6"
0042                           " - 7"
0043                           " - 8"
0044                           " - - 9"));
0045     } else {
0046         qDebug() << dataTag;
0047         return testData;
0048     }
0049     execute(ins);
0050     return testData;
0051 }
0052 
0053 void ModelCommander::init_testInsertWhenEmpty(const QString &dataTag)
0054 {
0055     Q_UNUSED(dataTag);
0056 }
0057 
0058 void ModelCommander::init_testInsertInRoot(const QString &dataTag)
0059 {
0060     initTestModel(dataTag);
0061 }
0062 
0063 void ModelCommander::init_testInsertInTopLevel(const QString &dataTag)
0064 {
0065     initTestModel(dataTag);
0066 }
0067 
0068 void ModelCommander::init_testInsertInSecondLevel(const QString &dataTag)
0069 {
0070     initTestModel(dataTag);
0071 }
0072 
0073 void ModelCommander::init_testRemoveFromRoot(const QString &dataTag)
0074 {
0075     initTestModel(dataTag);
0076 }
0077 
0078 void ModelCommander::init_testRemoveFromSecondLevel(const QString &dataTag)
0079 {
0080     initTestModel(dataTag);
0081 }
0082 
0083 void ModelCommander::init_testRemoveFromTopLevel(const QString &dataTag)
0084 {
0085     initTestModel(dataTag);
0086 }
0087 
0088 void ModelCommander::init_testMoveFromRoot(const QString &dataTag)
0089 {
0090     initTestModel(dataTag);
0091 }
0092 
0093 void ModelCommander::init_testMoveFromSecondLevel(const QString &dataTag)
0094 {
0095     initTestModel(dataTag);
0096 }
0097 
0098 void ModelCommander::init_testMoveFromTopLevel(const QString &dataTag)
0099 {
0100     initTestModel(dataTag);
0101 }
0102 
0103 void ModelCommander::init_testModifyInRoot(const QString &dataTag)
0104 {
0105     initTestModel(dataTag);
0106 }
0107 
0108 void ModelCommander::init_testModifyInTopLevel(const QString &dataTag)
0109 {
0110     initTestModel(dataTag);
0111 }
0112 
0113 void ModelCommander::init_testModifyInSecondLevel(const QString &dataTag)
0114 {
0115     initTestModel(dataTag);
0116 }
0117 
0118 void ModelCommander::initTestModel(const QString &dataTag)
0119 {
0120     Q_UNUSED(dataTag);
0121 
0122     // A standard initial model for all these tests.
0123     ModelInsertCommand *ins = new ModelInsertCommand(m_model, this);
0124     ins->setStartRow(0);
0125     ins->interpret(
0126         QLatin1String(" - 1"
0127                       " - 2"
0128                       " - - 3"
0129                       " - - 4"
0130                       " - - 5"
0131                       " - 6"
0132                       " - 7"
0133                       " - 8"
0134                       " - 9"
0135                       " - - 10"
0136                       " - - 11"
0137                       " - - - 12"
0138                       " - - - 13"
0139                       " - - 14"
0140                       " - - 15"
0141                       " - - 16"
0142                       " - - 17"
0143                       " - - - 18"
0144                       " - - - 19"
0145                       " - - - - 20"
0146                       " - - - - 21"
0147                       " - - - 22"
0148                       " - - - 23"
0149                       " - - - 24"
0150                       " - - - 25"
0151                       " - - - - 26"
0152                       " - - - - 27"
0153                       " - - - 28"
0154                       " - - - 29"
0155                       " - - - - 30"
0156                       " - - - 31"
0157                       " - - - 32"
0158                       " - - 33"
0159                       " - - 34"
0160                       " - - - 35"
0161                       " - - 36"
0162                       " - - 37"
0163                       " - 38"
0164                       " - 39"
0165                       " - - 40"
0166                       " - - 41"
0167                       " - 42"
0168                       " - 43"));
0169     execute(ins);
0170 }
0171 
0172 QStringList ModelCommander::execute_testInsertInRoot(const QString &dataTag)
0173 {
0174     return executeTestInsert(QList<int>(), dataTag);
0175 }
0176 
0177 QStringList ModelCommander::execute_testInsertInTopLevel(const QString &dataTag)
0178 {
0179     return executeTestInsert(QList<int>() << 5, dataTag);
0180 }
0181 
0182 QStringList ModelCommander::execute_testInsertInSecondLevel(const QString &dataTag)
0183 {
0184     return executeTestInsert(QList<int>() << 5 << 5, dataTag);
0185 }
0186 
0187 QStringList ModelCommander::execute_testRemoveFromRoot(const QString &dataTag)
0188 {
0189     return executeTestRemove(QList<int>(), dataTag);
0190 }
0191 
0192 QStringList ModelCommander::execute_testRemoveFromTopLevel(const QString &dataTag)
0193 {
0194     return executeTestRemove(QList<int>() << 5, dataTag);
0195 }
0196 
0197 QStringList ModelCommander::execute_testRemoveFromSecondLevel(const QString &dataTag)
0198 {
0199     return executeTestRemove(QList<int>() << 5 << 5, dataTag);
0200 }
0201 
0202 QStringList ModelCommander::execute_testMoveFromRoot(const QString &dataTag)
0203 {
0204     return executeTestMove(QList<int>(), dataTag);
0205 }
0206 
0207 QStringList ModelCommander::execute_testMoveFromTopLevel(const QString &dataTag)
0208 {
0209     return executeTestMove(QList<int>() << 5, dataTag);
0210 }
0211 
0212 QStringList ModelCommander::execute_testMoveFromSecondLevel(const QString &dataTag)
0213 {
0214     return executeTestMove(QList<int>() << 5 << 5, dataTag);
0215 }
0216 
0217 QStringList ModelCommander::execute_testModifyInRoot(const QString &dataTag)
0218 {
0219     return executeTestModify(QList<int>(), dataTag);
0220 }
0221 
0222 QStringList ModelCommander::execute_testModifyInTopLevel(const QString &dataTag)
0223 {
0224     return executeTestModify(QList<int>() << 5, dataTag);
0225 }
0226 
0227 QStringList ModelCommander::execute_testModifyInSecondLevel(const QString &dataTag)
0228 {
0229     return executeTestModify(QList<int>() << 5 << 5, dataTag);
0230 }
0231 
0232 void ModelCommander::execute(ModelChangeCommand *command)
0233 {
0234     m_currentCommand = command;
0235     command->doCommand();
0236     delete command;
0237     command = nullptr;
0238 }
0239 
0240 ModelChangeCommand *ModelCommander::currentCommand()
0241 {
0242     return m_currentCommand;
0243 }
0244 
0245 QStringList ModelCommander::executeTestRemove(QList<int> rowAncestors, const QString &dataTag)
0246 {
0247     static const QStringList testData = QStringList() << QStringLiteral("remove01") << QStringLiteral("remove02") << QStringLiteral("remove03");
0248 
0249     if (dataTag.isEmpty()) {
0250         return testData;
0251     }
0252 
0253     ModelRemoveCommand *rem = new ModelRemoveCommand(m_model, this);
0254     if (dataTag == testData.at(0)) {
0255         // Remove a single item from the top.
0256         rem->setAncestorRowNumbers(rowAncestors);
0257         rem->setStartRow(0);
0258         rem->setEndRow(0);
0259     } else if (dataTag == testData.at(1)) {
0260         // Remove four items form the top.
0261         rem->setAncestorRowNumbers(rowAncestors);
0262         rem->setStartRow(0);
0263         rem->setEndRow(4);
0264     } else if (dataTag == testData.at(2)) {
0265         // Remove a single item from the bottom.
0266         rem->setAncestorRowNumbers(rowAncestors);
0267         rem->setStartRow(m_model->rowCount() - 1);
0268         rem->setEndRow(m_model->rowCount() - 1);
0269     }
0270     execute(rem);
0271     return testData;
0272 }
0273 
0274 QStringList ModelCommander::executeTestMove(QList<int> rowAncestors, const QString &dataTag)
0275 {
0276     static const QStringList testData = QStringList() << QStringLiteral("move01") << QStringLiteral("move02") << QStringLiteral("move03")
0277                                                       << QStringLiteral("move04") << QStringLiteral("move05");
0278 
0279     if (dataTag.isEmpty()) {
0280         return testData;
0281     }
0282 
0283     ModelMoveCommand *move = new ModelMoveCommand(m_model, this);
0284     if (dataTag == testData.at(0)) {
0285         // Move a single item from the top to the middle in the same parent.
0286         move->setAncestorRowNumbers(rowAncestors);
0287         move->setStartRow(0);
0288         move->setEndRow(0);
0289         move->setDestAncestors(rowAncestors);
0290         move->setDestRow(5);
0291     } else if (dataTag == testData.at(1)) {
0292         // Move a single item from the middle to the top in the same parent.
0293         move->setAncestorRowNumbers(rowAncestors);
0294         move->setStartRow(4);
0295         move->setEndRow(4);
0296         move->setDestAncestors(rowAncestors);
0297         move->setDestRow(0);
0298     } else if (dataTag == testData.at(2)) {
0299         // Move a single item from the middle to the bottom in the same parent.
0300         move->setAncestorRowNumbers(rowAncestors);
0301         move->setStartRow(4);
0302         move->setEndRow(4);
0303         move->setDestAncestors(rowAncestors);
0304         move->setDestRow(10);
0305     } else if (dataTag == testData.at(3)) {
0306         // Move a single item from the bottom to the middle in the same parent.
0307         move->setAncestorRowNumbers(rowAncestors);
0308         move->setStartRow(9);
0309         move->setEndRow(9);
0310         move->setDestAncestors(rowAncestors);
0311         move->setDestRow(4);
0312     } else if (dataTag == testData.at(4)) {
0313         // Move a single item from the bottom to the top in the same parent.
0314         move->setAncestorRowNumbers(rowAncestors);
0315         move->setStartRow(9);
0316         move->setEndRow(9);
0317         move->setDestAncestors(rowAncestors);
0318         move->setDestRow(0);
0319     }
0320     execute(move);
0321     return testData;
0322 }
0323 
0324 QStringList ModelCommander::executeTestModify(QList<int> rowAncestors, const QString &dataTag)
0325 {
0326     static const QStringList testData = QStringList() << QStringLiteral("modify01") << QStringLiteral("modify02") << QStringLiteral("modify03")
0327                                                       << QStringLiteral("modify04") << QStringLiteral("modify05") << QStringLiteral("modify06")
0328                                                       << QStringLiteral("modify07");
0329 
0330     if (dataTag.isEmpty()) {
0331         return testData;
0332     }
0333 
0334     ModelDataChangeCommand *modify = new ModelDataChangeCommand(m_model, this);
0335     if (dataTag == testData.at(0)) {
0336         // Modify a single item at the top.
0337         modify->setAncestorRowNumbers(rowAncestors);
0338         modify->setStartRow(0);
0339         modify->setEndRow(0);
0340     } else if (dataTag == testData.at(1)) {
0341         // Modify four items at the top.
0342         modify->setAncestorRowNumbers(rowAncestors);
0343         modify->setStartRow(0);
0344         modify->setEndRow(4);
0345     } else if (dataTag == testData.at(2)) {
0346         // Modify a single item at the bottom.
0347         modify->setAncestorRowNumbers(rowAncestors);
0348         modify->setStartRow(m_model->rowCount() - 1);
0349         modify->setEndRow(m_model->rowCount() - 1);
0350     } else if (dataTag == testData.at(3)) {
0351         // Modify four items at the bottom.
0352         modify->setAncestorRowNumbers(rowAncestors);
0353         modify->setStartRow(m_model->rowCount() - 4);
0354         modify->setEndRow(m_model->rowCount() - 1);
0355     } else if (dataTag == testData.at(4)) {
0356         // Modify a single item in the middle.
0357         modify->setAncestorRowNumbers(rowAncestors);
0358         modify->setStartRow(4);
0359         modify->setEndRow(4);
0360     } else if (dataTag == testData.at(5)) {
0361         // Modify four items in the middle.
0362         modify->setAncestorRowNumbers(rowAncestors);
0363         modify->setStartRow(3);
0364         modify->setEndRow(7);
0365     } else if (dataTag == testData.at(6)) {
0366         // Modify all items.
0367         modify->setAncestorRowNumbers(rowAncestors);
0368         modify->setStartRow(0);
0369         modify->setEndRow(m_model->rowCount() - 1);
0370     }
0371     execute(modify);
0372     return testData;
0373 }
0374 
0375 QStringList ModelCommander::executeTestInsert(QList<int> rowAncestors, const QString &dataTag)
0376 {
0377     static const QStringList testData = QStringList() << QStringLiteral("insert01") << QStringLiteral("insert02") << QStringLiteral("insert03")
0378                                                       << QStringLiteral("insert04") << QStringLiteral("insert05") << QStringLiteral("insert06")
0379                                                       << QStringLiteral("insert07") << QStringLiteral("insert08") << QStringLiteral("insert09")
0380                                                       << QStringLiteral("insert10") << QStringLiteral("insert11") << QStringLiteral("insert12")
0381                                                       << QStringLiteral("insert13") << QStringLiteral("insert14") << QStringLiteral("insert15")
0382                                                       << QStringLiteral("insert16") << QStringLiteral("insert17") << QStringLiteral("insert18");
0383 
0384     if (dataTag.isEmpty()) {
0385         return testData;
0386     }
0387 
0388     ModelInsertCommand *ins = new ModelInsertCommand(m_model, this);
0389     if (dataTag == testData.at(0)) {
0390         // Insert a single item at the top.
0391         ins->setAncestorRowNumbers(rowAncestors);
0392         ins->setStartRow(0);
0393         ins->setEndRow(0);
0394     } else if (dataTag == testData.at(1)) {
0395         // Insert 10 items at the top.
0396         ins->setAncestorRowNumbers(rowAncestors);
0397         ins->setStartRow(0);
0398         ins->setEndRow(9);
0399     } else if (dataTag == testData.at(2)) {
0400         // Insert a single item at the bottom.
0401         ins->setAncestorRowNumbers(rowAncestors);
0402         ins->setStartRow(m_model->rowCount());
0403         ins->setEndRow(m_model->rowCount());
0404     } else if (dataTag == testData.at(3)) {
0405         // Insert a 10 items at the bottom.
0406         ins->setAncestorRowNumbers(rowAncestors);
0407         ins->setStartRow(m_model->rowCount());
0408         ins->setEndRow(m_model->rowCount() + 9);
0409     } else if (dataTag == testData.at(4)) {
0410         // Insert a single item in the middle
0411         ins->setAncestorRowNumbers(rowAncestors);
0412         ins->setStartRow(4);
0413         ins->setEndRow(4);
0414     } else if (dataTag == testData.at(5)) {
0415         // Insert 10 items in the middle
0416         ins->setAncestorRowNumbers(rowAncestors);
0417         ins->setStartRow(4);
0418         ins->setEndRow(13);
0419     } else if (dataTag == testData.at(6)) {
0420         // Insert a single item at with children at the top
0421         ins->setAncestorRowNumbers(rowAncestors);
0422         ins->setStartRow(0);
0423         ins->interpret(
0424             QLatin1String(" - 1"
0425                           " - - 2"
0426                           " - - - 3"
0427                           " - - - - 4"
0428                           " - - 5"));
0429     } else if (dataTag == testData.at(7)) {
0430         // Insert a single item at with children at the bottom
0431         ins->setAncestorRowNumbers(rowAncestors);
0432         ins->setStartRow(m_model->rowCount());
0433         ins->interpret(
0434             QLatin1String(" - 1"
0435                           " - - 2"
0436                           " - - - 3"
0437                           " - - - - 4"
0438                           " - - 5"));
0439     } else if (dataTag == testData.at(8)) {
0440         // Insert a single item at with children in the middle
0441         ins->setAncestorRowNumbers(rowAncestors);
0442         ins->setStartRow(4);
0443         ins->interpret(
0444             QLatin1String(" - 1"
0445                           " - - 2"
0446                           " - - - 3"
0447                           " - - - - 4"
0448                           " - - 5"));
0449     } else if (dataTag == testData.at(9)) {
0450         // Insert 5 items, some of which are parents at the top
0451         ins->setAncestorRowNumbers(rowAncestors);
0452         ins->setStartRow(0);
0453         ins->interpret(
0454             QLatin1String(" - 1"
0455                           " - - 2"
0456                           " - 3"
0457                           " - 4"
0458                           " - - 5"
0459                           " - - 6"
0460                           " - - - 7"
0461                           " - 8"
0462                           " - 9"
0463                           " - - 10"));
0464     } else if (dataTag == testData.at(10)) {
0465         // Insert 5 items, some of which are parents at the bottom
0466         ins->setAncestorRowNumbers(rowAncestors);
0467         ins->setStartRow(m_model->rowCount());
0468         ins->interpret(
0469             QLatin1String(" - 1"
0470                           " - - 2"
0471                           " - 3"
0472                           " - 4"
0473                           " - - 5"
0474                           " - - 6"
0475                           " - - - 7"
0476                           " - 8"
0477                           " - 9"
0478                           " - - 10"));
0479     } else if (dataTag == testData.at(11)) {
0480         // Insert 5 items, some of which are parents in the middle
0481         ins->setAncestorRowNumbers(rowAncestors);
0482         ins->setStartRow(4);
0483         ins->interpret(
0484             QLatin1String(" - 1"
0485                           " - - 2"
0486                           " - 3"
0487                           " - 4"
0488                           " - - 5"
0489                           " - - 6"
0490                           " - - - 7"
0491                           " - 8"
0492                           " - 9"
0493                           " - - 10"));
0494     } else if (dataTag == testData.at(12)) {
0495         // Insert 5 items, some of which are parents in the middle
0496         ins->setAncestorRowNumbers(rowAncestors << 0);
0497         ins->setStartRow(0);
0498         ins->setEndRow(0);
0499     } else if (dataTag == testData.at(13)) {
0500         // Insert 5 items, some of which are parents in the middle
0501         ins->setAncestorRowNumbers(rowAncestors << 0);
0502         ins->setStartRow(0);
0503         ins->setEndRow(9);
0504     } else if (dataTag == testData.at(14)) {
0505         // Insert 5 items, some of which are parents in the middle
0506         ins->setAncestorRowNumbers(rowAncestors << 0);
0507         ins->setStartRow(0);
0508         ins->interpret(
0509             QLatin1String(" - 1"
0510                           " - - 2"
0511                           " - 3"
0512                           " - 4"
0513                           " - - 5"
0514                           " - - 6"
0515                           " - - - 7"
0516                           " - 8"
0517                           " - 9"
0518                           " - - 10"));
0519     } else if (dataTag == testData.at(15)) {
0520         // Insert 5 items, some of which are parents in the middle
0521         ins->setAncestorRowNumbers(rowAncestors << 9);
0522         ins->setStartRow(0);
0523         ins->setEndRow(0);
0524     } else if (dataTag == testData.at(16)) {
0525         // Insert 5 items, some of which are parents in the middle
0526         ins->setAncestorRowNumbers(rowAncestors << 9);
0527         ins->setStartRow(0);
0528         ins->setEndRow(9);
0529     } else if (dataTag == testData.at(17)) {
0530         // Insert 5 items, some of which are parents in the middle
0531         ins->setAncestorRowNumbers(rowAncestors << 9);
0532         ins->setStartRow(0);
0533         ins->interpret(
0534             QLatin1String(" - 1"
0535                           " - - 2"
0536                           " - 3"
0537                           " - 4"
0538                           " - - 5"
0539                           " - - 6"
0540                           " - - - 7"
0541                           " - 8"
0542                           " - 9"
0543                           " - - 10"));
0544     } else {
0545         qDebug() << dataTag;
0546         delete ins;
0547         return testData;
0548     }
0549     execute(ins);
0550     return testData;
0551 }
0552 
0553 #include "moc_modelcommander.cpp"