ctkMenuComboBox.h 3.4 KB

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