summaryrefslogtreecommitdiffstats
path: root/kchart/kchartBackgroundPixmapConfigPage.cc
blob: 54127a9a37f29bcfb77271740564e5bfa3ed31c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/* This file is part of the KDE project
   Copyright (C) 2000 Matthias Kalle Dalheimer <kalle@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kchartBackgroundPixmapConfigPage.h"
#include "kchartBackgroundPixmapConfigPage.moc"

#include <klocale.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kfiledialog.h>
#include <kmessagebox.h>
#include <kdebug.h>
#include <kfilemetainfo.h>
#include <kstringhandler.h>

#include <tqcombobox.h>
#include <tqradiobutton.h>
#include <tqbuttongroup.h>
#include <tqspinbox.h>
#include <layout.h>
#include <tqhbox.h>
#include <tqpushbutton.h>
#include <tqlabel.h>
#include <tqwhatsthis.h>
#include <tqlistbox.h>
#include <kcolorbutton.h>

#include "kchart_params.h"

namespace KChart
{

KChartBackgroundPixmapConfigPage::KChartBackgroundPixmapConfigPage( KChartParams* params, TQWidget* parent )
    : TQWidget( parent, "KChartBackgroundPixmapConfigPage" ),
      _params( params )
{
    TQWhatsThis::add( this,
                     i18n( "On this page, you can select colors or images "
                           "to be displayed behind the different areas. You "
                           "can also select whether the images should be "
                           "stretched or scaled or centered or used as "
                           "background tiles." ) );

    TQHBoxLayout* toplevel = new TQHBoxLayout( this, 10 );

#if 0
    TQVBoxLayout* left=new TQVBoxLayout(10);
    toplevel->addLayout(left,2);
    regionList=new TQListBox(this);
    left->addWidget(regionList);
#endif

    TQVBoxLayout* center = new TQVBoxLayout( 10 );
    toplevel->addLayout( center, 2 );


    TQLabel* backgroundLA = new TQLabel( i18n( "&Background color:" ), this );
    center->addWidget( backgroundLA );
    _backgroundCB = new KColorButton( this );
    backgroundLA->setBuddy( _backgroundCB );
    center->addWidget( _backgroundCB);
    TQString wtstr = i18n( "Here you set the color in which the background "
                          "of the chart is painted." );
    TQWhatsThis::add( backgroundLA, wtstr );
    TQWhatsThis::add( _backgroundCB, wtstr );


    TQLabel* wallpaperLA = new TQLabel( i18n( "Background wallpaper:" ), this );
    center->addWidget( wallpaperLA );

    wallCB = new TQComboBox( false, this, "wallCombo" );
    wallpaperLA->setBuddy(wallCB);
    TQWhatsThis::add( wallCB, i18n( "You can select a background image from "
                                   "this list. Initially, the installed KDE "
                                   "wallpapers will be offered. If you do not "
                                   "find what you are looking for here, you can "
                                   "select any image file by clicking on the "
                                   "<i>Browse</i> button below." ) );
    center->addWidget( wallCB );
    wallCB->insertItem( i18n("None") );

    loadWallpaperFilesList();
    TQPushButton* browsePB = new TQPushButton( i18n("&Browse..."), this );
    TQWhatsThis::add( browsePB, i18n( "Click this button to select a background "
                                     "image not yet present in the list above. " ) );
    center->addWidget( browsePB );
    connect( browsePB, TQT_SIGNAL( clicked() ), TQT_SLOT( slotBrowse() ) );

    wallWidget = new TQWidget( this );
    TQWhatsThis::add( wallWidget, i18n( "This area will always display the "
                                       "currently selected background image. "
                                       "Note that the image will be scaled and "
                                       "thus might have a different ratio than "
                                       "it originally had." ) );
    center->addWidget( wallWidget );

    connect( wallCB, TQT_SIGNAL( activated( int ) ),
             this, TQT_SLOT( slotWallPaperChanged( int ) ) );

    right = new TQVGroupBox( i18n( "Wallpaper Configuration" ), this );
    TQWhatsThis::add( right, i18n( "In this box, you can set various settings "
                                  "that control how the background image is "
                                  "displayed." ) );
    toplevel->addWidget( right );

    TQHBox* intensityHB = new TQHBox( right );
    intensityHB->setSpacing( 10 );
    TQLabel* intensityLA = new TQLabel(
      // xgettext:no-c-format
      i18n( "&Intensity in %:" ), intensityHB );
    intensitySB = new TQSpinBox( 1, 100, 1, intensityHB );
    intensityLA->setBuddy( intensitySB );
    // xgettext:no-c-format
    TQString ttstr = i18n( "Here you can select how much the image should be "
                          "brightened up so that it does not disturb the "
                          "selected area too much.<br> Different images require "
                          "different settings, but 25% is a good value to start "
                          "with." );
    TQWhatsThis::add( intensityLA, ttstr );
    TQWhatsThis::add( intensitySB, ttstr );


    stretchedRB = new TQRadioButton( i18n( "Stretched" ), right );
    TQWhatsThis::add( stretchedRB,
                     i18n( "If you check this box, the selected image will "
                           "be scaled to fit the total size of the selected "
                           "area. Image ratio will be adjusted to match "
                           "the area size and height if necessary." ) );
    stretchedRB->setChecked( true );
    scaledRB = new TQRadioButton( i18n( "Scaled" ), right );
    TQWhatsThis::add( scaledRB,
                     i18n( "If you check this box, the selected image will "
                           "be scaled to match the height or width of the "
                           "selected area - whichever is reached first." ) );
    centeredRB = new TQRadioButton( i18n( "Centered" ), right );
    TQWhatsThis::add( centeredRB,
                     i18n( "If you check this box, the selected image will "
                           "be centered over the selected area. If the image "
                           "is larger then the area, you will only see the "
                           "middle part of it." ) );

    tiledRB = new TQRadioButton( i18n( "Tiled" ), right );
    TQWhatsThis::add( tiledRB,
                     i18n( "If you check this box, the selected image will "
                           "be used as a background tile. If the image is "
                           "larger then the selected area, you will only see "
                           "the upper left part of it." ) );
    TQButtonGroup* alignmentBG;
    alignmentBG = new TQButtonGroup( right, "GroupBox_Alignment" );
    alignmentBG->setFrameStyle( TQFrame::NoFrame );
    alignmentBG->insert( stretchedRB );
    alignmentBG->insert( scaledRB );
    alignmentBG->insert( centeredRB );
    alignmentBG->insert( tiledRB );

    intensitySB->hide(); //the property doesn't work atm
}

//Code from kcontrol/background/bgdialog.cc
void KChartBackgroundPixmapConfigPage::loadWallpaperFilesList()
{
   // Wallpapers
   // the following TQMap is lower cased names mapped to cased names and URLs
   // this way we get case insensitive sorting
   TQMap<TQString, TQPair<TQString, TQString> > papers;

   //search for .desktop files before searching for images without .desktop files
   TQStringList lst =  KGlobal::dirs()->findAllResources("wallpaper", "*desktop", false, true);
   TQStringList files;
   for (TQStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
   {
      KSimpleConfig fileConfig(*it);
      fileConfig.setGroup("Wallpaper");

      TQString imageCaption = fileConfig.readEntry("Name");
      TQString fileName = fileConfig.readEntry("File");

      if (imageCaption.isEmpty())
      {
         imageCaption = fileName;
         imageCaption.replace('_', ' ');
         imageCaption = KStringHandler::capwords(imageCaption);
      }

      // avoid name collisions
      TQString rs = imageCaption;
      TQString lrs = rs.lower();
      for (int n = 1; papers.find(lrs) != papers.end(); ++n)
      {
         rs = imageCaption + " (" + TQString::number(n) + ')';
         lrs = rs.lower();
      }
      int slash = (*it).findRev('/') + 1;
      TQString directory = (*it).left(slash);
      bool canLoadScaleable = false;
#ifdef HAVE_LIBART
      canLoadScaleable = true;
#endif
      if ( fileConfig.readEntry("ImageType") == "pixmap" || canLoadScaleable ) {
	      papers[lrs] = tqMakePair(rs, directory + fileName);
	      files.append(directory + fileName);
      }
   }

   //now find any wallpapers that don't have a .desktop file
   lst =  KGlobal::dirs()->findAllResources("wallpaper", "*", false, true);
   for (TQStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
   {
      if ( !(*it).endsWith(".desktop") && files.grep(*it).empty() ) {
         // First try to see if we have a comment describing the image.  If we do
         // just use the first line of said comment.
         KFileMetaInfo metaInfo(*it);
         TQString imageCaption;

         if (metaInfo.isValid() && metaInfo.item("Comment").isValid())
            imageCaption = metaInfo.item("Comment").string().section('\n', 0, 0);

         if (imageCaption.isEmpty())
         {
            int slash = (*it).findRev('/') + 1;
            int endDot = (*it).findRev('.');

            // strip the extension if it exists
            if (endDot != -1 && endDot > slash)
               imageCaption = (*it).mid(slash, endDot - slash);
            else
               imageCaption = (*it).mid(slash);

            imageCaption.replace('_', ' ');
            imageCaption = KStringHandler::capwords(imageCaption);
         }

         // avoid name collisions
         TQString rs = imageCaption;
         TQString lrs = rs.lower();
         for (int n = 1; papers.find(lrs) != papers.end(); ++n)
         {
            rs = imageCaption + " (" + TQString::number(n) + ')';
            lrs = rs.lower();
         }
         papers[lrs] = tqMakePair(rs, *it);
      }
   }

   m_wallpaper.clear();
   //don't start to 0, we have None element
   int i = 1;
   for (TQMap<TQString, TQPair<TQString, TQString> >::Iterator it = papers.begin();
        it != papers.end();
        ++it)
   {
      wallCB->insertItem(it.data().first);
      m_wallpaper[it.data().second] = i;
      i++;
   }
}

void KChartBackgroundPixmapConfigPage::init()
{
#if 0
    TQStringList lst;
    lst.append(i18n( "Outermost Region" ));
    lst.append(i18n( "Innermost Region" ));
    lst.append(i18n( "Header+Title+Subtitle" ));
    lst.append(i18n( "Footers" ));
    lst.append(i18n( "Data+Axes+Legend" ));
    lst.append(i18n( "Data+Axes" ));
    lst.append(i18n( "Data" ));
    lst.append(i18n( "Legend" ));
    lst.append(i18n( "Left Axis" ));
    lst.append(i18n( "Bottom Axis" ));
    lst.append(i18n( "Right Axis" ));
    lst.append(i18n( "Header A" ));
    lst.append(i18n( "Header B" ));
    lst.append(i18n( "Header C" ));
    lst.append(i18n( "Title A" ));
    lst.append(i18n( "Title B" ));
    lst.append(i18n( "Title C" ));
    lst.append(i18n( "Subtitle A" ));
    lst.append(i18n( "Subtitle B" ));
    lst.append(i18n( "Subtitle C" ));
    lst.append(i18n( "Footer 1 A" ));
    lst.append(i18n( "Footer 1 B" ));
    lst.append(i18n( "Footer 1 C" ));
    lst.append(i18n( "Footer 2 A" ));
    lst.append(i18n( "Footer 2 B" ));
    lst.append(i18n( "Footer 2 C" ));
    lst.append(i18n( "Footer 3 A" ));
    lst.append(i18n( "Footer 3 B" ));
    lst.append(i18n( "Footer 3 C" ));
    regionList->insertStringList(lst);
#endif
    // PENDING(kalle) Readapt
    //     showSettings( _params->backgroundPixmapName );
//     intensitySB->setValue( (int)(_params->backgroundPixmapIntensity * 100.0) );
//     scaledCB->setChecked( _params->backgroundPixmapScaled );
//     centeredCB->setChecked( _params->backgroundPixmapCentered );

    bool bFound;
    const KDChartParams::KDChartFrameSettings * innerFrame =
        _params->frameSettings( KDChartEnums::AreaInnermost, bFound );
    if( bFound )
    {
        const TQPixmap* backPixmap;
        bool isTiledMode = false;
        KDFrame::BackPixmapMode backPixmapMode;
        const TQBrush& background = innerFrame->frame().background( backPixmap, backPixmapMode );
        const TQColor backColor( background.color() );

        // Bg color has to be be set in any case,
        // even when a (tiled) pixmap is specified:
        _backgroundCB->setColor( backColor );


        if( !backPixmap || backPixmap->isNull() )
        {
            // A pixmap can be in the brush, if used as Tile.
            backPixmap = background.pixmap();
            if( backPixmap )
                isTiledMode = true;
        }

        if( !backPixmap || backPixmap->isNull() ) //color as background
        {
            right->setEnabled( false );
            wallCB->setCurrentItem( 0 );
        }
        else //pixmap as background
        {
            _backgroundCB->setEnabled( false );
            wallWidget->setPaletteBackgroundPixmap( *backPixmap );
            wallCB->setCurrentItem( 1 );
            if( isTiledMode )
            {
                tiledRB->setChecked( true );
            }
            else
            {
                switch( backPixmapMode ){
                    case KDFrame::PixCentered:
                        centeredRB->setChecked( true );
                        break;
                    case KDFrame::PixScaled:
                        scaledRB->setChecked( true );
                        break;
                    default:
                        stretchedRB->setChecked( true );
                }
            }
        }
    }
    else
        _backgroundCB->setColor(TQColor(230, 222, 222) );
}

void KChartBackgroundPixmapConfigPage::apply()
{
    // PENDING(kalle) Readapt
    //     if( wallCB->currentText() != _params->backgroundPixmapName ) {
//             bool load=true;
//             if(wallCB->currentText()==i18n("None")) {
//                     load=false;
//                 } else {
//                     _params->backgroundPixmapName = wallCB->currentText();
//                     bool load=_params->backgroundPixmap.load( locate( "wallpaper", _params->backgroundPixmapName ) );
//                     if(load)
//                         _params->backgroundPixmapIsDirty = true;
//                 }

//             if( !load ) {
//                     _params->backgroundPixmapName = "";
//                     _params->backgroundPixmap=TQPixmap("");
//                     _params->backgroundPixmapIsDirty = false;
//                 }
//         }
//     if( (int)(_params->backgroundPixmapIntensity * 100.0) !=
//         intensitySB->value() ) {
// 	_params->backgroundPixmapIntensity = (float)(intensitySB->value()) / 100.0;
// 	_params->backgroundPixmapIsDirty = true;
//     }

//     if( _params->backgroundPixmapScaled !=
//         scaledCB->isChecked() ) {
// 	_params->backgroundPixmapScaled = scaledCB->isChecked();
// 	_params->backgroundPixmapIsDirty = true;
//     }
//     if( _params->backgroundPixmapCentered !=
//         centeredCB->isChecked() ) {
// 	_params->backgroundPixmapCentered = centeredCB->isChecked();
// 	_params->backgroundPixmapIsDirty = true;
//     }

    //
    // temp. hack: the background is removed if "None" is selected in the combo box
    //
    //             For KOffice 1.5/2.0 this is to be removed by a checkbox.
    bool bFound;
    const KDChartParams::KDChartFrameSettings * innerFrame =
        _params->frameSettings( KDChartEnums::AreaInnermost, bFound );
    if( bFound )
    {
        const TQColor backColor( _backgroundCB->color() );
        KDFrame& frame( const_cast<KDFrame&>(innerFrame->frame()) );
        if ( wallCB->currentItem() == 0 )
        {
            frame.setBackPixmap( 0 );
            frame.setBackground( backColor );
        }
        else
        {
            const TQPixmap* pixmap = wallWidget->paletteBackgroundPixmap();
            if ( tiledRB->isChecked() ){
                // We remove the frame's extra pixmap,
                frame.setBackPixmap( 0 );
                // because a tiled image is set via a TQBrush.
                if( pixmap )
                    frame.setBackground( TQBrush( backColor, *pixmap ) );
                else
                    frame.setBackground( backColor );
            }else{
                KDFrame::BackPixmapMode backPixmapMode;
                if ( centeredRB->isChecked() )
                    backPixmapMode = KDFrame::PixCentered;
                else if ( scaledRB->isChecked() )
                    backPixmapMode = KDFrame::PixScaled;
                else
                    backPixmapMode = KDFrame::PixStretched;
                // We reset the background color, removing any tiled brush,
                frame.setBackground( backColor );
                // because we specify an extra pixmap instead.
                frame.setBackPixmap( pixmap, backPixmapMode );
            }
        }
    }
}


void KChartBackgroundPixmapConfigPage::showSettings( const TQString& fileName )
{
   wallCB->blockSignals(true);

   if (m_wallpaper.find(fileName) == m_wallpaper.end())
   {
      int i = wallCB->count();
      TQString imageCaption;
      int slash = fileName.findRev('/') + 1;
      int endDot = fileName.findRev('.');

      // strip the extension if it exists
      if (endDot != -1 && endDot > slash)
         imageCaption = fileName.mid(slash, endDot - slash);
      else
         imageCaption = fileName.mid(slash);
      if (wallCB->text(i-1) == imageCaption)
      {
         i--;
         wallCB->removeItem(i);
      }
      wallCB->insertItem(imageCaption);
      m_wallpaper[fileName] = i;
      wallCB->setCurrentItem(i);
   }
   else
   {
      wallCB->setCurrentItem(m_wallpaper[fileName]);
   }
   wallCB->blockSignals(false);
    loadWallPaper();
}


void KChartBackgroundPixmapConfigPage::slotBrowse()
{
    //TODO: Support non-local wallpapers
    KURL url = KFileDialog::getOpenFileName( 0 );
    if( url.isEmpty() )
        return;
    showSettings( url.path() );
}

void KChartBackgroundPixmapConfigPage::slotWallPaperChanged( int index )
{
    if ( index != 0 && loadWallPaper() )
    {
        _backgroundCB->setEnabled( false );
        right->setEnabled( true );
    }
    else
    {
        wallWidget->setPaletteBackgroundPixmap( 0 );
        _backgroundCB->setEnabled( true );
        right->setEnabled( false );
    }
}


bool KChartBackgroundPixmapConfigPage::loadWallPaper()
{
    int i = wallCB->currentItem();
    if ( i == -1 || i == 0 ) {  // 0 is 'None'
	wallPixmap.resize(0,0);
	wallFile = "";
    } else {
        for(TQMap<TQString,int>::ConstIterator it = m_wallpaper.begin();
            it != m_wallpaper.end();
            ++it)
        {
            if (it.data() == i)
            {
                wallFile = it.key();
                break;
            }
        }
        TQString file = locate("wallpaper", wallFile);
	if( file.isEmpty() ) {
            kdWarning(35001) << "Couldn't locate wallpaper " << wallFile << endl;
            wallPixmap.resize(0,0);
            wallFile = "";
            return false;
	} else {
            wallPixmap.load( file );

            if( wallPixmap.isNull() )
                return false;
// 		kdWarning(35001) << "Could not load wallpaper " << file << endl;
	}
    }
    wallWidget->setPaletteBackgroundPixmap( wallPixmap );
    return true;
}

}  //KChart namespace