1/*
2 Copyright (C) 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
3
4 Libkmahjongg is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17*/
18
19#include "kmahjonggbackgroundselector.h"
20
21#include <klocale.h>
22#include <kstandarddirs.h>
23#include <QPainter>
24#include "kmahjonggbackground.h"
25
26KMahjonggBackgroundSelector::KMahjonggBackgroundSelector( QWidget* parent, KConfigSkeleton * aconfig )
27 : QWidget( parent )
28{
29 setupUi(this);
30 setupData(aconfig);
31}
32
33KMahjonggBackgroundSelector::~KMahjonggBackgroundSelector()
34{
35 qDeleteAll(backgroundMap);
36}
37
38void KMahjonggBackgroundSelector::setupData(KConfigSkeleton * aconfig)
39{
40 //Get our currently configured Tileset entry
41 KConfig * config = aconfig->config();
42 KConfigGroup group = config->group("General");
43 QString initialGroup = group.readEntry("Background_file");
44
45 //The lineEdit widget holds our bg path, but the user does not manipulate it directly
46 kcfg_Background->hide();
47
48 KMahjonggBackground bg;
49
50 //Now get our tilesets into a list
51 QStringList bgsAvailable = KGlobal::dirs()->findAllResources("kmahjonggbackground", QLatin1String( "*.desktop"), KStandardDirs::Recursive);
52 QLatin1String namestr("Name");
53 int numvalidentries = 0;
54 for (int i = 0; i < bgsAvailable.size(); ++i)
55 {
56 KMahjonggBackground * abg = new KMahjonggBackground();
57 QString bgpath = bgsAvailable.at(i);
58 if (abg->load(bgpath,backgroundPreview->width(),backgroundPreview->height())) {
59 backgroundMap.insert(abg->authorProperty(namestr), abg);
60 backgroundList->addItem(abg->authorProperty(namestr));
61 //Find if this is our currently configured Tileset
62 if (bgpath==initialGroup) {
63 //Select current entry
64 backgroundList->setCurrentRow(numvalidentries);
65 backgroundChanged();
66 }
67 ++numvalidentries;
68 } else {
69 delete abg;
70 }
71 }
72
73 connect(backgroundList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, SLOT(backgroundChanged()));
74}
75
76void KMahjonggBackgroundSelector::backgroundChanged()
77{
78 KMahjonggBackground * selBG = backgroundMap.value(backgroundList->currentItem()->text());
79 //Sanity checkings. Should not happen.
80 if (!selBG) return;
81 if (selBG->path()==kcfg_Background->text()) {
82 return;
83 }
84 QLatin1String authstr("Author");
85 QLatin1String contactstr("AuthorEmail");
86 QLatin1String descstr("Description");
87 kcfg_Background->setText(selBG->path());
88 backgroundAuthor->setText(selBG->authorProperty(authstr));
89 backgroundContact->setText(selBG->authorProperty(contactstr));
90 backgroundDescription->setText(selBG->authorProperty(descstr));
91
92 if (selBG->authorProperty(QLatin1String( "Plain" )) == QLatin1String("1")) {
93 backgroundPreview->setPixmap(QPixmap());
94 return;
95 }
96
97 //Make sure SVG is loaded when graphics is selected
98 if (!selBG->loadGraphics()) return;
99
100 //Draw the preview
101 //TODO here: add code to load and keep proportions for non-tiled content?
102 QImage qiRend(backgroundPreview->size(),QImage::Format_ARGB32_Premultiplied);
103 qiRend.fill(0);
104 QPainter p(&qiRend);
105 p.fillRect(p.viewport(), selBG->getBackground() );
106 p.end();
107 backgroundPreview->setPixmap(QPixmap::fromImage(qiRend));
108
109}
110
111#include "kmahjonggbackgroundselector.moc"
112