QCMakeCacheView.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "QCMake.h"
  5. #include "QCMakeSizeType.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. // set the search filter string. any property key or value not matching will
  26. // be filtered out
  27. bool setSearchFilter(QString const&);
  28. public slots:
  29. // set whether to show advanced entries
  30. void setShowAdvanced(bool);
  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 = nullptr);
  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(QCMakePropertyList const& 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, QString const& name,
  75. QString const& description, QVariant const& value,
  76. bool advanced);
  77. public:
  78. // get the properties
  79. QCMakePropertyList properties() const;
  80. // editing enabled
  81. bool editEnabled() const;
  82. // returns how many new properties there are
  83. cm_qsizetype newPropertyCount() const;
  84. // return flags (overloaded to modify flag based on EditEnabled flag)
  85. Qt::ItemFlags flags(QModelIndex const& index) const;
  86. QModelIndex buddy(QModelIndex const& idx) const;
  87. // get the data in the model for this property
  88. void getPropertyData(QModelIndex const& idx1, QCMakeProperty& prop) const;
  89. // set the view type
  90. void setViewType(ViewType t);
  91. ViewType viewType() const;
  92. protected:
  93. bool EditEnabled;
  94. cm_qsizetype NewPropertyCount;
  95. bool ShowNewProperties;
  96. ViewType View;
  97. // set the data in the model for this property
  98. void setPropertyData(QModelIndex const& idx1, QCMakeProperty const& 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(QSet<QCMakeProperty> const& props,
  103. QMap<QString, QCMakePropertyList>& result);
  104. // gets the prefix of a string up to the first _
  105. static QString prefix(QString const& 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, QStyleOptionViewItem const& option,
  116. QModelIndex const& index) const;
  117. bool editorEvent(QEvent* event, QAbstractItemModel* model,
  118. QStyleOptionViewItem const& option,
  119. QModelIndex const& index);
  120. bool eventFilter(QObject* object, QEvent* event);
  121. void setModelData(QWidget* editor, QAbstractItemModel* model,
  122. QModelIndex const& index) const;
  123. QSize sizeHint(QStyleOptionViewItem const& option,
  124. QModelIndex const& 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, QModelIndex const& index);
  134. // properties changed by user via this delegate
  135. QSet<QCMakeProperty> mChanges;
  136. };