ctkComboBox.cpp 8.3 KB

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