ctkSearchBox.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // CTK includes
  19. #include "ctkWidgetsExport.h"
  20. class ctkSearchBoxPrivate;
  21. /// QLineEdit with two QIcons on each side: search and clear.
  22. /// "Search" selects all the text
  23. /// "Clear" clears the current text
  24. /// See QLineEdit::text to set/get the current text.
  25. /// ctkSearchBox's purpose is to be used to filter other widgets.
  26. /// e.g.:
  27. /// <code>
  28. /// ctkSearchBox searchBox;
  29. /// QSortFilterProxyModel filterModel;
  30. /// QObject::connect(&searchBox, SIGNAL(textChanged(QString)),
  31. /// &filterModel, SLOT(setFilterFixedString(QString)));
  32. /// ...
  33. /// </code>
  34. class CTK_WIDGETS_EXPORT ctkSearchBox : public QLineEdit
  35. {
  36. Q_OBJECT
  37. #if QT_VERSION < 0x040700
  38. /// Qt < 4.7 don't have a placeholderText property, as we need it, we define it
  39. /// manually.
  40. Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText)
  41. #endif
  42. Q_PROPERTY(bool showSearchIcon READ showSearchIcon WRITE setShowSearchIcon)
  43. public:
  44. /// Superclass typedef
  45. typedef QLineEdit Superclass;
  46. ctkSearchBox(QWidget *parent = 0);
  47. virtual ~ctkSearchBox();
  48. #if QT_VERSION < 0x040700
  49. QString placeholderText()const;
  50. void setPlaceholderText(const QString& defaultText);
  51. #endif
  52. /// False by default
  53. void setShowSearchIcon(bool);
  54. bool showSearchIcon()const;
  55. void setAlwaysShowClearIcon(bool);
  56. bool alwaysShowClearIcon()const;
  57. protected slots:
  58. /// Change the clear icon's state to enabled or disabled.
  59. void updateClearButtonState();
  60. protected:
  61. virtual void paintEvent(QPaintEvent*);
  62. virtual void mousePressEvent(QMouseEvent* event);
  63. virtual void mouseMoveEvent(QMouseEvent *event);
  64. virtual void resizeEvent(QResizeEvent * event);
  65. QScopedPointer<ctkSearchBoxPrivate> d_ptr;
  66. private:
  67. Q_DECLARE_PRIVATE(ctkSearchBox);
  68. Q_DISABLE_COPY(ctkSearchBox);
  69. };
  70. #endif // __ctkSearchBox_h