ctkModelTester.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.commontk.org/LICENSE
  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. #ifndef __ctkModelTester_h
  15. #define __ctkModelTester_h
  16. /// Qt includes
  17. #include <QObject>
  18. #include <QModelIndex>
  19. #include <QPersistentModelIndex>
  20. #include <QList>
  21. /// CTK includes
  22. #include "ctkPimpl.h"
  23. #include "ctkCoreExport.h"
  24. class QAbstractItemModel;
  25. class ctkModelTesterPrivate;
  26. /// \ingroup Core
  27. /// ctkModelTester is a tool that tests any QAbstractItemModel
  28. /// Most of the signals fired by the model set (ctkModelTester::setModel())
  29. /// are connected to the tester that check their consistency with the
  30. /// model contents.
  31. /// ctkModelTester is typically used when developing a new QAbstractItemModel
  32. /// or during unit tests.
  33. class CTK_CORE_EXPORT ctkModelTester: public QObject
  34. {
  35. Q_OBJECT
  36. Q_PROPERTY(bool nestedInserts READ nestedInserts WRITE setNestedInserts);
  37. public:
  38. ///
  39. /// Constructor
  40. /// No model is set by default. To be tested, a model must be set using
  41. /// setModel(...)
  42. explicit ctkModelTester(QObject *parent = 0);
  43. ///
  44. /// Constructor that set the model to test.
  45. /// A new model can later be set using setModel(...)
  46. ctkModelTester(QAbstractItemModel *model, QObject *parent = 0);
  47. ///
  48. /// Destructor
  49. virtual ~ctkModelTester();
  50. ///
  51. /// Set the model to be tested, the model must remain valid during
  52. /// the life ctkModelTester.
  53. void setModel(QAbstractItemModel* model);
  54. QAbstractItemModel* model()const;
  55. ///
  56. /// Throw an exception when an error is found in the model.
  57. /// True by default
  58. void setThrowOnError(bool throwException);
  59. bool throwOnError()const;
  60. ///
  61. /// nestedInserts controls wether the model is allowed to make
  62. /// nested row/column insertions ( an insertion signal is fired when an
  63. /// insertion a previous insertion was not finished). A row insertion
  64. /// consists of 2 signals: rowsAboutToBeInserted and rowsInserted
  65. /// It also applies for row/column suppressions.
  66. void setNestedInserts(bool enable);
  67. bool nestedInserts()const;
  68. ///
  69. /// When TestData is enabled, it checks if the display role of a valid
  70. /// model index is valid too.
  71. /// You can disable the test if you are ok with temporary invalid display
  72. /// roles.
  73. void setTestDataEnabled(bool enable);
  74. bool testDataEnabled()const;
  75. ///
  76. /// Test the data consistency of a QModelIndex.
  77. /// Note: Only DisplayRole is checked.
  78. virtual void testData(const QModelIndex& index)const;
  79. ///
  80. /// Run all the tests on the model previously set in setModel(...)
  81. virtual void testModel()const;
  82. ///
  83. /// Run a collection of tests on a QModelIndex
  84. virtual void testModelIndex(const QModelIndex& index)const;
  85. ///
  86. /// Check the hierarchy consistency of a QModelIndex
  87. /// child/parent/siblings relationships
  88. virtual void testParent(const QModelIndex& parent)const;
  89. ///
  90. /// Test a persistent model index
  91. virtual void testPersistentModelIndex(const QPersistentModelIndex& index)const;
  92. protected slots:
  93. void onColumnsAboutToBeInserted(const QModelIndex & parent, int start, int end);
  94. void onColumnsAboutToBeRemoved(const QModelIndex & parent, int start, int end);
  95. void onColumnsInserted(const QModelIndex & parent, int start, int end);
  96. void onColumnsRemoved(const QModelIndex & parent, int start, int end);
  97. void onDataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight);
  98. void onHeaderDataChanged(Qt::Orientation orientation, int first, int last);
  99. void onLayoutAboutToBeChanged();
  100. void onLayoutChanged();
  101. void onModelAboutToBeReset();
  102. void onModelReset();
  103. void onRowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
  104. void onRowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
  105. void onRowsInserted(const QModelIndex & parent, int start, int end);
  106. void onRowsRemoved(const QModelIndex & parent, int start, int end);
  107. protected:
  108. ///
  109. /// The logic of onColumnsAboutToBeInserted and onRowsAboutToBeInserted is
  110. /// gathered in onItemsAboutToBeInserted
  111. virtual void onItemsAboutToBeInserted(const QModelIndex& parent, Qt::Orientation, int start, int end);
  112. ///
  113. /// The logic of onColumnsAboutToBeRemoved and onRowsAboutToBeRemoved is
  114. /// gathered in onItemsAboutToBeRemoved
  115. virtual void onItemsAboutToBeRemoved(const QModelIndex& parent, Qt::Orientation, int start, int end);
  116. ///
  117. /// The logic of onColumnsInserted and onRowsInserted is gathered in
  118. /// onItemsInserted
  119. virtual void onItemsInserted(const QModelIndex& parent, Qt::Orientation, int start, int end);
  120. ///
  121. /// The logic of onColumnsRemoved and onRowsRemoved is gathered in
  122. /// onItemsRemoved
  123. virtual void onItemsRemoved(const QModelIndex& parent, Qt::Orientation, int start, int end);
  124. ///
  125. /// Create a list of persistent index of all the index's children
  126. QList<QPersistentModelIndex> persistentModelIndexes(const QModelIndex& index)const;
  127. ///
  128. /// Utility function that process the result of a test
  129. virtual void test(bool result, const QString& errorString)const;
  130. protected:
  131. QScopedPointer<ctkModelTesterPrivate> d_ptr;
  132. private:
  133. Q_DECLARE_PRIVATE(ctkModelTester);
  134. Q_DISABLE_COPY(ctkModelTester);
  135. };
  136. #endif