123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- /*=========================================================================
- Library: CTK
- Copyright (c) 2010 Kitware Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.commontk.org/LICENSE
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =========================================================================*/
- // Qt includes
- #include <QStylePainter>
- #include <QApplication>
- #include <QDebug>
- // CTK includes
- #include "ctkComboBox.h"
- // -------------------------------------------------------------------------
- class ctkComboBoxPrivate: public ctkPrivate<ctkComboBox>
- {
- public:
- ctkComboBoxPrivate();
- void initStyleOption(QStyleOptionComboBox* opt)const;
- QSize recomputeSizeHint(QSize &sh) const;
- QString DefaultText;
- QIcon DefaultIcon;
- bool ForceDefault;
- mutable QSize MinimumSizeHint;
- mutable QSize SizeHint;
- };
- // -------------------------------------------------------------------------
- ctkComboBoxPrivate::ctkComboBoxPrivate()
- {
- this->DefaultText = "Select an item...";
- this->ForceDefault = false;
- }
- // -------------------------------------------------------------------------
- ctkComboBox::ctkComboBox(QWidget* _parent)
- : QComboBox(_parent)
- {
- CTK_INIT_PRIVATE(ctkComboBox);
- }
- // -------------------------------------------------------------------------
- ctkComboBox::~ctkComboBox()
- {
- }
- // -------------------------------------------------------------------------
- void ctkComboBox::setDefaultText(const QString& newDefaultText)
- {
- CTK_D(ctkComboBox);
- d->DefaultText = newDefaultText;
- d->SizeHint = QSize();
- this->updateGeometry();
- }
- // -------------------------------------------------------------------------
- QString ctkComboBox::defaultText()const
- {
- CTK_D(const ctkComboBox);
- return d->DefaultText;
- }
- // -------------------------------------------------------------------------
- void ctkComboBox::setDefaultIcon(const QIcon& newIcon)
- {
- CTK_D(ctkComboBox);
- d->DefaultIcon = newIcon;
- d->SizeHint = QSize();
- this->updateGeometry();
- }
- // -------------------------------------------------------------------------
- QIcon ctkComboBox::defaultIcon()const
- {
- CTK_D(const ctkComboBox);
- return d->DefaultIcon;
- }
- // -------------------------------------------------------------------------
- void ctkComboBox::forceDefault(bool newForceDefault)
- {
- CTK_D(ctkComboBox);
- if (newForceDefault == d->ForceDefault)
- {
- return;
- }
- d->ForceDefault = newForceDefault;
- d->SizeHint = QSize();
- this->updateGeometry();
- }
- // -------------------------------------------------------------------------
- bool ctkComboBox::isDefaultForced()const
- {
- CTK_D(const ctkComboBox);
- return d->ForceDefault;
- }
- // -------------------------------------------------------------------------
- void ctkComboBox::paintEvent(QPaintEvent*)
- {
- CTK_D(ctkComboBox);
- QStylePainter painter(this);
- painter.setPen(palette().color(QPalette::Text));
- QStyleOptionComboBox opt;
- d->initStyleOption(&opt);
- // draw the combobox frame, focusrect and selected etc.
- painter.drawComplexControl(QStyle::CC_ComboBox, opt);
- // draw the icon and text
- painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
- }
- // -------------------------------------------------------------------------
- QSize ctkComboBox::minimumSizeHint() const
- {
- CTK_D(const ctkComboBox);
- return d->recomputeSizeHint(d->MinimumSizeHint);
- }
- // -------------------------------------------------------------------------
- /*!
- \reimp
- This implementation caches the size hint to avoid resizing when
- the contents change dynamically. To invalidate the cached value
- change the \l sizeAdjustPolicy.
- */
- QSize ctkComboBox::sizeHint() const
- {
- CTK_D(const ctkComboBox);
- return d->recomputeSizeHint(d->SizeHint);
- }
- // -------------------------------------------------------------------------
- QSize ctkComboBoxPrivate::recomputeSizeHint(QSize &sh) const
- {
- CTK_P(const ctkComboBox);
- if (sh.isValid())
- {
- return sh.expandedTo(QApplication::globalStrut());
- }
- bool hasIcon = false;
- int count = p->count();
- QSize iconSize = p->iconSize();
- const QFontMetrics &fm = p->fontMetrics();
- // text width
- if (&sh == &this->SizeHint || p->minimumContentsLength() == 0)
- {
- switch (p->sizeAdjustPolicy())
- {
- case QComboBox::AdjustToContents:
- case QComboBox::AdjustToContentsOnFirstShow:
- if (count == 0 || this->ForceDefault)
- {
- sh.rwidth() = this->DefaultText.isEmpty() ?
- 7 * fm.width(QLatin1Char('x')) :
- fm.boundingRect(this->DefaultText).width();
- if (!this->DefaultIcon.isNull())
- {
- hasIcon = true;
- sh.rwidth() += iconSize.width() + 4;
- }
- }
- for (int i = 0; i < count; ++i)
- {
- if (!p->itemIcon(i).isNull())
- {
- hasIcon = true;
- sh.setWidth(qMax(sh.width(), fm.boundingRect(p->itemText(i)).width() + iconSize.width() + 4));
- }
- else
- {
- sh.setWidth(qMax(sh.width(), fm.boundingRect(p->itemText(i)).width()));
- }
- }
- break;
- case QComboBox::AdjustToMinimumContentsLength:
- if ((count == 0 || this->ForceDefault) && !this->DefaultIcon.isNull())
- {
- hasIcon = true;
- }
- for (int i = 0; i < count && !hasIcon; ++i)
- {
- hasIcon = !p->itemIcon(i).isNull();
- }
- break;
- case QComboBox::AdjustToMinimumContentsLengthWithIcon:
- hasIcon = true;
- break;
- default:
- break;
- }
- }
- else // minimumsizehint is computing and minimumcontentslenght is > 0
- {
- if ((count == 0 || this->ForceDefault) && !this->DefaultIcon.isNull())
- {
- hasIcon = true;
- }
- for (int i = 0; i < count && !hasIcon; ++i)
- {
- hasIcon = !p->itemIcon(i).isNull();
- }
- }
- if (p->minimumContentsLength() > 0)
- {
- sh.setWidth(qMax(sh.width(),
- p->minimumContentsLength() * fm.width(QLatin1Char('X'))
- + (hasIcon ? iconSize.width() + 4 : 0)));
- }
- // height
- sh.setHeight(qMax(fm.height(), 14) + 2);
- if (hasIcon)
- {
- sh.setHeight(qMax(sh.height(), iconSize.height() + 2));
- }
- // add style and strut values
- QStyleOptionComboBox opt;
- this->initStyleOption(&opt);
- sh = p->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sh, p);
- return sh.expandedTo(QApplication::globalStrut());
- }
- // -------------------------------------------------------------------------
- void ctkComboBoxPrivate::initStyleOption(QStyleOptionComboBox* opt)const
- {
- CTK_P(const ctkComboBox);
- p->initStyleOption(opt);
- if (p->currentIndex() == -1 ||
- this->ForceDefault)
- {
- opt->currentText = this->DefaultText;
- opt->currentIcon = this->DefaultIcon;
- }
- }
|