Test change - can I push OK?
[kdeedu-porting.git] / kalzium / src / legendwidget.cpp
blobeffd138db65b1bf543a7ec6357cad6a711ba78dd
1 /***************************************************************************
2 * Copyright (C) 2007 by Carsten Niehaus *
3 * cniehaus@kde.org *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
20 #include "legendwidget.h"
21 #include "prefs.h"
22 #include "kalziumschemetype.h"
23 #include "kalziumgradienttype.h"
24 #include "kalziumpainter.h"
26 #include <kdebug.h>
27 #include <klocale.h>
29 #include <QPainter>
30 #include <QBrush>
31 #include <QGridLayout>
33 LegendWidget::LegendWidget( QWidget *parent )
34 : QWidget( parent )
36 setMinimumHeight(80);
39 void LegendWidget::setGradientType( KalziumGradientType * type )
41 kDebug() << "setGradientType";
42 m_gradientType = type;
43 updateContent();
45 void LegendWidget::setTableType( KalziumTableType * type )
47 m_tableType = type;
48 updateContent();
51 void LegendWidget::setScheme( KalziumSchemeType * type )
53 m_scheme = type;
54 updateContent();
57 void LegendWidget::setMode( KalziumPainter::MODE m )
59 m_mode = m;
60 updateContent();
63 void LegendWidget::updateContent()
65 QList< QPair<QString, QBrush> > items;
66 switch ( m_mode )
68 case KalziumPainter::NORMAL://nothing to do here, all logic done in SOM
69 case KalziumPainter::SOM:
71 if ( !m_scheme ) return;
73 QList<legendPair> items;
74 if ( m_mode == KalziumPainter::SOM )
76 items << qMakePair( i18nc("one of the three states of matter (solid, liquid, vaporous or unknown)", "Solid" ), QBrush( Prefs::color_solid() ) );
77 items << qMakePair( i18nc("one of the three states of matter (solid, liquid, vaporous or unknown)", "Liquid" ), QBrush( Prefs::color_liquid() ) );
78 items << qMakePair( i18nc("one of the three states of matter (solid, liquid, vaporous or unknown)", "Vaporous" ), QBrush( Prefs::color_vapor() ) );
79 items << qMakePair( i18nc("one of the three states of matter (solid, liquid, vaporous or unknown)", "Unknown" ), QBrush( Qt::lightGray ) );
80 } else {
81 items = m_scheme->legendItems();
84 updateLegendItemLayout( items );
86 break;
88 case KalziumPainter::GRADIENT:
90 QList<legendPair> items;
91 QString gradientDesc;
92 if(m_gradientType->logarithmicGradient())
93 gradientDesc = i18nc("one of the two types of gradients available", "logarithmic");
94 else
95 gradientDesc = i18nc("one of the two types of gradients available", "linear");
96 items << qMakePair( i18n( "Gradient: %1 (%2)" ,m_gradientType->description(), gradientDesc ), QBrush() );
97 items << qMakePair( i18nc( "Minimum value of the gradient" , "Minimum: %1" , m_gradientType->minValue() ), QBrush( m_gradientType->firstColor() ) );
98 items << qMakePair( i18nc( "Maximum value of the gradient" , "Maximum: %1" , m_gradientType->maxValue() ), QBrush( m_gradientType->secondColor() ) );
100 updateLegendItemLayout( items );
101 break;
103 case KalziumPainter::TIME:
105 break;
110 void LegendWidget::updateLegendItemLayout( const QList<legendPair>& list )
112 if (layout()) {
113 delete layout();
115 foreach ( LegendItem * i, m_legendItemList ) {
116 delete i;
119 m_legendItemList.clear();
121 QGridLayout * layout = new QGridLayout( this );
123 int x = 0;
124 int y = 0;
126 foreach ( const legendPair &pair, list )
128 LegendItem *item = new LegendItem( pair );
130 m_legendItemList.append(item);
132 layout->addWidget(item , x, y );
134 x++;
136 if ( x >= 4 ) {
137 x = 0;
138 y++;
142 setLayout( layout );
145 LegendItem::LegendItem(const QPair<QString, QBrush>& pair, QWidget * parent)
147 Q_UNUSED(parent);
148 m_pair = pair;
150 update();
153 void LegendItem::paintEvent( QPaintEvent * /* e */ )
155 QPainter p;
156 p.begin(this);
157 QRect rect(0, 0, height(), height() );
158 p.fillRect( rect , QBrush( m_pair.second ) );
160 QRect textRect( height() + 10 , 0 , width() - 10 , height() );
162 p.drawText( textRect, Qt::AlignLeft | Qt::AlignVCenter , m_pair.first );
163 p.end();
166 #include "legendwidget.moc"