QCMakeCacheView.h 4.9 KB

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