ctkMenuComboBox.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 __ctkMenuComboBox_h
  15. #define __ctkMenuComboBox_h
  16. // Qt includes
  17. #include <QMenu>
  18. #include <QMetaType>
  19. #include <QWidget>
  20. // CTK includes
  21. #include "ctkWidgetsExport.h"
  22. class ctkMenuComboBoxPrivate;
  23. /// \ingroup Widgets
  24. /// QComboBox linked with a QMenu. See ctkMenuComboBox::setMenu()
  25. /// ctkMenuComboBox can be editable, disable,
  26. /// editable on focus or editable on double click.
  27. /// if it is editable :
  28. /// the comboBox is always editable, you can filter the Menu or show it.
  29. /// if it is editable on focus - on double click:
  30. /// the combobox become editable when it has the focus in.
  31. /// So ctkMenuComboBox's purpose is to filter a menu, if you edit the current text
  32. /// or show the menu, if you click on the arrow.
  33. /// if it is disabled :
  34. /// the ctkMenuComboBox has the same behavior as a QPushButton. You can't filter the menu.
  35. /// By default ctkMenuComboBox is not editable with the search icon visible.
  36. /// See ctkmenuComboBox::setEditableType() to change the default behavior.
  37. /// and setIconSearchVisible() to show/hide the icon.
  38. class CTK_WIDGETS_EXPORT ctkMenuComboBox : public QWidget
  39. {
  40. Q_OBJECT
  41. Q_ENUMS(EditableBehavior)
  42. /// This property holds the text shown on the combobox when there is no
  43. /// selected item.
  44. /// Empty by default.
  45. Q_PROPERTY(QString defaultText READ defaultText WRITE setDefaultText)
  46. /// This property holds the icon shown on the combobox when the current item
  47. /// (QAction) doesn't have any icon associated.
  48. /// Empty by default
  49. Q_PROPERTY(QIcon defaultIcon READ defaultIcon WRITE setDefaultIcon)
  50. /// This property holds the edit behavior of the combobox, it defines what
  51. /// action is needed to turn the combobox into a search mode where the user
  52. /// can type the name of the item to select using the combobox line edit.
  53. /// ctkMenuComboBox::NotEditable by default
  54. /// \sa EditableType
  55. Q_PROPERTY(EditableBehavior editBehavior READ editableBehavior WRITE setEditableBehavior)
  56. /// This property controls whether the search tool button is visible or hidden.
  57. /// True by default
  58. Q_PROPERTY(bool searchIconVisible READ isSearchIconVisible WRITE setSearchIconVisible)
  59. /// This property holds whether the search tool button displays an icon only,
  60. /// text only, or text beside/below the icon.
  61. /// The default is Qt::ToolButtonIconOnly.
  62. /// \sa QToolButton::toolButtonStyle
  63. Q_PROPERTY(Qt::ToolButtonStyle toolButtonStyle READ toolButtonStyle WRITE setToolButtonStyle)
  64. public:
  65. enum EditableBehavior{
  66. NotEditable = 0,
  67. Editable,
  68. EditableOnFocus,
  69. EditableOnPopup
  70. };
  71. /// Superclass typedef
  72. typedef QWidget Superclass;
  73. ///
  74. ctkMenuComboBox(QWidget* parent = 0);
  75. virtual ~ctkMenuComboBox();
  76. /// Add a menu to the QcomboBox and set a QCompleter
  77. void setMenu(QMenu* menu);
  78. QMenu* menu()const;
  79. void setDefaultText(const QString&);
  80. QString defaultText()const;
  81. void setDefaultIcon(const QIcon&);
  82. QIcon defaultIcon()const;
  83. void setEditableBehavior(EditableBehavior editBehavior);
  84. EditableBehavior editableBehavior()const;
  85. void setSearchIconVisible(bool state);
  86. bool isSearchIconVisible() const;
  87. Qt::ToolButtonStyle toolButtonStyle() const;
  88. /// Set the minimum width of the combobox.
  89. /// \sa QComboBox::setMinimumContentsLength()
  90. void setMinimumContentsLength(int characters);
  91. protected:
  92. virtual bool eventFilter(QObject* target, QEvent* event);
  93. public Q_SLOTS:
  94. void clearActiveAction();
  95. void setToolButtonStyle(Qt::ToolButtonStyle style);
  96. Q_SIGNALS:
  97. void actionChanged(QAction* action);
  98. protected Q_SLOTS:
  99. /// Change the current text/icon on the QComboBox
  100. /// And trigger the action.
  101. /// action selected from the menu.
  102. void onActionSelected(QAction* action);
  103. /// action selected from the line edit or the completer.
  104. void onEditingFinished();
  105. protected:
  106. QScopedPointer<ctkMenuComboBoxPrivate> d_ptr;
  107. private:
  108. Q_DECLARE_PRIVATE(ctkMenuComboBox);
  109. Q_DISABLE_COPY(ctkMenuComboBox);
  110. };
  111. Q_DECLARE_METATYPE(ctkMenuComboBox::EditableBehavior)
  112. #endif