ctkTitleComboBox.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "ctkTitleComboBox.h"
  20. // -------------------------------------------------------------------------
  21. ctkTitleComboBox::ctkTitleComboBox(QWidget* _parent)
  22. : QComboBox(_parent)
  23. {
  24. this->Title = "Select an item...";
  25. }
  26. // -------------------------------------------------------------------------
  27. ctkTitleComboBox::~ctkTitleComboBox()
  28. {
  29. }
  30. // -------------------------------------------------------------------------
  31. void ctkTitleComboBox::setTitle(const QString& _title)
  32. {
  33. this->Title = _title;
  34. this->SizeHint = QSize();
  35. this->updateGeometry();
  36. }
  37. // -------------------------------------------------------------------------
  38. QString ctkTitleComboBox::title()const
  39. {
  40. return this->Title;
  41. }
  42. // -------------------------------------------------------------------------
  43. void ctkTitleComboBox::setIcon(const QIcon& _icon)
  44. {
  45. this->Icon = _icon;
  46. this->SizeHint = QSize();
  47. this->updateGeometry();
  48. }
  49. // -------------------------------------------------------------------------
  50. QIcon ctkTitleComboBox::icon()const
  51. {
  52. return this->Icon;
  53. }
  54. // -------------------------------------------------------------------------
  55. void ctkTitleComboBox::paintEvent(QPaintEvent*)
  56. {
  57. QStylePainter painter(this);
  58. painter.setPen(palette().color(QPalette::Text));
  59. QStyleOptionComboBox opt;
  60. initStyleOption(&opt);
  61. opt.currentText = this->Title;
  62. opt.currentIcon = this->Icon;
  63. if (!this->Icon.isNull() && !this->Icon.availableSizes().empty())
  64. {
  65. QList<QSize> sizes = this->Icon.availableSizes();
  66. // TODO: pick the best size (based on the pixelMetric maybe)
  67. //int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
  68. opt.iconSize = sizes[0];
  69. }
  70. // draw the combobox frame, focusrect and selected etc.
  71. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
  72. // draw the icon and text
  73. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
  74. }
  75. // -------------------------------------------------------------------------
  76. QSize ctkTitleComboBox::minimumSizeHint() const
  77. {
  78. return this->recomputeSizeHint(this->SizeHint);
  79. }
  80. // -------------------------------------------------------------------------
  81. /*!
  82. \reimp
  83. This implementation caches the size hint to avoid resizing when
  84. the contents change dynamically. To invalidate the cached value
  85. change the \l sizeAdjustPolicy.
  86. */
  87. QSize ctkTitleComboBox::sizeHint() const
  88. {
  89. return this->recomputeSizeHint(this->SizeHint);
  90. }
  91. // -------------------------------------------------------------------------
  92. QSize ctkTitleComboBox::recomputeSizeHint(QSize &sh) const
  93. {
  94. if (sh.isValid())
  95. {
  96. return sh.expandedTo(QApplication::globalStrut());
  97. }
  98. const QFontMetrics &fm = this->fontMetrics();
  99. QList<QSize> iconSizes = this->Icon.availableSizes();
  100. if (!this->Icon.isNull() && !iconSizes.empty())
  101. {
  102. // TODO: pick the best size (based on the pixelMetric maybe)
  103. QSize _iconSize = iconSizes[0];
  104. sh.setWidth(qMax(sh.width(), fm.boundingRect(this->Title).width() + _iconSize.width() + 4));
  105. }
  106. else
  107. {
  108. sh.setWidth(qMax(sh.width(), fm.boundingRect(this->Title).width()));
  109. }
  110. // height
  111. sh.setHeight(qMax(fm.lineSpacing(), 14) + 2);
  112. if (!this->Icon.isNull() && !iconSizes.empty())
  113. {
  114. // TODO: pick the best size (based on the pixelMetric maybe)
  115. QSize _iconSize = iconSizes[0];
  116. sh.setHeight(qMax(sh.height(), _iconSize.height() + 2));
  117. }
  118. // add style and strut values
  119. QStyleOptionComboBox opt;
  120. this->initStyleOption(&opt);
  121. sh = this->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sh, this);
  122. return sh.expandedTo(QApplication::globalStrut());
  123. }