1/*
2 KMahjonggLayoutSelector
3 Part of kmahjongg, the classic mahjongg game for KDE
4
5 Copyright (C) 2007 Mauricio Piacentini <mauricio@tabuleiro.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21*/
22
23#include "kmahjongglayoutselector.h"
24
25#include "boardwidget.h"
26#include "prefs.h"
27
28#include <klocale.h>
29#include <kstandarddirs.h>
30#include <QPainter>
31
32#include "kmahjongglayout.h"
33
34KMahjonggLayoutSelector::KMahjonggLayoutSelector( QWidget* parent, KConfigSkeleton * aconfig )
35 : QWidget( parent )
36{
37 setupUi(this);
38 bw = new BoardWidget( layoutPreview );
39 bw->resize(layoutPreview->size());
40 setupData(aconfig);
41}
42
43void KMahjonggLayoutSelector::setupData(KConfigSkeleton * aconfig)
44{
45 //Get our currently configured Layout entry
46 KConfig * config = aconfig->config();
47 KConfigGroup group = config->group("General");
48 QString initialGroup = group.readEntry("Layout_file");
49
50 //The lineEdit widget holds our tileset path, but the user does not manipulate it directly
51 kcfg_Layout->hide();
52
53 //No new stuff yet
54 getNewButton->hide();
55
56 //This will also load our resourcedir if it is not done already
57 KMahjonggLayout tile;
58
59 //Now get our tilesets into a list
60 QStringList tilesAvailable = KGlobal::dirs()->findAllResources("kmahjongglayout", QString("*.desktop"), KStandardDirs::Recursive);
61 tilesAvailable.sort();
62 QString namestr("Name");
63 int numvalidentries = 0;
64 for (int i = 0; i < tilesAvailable.size(); ++i)
65 {
66 KMahjonggLayout * aset = new KMahjonggLayout();
67 QString atileset = tilesAvailable.at(i);
68 if (aset->load(atileset)) {
69 layoutMap.insert(aset->authorProperty(namestr), aset);
70 layoutList->addItem(aset->authorProperty(namestr));
71 //Find if this is our currently configured Tileset
72 if (atileset==initialGroup) {
73 //Select current entry
74 layoutList->setCurrentRow(numvalidentries);
75 layoutChanged();
76 }
77 ++numvalidentries;
78 } else {
79 delete aset;
80 }
81 }
82
83 connect(layoutList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, SLOT(layoutChanged()));
84}
85
86void KMahjonggLayoutSelector::layoutChanged()
87{
88 KMahjonggLayout * selLayout = layoutMap.value(layoutList->currentItem()->text());
89 //Sanity checkings. Should not happen.
90 if (!selLayout) return;
91 if (selLayout->path()==kcfg_Layout->text()) {
92 return;
93 }
94 QString authstr("Author");
95 QString contactstr("AuthorEmail");
96 QString descstr("Description");
97 kcfg_Layout->setText(selLayout->path());
98 layoutAuthor->setText(selLayout->authorProperty(authstr));
99 layoutContact->setText(selLayout->authorProperty(contactstr));
100 layoutDescription->setText(selLayout->authorProperty(descstr));
101
102 //If settings for tiles/background have been applied, update our preview
103 if (bw->theTiles.path()!=Prefs::tileSet())
104 bw->loadTileset(Prefs::tileSet());
105 if (bw->theBackground.path()!=Prefs::background())
106 bw->loadBackground(Prefs::background());
107
108 //Now load the boardLayout temporarily
109 bw->loadBoardLayout(selLayout->path());
110 bw->calculateNewGame();
111
112 /* //Let the tileset calculate its ideal size for the preview area, but reduce the margins a bit (pass oversized drawing area)
113 QSize tilesize = selTileset->preferredTileSize(tilesetPreview->size()*1.3, 1, 1);
114 selTileset->reloadTileset(tilesize);
115 //Draw the preview
116 QImage qiRend(tilesetPreview->size(),QImage::Format_ARGB32_Premultiplied);
117 qiRend.fill(0);
118 QPainter p(&qiRend);
119 //Calculate the margins to center the tile
120 QSize margin = tilesetPreview->size() - tilesize;
121 //Draw unselected tile and first tileface
122 p.drawPixmap(margin.width()/2, margin.height()/2, selTileset->unselectedTile(1));
123 p.drawPixmap(margin.width()/2, margin.height()/2, selTileset->tileface(0));
124 tilesetPreview->setPixmap(QPixmap::fromImage(qiRend));*/
125}
126
127void KMahjonggLayoutSelector::useRandomLayoutToggled(bool active) {
128 widgetNoRandom->setEnabled(!active);
129}
130
131#include "kmahjongglayoutselector.moc"
132