ctkSearchBox.cpp 9.1 KB

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