kchart

kchartBackgroundPixmapConfigPage.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2000 Matthias Kalle Dalheimer <kalle@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kchartBackgroundPixmapConfigPage.h"
21#include "kchartBackgroundPixmapConfigPage.moc"
22
23#include <tdelocale.h>
24#include <tdeglobal.h>
25#include <kstandarddirs.h>
26#include <tdefiledialog.h>
27#include <tdemessagebox.h>
28#include <kdebug.h>
29#include <tdefilemetainfo.h>
30#include <kstringhandler.h>
31
32#include <tqcombobox.h>
33#include <tqradiobutton.h>
34#include <tqbuttongroup.h>
35#include <tqspinbox.h>
36#include <tqlayout.h>
37#include <tqhbox.h>
38#include <tqpushbutton.h>
39#include <tqlabel.h>
40#include <tqwhatsthis.h>
41#include <tqlistbox.h>
42#include <kcolorbutton.h>
43
44#include "kchart_params.h"
45
46namespace KChart
47{
48
49KChartBackgroundPixmapConfigPage::KChartBackgroundPixmapConfigPage( KChartParams* params, TQWidget* parent )
50 : TQWidget( parent, "KChartBackgroundPixmapConfigPage" ),
51 _params( params )
52{
53 TQWhatsThis::add( this,
54 i18n( "On this page, you can select colors or images "
55 "to be displayed behind the different areas. You "
56 "can also select whether the images should be "
57 "stretched or scaled or centered or used as "
58 "background tiles." ) );
59
60 TQHBoxLayout* toplevel = new TQHBoxLayout( this, 10 );
61
62#if 0
63 TQVBoxLayout* left=new TQVBoxLayout(10);
64 toplevel->addLayout(left,2);
65 regionList=new TQListBox(this);
66 left->addWidget(regionList);
67#endif
68
69 TQVBoxLayout* center = new TQVBoxLayout( 10 );
70 toplevel->addLayout( center, 2 );
71
72
73 TQLabel* backgroundLA = new TQLabel( i18n( "&Background color:" ), this );
74 center->addWidget( backgroundLA );
75 _backgroundCB = new KColorButton( this );
76 backgroundLA->setBuddy( _backgroundCB );
77 center->addWidget( _backgroundCB);
78 TQString wtstr = i18n( "Here you set the color in which the background "
79 "of the chart is painted." );
80 TQWhatsThis::add( backgroundLA, wtstr );
81 TQWhatsThis::add( _backgroundCB, wtstr );
82
83
84 TQLabel* wallpaperLA = new TQLabel( i18n( "Background wallpaper:" ), this );
85 center->addWidget( wallpaperLA );
86
87 wallCB = new TQComboBox( false, this, "wallCombo" );
88 wallpaperLA->setBuddy(wallCB);
89 TQWhatsThis::add( wallCB, i18n( "You can select a background image from "
90 "this list. Initially, the installed TDE "
91 "wallpapers will be offered. If you do not "
92 "find what you are looking for here, you can "
93 "select any image file by clicking on the "
94 "<i>Browse</i> button below." ) );
95 center->addWidget( wallCB );
96 wallCB->insertItem( i18n("None") );
97
98 loadWallpaperFilesList();
99 TQPushButton* browsePB = new TQPushButton( i18n("&Browse..."), this );
100 TQWhatsThis::add( browsePB, i18n( "Click this button to select a background "
101 "image not yet present in the list above. " ) );
102 center->addWidget( browsePB );
103 connect( browsePB, TQ_SIGNAL( clicked() ), TQ_SLOT( slotBrowse() ) );
104
105 wallWidget = new TQWidget( this );
106 TQWhatsThis::add( wallWidget, i18n( "This area will always display the "
107 "currently selected background image. "
108 "Note that the image will be scaled and "
109 "thus might have a different ratio than "
110 "it originally had." ) );
111 center->addWidget( wallWidget );
112
113 connect( wallCB, TQ_SIGNAL( activated( int ) ),
114 this, TQ_SLOT( slotWallPaperChanged( int ) ) );
115
116 right = new TQVGroupBox( i18n( "Wallpaper Configuration" ), this );
117 TQWhatsThis::add( right, i18n( "In this box, you can set various settings "
118 "that control how the background image is "
119 "displayed." ) );
120 toplevel->addWidget( right );
121
122 TQHBox* intensityHB = new TQHBox( right );
123 intensityHB->setSpacing( 10 );
124 TQLabel* intensityLA = new TQLabel(
125 // xgettext:no-c-format
126 i18n( "&Intensity in %:" ), intensityHB );
127 intensitySB = new TQSpinBox( 1, 100, 1, intensityHB );
128 intensityLA->setBuddy( intensitySB );
129 // xgettext:no-c-format
130 TQString ttstr = i18n( "Here you can select how much the image should be "
131 "brightened up so that it does not disturb the "
132 "selected area too much.<br> Different images require "
133 "different settings, but 25% is a good value to start "
134 "with." );
135 TQWhatsThis::add( intensityLA, ttstr );
136 TQWhatsThis::add( intensitySB, ttstr );
137
138
139 stretchedRB = new TQRadioButton( i18n( "Stretched" ), right );
140 TQWhatsThis::add( stretchedRB,
141 i18n( "If you check this box, the selected image will "
142 "be scaled to fit the total size of the selected "
143 "area. Image ratio will be adjusted to match "
144 "the area size and height if necessary." ) );
145 stretchedRB->setChecked( true );
146 scaledRB = new TQRadioButton( i18n( "Scaled" ), right );
147 TQWhatsThis::add( scaledRB,
148 i18n( "If you check this box, the selected image will "
149 "be scaled to match the height or width of the "
150 "selected area - whichever is reached first." ) );
151 centeredRB = new TQRadioButton( i18n( "Centered" ), right );
152 TQWhatsThis::add( centeredRB,
153 i18n( "If you check this box, the selected image will "
154 "be centered over the selected area. If the image "
155 "is larger then the area, you will only see the "
156 "middle part of it." ) );
157
158 tiledRB = new TQRadioButton( i18n( "Tiled" ), right );
159 TQWhatsThis::add( tiledRB,
160 i18n( "If you check this box, the selected image will "
161 "be used as a background tile. If the image is "
162 "larger then the selected area, you will only see "
163 "the upper left part of it." ) );
164 TQButtonGroup* alignmentBG;
165 alignmentBG = new TQButtonGroup( right, "GroupBox_Alignment" );
166 alignmentBG->setFrameStyle( TQFrame::NoFrame );
167 alignmentBG->insert( stretchedRB );
168 alignmentBG->insert( scaledRB );
169 alignmentBG->insert( centeredRB );
170 alignmentBG->insert( tiledRB );
171
172 intensitySB->hide(); //the property doesn't work atm
173}
174
175//Code from kcontrol/background/bgdialog.cpp
176void KChartBackgroundPixmapConfigPage::loadWallpaperFilesList()
177{
178 // Wallpapers
179 // the following TQMap is lower cased names mapped to cased names and URLs
180 // this way we get case insensitive sorting
181 TQMap<TQString, TQPair<TQString, TQString> > papers;
182
183 //search for .desktop files before searching for images without .desktop files
184 TQStringList lst = TDEGlobal::dirs()->findAllResources("wallpaper", "*desktop", false, true);
185 TQStringList files;
186 for (TQStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
187 {
188 KSimpleConfig fileConfig(*it);
189 fileConfig.setGroup("Wallpaper");
190
191 TQString imageCaption = fileConfig.readEntry("Name");
192 TQString fileName = fileConfig.readEntry("File");
193
194 if (imageCaption.isEmpty())
195 {
196 imageCaption = fileName;
197 imageCaption.replace('_', ' ');
198 imageCaption = KStringHandler::capwords(imageCaption);
199 }
200
201 // avoid name collisions
202 TQString rs = imageCaption;
203 TQString lrs = rs.lower();
204 for (int n = 1; papers.find(lrs) != papers.end(); ++n)
205 {
206 rs = imageCaption + " (" + TQString::number(n) + ')';
207 lrs = rs.lower();
208 }
209 int slash = (*it).findRev('/') + 1;
210 TQString directory = (*it).left(slash);
211 bool canLoadScaleable = false;
212#ifdef HAVE_LIBART
213 canLoadScaleable = true;
214#endif
215 if ( fileConfig.readEntry("ImageType") == "pixmap" || canLoadScaleable ) {
216 papers[lrs] = qMakePair(rs, directory + fileName);
217 files.append(directory + fileName);
218 }
219 }
220
221 //now find any wallpapers that don't have a .desktop file
222 lst = TDEGlobal::dirs()->findAllResources("wallpaper", "*", false, true);
223 for (TQStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
224 {
225 if ( !(*it).endsWith(".desktop") && files.grep(*it).empty() ) {
226 // First try to see if we have a comment describing the image. If we do
227 // just use the first line of said comment.
228 KFileMetaInfo metaInfo(*it);
229 TQString imageCaption;
230
231 if (metaInfo.isValid() && metaInfo.item("Comment").isValid())
232 imageCaption = metaInfo.item("Comment").string().section('\n', 0, 0);
233
234 if (imageCaption.isEmpty())
235 {
236 int slash = (*it).findRev('/') + 1;
237 int endDot = (*it).findRev('.');
238
239 // strip the extension if it exists
240 if (endDot != -1 && endDot > slash)
241 imageCaption = (*it).mid(slash, endDot - slash);
242 else
243 imageCaption = (*it).mid(slash);
244
245 imageCaption.replace('_', ' ');
246 imageCaption = KStringHandler::capwords(imageCaption);
247 }
248
249 // avoid name collisions
250 TQString rs = imageCaption;
251 TQString lrs = rs.lower();
252 for (int n = 1; papers.find(lrs) != papers.end(); ++n)
253 {
254 rs = imageCaption + " (" + TQString::number(n) + ')';
255 lrs = rs.lower();
256 }
257 papers[lrs] = qMakePair(rs, *it);
258 }
259 }
260
261 m_wallpaper.clear();
262 //don't start to 0, we have None element
263 int i = 1;
264 for (TQMap<TQString, TQPair<TQString, TQString> >::Iterator it = papers.begin();
265 it != papers.end();
266 ++it)
267 {
268 wallCB->insertItem(it.data().first);
269 m_wallpaper[it.data().second] = i;
270 i++;
271 }
272}
273
274void KChartBackgroundPixmapConfigPage::init()
275{
276#if 0
277 TQStringList lst;
278 lst.append(i18n( "Outermost Region" ));
279 lst.append(i18n( "Innermost Region" ));
280 lst.append(i18n( "Header+Title+Subtitle" ));
281 lst.append(i18n( "Footers" ));
282 lst.append(i18n( "Data+Axes+Legend" ));
283 lst.append(i18n( "Data+Axes" ));
284 lst.append(i18n( "Data" ));
285 lst.append(i18n( "Legend" ));
286 lst.append(i18n( "Left Axis" ));
287 lst.append(i18n( "Bottom Axis" ));
288 lst.append(i18n( "Right Axis" ));
289 lst.append(i18n( "Header A" ));
290 lst.append(i18n( "Header B" ));
291 lst.append(i18n( "Header C" ));
292 lst.append(i18n( "Title A" ));
293 lst.append(i18n( "Title B" ));
294 lst.append(i18n( "Title C" ));
295 lst.append(i18n( "Subtitle A" ));
296 lst.append(i18n( "Subtitle B" ));
297 lst.append(i18n( "Subtitle C" ));
298 lst.append(i18n( "Footer 1 A" ));
299 lst.append(i18n( "Footer 1 B" ));
300 lst.append(i18n( "Footer 1 C" ));
301 lst.append(i18n( "Footer 2 A" ));
302 lst.append(i18n( "Footer 2 B" ));
303 lst.append(i18n( "Footer 2 C" ));
304 lst.append(i18n( "Footer 3 A" ));
305 lst.append(i18n( "Footer 3 B" ));
306 lst.append(i18n( "Footer 3 C" ));
307 regionList->insertStringList(lst);
308#endif
309 // PENDING(kalle) Readapt
310 // showSettings( _params->backgroundPixmapName );
311// intensitySB->setValue( (int)(_params->backgroundPixmapIntensity * 100.0) );
312// scaledCB->setChecked( _params->backgroundPixmapScaled );
313// centeredCB->setChecked( _params->backgroundPixmapCentered );
314
315 bool bFound;
316 const KDChartParams::KDChartFrameSettings * innerFrame =
317 _params->frameSettings( KDChartEnums::AreaInnermost, bFound );
318 if( bFound )
319 {
320 const TQPixmap* backPixmap;
321 bool isTiledMode = false;
322 KDFrame::BackPixmapMode backPixmapMode;
323 const TQBrush& background = innerFrame->frame().background( backPixmap, backPixmapMode );
324 const TQColor backColor( background.color() );
325
326 // Bg color has to be be set in any case,
327 // even when a (tiled) pixmap is specified:
328 _backgroundCB->setColor( backColor );
329
330
331 if( !backPixmap || backPixmap->isNull() )
332 {
333 // A pixmap can be in the brush, if used as Tile.
334 backPixmap = background.pixmap();
335 if( backPixmap )
336 isTiledMode = true;
337 }
338
339 if( !backPixmap || backPixmap->isNull() ) //color as background
340 {
341 right->setEnabled( false );
342 wallCB->setCurrentItem( 0 );
343 }
344 else //pixmap as background
345 {
346 _backgroundCB->setEnabled( false );
347 wallWidget->setPaletteBackgroundPixmap( *backPixmap );
348 wallCB->setCurrentItem( 1 );
349 if( isTiledMode )
350 {
351 tiledRB->setChecked( true );
352 }
353 else
354 {
355 switch( backPixmapMode ){
356 case KDFrame::PixCentered:
357 centeredRB->setChecked( true );
358 break;
359 case KDFrame::PixScaled:
360 scaledRB->setChecked( true );
361 break;
362 default:
363 stretchedRB->setChecked( true );
364 }
365 }
366 }
367 }
368 else
369 _backgroundCB->setColor(TQColor(230, 222, 222) );
370}
371
372void KChartBackgroundPixmapConfigPage::apply()
373{
374 // PENDING(kalle) Readapt
375 // if( wallCB->currentText() != _params->backgroundPixmapName ) {
376// bool load=true;
377// if(wallCB->currentText()==i18n("None")) {
378// load=false;
379// } else {
380// _params->backgroundPixmapName = wallCB->currentText();
381// bool load=_params->backgroundPixmap.load( locate( "wallpaper", _params->backgroundPixmapName ) );
382// if(load)
383// _params->backgroundPixmapIsDirty = true;
384// }
385
386// if( !load ) {
387// _params->backgroundPixmapName = "";
388// _params->backgroundPixmap=TQPixmap("");
389// _params->backgroundPixmapIsDirty = false;
390// }
391// }
392// if( (int)(_params->backgroundPixmapIntensity * 100.0) !=
393// intensitySB->value() ) {
394// _params->backgroundPixmapIntensity = (float)(intensitySB->value()) / 100.0;
395// _params->backgroundPixmapIsDirty = true;
396// }
397
398// if( _params->backgroundPixmapScaled !=
399// scaledCB->isChecked() ) {
400// _params->backgroundPixmapScaled = scaledCB->isChecked();
401// _params->backgroundPixmapIsDirty = true;
402// }
403// if( _params->backgroundPixmapCentered !=
404// centeredCB->isChecked() ) {
405// _params->backgroundPixmapCentered = centeredCB->isChecked();
406// _params->backgroundPixmapIsDirty = true;
407// }
408
409 //
410 // temp. hack: the background is removed if "None" is selected in the combo box
411 //
412 // For KOffice 1.5/2.0 this is to be removed by a checkbox.
413 bool bFound;
414 const KDChartParams::KDChartFrameSettings * innerFrame =
415 _params->frameSettings( KDChartEnums::AreaInnermost, bFound );
416 if( bFound )
417 {
418 const TQColor backColor( _backgroundCB->color() );
419 KDFrame& frame( const_cast<KDFrame&>(innerFrame->frame()) );
420 if ( wallCB->currentItem() == 0 )
421 {
422 frame.setBackPixmap( 0 );
423 frame.setBackground( backColor );
424 }
425 else
426 {
427 const TQPixmap* pixmap = wallWidget->paletteBackgroundPixmap();
428 if ( tiledRB->isChecked() ){
429 // We remove the frame's extra pixmap,
430 frame.setBackPixmap( 0 );
431 // because a tiled image is set via a TQBrush.
432 if( pixmap )
433 frame.setBackground( TQBrush( backColor, *pixmap ) );
434 else
435 frame.setBackground( backColor );
436 }else{
437 KDFrame::BackPixmapMode backPixmapMode;
438 if ( centeredRB->isChecked() )
439 backPixmapMode = KDFrame::PixCentered;
440 else if ( scaledRB->isChecked() )
441 backPixmapMode = KDFrame::PixScaled;
442 else
443 backPixmapMode = KDFrame::PixStretched;
444 // We reset the background color, removing any tiled brush,
445 frame.setBackground( backColor );
446 // because we specify an extra pixmap instead.
447 frame.setBackPixmap( pixmap, backPixmapMode );
448 }
449 }
450 }
451}
452
453
454void KChartBackgroundPixmapConfigPage::showSettings( const TQString& fileName )
455{
456 wallCB->blockSignals(true);
457
458 if (m_wallpaper.find(fileName) == m_wallpaper.end())
459 {
460 int i = wallCB->count();
461 TQString imageCaption;
462 int slash = fileName.findRev('/') + 1;
463 int endDot = fileName.findRev('.');
464
465 // strip the extension if it exists
466 if (endDot != -1 && endDot > slash)
467 imageCaption = fileName.mid(slash, endDot - slash);
468 else
469 imageCaption = fileName.mid(slash);
470 if (wallCB->text(i-1) == imageCaption)
471 {
472 i--;
473 wallCB->removeItem(i);
474 }
475 wallCB->insertItem(imageCaption);
476 m_wallpaper[fileName] = i;
477 wallCB->setCurrentItem(i);
478 }
479 else
480 {
481 wallCB->setCurrentItem(m_wallpaper[fileName]);
482 }
483 wallCB->blockSignals(false);
484 loadWallPaper();
485}
486
487
488void KChartBackgroundPixmapConfigPage::slotBrowse()
489{
490 //TODO: Support non-local wallpapers
491 KURL url = KFileDialog::getOpenFileName( 0 );
492 if( url.isEmpty() )
493 return;
494 showSettings( url.path() );
495}
496
497void KChartBackgroundPixmapConfigPage::slotWallPaperChanged( int index )
498{
499 if ( index != 0 && loadWallPaper() )
500 {
501 _backgroundCB->setEnabled( false );
502 right->setEnabled( true );
503 }
504 else
505 {
506 wallWidget->setPaletteBackgroundPixmap( 0 );
507 _backgroundCB->setEnabled( true );
508 right->setEnabled( false );
509 }
510}
511
512
513bool KChartBackgroundPixmapConfigPage::loadWallPaper()
514{
515 int i = wallCB->currentItem();
516 if ( i == -1 || i == 0 ) { // 0 is 'None'
517 wallPixmap.resize(0,0);
518 wallFile = "";
519 } else {
520 for(TQMap<TQString,int>::ConstIterator it = m_wallpaper.begin();
521 it != m_wallpaper.end();
522 ++it)
523 {
524 if (it.data() == i)
525 {
526 wallFile = it.key();
527 break;
528 }
529 }
530 TQString file = locate("wallpaper", wallFile);
531 if( file.isEmpty() ) {
532 kdWarning(35001) << "Couldn't locate wallpaper " << wallFile << endl;
533 wallPixmap.resize(0,0);
534 wallFile = "";
535 return false;
536 } else {
537 wallPixmap.load( file );
538
539 if( wallPixmap.isNull() )
540 return false;
541// kdWarning(35001) << "Could not load wallpaper " << file << endl;
542 }
543 }
544 wallWidget->setPaletteBackgroundPixmap( wallPixmap );
545 return true;
546}
547
548} //KChart namespace
Stores the frame settings for one of the chart areas:
const KDFrame & frame() const
Return the KDFrame object used for drawing this areas frame.
The main class of KDFrame.
Definition KDFrame.h:88
BackPixmapMode
These are ways how to display a pixmap that might be painted into the inner area.
Definition KDFrame.h:109
const TQBrush & background(const TQPixmap *&backPixmap, BackPixmapMode &backPixmapMode) const
Returns the brush that is used to fill the inner area of this frame, or a TQBrush( \cNoBrush ) if no ...
Definition KDFrame.h:763