ctkModelTesterTest1.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Qt includes
  15. #include <QAbstractItemModel>
  16. #include <QApplication>
  17. #include <QList>
  18. #include <QModelIndex>
  19. #include <QTreeWidget>
  20. // CTK includes
  21. #include "ctkModelTester.h"
  22. // STL includes
  23. #include <cstdlib>
  24. #include <iostream>
  25. //-----------------------------------------------------------------------------
  26. class QAbstractItemModelHelper : public QAbstractItemModel
  27. {
  28. public:
  29. virtual QModelIndex index(int, int, const QModelIndex&) const { return QModelIndex(); }
  30. virtual QModelIndex parent(const QModelIndex&) const { return QModelIndex(); }
  31. virtual int rowCount(const QModelIndex&) const { return 0; }
  32. virtual int columnCount(const QModelIndex&) const { return 0; }
  33. virtual QVariant data(const QModelIndex&, int) const { return QVariant(); }
  34. };
  35. //-----------------------------------------------------------------------------
  36. int ctkModelTesterTest1(int argc, char * argv [] )
  37. {
  38. QApplication app(argc, argv);
  39. QAbstractItemModelHelper * item = new QAbstractItemModelHelper;
  40. QObject * object = new QObject;
  41. ctkModelTester ctkTester( item, object );
  42. delete item;
  43. try
  44. {
  45. // as we can infer that QTreeWidget is correct, ctkModelTester shall not fail
  46. // for any of the actions on QTreeWidget.
  47. QTreeWidget treeWidget(0);
  48. ctkModelTester treeModelTester(treeWidget.model());
  49. treeWidget.setColumnCount(1);
  50. QList<QTreeWidgetItem *> items;
  51. for (int i = 0; i < 10; ++i)
  52. {
  53. items.append(new QTreeWidgetItem(
  54. reinterpret_cast<QTreeWidget*>(0), QStringList(QString("item: %1").arg(i))));
  55. }
  56. treeWidget.addTopLevelItems(items);
  57. treeWidget.takeTopLevelItem(0);
  58. treeWidget.takeTopLevelItem(treeWidget.topLevelItemCount() / 2 );
  59. treeWidget.takeTopLevelItem(treeWidget.topLevelItemCount() - 1);
  60. treeWidget.insertTopLevelItem(0, new QTreeWidgetItem(&treeWidget, QStringList("new item 0")));
  61. treeWidget.insertTopLevelItem(treeWidget.topLevelItemCount() / 2, new QTreeWidgetItem(
  62. reinterpret_cast<QTreeWidget*>(0), QStringList("new item 1")));
  63. treeWidget.insertTopLevelItem(treeWidget.topLevelItemCount(), new QTreeWidgetItem(
  64. reinterpret_cast<QTreeWidget*>(0), QStringList("new item 2")));
  65. new QTreeWidgetItem(treeWidget.topLevelItem(0), QStringList("new item 3"));
  66. QAbstractItemModel* model = treeWidget.model();
  67. model->setData(model->index(0,0),QString("foo"));
  68. treeWidget.sortItems(0, Qt::DescendingOrder);
  69. }
  70. catch (const char* error)
  71. {
  72. std::cerr << error << std::endl;
  73. return EXIT_FAILURE;
  74. }
  75. return EXIT_SUCCESS;
  76. }