kchart

kchartBackgroundPixmapConfigPage.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2000 Matthias Kalle Dalheimer <kalle@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 "kchartBackgroundPixmapConfigPage.h"
00021 #include "kchartBackgroundPixmapConfigPage.moc"
00022 
00023 #include <klocale.h>
00024 #include <kglobal.h>
00025 #include <kstandarddirs.h>
00026 #include <kfiledialog.h>
00027 #include <kmessagebox.h>
00028 #include <kdebug.h>
00029 #include <kfilemetainfo.h>
00030 #include <kstringhandler.h>
00031 
00032 #include <qcombobox.h>
00033 #include <qradiobutton.h>
00034 #include <qbuttongroup.h>
00035 #include <qspinbox.h>
00036 #include <qlayout.h>
00037 #include <qhbox.h>
00038 #include <qpushbutton.h>
00039 #include <qlabel.h>
00040 #include <qwhatsthis.h>
00041 #include <qlistbox.h>
00042 #include <kcolorbutton.h>
00043 
00044 #include "kchart_params.h"
00045 
00046 namespace KChart
00047 {
00048 
00049 KChartBackgroundPixmapConfigPage::KChartBackgroundPixmapConfigPage( KChartParams* params, QWidget* parent )
00050     : QWidget( parent, "KChartBackgroundPixmapConfigPage" ),
00051       _params( params )
00052 {
00053     QWhatsThis::add( this,
00054                      i18n( "On this page, you can select colors or images "
00055                            "to be displayed behind the different areas. You "
00056                            "can also select whether the images should be "
00057                            "stretched or scaled or centered or used as "
00058                            "background tiles." ) );
00059 
00060     QHBoxLayout* toplevel = new QHBoxLayout( this, 10 );
00061 
00062 #if 0
00063     QVBoxLayout* left=new QVBoxLayout(10);
00064     toplevel->addLayout(left,2);
00065     regionList=new QListBox(this);
00066     left->addWidget(regionList);
00067 #endif
00068 
00069     QVBoxLayout* center = new QVBoxLayout( 10 );
00070     toplevel->addLayout( center, 2 );
00071 
00072 
00073     QLabel* backgroundLA = new QLabel( i18n( "&Background color:" ), this );
00074     center->addWidget( backgroundLA );
00075     _backgroundCB = new KColorButton( this );
00076     backgroundLA->setBuddy( _backgroundCB );
00077     center->addWidget( _backgroundCB);
00078     QString wtstr = i18n( "Here you set the color in which the background "
00079                           "of the chart is painted." );
00080     QWhatsThis::add( backgroundLA, wtstr );
00081     QWhatsThis::add( _backgroundCB, wtstr );
00082 
00083 
00084     QLabel* wallpaperLA = new QLabel( i18n( "Background wallpaper:" ), this );
00085     center->addWidget( wallpaperLA );
00086 
00087     wallCB = new QComboBox( false, this, "wallCombo" );
00088     wallpaperLA->setBuddy(wallCB);
00089     QWhatsThis::add( wallCB, i18n( "You can select a background image from "
00090                                    "this list. Initially, the installed KDE "
00091                                    "wallpapers will be offered. If you do not "
00092                                    "find what you are looking for here, you can "
00093                                    "select any image file by clicking on the "
00094                                    "<i>Browse</i> button below." ) );
00095     center->addWidget( wallCB );
00096     wallCB->insertItem( i18n("None") );
00097 
00098     loadWallpaperFilesList();
00099     QPushButton* browsePB = new QPushButton( i18n("&Browse..."), this );
00100     QWhatsThis::add( browsePB, i18n( "Click this button to select a background "
00101                                      "image not yet present in the list above. " ) );
00102     center->addWidget( browsePB );
00103     connect( browsePB, SIGNAL( clicked() ), SLOT( slotBrowse() ) );
00104 
00105     wallWidget = new QWidget( this );
00106     QWhatsThis::add( wallWidget, i18n( "This area will always display the "
00107                                        "currently selected background image. "
00108                                        "Note that the image will be scaled and "
00109                                        "thus might have a different ratio than "
00110                                        "it originally had." ) );
00111     center->addWidget( wallWidget );
00112 
00113     connect( wallCB, SIGNAL( activated( int ) ),
00114              this, SLOT( slotWallPaperChanged( int ) ) );
00115 
00116     right = new QVGroupBox( i18n( "Wallpaper Configuration" ), this );
00117     QWhatsThis::add( right, i18n( "In this box, you can set various settings "
00118                                   "that control how the background image is "
00119                                   "displayed." ) );
00120     toplevel->addWidget( right );
00121 
00122     QHBox* intensityHB = new QHBox( right );
00123     intensityHB->setSpacing( 10 );
00124     QLabel* intensityLA = new QLabel(
00125       // xgettext:no-c-format
00126       i18n( "&Intensity in %:" ), intensityHB );
00127     intensitySB = new QSpinBox( 1, 100, 1, intensityHB );
00128     intensityLA->setBuddy( intensitySB );
00129     // xgettext:no-c-format
00130     QString ttstr = i18n( "Here you can select how much the image should be "
00131                           "brightened up so that it does not disturb the "
00132                           "selected area too much.<br> Different images require "
00133                           "different settings, but 25% is a good value to start "
00134                           "with." );
00135     QWhatsThis::add( intensityLA, ttstr );
00136     QWhatsThis::add( intensitySB, ttstr );
00137 
00138 
00139     stretchedRB = new QRadioButton( i18n( "Stretched" ), right );
00140     QWhatsThis::add( stretchedRB,
00141                      i18n( "If you check this box, the selected image will "
00142                            "be scaled to fit the total size of the selected "
00143                            "area. Image ratio will be adjusted to match "
00144                            "the area size and height if necessary." ) );
00145     stretchedRB->setChecked( true );
00146     scaledRB = new QRadioButton( i18n( "Scaled" ), right );
00147     QWhatsThis::add( scaledRB,
00148                      i18n( "If you check this box, the selected image will "
00149                            "be scaled to match the height or width of the "
00150                            "selected area - whichever is reached first." ) );
00151     centeredRB = new QRadioButton( i18n( "Centered" ), right );
00152     QWhatsThis::add( centeredRB,
00153                      i18n( "If you check this box, the selected image will "
00154                            "be centered over the selected area. If the image "
00155                            "is larger then the area, you will only see the "
00156                            "middle part of it." ) );
00157 
00158     tiledRB = new QRadioButton( i18n( "Tiled" ), right );
00159     QWhatsThis::add( tiledRB,
00160                      i18n( "If you check this box, the selected image will "
00161                            "be used as a background tile. If the image is "
00162                            "larger then the selected area, you will only see "
00163                            "the upper left part of it." ) );
00164     QButtonGroup* alignmentBG;
00165     alignmentBG = new QButtonGroup( right, "GroupBox_Alignment" );
00166     alignmentBG->setFrameStyle( QFrame::NoFrame );
00167     alignmentBG->insert( stretchedRB );
00168     alignmentBG->insert( scaledRB );
00169     alignmentBG->insert( centeredRB );
00170     alignmentBG->insert( tiledRB );
00171 
00172     intensitySB->hide(); //the property doesn't work atm
00173 }
00174 
00175 //Code from kcontrol/background/bgdialog.cc
00176 void KChartBackgroundPixmapConfigPage::loadWallpaperFilesList()
00177 {
00178    // Wallpapers
00179    // the following QMap is lower cased names mapped to cased names and URLs
00180    // this way we get case insensitive sorting
00181    QMap<QString, QPair<QString, QString> > papers;
00182 
00183    //search for .desktop files before searching for images without .desktop files
00184    QStringList lst =  KGlobal::dirs()->findAllResources("wallpaper", "*desktop", false, true);
00185    QStringList files;
00186    for (QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
00187    {
00188       KSimpleConfig fileConfig(*it);
00189       fileConfig.setGroup("Wallpaper");
00190 
00191       QString imageCaption = fileConfig.readEntry("Name");
00192       QString fileName = fileConfig.readEntry("File");
00193 
00194       if (imageCaption.isEmpty())
00195       {
00196          imageCaption = fileName;
00197          imageCaption.replace('_', ' ');
00198          imageCaption = KStringHandler::capwords(imageCaption);
00199       }
00200 
00201       // avoid name collisions
00202       QString rs = imageCaption;
00203       QString lrs = rs.lower();
00204       for (int n = 1; papers.find(lrs) != papers.end(); ++n)
00205       {
00206          rs = imageCaption + " (" + QString::number(n) + ')';
00207          lrs = rs.lower();
00208       }
00209       int slash = (*it).findRev('/') + 1;
00210       QString directory = (*it).left(slash);
00211       bool canLoadScaleable = false;
00212 #ifdef HAVE_LIBART
00213       canLoadScaleable = true;
00214 #endif
00215       if ( fileConfig.readEntry("ImageType") == "pixmap" || canLoadScaleable ) {
00216           papers[lrs] = qMakePair(rs, directory + fileName);
00217           files.append(directory + fileName);
00218       }
00219    }
00220 
00221    //now find any wallpapers that don't have a .desktop file
00222    lst =  KGlobal::dirs()->findAllResources("wallpaper", "*", false, true);
00223    for (QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
00224    {
00225       if ( !(*it).endsWith(".desktop") && files.grep(*it).empty() ) {
00226          // First try to see if we have a comment describing the image.  If we do
00227          // just use the first line of said comment.
00228          KFileMetaInfo metaInfo(*it);
00229          QString imageCaption;
00230 
00231          if (metaInfo.isValid() && metaInfo.item("Comment").isValid())
00232             imageCaption = metaInfo.item("Comment").string().section('\n', 0, 0);
00233 
00234          if (imageCaption.isEmpty())
00235          {
00236             int slash = (*it).findRev('/') + 1;
00237             int endDot = (*it).findRev('.');
00238 
00239             // strip the extension if it exists
00240             if (endDot != -1 && endDot > slash)
00241                imageCaption = (*it).mid(slash, endDot - slash);
00242             else
00243                imageCaption = (*it).mid(slash);
00244 
00245             imageCaption.replace('_', ' ');
00246             imageCaption = KStringHandler::capwords(imageCaption);
00247          }
00248 
00249          // avoid name collisions
00250          QString rs = imageCaption;
00251          QString lrs = rs.lower();
00252          for (int n = 1; papers.find(lrs) != papers.end(); ++n)
00253          {
00254             rs = imageCaption + " (" + QString::number(n) + ')';
00255             lrs = rs.lower();
00256          }
00257          papers[lrs] = qMakePair(rs, *it);
00258       }
00259    }
00260 
00261    m_wallpaper.clear();
00262    //don't start to 0, we have None element
00263    int i = 1;
00264    for (QMap<QString, QPair<QString, QString> >::Iterator it = papers.begin();
00265         it != papers.end();
00266         ++it)
00267    {
00268       wallCB->insertItem(it.data().first);
00269       m_wallpaper[it.data().second] = i;
00270       i++;
00271    }
00272 }
00273 
00274 void KChartBackgroundPixmapConfigPage::init()
00275 {
00276 #if 0
00277     QStringList lst;
00278     lst.append(i18n( "Outermost Region" ));
00279     lst.append(i18n( "Innermost Region" ));
00280     lst.append(i18n( "Header+Title+Subtitle" ));
00281     lst.append(i18n( "Footers" ));
00282     lst.append(i18n( "Data+Axes+Legend" ));
00283     lst.append(i18n( "Data+Axes" ));
00284     lst.append(i18n( "Data" ));
00285     lst.append(i18n( "Legend" ));
00286     lst.append(i18n( "Left Axis" ));
00287     lst.append(i18n( "Bottom Axis" ));
00288     lst.append(i18n( "Right Axis" ));
00289     lst.append(i18n( "Header A" ));
00290     lst.append(i18n( "Header B" ));
00291     lst.append(i18n( "Header C" ));
00292     lst.append(i18n( "Title A" ));
00293     lst.append(i18n( "Title B" ));
00294     lst.append(i18n( "Title C" ));
00295     lst.append(i18n( "Subtitle A" ));
00296     lst.append(i18n( "Subtitle B" ));
00297     lst.append(i18n( "Subtitle C" ));
00298     lst.append(i18n( "Footer 1 A" ));
00299     lst.append(i18n( "Footer 1 B" ));
00300     lst.append(i18n( "Footer 1 C" ));
00301     lst.append(i18n( "Footer 2 A" ));
00302     lst.append(i18n( "Footer 2 B" ));
00303     lst.append(i18n( "Footer 2 C" ));
00304     lst.append(i18n( "Footer 3 A" ));
00305     lst.append(i18n( "Footer 3 B" ));
00306     lst.append(i18n( "Footer 3 C" ));
00307     regionList->insertStringList(lst);
00308 #endif
00309     // PENDING(kalle) Readapt
00310     //     showSettings( _params->backgroundPixmapName );
00311 //     intensitySB->setValue( (int)(_params->backgroundPixmapIntensity * 100.0) );
00312 //     scaledCB->setChecked( _params->backgroundPixmapScaled );
00313 //     centeredCB->setChecked( _params->backgroundPixmapCentered );
00314 
00315     bool bFound;
00316     const KDChartParams::KDChartFrameSettings * innerFrame =
00317         _params->frameSettings( KDChartEnums::AreaInnermost, bFound );
00318     if( bFound )
00319     {
00320         const QPixmap* backPixmap;
00321         bool isTiledMode = false;
00322         KDFrame::BackPixmapMode backPixmapMode;
00323         const QBrush& background = innerFrame->frame().background( backPixmap, backPixmapMode );
00324         const QColor backColor( background.color() );
00325 
00326         // Bg color has to be be set in any case,
00327         // even when a (tiled) pixmap is specified:
00328         _backgroundCB->setColor( backColor );
00329 
00330 
00331         if( !backPixmap || backPixmap->isNull() )
00332         {
00333             // A pixmap can be in the brush, if used as Tile.
00334             backPixmap = background.pixmap();
00335             if( backPixmap )
00336                 isTiledMode = true;
00337         }
00338 
00339         if( !backPixmap || backPixmap->isNull() ) //color as background
00340         {
00341             right->setEnabled( false );
00342             wallCB->setCurrentItem( 0 );
00343         }
00344         else //pixmap as background
00345         {
00346             _backgroundCB->setEnabled( false );
00347             wallWidget->setPaletteBackgroundPixmap( *backPixmap );
00348             wallCB->setCurrentItem( 1 );
00349             if( isTiledMode )
00350             {
00351                 tiledRB->setChecked( true );
00352             }
00353             else
00354             {
00355                 switch( backPixmapMode ){
00356                     case KDFrame::PixCentered:
00357                         centeredRB->setChecked( true );
00358                         break;
00359                     case KDFrame::PixScaled:
00360                         scaledRB->setChecked( true );
00361                         break;
00362                     default:
00363                         stretchedRB->setChecked( true );
00364                 }
00365             }
00366         }
00367     }
00368     else
00369         _backgroundCB->setColor(QColor(230, 222, 222) );
00370 }
00371 
00372 void KChartBackgroundPixmapConfigPage::apply()
00373 {
00374     // PENDING(kalle) Readapt
00375     //     if( wallCB->currentText() != _params->backgroundPixmapName ) {
00376 //             bool load=true;
00377 //             if(wallCB->currentText()==i18n("None")) {
00378 //                     load=false;
00379 //                 } else {
00380 //                     _params->backgroundPixmapName = wallCB->currentText();
00381 //                     bool load=_params->backgroundPixmap.load( locate( "wallpaper", _params->backgroundPixmapName ) );
00382 //                     if(load)
00383 //                         _params->backgroundPixmapIsDirty = true;
00384 //                 }
00385 
00386 //             if( !load ) {
00387 //                     _params->backgroundPixmapName = "";
00388 //                     _params->backgroundPixmap=QPixmap("");
00389 //                     _params->backgroundPixmapIsDirty = false;
00390 //                 }
00391 //         }
00392 //     if( (int)(_params->backgroundPixmapIntensity * 100.0) !=
00393 //         intensitySB->value() ) {
00394 //  _params->backgroundPixmapIntensity = (float)(intensitySB->value()) / 100.0;
00395 //  _params->backgroundPixmapIsDirty = true;
00396 //     }
00397 
00398 //     if( _params->backgroundPixmapScaled !=
00399 //         scaledCB->isChecked() ) {
00400 //  _params->backgroundPixmapScaled = scaledCB->isChecked();
00401 //  _params->backgroundPixmapIsDirty = true;
00402 //     }
00403 //     if( _params->backgroundPixmapCentered !=
00404 //         centeredCB->isChecked() ) {
00405 //  _params->backgroundPixmapCentered = centeredCB->isChecked();
00406 //  _params->backgroundPixmapIsDirty = true;
00407 //     }
00408 
00409     //
00410     // temp. hack: the background is removed if "None" is selected in the combo box
00411     //
00412     //             For KOffice 1.5/2.0 this is to be removed by a checkbox.
00413     bool bFound;
00414     const KDChartParams::KDChartFrameSettings * innerFrame =
00415         _params->frameSettings( KDChartEnums::AreaInnermost, bFound );
00416     if( bFound )
00417     {
00418         const QColor backColor( _backgroundCB->color() );
00419         KDFrame& frame( const_cast<KDFrame&>(innerFrame->frame()) );
00420         if ( wallCB->currentItem() == 0 )
00421         {
00422             frame.setBackPixmap( 0 );
00423             frame.setBackground( backColor );
00424         }
00425         else
00426         {
00427             const QPixmap* pixmap = wallWidget->paletteBackgroundPixmap();
00428             if ( tiledRB->isChecked() ){
00429                 // We remove the frame's extra pixmap,
00430                 frame.setBackPixmap( 0 );
00431                 // because a tiled image is set via a QBrush.
00432                 if( pixmap )
00433                     frame.setBackground( QBrush( backColor, *pixmap ) );
00434                 else
00435                     frame.setBackground( backColor );
00436             }else{
00437                 KDFrame::BackPixmapMode backPixmapMode;
00438                 if ( centeredRB->isChecked() )
00439                     backPixmapMode = KDFrame::PixCentered;
00440                 else if ( scaledRB->isChecked() )
00441                     backPixmapMode = KDFrame::PixScaled;
00442                 else
00443                     backPixmapMode = KDFrame::PixStretched;
00444                 // We reset the background color, removing any tiled brush,
00445                 frame.setBackground( backColor );
00446                 // because we specify an extra pixmap instead.
00447                 frame.setBackPixmap( pixmap, backPixmapMode );
00448             }
00449         }
00450     }
00451 }
00452 
00453 
00454 void KChartBackgroundPixmapConfigPage::showSettings( const QString& fileName )
00455 {
00456    wallCB->blockSignals(true);
00457 
00458    if (m_wallpaper.find(fileName) == m_wallpaper.end())
00459    {
00460       int i = wallCB->count();
00461       QString imageCaption;
00462       int slash = fileName.findRev('/') + 1;
00463       int endDot = fileName.findRev('.');
00464 
00465       // strip the extension if it exists
00466       if (endDot != -1 && endDot > slash)
00467          imageCaption = fileName.mid(slash, endDot - slash);
00468       else
00469          imageCaption = fileName.mid(slash);
00470       if (wallCB->text(i-1) == imageCaption)
00471       {
00472          i--;
00473          wallCB->removeItem(i);
00474       }
00475       wallCB->insertItem(imageCaption);
00476       m_wallpaper[fileName] = i;
00477       wallCB->setCurrentItem(i);
00478    }
00479    else
00480    {
00481       wallCB->setCurrentItem(m_wallpaper[fileName]);
00482    }
00483    wallCB->blockSignals(false);
00484     loadWallPaper();
00485 }
00486 
00487 
00488 void KChartBackgroundPixmapConfigPage::slotBrowse()
00489 {
00490     //TODO: Support non-local wallpapers
00491     KURL url = KFileDialog::getOpenFileName( 0 );
00492     if( url.isEmpty() )
00493         return;
00494     showSettings( url.path() );
00495 }
00496 
00497 void KChartBackgroundPixmapConfigPage::slotWallPaperChanged( int index )
00498 {
00499     if ( index != 0 && loadWallPaper() )
00500     {
00501         _backgroundCB->setEnabled( false );
00502         right->setEnabled( true );
00503     }
00504     else
00505     {
00506         wallWidget->setPaletteBackgroundPixmap( 0 );
00507         _backgroundCB->setEnabled( true );
00508         right->setEnabled( false );
00509     }
00510 }
00511 
00512 
00513 bool KChartBackgroundPixmapConfigPage::loadWallPaper()
00514 {
00515     int i = wallCB->currentItem();
00516     if ( i == -1 || i == 0 ) {  // 0 is 'None'
00517     wallPixmap.resize(0,0);
00518     wallFile = "";
00519     } else {
00520         for(QMap<QString,int>::ConstIterator it = m_wallpaper.begin();
00521             it != m_wallpaper.end();
00522             ++it)
00523         {
00524             if (it.data() == i)
00525             {
00526                 wallFile = it.key();
00527                 break;
00528             }
00529         }
00530         QString file = locate("wallpaper", wallFile);
00531     if( file.isEmpty() ) {
00532             kdWarning(35001) << "Couldn't locate wallpaper " << wallFile << endl;
00533             wallPixmap.resize(0,0);
00534             wallFile = "";
00535             return false;
00536     } else {
00537             wallPixmap.load( file );
00538 
00539             if( wallPixmap.isNull() )
00540                 return false;
00541 //      kdWarning(35001) << "Could not load wallpaper " << file << endl;
00542     }
00543     }
00544     wallWidget->setPaletteBackgroundPixmap( wallPixmap );
00545     return true;
00546 }
00547 
00548 }  //KChart namespace
KDE Home | KDE Accessibility Home | Description of Access Keys