ctkSearchBox.h 2.6 KB

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