ctkActionsWidget.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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.commontk.org/LICENSE
  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. //---------------------------------------------------------------------------
  30. class CTK_WIDGETS_EXPORT ctkActionsWidget : public QWidget
  31. {
  32. Q_OBJECT
  33. Q_PROPERTY(bool actionsWithNoShortcutVisible READ areActionsWithNoShortcutVisible WRITE setActionsWithNoShortcutVisible)
  34. Q_PROPERTY(bool menuActionsVisible READ areMenuActionsVisible WRITE setMenuActionsVisible)
  35. public:
  36. explicit ctkActionsWidget(QWidget* parent = 0);
  37. /// Add an action into a specified group (or at top level if group is empty)
  38. /// An action can be added multiple times (in a different group). Once added,
  39. /// ctkActionsWidget listens to the QAction and updates the action properties
  40. /// TODO: check that the action hasn't been already added into a group
  41. void addAction(QAction* action, const QString& group = QString());
  42. /// Convenient function to add a list of action at once
  43. void addActions(QList<QAction*> actions, const QString& group = QString());
  44. /// Remove all the actions and groups
  45. void clear();
  46. /// Return a pointer on a group item (you probably have no use for it)
  47. QStandardItem* groupItem(const QString& category);
  48. /// Show/hide QActions that have an empty shortcut
  49. void setActionsWithNoShortcutVisible(bool show);
  50. bool areActionsWithNoShortcutVisible()const;
  51. /// Show/hide QActions that have an empty shortcut
  52. void setMenuActionsVisible(bool show);
  53. bool areMenuActionsVisible()const;
  54. QStandardItemModel* model()const;
  55. QTreeView* view()const;
  56. protected slots:
  57. void updateAction();
  58. protected:
  59. enum ActionColumn{
  60. NameColumn = 0,
  61. ShortcutColumn,
  62. ContextColumn,
  63. DetailsColumn
  64. };
  65. private:
  66. friend class ctkSortFilterActionsProxyModel;
  67. CTK_DECLARE_PRIVATE(ctkActionsWidget);
  68. };
  69. //---------------------------------------------------------------------------
  70. class ctkSortFilterActionsProxyModel : public QSortFilterProxyModel
  71. {
  72. Q_OBJECT
  73. public:
  74. ctkSortFilterActionsProxyModel(QObject* parent=0);
  75. void setActionsWithNoShortcutVisible(bool);
  76. bool areActionsWithNoShortcutVisible()const;
  77. void setMenuActionsVisible(bool);
  78. bool areMenuActionsVisible()const;
  79. protected:
  80. bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const;
  81. private:
  82. CTK_DECLARE_PRIVATE(ctkSortFilterActionsProxyModel);
  83. };
  84. //---------------------------------------------------------------------------
  85. class ctkRichTextItemDelegate : public QStyledItemDelegate
  86. {
  87. Q_OBJECT
  88. protected:
  89. virtual void paint(QPainter * painter, const QStyleOptionViewItem & option,
  90. const QModelIndex & index) const;
  91. virtual QSize sizeHint(const QStyleOptionViewItem & option,
  92. const QModelIndex & index)const;
  93. };
  94. #endif