ctkMenuComboBox.h 5.0 KB

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