File indexing completed on 2024-04-28 07:52:03
0001 // 0002 // C++ Implementation: cgaugebaritem 0003 // 0004 // Description: 0005 // 0006 /* 0007 Copyright 2004-2011 Tomas Mecir <kmuddy@kmuddy.com> 0008 0009 This program is free software; you can redistribute it and/or 0010 modify it under the terms of the GNU General Public License as 0011 published by the Free Software Foundation; either version 2 of 0012 the License, or (at your option) any later version. 0013 0014 This program is distributed in the hope that it will be useful, 0015 but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 GNU General Public License for more details. 0018 0019 You should have received a copy of the GNU General Public License 0020 along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 */ 0022 0023 #include "cgaugebaritem.h" 0024 0025 #include <qlabel.h> 0026 #include <qpainter.h> 0027 #include <QHBoxLayout> 0028 0029 cGaugeBarItem::cGaugeBarItem (QWidget *parent) 0030 : QWidget(parent) 0031 { 0032 QHBoxLayout *layout = new QHBoxLayout (this); 0033 layout->setSpacing (2); 0034 setContentsMargins (0, 0, 0, 0); 0035 setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Preferred); 0036 label = new QLabel (this); 0037 label->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed); 0038 gaugepriv = new cGaugeBarItemPrivate (this); 0039 layout->addWidget (label); 0040 layout->addWidget (gaugepriv); 0041 } 0042 0043 cGaugeBarItem::~cGaugeBarItem () 0044 { 0045 } 0046 0047 void cGaugeBarItem::setText (const QString &caption) 0048 { 0049 label->setText (caption); 0050 } 0051 0052 QString cGaugeBarItem::text () const 0053 { 0054 return label->text(); 0055 } 0056 0057 void cGaugeBarItem::setValue (int value) 0058 { 0059 gaugepriv->setValue (value); 0060 } 0061 0062 int cGaugeBarItem::value () const 0063 { 0064 return gaugepriv->value(); 0065 } 0066 0067 void cGaugeBarItem::setColor (const QColor &color) 0068 { 0069 gaugepriv->setColor (color); 0070 } 0071 0072 QColor cGaugeBarItem::color () const 0073 { 0074 return gaugepriv->color (); 0075 } 0076 0077 cGaugeBarItemPrivate::cGaugeBarItemPrivate (QWidget *parent) 0078 : QWidget(parent) 0079 { 0080 val = 0; 0081 col = Qt::white; 0082 0083 setMaximumWidth (102); 0084 setFocusPolicy (Qt::NoFocus); 0085 } 0086 0087 cGaugeBarItemPrivate::~cGaugeBarItemPrivate () 0088 { 0089 0090 } 0091 0092 void cGaugeBarItemPrivate::setValue (int value) 0093 { 0094 val = value; 0095 update (); 0096 } 0097 0098 int cGaugeBarItemPrivate::value () const 0099 { 0100 return val; 0101 } 0102 0103 void cGaugeBarItemPrivate::setColor (const QColor &color) 0104 { 0105 col = color; 0106 update (); 0107 } 0108 0109 QColor cGaugeBarItemPrivate::color () const 0110 { 0111 return col; 0112 } 0113 0114 void cGaugeBarItemPrivate::paintEvent (QPaintEvent *) 0115 { 0116 int w = width(); 0117 int h = height(); 0118 QPainter p (this); 0119 p.setPen (QPen (palette().dark(), 1)); 0120 p.drawRect (0, 0, w - 1, h - 1); 0121 if (val) 0122 { 0123 w = val * (w - 2) / 100; //width of the colored part 0124 p.fillRect (1, 1, w, h - 2, col); 0125 } 0126 } 0127 0128 QSize cGaugeBarItemPrivate::sizeHint () const { 0129 return QSize (102, 16); 0130 } 0131 0132 #include "moc_cgaugebaritem.cpp"