ctkSearchBox.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. // Qt includes
  15. #include <QApplication>
  16. #include <QDebug>
  17. #include <QIcon>
  18. #include <QMouseEvent>
  19. #include <QPainter>
  20. #include <QRect>
  21. #include <QStyleOption>
  22. // CTK includes
  23. #include "ctkSearchBox.h"
  24. // --------------------------------------------------
  25. class ctkSearchBoxPrivate
  26. {
  27. Q_DECLARE_PUBLIC(ctkSearchBox);
  28. protected:
  29. ctkSearchBox* const q_ptr;
  30. public:
  31. ctkSearchBoxPrivate(ctkSearchBox& object);
  32. void init();
  33. /// Position and size for the clear icon in the QLineEdit
  34. QRect clearRect()const;
  35. /// Position and size for the search icon in the QLineEdit
  36. QRect searchRect()const;
  37. QIcon clearIcon;
  38. QIcon searchIcon;
  39. QIcon::Mode clearIconMode;
  40. #if QT_VERSION < 0x040700
  41. QString placeholderText;
  42. #endif
  43. };
  44. // --------------------------------------------------
  45. ctkSearchBoxPrivate::ctkSearchBoxPrivate(ctkSearchBox &object)
  46. : q_ptr(&object)
  47. {
  48. this->clearIcon = QIcon(":Icons/clear.svg");
  49. this->searchIcon = QIcon(":Icons/search.svg");
  50. this->clearIconMode = QIcon::Disabled;
  51. }
  52. // --------------------------------------------------
  53. void ctkSearchBoxPrivate::init()
  54. {
  55. Q_Q(ctkSearchBox);
  56. // Set a text by default on the QLineEdit
  57. q->setPlaceholderText(q->tr("Search..."));
  58. QObject::connect(q, SIGNAL(textChanged(const QString&)),
  59. q, SLOT(updateClearButtonState()));
  60. }
  61. // --------------------------------------------------
  62. QRect ctkSearchBoxPrivate::clearRect()const
  63. {
  64. Q_Q(const ctkSearchBox);
  65. QRect cRect = this->searchRect();
  66. cRect.moveLeft(q->width() - cRect.width() - cRect.left());
  67. return cRect;
  68. }
  69. // --------------------------------------------------
  70. QRect ctkSearchBoxPrivate::searchRect()const
  71. {
  72. Q_Q(const ctkSearchBox);
  73. QRect sRect;
  74. // If the QLineEdit has a frame, the icon must be shifted from
  75. // the frame line width
  76. if (q->hasFrame())
  77. {
  78. QStyleOptionFrameV2 opt;
  79. q->initStyleOption(&opt);
  80. sRect.moveTopLeft(QPoint(opt.lineWidth, opt.lineWidth));
  81. }
  82. // Hardcoded: shift by 1 pixel because some styles have a focus frame inside
  83. // the line edit frame.
  84. sRect.translate(QPoint(1,1));
  85. // Square size
  86. sRect.setSize(QSize(q->height(),q->height()) -
  87. 2*QSize(sRect.left(), sRect.top()));
  88. return sRect;
  89. }
  90. // --------------------------------------------------
  91. ctkSearchBox::ctkSearchBox(QWidget* _parent)
  92. : QLineEdit(_parent)
  93. , d_ptr(new ctkSearchBoxPrivate(*this))
  94. {
  95. Q_D(ctkSearchBox);
  96. d->init();
  97. }
  98. // --------------------------------------------------
  99. ctkSearchBox::~ctkSearchBox()
  100. {
  101. }
  102. #if QT_VERSION < 0x040700
  103. // --------------------------------------------------
  104. QString ctkSearchBox::placeholderText()const
  105. {
  106. Q_D(const ctkSearchBox);
  107. return d->placeholderText;
  108. }
  109. // --------------------------------------------------
  110. void ctkSearchBox::setPlaceholderText(const QString &defaultText)
  111. {
  112. Q_D(ctkSearchBox);
  113. d->placeholderText = defaultText;
  114. if (!this->hasFocus())
  115. {
  116. this->update();
  117. }
  118. }
  119. #endif
  120. // --------------------------------------------------
  121. void ctkSearchBox::paintEvent(QPaintEvent * event)
  122. {
  123. Q_D(ctkSearchBox);
  124. // Draw the line edit with text.
  125. // Text has already been shifted to the right (in resizeEvent()) to leave
  126. // space for the search icon.
  127. this->Superclass::paintEvent(event);
  128. QPainter p(this);
  129. QRect cRect = d->clearRect();
  130. QRect sRect = d->searchRect();
  131. #if QT_VERSION >= 0x040700
  132. QRect r = rect();
  133. QPalette pal = palette();
  134. QStyleOptionFrameV2 panel;
  135. initStyleOption(&panel);
  136. r = this->style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
  137. r.setX(r.x() + this->textMargins().left());
  138. r.setY(r.y() + this->textMargins().top());
  139. r.setRight(r.right() - this->textMargins().right());
  140. r.setBottom(r.bottom() - this->textMargins().bottom());
  141. p.setClipRect(r);
  142. QFontMetrics fm = fontMetrics();
  143. Qt::Alignment va = QStyle::visualAlignment(this->layoutDirection(),
  144. QFlag(this->alignment()));
  145. int vscroll = 0;
  146. const int verticalMargin = 1;
  147. const int horizontalMargin = 2;
  148. switch (va & Qt::AlignVertical_Mask) {
  149. case Qt::AlignBottom:
  150. vscroll = r.y() + r.height() - fm.height() - verticalMargin;
  151. break;
  152. case Qt::AlignTop:
  153. vscroll = r.y() + verticalMargin;
  154. break;
  155. default:
  156. //center
  157. vscroll = r.y() + (r.height() - fm.height() + 1) / 2;
  158. break;
  159. }
  160. QRect lineRect(r.x() + horizontalMargin, vscroll,
  161. r.width() - 2*horizontalMargin, fm.height());
  162. int minLB = qMax(0, -fm.minLeftBearing());
  163. if (this->text().isEmpty())
  164. {
  165. if (!this->hasFocus() && !this->placeholderText().isEmpty())
  166. {
  167. QColor col = pal.text().color();
  168. col.setAlpha(128);
  169. QPen oldpen = p.pen();
  170. p.setPen(col);
  171. lineRect.adjust(minLB, 0, 0, 0);
  172. QString elidedText = fm.elidedText(this->placeholderText(), Qt::ElideRight, lineRect.width());
  173. p.drawText(lineRect, va, elidedText);
  174. p.setPen(oldpen);
  175. }
  176. }
  177. p.setClipRect(this->rect());
  178. #endif
  179. // Draw clearIcon
  180. QPixmap closePixmap = d->clearIcon.pixmap(cRect.size(),d->clearIconMode);
  181. this->style()->drawItemPixmap(&p, cRect, Qt::AlignCenter, closePixmap);
  182. //Draw searchIcon
  183. QPixmap searchPixmap = d->searchIcon.pixmap(sRect.size());
  184. this->style()->drawItemPixmap(&p, sRect, Qt::AlignCenter, searchPixmap);
  185. }
  186. // --------------------------------------------------
  187. void ctkSearchBox::mousePressEvent(QMouseEvent *e)
  188. {
  189. Q_D(ctkSearchBox);
  190. if(d->clearRect().contains(e->pos()))
  191. {
  192. this->clear();
  193. return;
  194. }
  195. if(d->searchRect().contains(e->pos()))
  196. {
  197. this->selectAll();
  198. return;
  199. }
  200. this->Superclass::mousePressEvent(e);
  201. }
  202. // --------------------------------------------------
  203. void ctkSearchBox::mouseMoveEvent(QMouseEvent *e)
  204. {
  205. Q_D(ctkSearchBox);
  206. if(d->clearRect().contains(e->pos()) || d->searchRect().contains(e->pos()))
  207. {
  208. this->setCursor(Qt::ArrowCursor);
  209. }
  210. else
  211. {
  212. this->setCursor(this->isReadOnly() ? Qt::ArrowCursor : Qt::IBeamCursor);
  213. }
  214. this->Superclass::mouseMoveEvent(e);
  215. }
  216. // --------------------------------------------------
  217. void ctkSearchBox::resizeEvent(QResizeEvent * event)
  218. {
  219. Q_D(ctkSearchBox);
  220. static int iconSpacing = 4; // hardcoded, same way as pushbutton icon spacing
  221. QRect cRect = d->clearRect();
  222. QRect sRect = d->searchRect();
  223. // Set 2 margins each sides of the QLineEdit, according to the icons
  224. this->setTextMargins( sRect.right() + iconSpacing, 0,
  225. event->size().width() - cRect.left() - iconSpacing,0);
  226. }
  227. // --------------------------------------------------
  228. void ctkSearchBox::updateClearButtonState()
  229. {
  230. Q_D(ctkSearchBox);
  231. d->clearIconMode = this->text().isEmpty() ? QIcon::Disabled : QIcon::Normal;
  232. }