ctkActionsWidget.h 5.0 KB

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