ctkActionsWidget.h 4.9 KB

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