QCMakeCacheView.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef QCMakeCacheView_h
  4. #define QCMakeCacheView_h
  5. #include "QCMake.h"
  6. #include <QItemDelegate>
  7. #include <QSet>
  8. #include <QStandardItemModel>
  9. #include <QTreeView>
  10. class QSortFilterProxyModel;
  11. class QCMakeCacheModel;
  12. class QCMakeAdvancedFilter;
  13. /// Qt view class for cache properties
  14. class QCMakeCacheView : public QTreeView
  15. {
  16. Q_OBJECT
  17. public:
  18. QCMakeCacheView(QWidget* p);
  19. // retrieve the QCMakeCacheModel storing all the pointers
  20. // this isn't necessarily the model one would get from model()
  21. QCMakeCacheModel* cacheModel() const;
  22. // get whether to show advanced entries
  23. bool showAdvanced() const;
  24. QSize sizeHint() const { return QSize(200, 200); }
  25. public slots:
  26. // set whether to show advanced entries
  27. void setShowAdvanced(bool);
  28. // set the search filter string. any property key or value not matching will
  29. // be filtered out
  30. void setSearchFilter(const QString&);
  31. protected:
  32. QModelIndex moveCursor(CursorAction, Qt::KeyboardModifiers);
  33. bool event(QEvent* e);
  34. QCMakeCacheModel* CacheModel;
  35. QCMakeAdvancedFilter* AdvancedFilter;
  36. QSortFilterProxyModel* SearchFilter;
  37. };
  38. /// Qt model class for cache properties
  39. class QCMakeCacheModel : public QStandardItemModel
  40. {
  41. Q_OBJECT
  42. public:
  43. QCMakeCacheModel(QObject* parent);
  44. ~QCMakeCacheModel();
  45. // roles used to retrieve extra data such has help strings, types of
  46. // properties, and the advanced flag
  47. enum
  48. {
  49. HelpRole = Qt::ToolTipRole,
  50. TypeRole = Qt::UserRole,
  51. AdvancedRole,
  52. StringsRole,
  53. GroupRole
  54. };
  55. enum ViewType
  56. {
  57. FlatView,
  58. GroupView
  59. };
  60. public slots:
  61. // set a list of properties. This list will be sorted and grouped according
  62. // to prefix. Any property that existed already and which is found in this
  63. // list of properties to set will become an old property. All others will
  64. // become new properties and be marked red.
  65. void setProperties(const QCMakePropertyList& props);
  66. // set whether to show new properties in red
  67. void setShowNewProperties(bool);
  68. // clear everything from the model
  69. void clear();
  70. // set flag whether the model can currently be edited.
  71. void setEditEnabled(bool);
  72. // insert a new property at a row specifying all the information about the
  73. // property
  74. bool insertProperty(QCMakeProperty::PropertyType t, const QString& name,
  75. const QString& description, const QVariant& value,
  76. bool advanced);
  77. // set the view type
  78. void setViewType(ViewType t);
  79. ViewType viewType() const;
  80. public:
  81. // get the properties
  82. QCMakePropertyList properties() const;
  83. // editing enabled
  84. bool editEnabled() const;
  85. // returns how many new properties there are
  86. int newPropertyCount() const;
  87. // return flags (overloaded to modify flag based on EditEnabled flag)
  88. Qt::ItemFlags flags(const QModelIndex& index) const;
  89. QModelIndex buddy(const QModelIndex& idx) const;
  90. // get the data in the model for this property
  91. void getPropertyData(const QModelIndex& idx1, QCMakeProperty& prop) const;
  92. protected:
  93. bool EditEnabled;
  94. int NewPropertyCount;
  95. bool ShowNewProperties;
  96. ViewType View;
  97. // set the data in the model for this property
  98. void setPropertyData(const QModelIndex& idx1, const QCMakeProperty& p,
  99. bool isNew);
  100. // breaks up he property list into groups
  101. // where each group has the same prefix up to the first underscore
  102. static void breakProperties(const QSet<QCMakeProperty>& props,
  103. QMap<QString, QCMakePropertyList>& result);
  104. // gets the prefix of a string up to the first _
  105. static QString prefix(const QString& s);
  106. };
  107. /// Qt delegate class for interaction (or other customization)
  108. /// with cache properties
  109. class QCMakeCacheModelDelegate : public QItemDelegate
  110. {
  111. Q_OBJECT
  112. public:
  113. QCMakeCacheModelDelegate(QObject* p);
  114. /// create our own editors for cache properties
  115. QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option,
  116. const QModelIndex& index) const;
  117. bool editorEvent(QEvent* event, QAbstractItemModel* model,
  118. const QStyleOptionViewItem& option,
  119. const QModelIndex& index);
  120. bool eventFilter(QObject* object, QEvent* event);
  121. void setModelData(QWidget* editor, QAbstractItemModel* model,
  122. const QModelIndex& index) const;
  123. QSize sizeHint(const QStyleOptionViewItem& option,
  124. const QModelIndex& index) const;
  125. QSet<QCMakeProperty> changes() const;
  126. void clearChanges();
  127. protected slots:
  128. void setFileDialogFlag(bool);
  129. protected:
  130. bool FileDialogFlag;
  131. // record a change to an item in the model.
  132. // this simply saves the item in the set of changes
  133. void recordChange(QAbstractItemModel* model, const QModelIndex& index);
  134. // properties changed by user via this delegate
  135. QSet<QCMakeProperty> mChanges;
  136. };
  137. #endif