ctkModelTester.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #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. Q_PROPERTY(bool testDataEnabled READ testDataEnabled WRITE setTestDataEnabled);
  38. Q_PROPERTY(bool throwOnError READ throwOnError WRITE setThrowOnError);
  39. Q_PROPERTY(bool verbose READ verbose WRITE setVerbose);
  40. public:
  41. ///
  42. /// Constructor
  43. /// No model is set by default. To be tested, a model must be set using
  44. /// setModel(...)
  45. explicit ctkModelTester(QObject *parent = 0);
  46. ///
  47. /// Constructor that set the model to test.
  48. /// A new model can later be set using setModel(...)
  49. /// by default, throwOnError is true,
  50. /// nestedInsert is false,
  51. /// testDataEnabled is true,
  52. /// verbose is true.
  53. ctkModelTester(QAbstractItemModel *model, QObject *parent = 0);
  54. ///
  55. /// Destructor
  56. virtual ~ctkModelTester();
  57. ///
  58. /// Set the model to be tested, the model must remain valid during
  59. /// the life ctkModelTester.
  60. void setModel(QAbstractItemModel* model);
  61. QAbstractItemModel* model()const;
  62. ///
  63. /// Throw an exception when an error is found in the model.
  64. /// True by default
  65. void setThrowOnError(bool throwException);
  66. bool throwOnError()const;
  67. ///
  68. /// nestedInserts controls wether the model is allowed to make
  69. /// nested row/column insertions ( an insertion signal is fired when an
  70. /// insertion a previous insertion was not finished). A row insertion
  71. /// consists of 2 signals: rowsAboutToBeInserted and rowsInserted
  72. /// It also applies for row/column suppressions.
  73. void setNestedInserts(bool enable);
  74. bool nestedInserts()const;
  75. ///
  76. /// When TestData is enabled, it checks if the display role of a valid
  77. /// model index is valid too.
  78. /// You can disable the test if you are ok with temporary invalid display
  79. /// roles.
  80. void setTestDataEnabled(bool enable);
  81. bool testDataEnabled()const;
  82. /// When Verbose is enabled, message will be printed to standard or error output.
  83. void setVerbose(bool enable);
  84. bool verbose()const;
  85. ///
  86. /// Test the data consistency of a QModelIndex.
  87. /// Note: Only DisplayRole is checked.
  88. virtual void testData(const QModelIndex& index)const;
  89. ///
  90. /// Run all the tests on the model previously set in setModel(...)
  91. virtual void testModel()const;
  92. ///
  93. /// Run a collection of tests on a QModelIndex
  94. virtual void testModelIndex(const QModelIndex& index)const;
  95. ///
  96. /// Check the hierarchy consistency of a QModelIndex
  97. /// child/parent/siblings relationships
  98. virtual void testParent(const QModelIndex& parent)const;
  99. ///
  100. /// Test a persistent model index
  101. virtual void testPersistentModelIndex(const QPersistentModelIndex& index)const;
  102. protected Q_SLOTS:
  103. void onColumnsAboutToBeInserted(const QModelIndex & parent, int start, int end);
  104. void onColumnsAboutToBeRemoved(const QModelIndex & parent, int start, int end);
  105. void onColumnsInserted(const QModelIndex & parent, int start, int end);
  106. void onColumnsRemoved(const QModelIndex & parent, int start, int end);
  107. void onDataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight);
  108. void onHeaderDataChanged(Qt::Orientation orientation, int first, int last);
  109. void onLayoutAboutToBeChanged();
  110. void onLayoutChanged();
  111. void onModelAboutToBeReset();
  112. void onModelReset();
  113. void onRowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
  114. void onRowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
  115. void onRowsInserted(const QModelIndex & parent, int start, int end);
  116. void onRowsRemoved(const QModelIndex & parent, int start, int end);
  117. protected:
  118. ///
  119. /// The logic of onColumnsAboutToBeInserted and onRowsAboutToBeInserted is
  120. /// gathered in onItemsAboutToBeInserted
  121. virtual void onItemsAboutToBeInserted(const QModelIndex& parent, Qt::Orientation, int start, int end);
  122. ///
  123. /// The logic of onColumnsAboutToBeRemoved and onRowsAboutToBeRemoved is
  124. /// gathered in onItemsAboutToBeRemoved
  125. virtual void onItemsAboutToBeRemoved(const QModelIndex& parent, Qt::Orientation, int start, int end);
  126. ///
  127. /// The logic of onColumnsInserted and onRowsInserted is gathered in
  128. /// onItemsInserted
  129. virtual void onItemsInserted(const QModelIndex& parent, Qt::Orientation, int start, int end);
  130. ///
  131. /// The logic of onColumnsRemoved and onRowsRemoved is gathered in
  132. /// onItemsRemoved
  133. virtual void onItemsRemoved(const QModelIndex& parent, Qt::Orientation, int start, int end);
  134. ///
  135. /// Create a list of persistent index of all the index's children
  136. QList<QPersistentModelIndex> persistentModelIndexes(const QModelIndex& index)const;
  137. ///
  138. /// Utility function that process the result of a test
  139. virtual void test(bool result, const QString& errorString)const;
  140. protected:
  141. QScopedPointer<ctkModelTesterPrivate> d_ptr;
  142. private:
  143. Q_DECLARE_PRIVATE(ctkModelTester);
  144. Q_DISABLE_COPY(ctkModelTester);
  145. };
  146. #endif