ctkMenuComboBox.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /// QComboBox linked with a QMenu. See ctkMenuComboBox::setMenu()
  24. /// ctkMenuComboBox can be editable, disable,
  25. /// editable on focus or editable on double click.
  26. /// if it is editable :
  27. /// the comboBox is always editable, you can filter the Menu or show it.
  28. /// if it is editable on focus - on double click:
  29. /// the combobox become editable when it has the focus in.
  30. /// So ctkMenuComboBox's purpose is to filter a menu, if you edit the current text
  31. /// or show the menu, if you click on the arrow.
  32. /// if it is disabled :
  33. /// the ctkMenuComboBox has the same behavior as a QPushButton. You can't filter the menu.
  34. /// By default ctkMenuComboBox is editable on double click.
  35. /// See ctkmenuComboBox::setEditableType() to change the default behavior.
  36. class CTK_WIDGETS_EXPORT ctkMenuComboBox : public QWidget
  37. {
  38. Q_OBJECT
  39. Q_ENUMS(EditableBehavior)
  40. Q_PROPERTY(QString defaultText READ defaultText WRITE setDefaultText)
  41. Q_PROPERTY(QIcon defaultIcon READ defaultIcon WRITE setDefaultIcon)
  42. Q_PROPERTY(EditableBehavior editBehavior READ editableBehavior WRITE setEditableBehavior)
  43. public:
  44. enum EditableBehavior{
  45. NotEditable = 0,
  46. Editable,
  47. EditableOnFocus,
  48. EditableOnDoubleClick
  49. };
  50. /// Superclass typedef
  51. typedef QWidget Superclass;
  52. ///
  53. ctkMenuComboBox(QWidget* parent = 0);
  54. virtual ~ctkMenuComboBox();
  55. /// Add a menu to the QcomboBox and set a QCompleter
  56. void setMenu(QMenu* menu);
  57. QMenu* menu()const;
  58. /// Empty by default
  59. /// set the first default text.
  60. void setDefaultText(const QString&);
  61. QString defaultText()const;
  62. /// Empty by default
  63. /// if a QAction doesn't have icon in the menu, the comboBox takes the defaultIcon.
  64. void setDefaultIcon(const QIcon&);
  65. QIcon defaultIcon()const;
  66. /// set/get the editableType; See enum EditableType for more details.
  67. void setEditableBehavior(EditableBehavior editBehavior);
  68. EditableBehavior editableBehavior()const;
  69. /// See QComboBox::setMinimumContentsLength()
  70. void setMinimumContentsLength(int characters);
  71. virtual bool eventFilter(QObject* target, QEvent* event);
  72. public slots:
  73. void clearActiveAction();
  74. signals:
  75. void actionChanged(QAction* action);
  76. protected slots:
  77. /// Change the current text/icon on the QComboBox
  78. /// And trigger the action.
  79. /// action selected from the menu.
  80. void onActionSelected(QAction* action);
  81. /// action selected from the line edit or the completer.
  82. void onReturnPressed();
  83. protected:
  84. QScopedPointer<ctkMenuComboBoxPrivate> d_ptr;
  85. private:
  86. Q_DECLARE_PRIVATE(ctkMenuComboBox);
  87. Q_DISABLE_COPY(ctkMenuComboBox);
  88. };
  89. Q_DECLARE_METATYPE(ctkMenuComboBox::EditableBehavior)
  90. #endif