ctkActionsWidget.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 __ctkActionsWidget_h
  15. #define __ctkActionsWidget_h
  16. // Qt includes
  17. #include <QStyledItemDelegate>
  18. #include <QSortFilterProxyModel>
  19. #include <QWidget>
  20. // CTK includes
  21. #include "ctkPimpl.h"
  22. #include "ctkWidgetsExport.h"
  23. class ctkActionsWidgetPrivate;
  24. class ctkSortFilterActionsProxyModelPrivate;
  25. class QAction;
  26. class QStandardItemModel;
  27. class QStandardItem;
  28. class QTreeView;
  29. /// \ingroup Widgets
  30. /// ctkActionsWidget presents a list of QAction to the user. The QAction's are
  31. /// displayed in a multi column tree view. The columns contain the QAction's
  32. /// text, shortcut, context and tooltip in that order.
  33. /// The typical use is to show what shortcuts are associated to what commands
  34. /// in an application.
  35. /// ctkActionsWidget internally uses a QStandardItemModel where each item data
  36. /// (QStandardItem::data) contain a pointer to the QAction.
  37. /// QActions can optionally be ordered by group
  38. /// TODO: Add "hide empty group" property to hide empty groups
  39. class CTK_WIDGETS_EXPORT ctkActionsWidget : public QWidget
  40. {
  41. Q_OBJECT
  42. Q_PROPERTY(bool actionsWithNoShortcutVisible READ areActionsWithNoShortcutVisible WRITE setActionsWithNoShortcutVisible)
  43. Q_PROPERTY(bool menuActionsVisible READ areMenuActionsVisible WRITE setMenuActionsVisible)
  44. /// This property controls whether the actions are sorted by column (>=0) or
  45. /// not (-1). Not sorted by default.
  46. /// \sa sortColumn(), setSortColumn()
  47. Q_PROPERTY(int sortColumn READ sortColumn WRITE setSortColumn)
  48. public:
  49. explicit ctkActionsWidget(QWidget* parent = 0);
  50. virtual ~ctkActionsWidget();
  51. /// Add an action into a specified group (or at top level if group is empty)
  52. /// An action can be added multiple times (in a different group). Once added,
  53. /// ctkActionsWidget listens to the QAction and updates the action properties
  54. /// TODO: check that the action hasn't been already added into a group
  55. void addAction(QAction* action, const QString& group = QString());
  56. /// Convenient function to add a list of action at once
  57. void addActions(QList<QAction*> actions, const QString& group = QString());
  58. /// Remove all the actions and groups
  59. void clear();
  60. /// Return a pointer on a group item (you probably have no use for it)
  61. QStandardItem* groupItem(const QString& category);
  62. /// If true, shows QActions that have an empty shortcut, otherwise hide them.
  63. /// True by default
  64. void setActionsWithNoShortcutVisible(bool show);
  65. bool areActionsWithNoShortcutVisible()const;
  66. /// If true, shows QMenus, otherwise hide them.
  67. /// True by default
  68. void setMenuActionsVisible(bool show);
  69. bool areMenuActionsVisible()const;
  70. /// Set the sortColumn property value.
  71. /// \sa sortColumn
  72. void setSortColumn(int column);
  73. /// Return the sortColumn property value.
  74. /// \sa sortColumn
  75. int sortColumn()const;
  76. /// Return the unsorted/unfiltered model of all the actions
  77. QStandardItemModel* model()const;
  78. /// return the view used to display the action model
  79. QTreeView* view()const;
  80. protected Q_SLOTS:
  81. void updateAction();
  82. protected:
  83. enum ActionColumn{
  84. NameColumn = 0,
  85. ShortcutColumn,
  86. ContextColumn,
  87. DetailsColumn
  88. };
  89. protected:
  90. QScopedPointer<ctkActionsWidgetPrivate> d_ptr;
  91. private:
  92. Q_DECLARE_PRIVATE(ctkActionsWidget);
  93. Q_DISABLE_COPY(ctkActionsWidget);
  94. friend class ctkSortFilterActionsProxyModel;
  95. };
  96. /// \ingroup Widgets
  97. /// ctkSortFilterActionsProxyModel is a utility class that is needed by
  98. /// ctkActionsWidget. It's a specialization of a QSortFilterProxyModel and
  99. /// control what action is visible to the tree view.
  100. class ctkSortFilterActionsProxyModel : public QSortFilterProxyModel
  101. {
  102. Q_OBJECT
  103. public:
  104. explicit ctkSortFilterActionsProxyModel(QObject* parent=0);
  105. virtual ~ctkSortFilterActionsProxyModel();
  106. void setActionsWithNoShortcutVisible(bool);
  107. bool areActionsWithNoShortcutVisible()const;
  108. void setMenuActionsVisible(bool);
  109. bool areMenuActionsVisible()const;
  110. protected:
  111. bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const;
  112. QScopedPointer<ctkSortFilterActionsProxyModelPrivate> d_ptr;
  113. private:
  114. Q_DECLARE_PRIVATE(ctkSortFilterActionsProxyModel);
  115. Q_DISABLE_COPY(ctkSortFilterActionsProxyModel);
  116. };
  117. /// \ingroup Widgets
  118. /// ctkRichTextItemDelegate is a utility class that is needed by
  119. /// ctkActionsWidget. It control how QAction tree items are displayed when the
  120. /// text is written in HTML.
  121. class ctkRichTextItemDelegate : public QStyledItemDelegate
  122. {
  123. Q_OBJECT
  124. protected:
  125. virtual void paint(QPainter * painter, const QStyleOptionViewItem & option,
  126. const QModelIndex & index) const;
  127. virtual QSize sizeHint(const QStyleOptionViewItem & option,
  128. const QModelIndex & index)const;
  129. };
  130. #endif