ctkSearchBox.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <QLineEdit>
  18. #include <QIcon>
  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. Q_PROPERTY(bool showSearchIcon READ showSearchIcon WRITE setShowSearchIcon)
  45. Q_PROPERTY(QIcon searchIcon READ searchIcon WRITE setSearchIcon)
  46. Q_PROPERTY(QIcon clearIcon READ clearIcon WRITE setClearIcon)
  47. Q_PROPERTY(QIcon::Mode clearIconMode READ clearIconMode WRITE setClearIconMode)
  48. public:
  49. /// Superclass typedef
  50. typedef QLineEdit Superclass;
  51. ctkSearchBox(QWidget *parent = 0);
  52. virtual ~ctkSearchBox();
  53. #if QT_VERSION < 0x040700
  54. QString placeholderText()const;
  55. void setPlaceholderText(const QString& defaultText);
  56. #endif
  57. /// False by default
  58. void setShowSearchIcon(bool);
  59. bool showSearchIcon()const;
  60. void setAlwaysShowClearIcon(bool);
  61. bool alwaysShowClearIcon()const;
  62. void setSearchIcon(const QIcon&);
  63. QIcon searchIcon()const;
  64. void setClearIcon(const QIcon&);
  65. QIcon clearIcon()const;
  66. void setClearIconMode(QIcon::Mode);
  67. QIcon::Mode clearIconMode()const;
  68. protected Q_SLOTS:
  69. /// Change the clear icon's state to enabled or disabled.
  70. void updateClearButtonState();
  71. protected:
  72. virtual void paintEvent(QPaintEvent*);
  73. virtual void mousePressEvent(QMouseEvent* event);
  74. virtual void mouseMoveEvent(QMouseEvent *event);
  75. virtual void resizeEvent(QResizeEvent * event);
  76. QScopedPointer<ctkSearchBoxPrivate> d_ptr;
  77. private:
  78. Q_DECLARE_PRIVATE(ctkSearchBox);
  79. Q_DISABLE_COPY(ctkSearchBox);
  80. };
  81. #endif // __ctkSearchBox_h