ctkSearchBox.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 __ctkSearchBox_h
  15. #define __ctkSearchBox_h
  16. // QT includes
  17. #include <QIcon>
  18. #include <QLineEdit>
  19. // CTK includes
  20. #include "ctkWidgetsExport.h"
  21. class ctkSearchBoxPrivate;
  22. /// \ingroup Widgets
  23. /// QLineEdit with two QIcons on each side: search and clear.
  24. /// "Search" selects all the text
  25. /// "Clear" clears the current text
  26. /// See QLineEdit::text to set/get the current text.
  27. /// ctkSearchBox's purpose is to be used to filter other widgets.
  28. /// e.g.:
  29. /// <code>
  30. /// ctkSearchBox searchBox;
  31. /// QSortFilterProxyModel filterModel;
  32. /// QObject::connect(&searchBox, SIGNAL(textChanged(QString)),
  33. /// &filterModel, SLOT(setFilterFixedString(QString)));
  34. /// ...
  35. /// </code>
  36. class CTK_WIDGETS_EXPORT ctkSearchBox : public QLineEdit
  37. {
  38. Q_OBJECT
  39. #if QT_VERSION < 0x040700
  40. /// Qt < 4.7 don't have a placeholderText property, as we need it, we define it
  41. /// manually.
  42. Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText)
  43. #endif
  44. /// Show an icon at left side of the line edit, indicating that the text
  45. /// field is used to search/filter something. The default is <code>false</code>.
  46. Q_PROPERTY(bool showSearchIcon READ showSearchIcon WRITE setShowSearchIcon)
  47. /// The QIcon to use for the search icon at the left. The default is a
  48. /// magnifying glass icon.
  49. Q_PROPERTY(QIcon searchIcon READ searchIcon WRITE setSearchIcon)
  50. /// The QIcon to use for the clear icon. The default is a round grey button
  51. /// with a white cross.
  52. Q_PROPERTY(QIcon clearIcon READ clearIcon WRITE setClearIcon)
  53. public:
  54. /// Superclass typedef
  55. typedef QLineEdit Superclass;
  56. ctkSearchBox(QWidget *parent = 0);
  57. virtual ~ctkSearchBox();
  58. #if QT_VERSION < 0x040700
  59. QString placeholderText()const;
  60. void setPlaceholderText(const QString& defaultText);
  61. #endif
  62. /// False by default
  63. void setShowSearchIcon(bool show);
  64. bool showSearchIcon()const;
  65. /// False by default
  66. void setAlwaysShowClearIcon(bool show);
  67. bool alwaysShowClearIcon()const;
  68. /// Set the search icon.
  69. void setSearchIcon(const QIcon& icon);
  70. /// Get the current search icon.
  71. QIcon searchIcon()const;
  72. /// Set the clear icon.
  73. void setClearIcon(const QIcon& icon);
  74. /// Get the current clear icon.
  75. QIcon clearIcon()const;
  76. protected Q_SLOTS:
  77. /// Change the clear icon's state to enabled or disabled.
  78. void updateClearButtonState();
  79. protected:
  80. virtual void paintEvent(QPaintEvent*);
  81. virtual void mousePressEvent(QMouseEvent* event);
  82. virtual void mouseMoveEvent(QMouseEvent *event);
  83. virtual void resizeEvent(QResizeEvent * event);
  84. QScopedPointer<ctkSearchBoxPrivate> d_ptr;
  85. private:
  86. Q_DECLARE_PRIVATE(ctkSearchBox);
  87. Q_DISABLE_COPY(ctkSearchBox);
  88. };
  89. #endif // __ctkSearchBox_h