ctkComboBox.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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.commontk.org/LICENSE
  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 <QStylePainter>
  16. #include <QApplication>
  17. #include <QDebug>
  18. // CTK includes
  19. #include "ctkComboBox.h"
  20. // -------------------------------------------------------------------------
  21. class ctkComboBoxPrivate: public ctkPrivate<ctkComboBox>
  22. {
  23. public:
  24. ctkComboBoxPrivate();
  25. void initStyleOption(QStyleOptionComboBox* opt)const;
  26. QSize recomputeSizeHint(QSize &sh) const;
  27. QString DefaultText;
  28. QIcon DefaultIcon;
  29. bool ForceDefault;
  30. mutable QSize MinimumSizeHint;
  31. mutable QSize SizeHint;
  32. };
  33. // -------------------------------------------------------------------------
  34. ctkComboBoxPrivate::ctkComboBoxPrivate()
  35. {
  36. this->DefaultText = "Select an item...";
  37. this->ForceDefault = false;
  38. }
  39. // -------------------------------------------------------------------------
  40. ctkComboBox::ctkComboBox(QWidget* _parent)
  41. : QComboBox(_parent)
  42. {
  43. CTK_INIT_PRIVATE(ctkComboBox);
  44. }
  45. // -------------------------------------------------------------------------
  46. ctkComboBox::~ctkComboBox()
  47. {
  48. }
  49. // -------------------------------------------------------------------------
  50. void ctkComboBox::setDefaultText(const QString& newDefaultText)
  51. {
  52. CTK_D(ctkComboBox);
  53. d->DefaultText = newDefaultText;
  54. d->SizeHint = QSize();
  55. this->updateGeometry();
  56. }
  57. // -------------------------------------------------------------------------
  58. QString ctkComboBox::defaultText()const
  59. {
  60. CTK_D(const ctkComboBox);
  61. return d->DefaultText;
  62. }
  63. // -------------------------------------------------------------------------
  64. void ctkComboBox::setDefaultIcon(const QIcon& newIcon)
  65. {
  66. CTK_D(ctkComboBox);
  67. d->DefaultIcon = newIcon;
  68. d->SizeHint = QSize();
  69. this->updateGeometry();
  70. }
  71. // -------------------------------------------------------------------------
  72. QIcon ctkComboBox::defaultIcon()const
  73. {
  74. CTK_D(const ctkComboBox);
  75. return d->DefaultIcon;
  76. }
  77. // -------------------------------------------------------------------------
  78. void ctkComboBox::forceDefault(bool newForceDefault)
  79. {
  80. CTK_D(ctkComboBox);
  81. if (newForceDefault == d->ForceDefault)
  82. {
  83. return;
  84. }
  85. d->ForceDefault = newForceDefault;
  86. d->SizeHint = QSize();
  87. this->updateGeometry();
  88. }
  89. // -------------------------------------------------------------------------
  90. bool ctkComboBox::isDefaultForced()const
  91. {
  92. CTK_D(const ctkComboBox);
  93. return d->ForceDefault;
  94. }
  95. // -------------------------------------------------------------------------
  96. void ctkComboBox::paintEvent(QPaintEvent*)
  97. {
  98. CTK_D(ctkComboBox);
  99. QStylePainter painter(this);
  100. painter.setPen(palette().color(QPalette::Text));
  101. QStyleOptionComboBox opt;
  102. d->initStyleOption(&opt);
  103. // draw the combobox frame, focusrect and selected etc.
  104. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
  105. // draw the icon and text
  106. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
  107. }
  108. // -------------------------------------------------------------------------
  109. QSize ctkComboBox::minimumSizeHint() const
  110. {
  111. CTK_D(const ctkComboBox);
  112. return d->recomputeSizeHint(d->MinimumSizeHint);
  113. }
  114. // -------------------------------------------------------------------------
  115. /*!
  116. \reimp
  117. This implementation caches the size hint to avoid resizing when
  118. the contents change dynamically. To invalidate the cached value
  119. change the \l sizeAdjustPolicy.
  120. */
  121. QSize ctkComboBox::sizeHint() const
  122. {
  123. CTK_D(const ctkComboBox);
  124. return d->recomputeSizeHint(d->SizeHint);
  125. }
  126. // -------------------------------------------------------------------------
  127. QSize ctkComboBoxPrivate::recomputeSizeHint(QSize &sh) const
  128. {
  129. CTK_P(const ctkComboBox);
  130. if (sh.isValid())
  131. {
  132. return sh.expandedTo(QApplication::globalStrut());
  133. }
  134. bool hasIcon = false;
  135. int count = p->count();
  136. QSize iconSize = p->iconSize();
  137. const QFontMetrics &fm = p->fontMetrics();
  138. // text width
  139. if (&sh == &this->SizeHint || p->minimumContentsLength() == 0)
  140. {
  141. switch (p->sizeAdjustPolicy())
  142. {
  143. case QComboBox::AdjustToContents:
  144. case QComboBox::AdjustToContentsOnFirstShow:
  145. if (count == 0 || this->ForceDefault)
  146. {
  147. sh.rwidth() = this->DefaultText.isEmpty() ?
  148. 7 * fm.width(QLatin1Char('x')) :
  149. fm.boundingRect(this->DefaultText).width();
  150. if (!this->DefaultIcon.isNull())
  151. {
  152. hasIcon = true;
  153. sh.rwidth() += iconSize.width() + 4;
  154. }
  155. }
  156. for (int i = 0; i < count; ++i)
  157. {
  158. if (!p->itemIcon(i).isNull())
  159. {
  160. hasIcon = true;
  161. sh.setWidth(qMax(sh.width(), fm.boundingRect(p->itemText(i)).width() + iconSize.width() + 4));
  162. }
  163. else
  164. {
  165. sh.setWidth(qMax(sh.width(), fm.boundingRect(p->itemText(i)).width()));
  166. }
  167. }
  168. break;
  169. case QComboBox::AdjustToMinimumContentsLength:
  170. if ((count == 0 || this->ForceDefault) && !this->DefaultIcon.isNull())
  171. {
  172. hasIcon = true;
  173. }
  174. for (int i = 0; i < count && !hasIcon; ++i)
  175. {
  176. hasIcon = !p->itemIcon(i).isNull();
  177. }
  178. break;
  179. case QComboBox::AdjustToMinimumContentsLengthWithIcon:
  180. hasIcon = true;
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. else // minimumsizehint is computing and minimumcontentslenght is > 0
  187. {
  188. if ((count == 0 || this->ForceDefault) && !this->DefaultIcon.isNull())
  189. {
  190. hasIcon = true;
  191. }
  192. for (int i = 0; i < count && !hasIcon; ++i)
  193. {
  194. hasIcon = !p->itemIcon(i).isNull();
  195. }
  196. }
  197. if (p->minimumContentsLength() > 0)
  198. {
  199. sh.setWidth(qMax(sh.width(),
  200. p->minimumContentsLength() * fm.width(QLatin1Char('X'))
  201. + (hasIcon ? iconSize.width() + 4 : 0)));
  202. }
  203. // height
  204. sh.setHeight(qMax(fm.height(), 14) + 2);
  205. if (hasIcon)
  206. {
  207. sh.setHeight(qMax(sh.height(), iconSize.height() + 2));
  208. }
  209. // add style and strut values
  210. QStyleOptionComboBox opt;
  211. this->initStyleOption(&opt);
  212. sh = p->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sh, p);
  213. return sh.expandedTo(QApplication::globalStrut());
  214. }
  215. // -------------------------------------------------------------------------
  216. void ctkComboBoxPrivate::initStyleOption(QStyleOptionComboBox* opt)const
  217. {
  218. CTK_P(const ctkComboBox);
  219. p->initStyleOption(opt);
  220. if (p->currentIndex() == -1 ||
  221. this->ForceDefault)
  222. {
  223. opt->currentText = this->DefaultText;
  224. opt->currentIcon = this->DefaultIcon;
  225. }
  226. }