ctkTitleComboBox.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. // Qt includes
  11. #include <QStylePainter>
  12. #include <QApplication>
  13. #include <QDebug>
  14. // CTK includes
  15. #include "ctkTitleComboBox.h"
  16. // -------------------------------------------------------------------------
  17. ctkTitleComboBox::ctkTitleComboBox(QWidget* _parent)
  18. : QComboBox(_parent)
  19. {
  20. this->Title = "Select an item...";
  21. }
  22. // -------------------------------------------------------------------------
  23. ctkTitleComboBox::~ctkTitleComboBox()
  24. {
  25. }
  26. // -------------------------------------------------------------------------
  27. void ctkTitleComboBox::setTitle(const QString& _title)
  28. {
  29. this->Title = _title;
  30. this->SizeHint = QSize();
  31. this->updateGeometry();
  32. }
  33. // -------------------------------------------------------------------------
  34. QString ctkTitleComboBox::title()const
  35. {
  36. return this->Title;
  37. }
  38. // -------------------------------------------------------------------------
  39. void ctkTitleComboBox::setIcon(const QIcon& _icon)
  40. {
  41. this->Icon = _icon;
  42. this->SizeHint = QSize();
  43. this->updateGeometry();
  44. }
  45. // -------------------------------------------------------------------------
  46. QIcon ctkTitleComboBox::icon()const
  47. {
  48. return this->Icon;
  49. }
  50. // -------------------------------------------------------------------------
  51. void ctkTitleComboBox::paintEvent(QPaintEvent*)
  52. {
  53. QStylePainter painter(this);
  54. painter.setPen(palette().color(QPalette::Text));
  55. QStyleOptionComboBox opt;
  56. initStyleOption(&opt);
  57. opt.currentText = this->Title;
  58. opt.currentIcon = this->Icon;
  59. if (!this->Icon.isNull() && !this->Icon.availableSizes().empty())
  60. {
  61. QList<QSize> sizes = this->Icon.availableSizes();
  62. // TODO: pick the best size (based on the pixelMetric maybe)
  63. //int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
  64. opt.iconSize = sizes[0];
  65. }
  66. // draw the combobox frame, focusrect and selected etc.
  67. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
  68. // draw the icon and text
  69. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
  70. }
  71. // -------------------------------------------------------------------------
  72. QSize ctkTitleComboBox::minimumSizeHint() const
  73. {
  74. return this->recomputeSizeHint(this->SizeHint);
  75. }
  76. // -------------------------------------------------------------------------
  77. /*!
  78. \reimp
  79. This implementation caches the size hint to avoid resizing when
  80. the contents change dynamically. To invalidate the cached value
  81. change the \l sizeAdjustPolicy.
  82. */
  83. QSize ctkTitleComboBox::sizeHint() const
  84. {
  85. return this->recomputeSizeHint(this->SizeHint);
  86. }
  87. // -------------------------------------------------------------------------
  88. QSize ctkTitleComboBox::recomputeSizeHint(QSize &sh) const
  89. {
  90. if (sh.isValid())
  91. {
  92. return sh.expandedTo(QApplication::globalStrut());
  93. }
  94. const QFontMetrics &fm = this->fontMetrics();
  95. QList<QSize> iconSizes = this->Icon.availableSizes();
  96. if (!this->Icon.isNull() && !iconSizes.empty())
  97. {
  98. // TODO: pick the best size (based on the pixelMetric maybe)
  99. QSize _iconSize = iconSizes[0];
  100. sh.setWidth(qMax(sh.width(), fm.boundingRect(this->Title).width() + _iconSize.width() + 4));
  101. }
  102. else
  103. {
  104. sh.setWidth(qMax(sh.width(), fm.boundingRect(this->Title).width()));
  105. }
  106. // height
  107. sh.setHeight(qMax(fm.lineSpacing(), 14) + 2);
  108. if (!this->Icon.isNull() && !iconSizes.empty())
  109. {
  110. // TODO: pick the best size (based on the pixelMetric maybe)
  111. QSize _iconSize = iconSizes[0];
  112. sh.setHeight(qMax(sh.height(), _iconSize.height() + 2));
  113. }
  114. // add style and strut values
  115. QStyleOptionComboBox opt;
  116. this->initStyleOption(&opt);
  117. sh = this->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sh, this);
  118. return sh.expandedTo(QApplication::globalStrut());
  119. }