ctkTitleComboBox.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // Qt includes
00022 #include <QStylePainter>
00023 #include <QApplication>
00024 #include <QDebug>
00025 
00026 // CTK includes
00027 #include "ctkTitleComboBox.h"
00028 
00029 // -------------------------------------------------------------------------
00030 ctkTitleComboBox::ctkTitleComboBox(QWidget* _parent)
00031   : QComboBox(_parent)
00032 {
00033   this->Title = "Select an item...";
00034 }
00035 
00036 // -------------------------------------------------------------------------
00037 ctkTitleComboBox::~ctkTitleComboBox()
00038 {
00039   
00040 }
00041 
00042 // -------------------------------------------------------------------------
00043 void ctkTitleComboBox::setTitle(const QString& _title)
00044 {
00045   this->Title = _title;
00046   this->SizeHint = QSize();
00047   this->updateGeometry();
00048 }
00049 
00050 // -------------------------------------------------------------------------
00051 QString ctkTitleComboBox::title()const
00052 {
00053   return this->Title;
00054 }
00055 
00056 // -------------------------------------------------------------------------
00057 void ctkTitleComboBox::setIcon(const QIcon& _icon)
00058 {
00059   this->Icon = _icon;
00060   this->SizeHint = QSize();
00061   this->updateGeometry();
00062 }
00063 
00064 // -------------------------------------------------------------------------
00065 QIcon ctkTitleComboBox::icon()const
00066 {
00067   return this->Icon;
00068 }
00069 
00070 // -------------------------------------------------------------------------
00071 void ctkTitleComboBox::paintEvent(QPaintEvent*)
00072 {
00073   QStylePainter painter(this);
00074   painter.setPen(palette().color(QPalette::Text));
00075 
00076   QStyleOptionComboBox opt;
00077   initStyleOption(&opt);
00078 
00079   opt.currentText = this->Title;
00080   opt.currentIcon = this->Icon;
00081   if (!this->Icon.isNull() && !this->Icon.availableSizes().empty())
00082     {
00083     QList<QSize> sizes = this->Icon.availableSizes();
00084     // TODO: pick the best size (based on the pixelMetric maybe)
00085     //int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
00086     opt.iconSize = sizes[0];
00087     }
00088 
00089   // draw the combobox frame, focusrect and selected etc.
00090   painter.drawComplexControl(QStyle::CC_ComboBox, opt);
00091   // draw the icon and text
00092   painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
00093 }
00094 
00095 // -------------------------------------------------------------------------
00096 QSize ctkTitleComboBox::minimumSizeHint() const
00097 {
00098   return this->recomputeSizeHint(this->SizeHint);
00099 }
00100 
00101 // -------------------------------------------------------------------------
00109 QSize ctkTitleComboBox::sizeHint() const
00110 {
00111   return this->recomputeSizeHint(this->SizeHint);
00112 }
00113 
00114 // -------------------------------------------------------------------------
00115 QSize ctkTitleComboBox::recomputeSizeHint(QSize &sh) const
00116 {
00117   if (sh.isValid()) 
00118     {
00119     return sh.expandedTo(QApplication::globalStrut());
00120     }
00121   const QFontMetrics &fm = this->fontMetrics();
00122   QList<QSize> iconSizes = this->Icon.availableSizes();
00123   if (!this->Icon.isNull() && !iconSizes.empty()) 
00124     {
00125     // TODO: pick the best size (based on the pixelMetric maybe)
00126     QSize _iconSize = iconSizes[0];
00127     sh.setWidth(qMax(sh.width(), fm.boundingRect(this->Title).width() + _iconSize.width() + 4));
00128     }
00129   else
00130     {
00131     sh.setWidth(qMax(sh.width(), fm.boundingRect(this->Title).width()));
00132     }
00133 
00134   // height
00135   sh.setHeight(qMax(fm.lineSpacing(), 14) + 2);
00136   if (!this->Icon.isNull() && !iconSizes.empty()) 
00137     {
00138     // TODO: pick the best size (based on the pixelMetric maybe)
00139     QSize _iconSize = iconSizes[0];
00140     sh.setHeight(qMax(sh.height(), _iconSize.height() + 2));
00141     }
00142 
00143   // add style and strut values
00144   QStyleOptionComboBox opt;
00145   this->initStyleOption(&opt);
00146   sh = this->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sh, this);
00147 
00148   return sh.expandedTo(QApplication::globalStrut());
00149 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1