ctkModelTesterTest1.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QAbstractItemModel>
  16. #include <QCoreApplication>
  17. #include <QList>
  18. #include <QModelIndex>
  19. #include <QStandardItem>
  20. #include <QStandardItemModel>
  21. // CTK includes
  22. #include "ctkModelTester.h"
  23. // STL includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. //-----------------------------------------------------------------------------
  27. class QAbstractItemModelHelper : public QAbstractItemModel
  28. {
  29. public:
  30. virtual QModelIndex index(int, int, const QModelIndex&) const { return QModelIndex(); }
  31. virtual QModelIndex parent(const QModelIndex&) const { return QModelIndex(); }
  32. virtual int rowCount(const QModelIndex&) const { return 0; }
  33. virtual int columnCount(const QModelIndex&) const { return 0; }
  34. virtual QVariant data(const QModelIndex&, int) const { return QVariant(); }
  35. };
  36. //-----------------------------------------------------------------------------
  37. int ctkModelTesterTest1(int argc, char * argv [] )
  38. {
  39. QCoreApplication app(argc, argv);
  40. QAbstractItemModelHelper * item = new QAbstractItemModelHelper;
  41. QObject * object = new QObject;
  42. ctkModelTester ctkTester( item, object );
  43. delete item;
  44. try
  45. {
  46. // as we can infer that QStandardItemModel is correct,
  47. // ctkModelTester shall not fail for any of the actions on the model.
  48. // Please note here that takeRow() doesn't delete the items so we end up
  49. // with mem leaks.
  50. QStandardItemModel model;
  51. ctkModelTester treeModelTester(&model);
  52. //------ Test on row--------
  53. QList<QStandardItem*> items;
  54. items << new QStandardItem("col1") << new QStandardItem("col2");
  55. model.appendRow(items);
  56. QList<QStandardItem*> items2 = model.takeRow(0);
  57. if (items2 != items)
  58. {
  59. std::cerr << "Line : " << __LINE__ << "Error" << std::endl;
  60. return EXIT_FAILURE;
  61. }
  62. model.appendRow(items);
  63. for (int i = 0; i < 10; ++i)
  64. {
  65. model.appendRow(QList<QStandardItem*>() << new QStandardItem("col1") << new QStandardItem("col2"));
  66. }
  67. model.takeRow(0);
  68. model.setHeaderData(0, Qt::Vertical, QString("ID"));
  69. model.takeRow(model.rowCount() / 2 );
  70. model.takeRow(model.rowCount() - 1);
  71. items2.clear();
  72. items2 << new QStandardItem("col1") << new QStandardItem("col2");
  73. items2[0]->appendRow(QList<QStandardItem*>() << new QStandardItem("subcol1") << new QStandardItem("subcol2"));
  74. items2[0]->appendRow(QList<QStandardItem*>() << new QStandardItem("subcol1") << new QStandardItem("subcol2"));
  75. model.appendRow(items2);
  76. items2[0]->child(0,0)->setText("foo");
  77. model.sort(0);
  78. //------ Test on Column-----
  79. QStandardItemModel model2;
  80. ctkModelTester treeModelTester2(&model2);
  81. QList<QStandardItem*> itemsCol;
  82. itemsCol << new QStandardItem("row1") << new QStandardItem("row2");
  83. model2.appendColumn(itemsCol);
  84. QList<QStandardItem*> itemsCol2 = model2.takeColumn(0);
  85. if (itemsCol2 != itemsCol)
  86. {
  87. std::cerr << "Line : " << __LINE__ << "Error" << std::endl;
  88. return EXIT_FAILURE;
  89. }
  90. model2.appendColumn(itemsCol);
  91. for (int i = 0; i < 10; ++i)
  92. {
  93. model2.appendColumn(QList<QStandardItem*>() << new QStandardItem("row1") << new QStandardItem("row2"));
  94. }
  95. model2.takeColumn(0);
  96. model2.takeColumn(model2.columnCount() / 2 );
  97. model2.takeColumn(model2.columnCount() - 1);
  98. itemsCol2 << new QStandardItem("row1") << new QStandardItem("row2");
  99. itemsCol2[0]->appendColumn(QList<QStandardItem*>() << new QStandardItem("subrow1") << new QStandardItem("subrow2"));
  100. itemsCol2[0]->appendColumn(QList<QStandardItem*>() << new QStandardItem("subrow1") << new QStandardItem("subrow2"));
  101. model2.setData(model2.index(0,0), QString("foo"));
  102. model2.sort(0);
  103. model2.clear();
  104. //------ Test setDataHeader ----------
  105. for (int i = 0; i < 10; ++i)
  106. {
  107. QList<QStandardItem*> columns;
  108. columns << new QStandardItem("row1") << new QStandardItem("row2");
  109. model2.appendColumn(columns);
  110. model2.setHorizontalHeaderItem(i,new QStandardItem(QString("Column %1").arg(i)));
  111. }
  112. model2.setHeaderData(0, Qt::Horizontal, QString("ID"));
  113. }
  114. catch (const char* error)
  115. {
  116. std::cerr << error << std::endl;
  117. return EXIT_FAILURE;
  118. }
  119. return EXIT_SUCCESS;
  120. }