1/*
2* Copyright (C) 2010 Andreas Scherf <ascherfy@gmail.com>
3*
4* This file is part of the KDE project "KBounce"
5*
6* KBounce is free software; you can redistribute it and/or
7* modify it under the terms of the GNU Library General Public
8* License as published by the Free Software Foundation; either
9* version 2 of the License, or (at your option) any later version.
10*
11* KBounce is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14* Library General Public License for more details.
15*
16* You should have received a copy of the GNU Library General Public
17* License along with KBounce; if not, write to the Free
18* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19* Boston, MA 02110-1301, USA.
20*/
21
22#include "backgroundselector.h"
23#include "ui_backgroundselector.h"
24#include <kfiledialog.h>
25#include <KConfigDialog>
26#include "renderer.h"
27#include <QPainter>
28#include <KColorScheme>
29#include <KLocalizedString>
30
31BackgroundSelector::BackgroundSelector(QWidget* parent, KConfigSkeleton* config) :
32 QWidget(parent),
33 ui(new Ui::KBounceBackgroundSelector),m_config(config)
34{
35 ui->setupUi(this);
36 ui->kurlrequester->fileDialog()->setMode(KFile::Directory);
37 setupData();
38}
39
40BackgroundSelector::~BackgroundSelector()
41{
42 delete ui;
43}
44
45void BackgroundSelector::changeEvent(QEvent *e)
46{
47 QWidget::changeEvent(e);
48 switch (e->type()) {
49 case QEvent::LanguageChange:
50 ui->retranslateUi(this);
51 break;
52 default:
53 break;
54 }
55}
56
57
58void BackgroundSelector::setupData()
59{
60 if (m_config)
61 {
62 //The lineEdit widget holds our bg path, but the user does not manipulate it directly
63 ui->kcfg_BackgroundPicturePath->hide();
64 connect(ui->kurlrequester, SIGNAL(textChanged(QString)), this, SLOT(imagePathChanged(QString)));
65 connect(ui->kcfg_UseRandomBackgroundPictures, SIGNAL(toggled(bool)),this,SLOT(useRandomBackgroundPicturesChanged(bool)));
66
67 //Get our currently configured Tileset entry
68 KConfig * config = m_config->config();
69 KConfigGroup group = config->group("General");
70 QString picturePath = group.readPathEntry("BackgroundPicturePath",QDir::homePath() );
71 ui->kurlrequester->setUrl(picturePath);
72 ui->kurlrequester->setStartDir(picturePath);
73 }
74}
75
76void BackgroundSelector::useRandomBackgroundPicturesChanged(bool toggled)
77{
78 if (toggled)
79 {
80 enableSettings(true);
81 previewBackgroundPicture();
82 }
83 else
84 {
85 enableSettings(false);
86 }
87}
88
89void BackgroundSelector::enableSettings(bool enable)
90{
91 ui->kurlrequester->setEnabled(enable);
92 ui->backgroundPicturePreview->setEnabled(enable);
93}
94
95void BackgroundSelector::imagePathChanged(const QString& path)
96{
97 ui->kcfg_BackgroundPicturePath->setText(path);
98 previewBackgroundPicture();
99}
100
101void BackgroundSelector::previewBackgroundPicture()
102{
103 KBounceRenderer render;
104 QSize previewSize = ui->backgroundPicturePreview->size();
105 QPixmap previewPixmap = render.getRandomBackgroundPixmap(ui->kcfg_BackgroundPicturePath->text());
106 if (previewPixmap.isNull())
107 {
108 previewPixmap = QPixmap(previewSize);
109 previewPixmap.fill( Qt::transparent );
110 QPainter p( &previewPixmap );
111 QString text=i18n("No background pictures found.");
112 QFont font;
113 font.setPointSize( 11 );
114 p.setFont( font );
115 KColorScheme kcs(QPalette::Normal,KColorScheme::Window);
116 p.setPen(kcs.foreground(KColorScheme::NeutralText).color());
117 p.drawText( p.viewport(), Qt::AlignCenter | Qt::AlignTop | Qt::TextWordWrap, text );
118 p.end();
119 }
120 ui->backgroundPicturePreview->setPixmap(previewPixmap.scaled(previewSize,Qt::KeepAspectRatio,Qt::SmoothTransformation));
121}
122
123
124
125
126