kchart

kchartLegendConfigPage.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001,2002,2003,2004 Laurent Montel <montel@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kchartLegendConfigPage.h"
00021 #include "kchartLegendConfigPage.moc"
00022 
00023 #include <kapplication.h>
00024 #include <klocale.h>
00025 #include <kcolorbutton.h>
00026 #include <kfontdialog.h>
00027 #include <kiconloader.h>
00028 
00029 #include <qlabel.h>
00030 #include <qbuttongroup.h>
00031 #include <qvbuttongroup.h>
00032 #include <qradiobutton.h>
00033 #include <qlineedit.h>
00034 #include <qtooltip.h> 
00035 #include <qwhatsthis.h>
00036 
00037 #include "kchart_params.h"
00038 #include "kchart_factory.h"
00039 
00040 #include "KDFrameProfileSection.h"
00041 
00042 
00043 namespace KChart
00044 {
00045 
00046 
00047 KChartLegendConfigPage::KChartLegendConfigPage( KChartParams* params,
00048                                                 QWidget* parent ) :
00049     QWidget( parent ),_params( params )
00050 {
00051   //Layout for 4 blocks
00052   QGridLayout* layout = new QGridLayout( this, 3, 2, KDialog::marginHint(), KDialog::spacingHint() );
00053 
00054   //1. Block: General settings
00055   QButtonGroup* gb = new QButtonGroup( 0, Qt::Vertical, i18n("General"), this );
00056   gb->layout()->setSpacing(KDialog::spacingHint());
00057   gb->layout()->setMargin(KDialog::marginHint());
00058   layout->addWidget( gb, 0, 0 );
00059 
00060   QGridLayout *grid2 = new QGridLayout( gb->layout(), 4, 2 );
00061 
00062   QLabel* lab = new QLabel( i18n("Title:"), gb );
00063   QWhatsThis::add(lab, i18n("Write here the title of the legend, which is displayed at the top of the legend box."));
00064   grid2->addWidget( lab, 0, 0 );
00065 
00066   title = new QLineEdit( gb );
00067   grid2->addWidget( title, 0, 1 );
00068 
00069   // 2. Block: Legend position
00070   gb = new QButtonGroup( 0, Qt::Vertical, i18n("Legend Position"), this );
00071   QWhatsThis::add(gb, i18n("<qt>Choose the location of the legend on the chart by "
00072       "clicking a location button.\nUse the central button to <b>hide</b> the legend.</qt>"));
00073   gb->layout()->setSpacing(KDialog::spacingHint());
00074   gb->layout()->setMargin(KDialog::marginHint());
00075   gb->setExclusive( true );
00076 
00077   QGridLayout *grid1 = new QGridLayout( gb->layout(), 5, 5 );
00078 
00079   lTopLeft          = addButton( grid1, gb, i18n("Top-Left"),      "chart_legend_topleft",     0, 0 );
00080   lTopLeftTop       = addButton( grid1, gb, i18n("Top-Left-Top"),  "chart_legend_toplefttop",  0, 1 );
00081   lTop              = addButton( grid1, gb, i18n("Top"),           "chart_legend_top",         0, 2 );
00082   lTopRightTop      = addButton( grid1, gb, i18n("Top-Right-Top"), "chart_legend_toprighttop", 0, 3 );
00083   lTopRight         = addButton( grid1, gb, i18n("Top-Right"),     "chart_legend_topright",    0, 4 );
00084 
00085   lTopLeftLeft      = addButton( grid1, gb, i18n("Top-Left-Left"),   "chart_legend_topleftleft",   1, 0 );
00086   lTopRightRight    = addButton( grid1, gb, i18n("Top-Right-Right"), "chart_legend_toprightright", 1, 4 );
00087 
00088   lLeft             = addButton( grid1, gb, i18n("Left"),      "chart_legend_left",     2, 0 );
00089   noLegend          = addButton( grid1, gb, i18n("No Legend"), "chart_legend_nolegend", 2, 2 );
00090   lRight            = addButton( grid1, gb, i18n("Right"),     "chart_legend_right",    2, 4 );
00091 
00092   lBottomLeftLeft   = addButton( grid1, gb, i18n("Bottom-Left-Left"),    "chart_legend_bottomleftleft",   3, 0 );
00093   lBottomRightRight = addButton( grid1, gb, i18n("Bottom-Right-Right"),  "chart_legend_bottomrightright", 3, 4 );
00094 
00095   lBottomLeft       = addButton( grid1, gb, i18n("Bottom-Left"),         "chart_legend_bottomleft",       4, 0 );
00096   lBottomLeftBottom = addButton( grid1, gb, i18n("Bottom-Left-Bottom"),  "chart_legend_bottomleftbottom", 4, 1 );
00097   lBottom           = addButton( grid1, gb, i18n("Bottom"),              "chart_legend_bottom",           4, 2 );
00098   lBottomRightBottom= addButton( grid1, gb, i18n("Bottom-Right-Bottom"), "chart_legend_bottomrightbottom",4, 3 );
00099   lBottomRight      = addButton( grid1, gb, i18n("Bottom-Right"),        "chart_legend_bottomright",      4, 4 );
00100 
00101   gb->setAlignment( Qt::AlignLeft );
00102   layout->addMultiCellWidget( gb, 1,2, 0,0 );
00103 
00104   // 3. Block: Font
00105   gb = new QButtonGroup( 0, Qt::Vertical, i18n("Font"), this );
00106   QWhatsThis::add(gb, i18n("This Font box can be used to set different fonts for the legend title and the individual entries."));
00107   gb->layout()->setSpacing(KDialog::spacingHint());
00108   gb->layout()->setMargin(KDialog::marginHint());
00109   layout->addWidget( gb, 0, 1 );
00110 
00111   QGridLayout *grid4 = new QGridLayout( gb->layout(), 4, 2 );
00112   titleLegendFontButton = new QPushButton( gb );
00113 
00114   lab = new QLabel( i18n("Legend title font:"), gb );
00115   grid4->addWidget( lab, 0 ,0 );
00116 
00117   titleLegendFontButton->setText( i18n("Select Font...") );
00118   QWhatsThis::add(titleLegendFontButton, i18n("Click here to display the KDE font chooser dialog. You will be able to change the font family, style and size for the legend title."));
00119   grid4->addWidget( titleLegendFontButton, 1, 0 );
00120 
00121   lab = new QLabel( i18n("Legend text font:"), gb );
00122   grid4->addWidget( lab, 2, 0 );
00123   textLegendFontButton = new QPushButton( gb );
00124   textLegendFontButton->setText( i18n("Select Font...") );
00125   QWhatsThis::add(textLegendFontButton, i18n("Click here to display the KDE font chooser dialog. You will be able to change the font family, style and size for the legend text."));
00126   grid4->addWidget( textLegendFontButton, 3, 0 );
00127 
00128   connect( titleLegendFontButton, SIGNAL(clicked()), 
00129        this, SLOT(changeTitleLegendFont()));
00130   connect( textLegendFontButton, SIGNAL(clicked()),
00131        this, SLOT(changeTextLegendFont()));
00132 
00133   // 4. Block: Orientation
00134   orientationGroup = new QVButtonGroup( i18n("Orientation"), this );
00135   QWhatsThis::add(orientationGroup, i18n("Select, if the legend's items should be drawn next to each other, or below each other."));
00136   orientationGroup->setRadioButtonExclusive(true);
00137   orientationGroup->layout()->setSpacing(KDialog::spacingHint());
00138   orientationGroup->layout()->setMargin(KDialog::marginHint());
00139   layout->addWidget( orientationGroup, 1, 1 );
00140 
00141   QRadioButton* orientation = new QRadioButton( i18n("Vertically"), orientationGroup );
00142   orientation = new QRadioButton( i18n("Horizontally"), orientationGroup );
00143   Q_UNUSED(orientation);
00144 
00145   // 5. Block: Text Colors
00146   gb = new QButtonGroup( 0, Qt::Vertical, i18n("Color"), this );
00147   QWhatsThis::add(gb, i18n("This Color box can be used to set different colors for the legend title and text."));
00148   gb->layout()->setSpacing(KDialog::spacingHint());
00149   gb->layout()->setMargin(KDialog::marginHint());
00150   layout->addWidget( gb, 2, 1 );
00151 
00152   QGridLayout *grid3 = new QGridLayout( gb->layout(), 6, 2 );
00153 
00154   lab = new QLabel( i18n("Legend title color:"), gb );
00155   grid3->addWidget( lab, 0, 0 );
00156 
00157   legendTitleColor = new KColorButton( gb );
00158   QWhatsThis::add(legendTitleColor, i18n("Click here to display the KDE Select Color dialog. You will be able to change the color for the legend title."));
00159   grid3->addWidget( legendTitleColor, 1, 0 );
00160 
00161 
00162   lab = new QLabel( i18n("Legend text color:"), gb );
00163   grid3->addWidget( lab, 2, 0 );
00164 
00165   legendTextColor = new KColorButton( gb );
00166   QWhatsThis::add(legendTextColor, i18n("Click here to display the KDE Select Color dialog. You will be able to change the color for the legend text."));
00167   grid3->addWidget( legendTextColor, 3, 0 );
00168 
00169 
00170   lab = new QLabel( i18n("Legend frame color:"), gb );
00171   grid3->addWidget( lab, 4, 0 );
00172 
00173   legendFrameColor = new KColorButton( gb );
00174   QWhatsThis::add(legendFrameColor, i18n("Click here to display the KDE Select Color dialog. You will be able to change the color for the legend frame."));
00175 
00176   grid3->addWidget( legendFrameColor, 5, 0 );
00177 
00178   //it's not good but I don't know how
00179   //to reduce space
00180   layout->addColSpacing( 1, 300 );
00181 }
00182 
00183 void KChartLegendConfigPage::init()
00184 {
00185     switch( _params->legendPosition())
00186     {
00187     case KDChartParams::NoLegend:
00188         noLegend->setOn( true );
00189         break;
00190     case KDChartParams::LegendTop:
00191         lTop->setOn( true );
00192         break;
00193     case KDChartParams::LegendBottom:
00194         lBottom->setOn( true );
00195         break;
00196     case KDChartParams::LegendLeft:
00197         lLeft->setOn( true );
00198         break;
00199     case KDChartParams::LegendRight:
00200         lRight->setOn( true );
00201         break;
00202     case KDChartParams::LegendTopLeft:
00203         lTopLeft->setOn( true );
00204         break;
00205     case KDChartParams::LegendTopLeftTop:
00206         lTopLeftTop->setOn( true );
00207         break;
00208     case KDChartParams::LegendTopLeftLeft:
00209         lTopLeftLeft->setOn( true );
00210         break;
00211     case KDChartParams::LegendTopRight:
00212         lTopRight->setOn( true );
00213         break;
00214     case KDChartParams::LegendTopRightTop:
00215         lTopRightTop->setOn( true );
00216         break;
00217     case KDChartParams::LegendTopRightRight:
00218         lTopRightRight->setOn( true );
00219         break;
00220     case KDChartParams::LegendBottomRight:
00221         lBottomRight->setOn( true );
00222         break;
00223     case KDChartParams::LegendBottomRightBottom:
00224         lBottomRightBottom->setOn( true );
00225         break;
00226     case KDChartParams::LegendBottomRightRight:
00227         lBottomRightRight->setOn( true );
00228         break;
00229     case  KDChartParams::LegendBottomLeft:
00230         lBottomLeft->setOn( true );
00231         break;
00232     case  KDChartParams::LegendBottomLeftBottom:
00233         lBottomLeftBottom->setOn( true );
00234         break;
00235     case  KDChartParams::LegendBottomLeftLeft:
00236         lBottomLeftLeft->setOn( true );
00237         break;
00238     default:
00239         lRight->setOn( true );
00240         break;
00241     }
00242 
00243     title->setText(_params->legendTitleText());
00244 
00245     legendTitleColor->setColor(_params->legendTitleTextColor());
00246 
00247     legendTextColor->setColor(_params->legendTextColor());
00248 
00249     QColor frameColor(Qt::black);
00250     bool bFound;
00251     const KDChartParams::KDChartFrameSettings * legendFrame =
00252         _params->frameSettings( KDChartEnums::AreaLegend, bFound );
00253     if( bFound )
00254     {
00255         const KDFrameProfileSection * top =
00256             const_cast<KDFrameProfile&>(legendFrame->frame().profile( KDFrame::ProfileTop )).first();
00257         if( top )
00258             frameColor = top->pen().color();
00259     }
00260     legendFrameColor->setColor(frameColor);
00261 
00262     if( _params->legendOrientation() == Qt::Vertical )
00263         orientationGroup->setButton(0);
00264     else
00265         orientationGroup->setButton(1);
00266 
00267     titleLegend = _params->legendTitleFont();
00268     titleLegendIsRelative = _params->legendTitleFontUseRelSize()
00269                           ? QButton::On
00270                           : QButton::Off;
00271     if( QButton::On == titleLegendIsRelative )
00272         titleLegend.setPointSize( _params->legendTitleFontRelSize() );
00273 
00274     textLegend=_params->legendFont();
00275     textLegendIsRelative = _params->legendFontUseRelSize()
00276                          ? QButton::On
00277                          : QButton::Off;
00278     if( QButton::On == textLegendIsRelative )
00279         textLegend.setPointSize(_params->legendFontRelSize());
00280 }
00281 
00282 void KChartLegendConfigPage::changeTitleLegendFont()
00283 {
00284     QButton::ToggleState state = titleLegendIsRelative;
00285     if (    KFontDialog::getFont( titleLegend,false,this, true,&state ) != QDialog::Rejected
00286          && QButton::NoChange != state )
00287         titleLegendIsRelative = state;
00288 }
00289 
00290 void KChartLegendConfigPage::changeTextLegendFont()
00291 {
00292     QButton::ToggleState state = textLegendIsRelative;
00293     if (    KFontDialog::getFont( textLegend,false,this, true,&state ) != QDialog::Rejected
00294          && QButton::NoChange != state )
00295         textLegendIsRelative = state;
00296 }
00297 
00298 void KChartLegendConfigPage::apply()
00299 {
00300     if( noLegend->isOn() )
00301         _params->setLegendPosition( KDChartParams::NoLegend );
00302     else if( lTop->isOn() )
00303         _params->setLegendPosition( KDChartParams::LegendTop );
00304     else if( lBottom->isOn() )
00305         _params->setLegendPosition( KDChartParams::LegendBottom );
00306     else if( lLeft->isOn() )
00307         _params->setLegendPosition( KDChartParams::LegendLeft );
00308     else if( lRight->isOn() )
00309         _params->setLegendPosition( KDChartParams::LegendRight );
00310     else if( lTopLeft->isOn() )
00311         _params->setLegendPosition( KDChartParams::LegendTopLeft );
00312     else if( lTopLeftTop->isOn() )
00313         _params->setLegendPosition( KDChartParams::LegendTopLeftTop );
00314     else if( lTopLeftLeft->isOn() )
00315         _params->setLegendPosition( KDChartParams::LegendTopLeftLeft );
00316     else if( lTopRight->isOn() )
00317         _params->setLegendPosition( KDChartParams::LegendTopRight );
00318     else if( lTopRightTop->isOn() )
00319         _params->setLegendPosition( KDChartParams::LegendTopRightTop );
00320     else if( lTopRightRight->isOn() )
00321         _params->setLegendPosition( KDChartParams::LegendTopRightRight );
00322     else if( lBottomRight->isOn() )
00323         _params->setLegendPosition( KDChartParams::LegendBottomRight );
00324     else if( lBottomRightBottom->isOn() )
00325         _params->setLegendPosition( KDChartParams::LegendBottomRightBottom );
00326     else if( lBottomRightRight->isOn() )
00327         _params->setLegendPosition( KDChartParams::LegendBottomRightRight );
00328     else if( lBottomLeft->isOn() )
00329         _params->setLegendPosition( KDChartParams::LegendBottomLeft );
00330     else if( lBottomLeftBottom->isOn() )
00331         _params->setLegendPosition( KDChartParams::LegendBottomLeftBottom );
00332     else if( lBottomLeftLeft->isOn() )
00333         _params->setLegendPosition( KDChartParams::LegendBottomLeftLeft );
00334     else
00335         _params->setLegendPosition( KDChartParams::LegendRight );
00336 
00337     if( orientationGroup->selectedId() == 0 )
00338         _params->setLegendOrientation( Qt::Vertical );
00339     else
00340         _params->setLegendOrientation( Qt::Horizontal );
00341 
00342     _params->setLegendTitleText(title->text());
00343     _params->setLegendTitleTextColor(legendTitleColor->color());
00344     _params->setLegendTextColor(legendTextColor->color());
00345 
00346     const QColor frameColor(legendFrameColor->color());
00347     if( frameColor == Qt::black )
00348         _params->removeFrame( KDChartEnums::AreaLegend );
00349     else
00350         _params->setSimpleFrame( KDChartEnums::AreaLegend,
00351             0,0,  0,0,
00352             true,
00353             true,
00354             KDFrame::FrameFlat,
00355             1,
00356             0,
00357             frameColor );
00358 
00359     _params->setLegendTitleFont(titleLegend, QButton::Off == titleLegendIsRelative);
00360     if( QButton::On == titleLegendIsRelative )
00361         _params->setLegendTitleFontRelSize(titleLegend.pointSize());
00362     _params->setLegendFont(textLegend, QButton::Off == textLegendIsRelative);
00363     if( QButton::On == textLegendIsRelative )
00364         _params->setLegendFontRelSize(textLegend.pointSize());
00365 }
00366 
00367 QPushButton* KChartLegendConfigPage::addButton( QGridLayout* layout,
00368                                                 QButtonGroup* gb,
00369                                                 const QString &toolTipText,
00370                                                 const QString &icon,
00371                                                 int posY,
00372                                                 int posX )
00373 {
00374   QPushButton* button = new QPushButton( gb );
00375   button->setToggleButton( true );
00376   button->setPixmap( BarIcon( icon,
00377                               KIcon::SizeMedium,
00378                               KIcon::DefaultState,
00379                               KChartFactory::global() ) );
00380   QToolTip::add( button, toolTipText );
00381   layout->addWidget( button, posY, posX );
00382   return button;
00383 }
00384 
00385 }  //KChart namespace
KDE Home | KDE Accessibility Home | Description of Access Keys