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 "kmahjonggtilesetselector.h"
20
21#include <klocale.h>
22#include <kstandarddirs.h>
23#include <QPainter>
24
25#include "kmahjonggtileset.h"
26
27KMahjonggTilesetSelector::KMahjonggTilesetSelector( QWidget* parent, KConfigSkeleton * aconfig )
28 : QWidget( parent )
29{
30 setupUi(this);
31 setupData(aconfig);
32}
33
34KMahjonggTilesetSelector::~KMahjonggTilesetSelector()
35{
36 tilesetMap.clear();
37}
38
39void KMahjonggTilesetSelector::setupData(KConfigSkeleton * aconfig)
40{
41 //Get our currently configured Tileset entry
42 KConfig * config = aconfig->config();
43 KConfigGroup group = config->group("General");
44 QString initialGroup = group.readEntry("Tileset_file");
45
46 //The lineEdit widget holds our tileset path, but the user does not manipulate it directly
47 kcfg_TileSet->hide();
48
49 //This will also load our resourcedir if it is not done already
50 KMahjonggTileset tile;
51
52 //Now get our tilesets into a list
53 QStringList tilesAvailable = KGlobal::dirs()->findAllResources("kmahjonggtileset", QLatin1String( "*.desktop"), KStandardDirs::Recursive);
54
55 QLatin1String namestr("Name");
56 int numvalidentries = 0;
57 for (int i = 0; i < tilesAvailable.size(); ++i)
58 {
59 KMahjonggTileset * aset = new KMahjonggTileset();
60 QString atileset = tilesAvailable.at(i);
61 if (aset->loadTileset(atileset)) {
62 tilesetMap.insert(aset->authorProperty(namestr), aset);
63 tilesetList->addItem(aset->authorProperty(namestr));
64 //Find if this is our currently configured Tileset
65 if (atileset==initialGroup) {
66 //Select current entry
67 tilesetList->setCurrentRow(numvalidentries);
68 tilesetChanged();
69 }
70 ++numvalidentries;
71 } else {
72 delete aset;
73 }
74 }
75
76 connect(tilesetList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, SLOT(tilesetChanged()));
77}
78
79void KMahjonggTilesetSelector::tilesetChanged()
80{
81 KMahjonggTileset * selTileset = tilesetMap.value(tilesetList->currentItem()->text());
82 //Sanity checkings. Should not happen.
83 if (!selTileset) return;
84 if (selTileset->path()==kcfg_TileSet->text()) {
85 return;
86 }
87 QLatin1String authstr("Author");
88 QLatin1String contactstr("AuthorEmail");
89 QLatin1String descstr("Description");
90 kcfg_TileSet->setText(selTileset->path());
91 tilesetAuthor->setText(selTileset->authorProperty(authstr));
92 tilesetContact->setText(selTileset->authorProperty(contactstr));
93 tilesetDescription->setText(selTileset->authorProperty(descstr));
94
95 //Make sure SVG is loaded when graphics is selected
96 if (!selTileset->loadGraphics()) return;
97 //Let the tileset calculate its ideal size for the preview area, but reduce the margins a bit (pass oversized drawing area)
98 QSize tilesize = selTileset->preferredTileSize(tilesetPreview->size()*1.3, 1, 1);
99 selTileset->reloadTileset(tilesize);
100 //Draw the preview
101 QImage qiRend(tilesetPreview->size(),QImage::Format_ARGB32_Premultiplied);
102 qiRend.fill(0);
103 QPainter p(&qiRend);
104 //Calculate the margins to center the tile
105 QSize margin = tilesetPreview->size() - tilesize;
106 //Draw unselected tile and first tileface
107 p.drawPixmap(margin.width()/2, margin.height()/2, selTileset->unselectedTile(1));
108 p.drawPixmap(margin.width()/2, margin.height()/2, selTileset->tileface(0));
109 p.end();
110 tilesetPreview->setPixmap(QPixmap::fromImage(qiRend));
111
112}
113
114#include "kmahjonggtilesetselector.moc"
115