ctkComboBox.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <QAbstractScrollArea>
  16. #include <QApplication>
  17. #include <QDebug>
  18. #include <QScrollBar>
  19. #include <QStylePainter>
  20. #include <QWheelEvent>
  21. // CTK includes
  22. #include "ctkComboBox.h"
  23. // -------------------------------------------------------------------------
  24. class ctkComboBoxPrivate
  25. {
  26. Q_DECLARE_PUBLIC(ctkComboBox);
  27. protected:
  28. ctkComboBox* const q_ptr;
  29. public:
  30. ctkComboBoxPrivate(ctkComboBox& object);
  31. void initStyleOption(QStyleOptionComboBox* opt)const;
  32. QSize recomputeSizeHint(QSize &sh) const;
  33. QString DefaultText;
  34. QIcon DefaultIcon;
  35. bool ForceDefault;
  36. Qt::TextElideMode ElideMode;
  37. ctkComboBox::ScrollEffect ScrollWheelEffect;
  38. mutable QSize MinimumSizeHint;
  39. mutable QSize SizeHint;
  40. };
  41. // -------------------------------------------------------------------------
  42. ctkComboBoxPrivate::ctkComboBoxPrivate(ctkComboBox& object)
  43. :q_ptr(&object)
  44. {
  45. this->DefaultText = "";
  46. this->ForceDefault = false;
  47. this->ElideMode = Qt::ElideNone;
  48. this->ScrollWheelEffect = ctkComboBox::AlwaysScroll;
  49. }
  50. // -------------------------------------------------------------------------
  51. QSize ctkComboBoxPrivate::recomputeSizeHint(QSize &sh) const
  52. {
  53. Q_Q(const ctkComboBox);
  54. if (sh.isValid())
  55. {
  56. return sh.expandedTo(QApplication::globalStrut());
  57. }
  58. bool hasIcon = false;
  59. int count = q->count();
  60. QSize iconSize = q->iconSize();
  61. const QFontMetrics &fm = q->fontMetrics();
  62. // text width
  63. if (&sh == &this->SizeHint || q->minimumContentsLength() == 0)
  64. {
  65. switch (q->sizeAdjustPolicy())
  66. {
  67. case QComboBox::AdjustToContents:
  68. case QComboBox::AdjustToContentsOnFirstShow:
  69. if (count == 0 || this->ForceDefault)
  70. {
  71. sh.rwidth() = this->DefaultText.isEmpty() ?
  72. 7 * fm.width(QLatin1Char('x')) :
  73. fm.boundingRect(this->DefaultText).width();
  74. if (!this->DefaultIcon.isNull())
  75. {
  76. hasIcon = true;
  77. sh.rwidth() += iconSize.width() + 4;
  78. }
  79. }
  80. for (int i = 0; i < count; ++i)
  81. {
  82. if (!q->itemIcon(i).isNull())
  83. {
  84. hasIcon = true;
  85. sh.setWidth(qMax(sh.width(), fm.boundingRect(q->itemText(i)).width() + iconSize.width() + 4));
  86. }
  87. else
  88. {
  89. sh.setWidth(qMax(sh.width(), fm.boundingRect(q->itemText(i)).width()));
  90. }
  91. }
  92. break;
  93. case QComboBox::AdjustToMinimumContentsLength:
  94. if ((count == 0 || this->ForceDefault) && !this->DefaultIcon.isNull())
  95. {
  96. hasIcon = true;
  97. }
  98. for (int i = 0; i < count && !hasIcon; ++i)
  99. {
  100. hasIcon = !q->itemIcon(i).isNull();
  101. }
  102. break;
  103. case QComboBox::AdjustToMinimumContentsLengthWithIcon:
  104. hasIcon = true;
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. else // minimumsizehint is computing and minimumcontentslenght is > 0
  111. {
  112. if ((count == 0 || this->ForceDefault) && !this->DefaultIcon.isNull())
  113. {
  114. hasIcon = true;
  115. }
  116. for (int i = 0; i < count && !hasIcon; ++i)
  117. {
  118. hasIcon = !q->itemIcon(i).isNull();
  119. }
  120. }
  121. if (q->minimumContentsLength() > 0)
  122. {
  123. sh.setWidth(qMax(sh.width(),
  124. q->minimumContentsLength() * fm.width(QLatin1Char('X'))
  125. + (hasIcon ? iconSize.width() + 4 : 0)));
  126. }
  127. // height
  128. sh.setHeight(qMax(fm.height(), 14) + 2);
  129. if (hasIcon)
  130. {
  131. sh.setHeight(qMax(sh.height(), iconSize.height() + 2));
  132. }
  133. // add style and strut values
  134. QStyleOptionComboBox opt;
  135. this->initStyleOption(&opt);
  136. sh = q->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sh, q);
  137. return sh.expandedTo(QApplication::globalStrut());
  138. }
  139. // -------------------------------------------------------------------------
  140. void ctkComboBoxPrivate::initStyleOption(QStyleOptionComboBox* opt)const
  141. {
  142. Q_Q(const ctkComboBox);
  143. q->initStyleOption(opt);
  144. if (q->currentIndex() == -1 ||
  145. this->ForceDefault)
  146. {
  147. opt->currentText = this->DefaultText;
  148. opt->currentIcon = this->DefaultIcon;
  149. }
  150. QRect textRect = q->style()->subControlRect(
  151. QStyle::CC_ComboBox, opt, QStyle::SC_ComboBoxEditField, q);
  152. // TODO substract icon size
  153. opt->currentText = opt->fontMetrics.elidedText(opt->currentText,
  154. this->ElideMode,
  155. textRect.width());
  156. }
  157. // -------------------------------------------------------------------------
  158. ctkComboBox::ctkComboBox(QWidget* _parent)
  159. : QComboBox(_parent)
  160. , d_ptr(new ctkComboBoxPrivate(*this))
  161. {
  162. }
  163. // -------------------------------------------------------------------------
  164. ctkComboBox::~ctkComboBox()
  165. {
  166. }
  167. // -------------------------------------------------------------------------
  168. void ctkComboBox::setDefaultText(const QString& newDefaultText)
  169. {
  170. Q_D(ctkComboBox);
  171. d->DefaultText = newDefaultText;
  172. d->SizeHint = QSize();
  173. this->update();
  174. }
  175. // -------------------------------------------------------------------------
  176. QString ctkComboBox::defaultText()const
  177. {
  178. Q_D(const ctkComboBox);
  179. return d->DefaultText;
  180. }
  181. // -------------------------------------------------------------------------
  182. void ctkComboBox::setDefaultIcon(const QIcon& newIcon)
  183. {
  184. Q_D(ctkComboBox);
  185. d->DefaultIcon = newIcon;
  186. d->SizeHint = QSize();
  187. this->update();
  188. }
  189. // -------------------------------------------------------------------------
  190. QIcon ctkComboBox::defaultIcon()const
  191. {
  192. Q_D(const ctkComboBox);
  193. return d->DefaultIcon;
  194. }
  195. // -------------------------------------------------------------------------
  196. void ctkComboBox::forceDefault(bool newForceDefault)
  197. {
  198. Q_D(ctkComboBox);
  199. if (newForceDefault == d->ForceDefault)
  200. {
  201. return;
  202. }
  203. d->ForceDefault = newForceDefault;
  204. d->SizeHint = QSize();
  205. this->updateGeometry();
  206. }
  207. // -------------------------------------------------------------------------
  208. void ctkComboBox::setElideMode(const Qt::TextElideMode& newMode)
  209. {
  210. Q_D(ctkComboBox);
  211. d->ElideMode = newMode;
  212. this->update();
  213. }
  214. // -------------------------------------------------------------------------
  215. Qt::TextElideMode ctkComboBox::elideMode()const
  216. {
  217. Q_D(const ctkComboBox);
  218. return d->ElideMode;
  219. }
  220. // -------------------------------------------------------------------------
  221. bool ctkComboBox::isDefaultForced()const
  222. {
  223. Q_D(const ctkComboBox);
  224. return d->ForceDefault;
  225. }
  226. // -------------------------------------------------------------------------
  227. ctkComboBox::ScrollEffect ctkComboBox::scrollWheelEffect()const
  228. {
  229. Q_D(const ctkComboBox);
  230. return d->ScrollWheelEffect;
  231. }
  232. // -------------------------------------------------------------------------
  233. void ctkComboBox::setScrollWheelEffect(ctkComboBox::ScrollEffect scroll)
  234. {
  235. Q_D(ctkComboBox);
  236. d->ScrollWheelEffect = scroll;
  237. this->setFocusPolicy( d->ScrollWheelEffect == ctkComboBox::ScrollWithFocus ?
  238. Qt::StrongFocus : Qt::WheelFocus );
  239. }
  240. // -------------------------------------------------------------------------
  241. void ctkComboBox::paintEvent(QPaintEvent*)
  242. {
  243. Q_D(ctkComboBox);
  244. QStylePainter painter(this);
  245. painter.setPen(palette().color(QPalette::Text));
  246. QStyleOptionComboBox opt;
  247. d->initStyleOption(&opt);
  248. // draw the combobox frame, focusrect and selected etc.
  249. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
  250. // draw the icon and text
  251. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
  252. }
  253. // -------------------------------------------------------------------------
  254. QSize ctkComboBox::minimumSizeHint() const
  255. {
  256. Q_D(const ctkComboBox);
  257. return d->recomputeSizeHint(d->MinimumSizeHint);
  258. }
  259. // -------------------------------------------------------------------------
  260. /*!
  261. \reimp
  262. This implementation caches the size hint to avoid resizing when
  263. the contents change dynamically. To invalidate the cached value
  264. change the \l sizeAdjustPolicy.
  265. */
  266. QSize ctkComboBox::sizeHint() const
  267. {
  268. Q_D(const ctkComboBox);
  269. return d->recomputeSizeHint(d->SizeHint);
  270. }
  271. // -------------------------------------------------------------------------
  272. void ctkComboBox::changeEvent(QEvent *e)
  273. {
  274. Q_D(const ctkComboBox);
  275. switch (e->type())
  276. {
  277. case QEvent::StyleChange:
  278. case QEvent::MacSizeChange:
  279. case QEvent::FontChange:
  280. d->SizeHint = QSize();
  281. d->MinimumSizeHint = QSize();
  282. break;
  283. default:
  284. break;
  285. }
  286. this->QComboBox::changeEvent(e);
  287. }
  288. // -------------------------------------------------------------------------
  289. void ctkComboBox::wheelEvent(QWheelEvent* event)
  290. {
  291. Q_D(ctkComboBox);
  292. bool scroll = false;
  293. switch (d->ScrollWheelEffect)
  294. {
  295. case AlwaysScroll:
  296. scroll = true;
  297. break;
  298. case ScrollWithFocus:
  299. scroll = this->hasFocus();
  300. break;
  301. case ScrollWithNoVScrollBar:
  302. scroll = true;
  303. for (QWidget* ancestor = this->parentWidget();
  304. ancestor; ancestor = ancestor->parentWidget())
  305. {
  306. if (QAbstractScrollArea* scrollArea =
  307. qobject_cast<QAbstractScrollArea*>(ancestor))
  308. {
  309. scroll = !scrollArea->verticalScrollBar()->isVisible();
  310. if (!scroll)
  311. {
  312. break;
  313. }
  314. }
  315. }
  316. break;
  317. default:
  318. case NeverScroll:
  319. break;
  320. }
  321. if (scroll)
  322. {
  323. this->QComboBox::wheelEvent(event);
  324. }
  325. else
  326. {
  327. event->ignore();
  328. }
  329. }
  330. // -------------------------------------------------------------------------
  331. QString ctkComboBox::currentUserDataAsString()const
  332. {
  333. return this->itemData(this->currentIndex()).toString();
  334. }
  335. // -------------------------------------------------------------------------
  336. void ctkComboBox::setCurrentUserDataAsString(QString userData)
  337. {
  338. for (int index=0; index<this->count(); ++index)
  339. {
  340. QString currentItemUserData = this->itemData(index).toString();
  341. if (!userData.compare(currentItemUserData))
  342. {
  343. this->setCurrentIndex(index);
  344. return;
  345. }
  346. }
  347. qWarning() << Q_FUNC_INFO << ": No item found with user data string " << userData;
  348. }