ctkMenuComboBox.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. Q_PROPERTY(QString defaultText READ defaultText WRITE setDefaultText)
  43. Q_PROPERTY(QIcon defaultIcon READ defaultIcon WRITE setDefaultIcon)
  44. Q_PROPERTY(EditableBehavior editBehavior READ editableBehavior WRITE setEditableBehavior)
  45. Q_PROPERTY(bool searchIconVisible READ isSearchIconVisible WRITE setSearchIconVisible)
  46. /// This property holds whether the search tool button displays an icon only,
  47. /// text only, or text beside/below the icon.
  48. /// The default is Qt::ToolButtonIconOnly.
  49. /// \sa QToolButton::toolButtonStyle
  50. Q_PROPERTY(Qt::ToolButtonStyle toolButtonStyle READ toolButtonStyle WRITE setToolButtonStyle)
  51. public:
  52. enum EditableBehavior{
  53. NotEditable = 0,
  54. Editable,
  55. EditableOnFocus,
  56. EditableOnPopup
  57. };
  58. /// Superclass typedef
  59. typedef QWidget Superclass;
  60. ///
  61. ctkMenuComboBox(QWidget* parent = 0);
  62. virtual ~ctkMenuComboBox();
  63. /// Add a menu to the QcomboBox and set a QCompleter
  64. void setMenu(QMenu* menu);
  65. QMenu* menu()const;
  66. /// Empty by default
  67. /// set the first default text.
  68. void setDefaultText(const QString&);
  69. QString defaultText()const;
  70. /// Empty by default
  71. /// if a QAction doesn't have icon in the menu, the comboBox takes the defaultIcon.
  72. void setDefaultIcon(const QIcon&);
  73. QIcon defaultIcon()const;
  74. /// set/get the editableType; See enum EditableType for more details.
  75. void setEditableBehavior(EditableBehavior editBehavior);
  76. EditableBehavior editableBehavior()const;
  77. /// set the icon search visible
  78. void setSearchIconVisible(bool state);
  79. bool isSearchIconVisible() const;
  80. Qt::ToolButtonStyle toolButtonStyle() const;
  81. /// See QComboBox::setMinimumContentsLength()
  82. void setMinimumContentsLength(int characters);
  83. protected:
  84. virtual bool eventFilter(QObject* target, QEvent* event);
  85. public Q_SLOTS:
  86. void clearActiveAction();
  87. void setToolButtonStyle(Qt::ToolButtonStyle style);
  88. Q_SIGNALS:
  89. void actionChanged(QAction* action);
  90. protected Q_SLOTS:
  91. /// Change the current text/icon on the QComboBox
  92. /// And trigger the action.
  93. /// action selected from the menu.
  94. void onActionSelected(QAction* action);
  95. /// action selected from the line edit or the completer.
  96. void onEditingFinished();
  97. protected:
  98. QScopedPointer<ctkMenuComboBoxPrivate> d_ptr;
  99. private:
  100. Q_DECLARE_PRIVATE(ctkMenuComboBox);
  101. Q_DISABLE_COPY(ctkMenuComboBox);
  102. };
  103. Q_DECLARE_METATYPE(ctkMenuComboBox::EditableBehavior)
  104. #endif