ctkSearchBox.cpp 8.4 KB

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