summaryrefslogtreecommitdiff
path: root/noncore/games/fifteen/fifteen.cpp
Unidiff
Diffstat (limited to 'noncore/games/fifteen/fifteen.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/games/fifteen/fifteen.cpp187
1 files changed, 150 insertions, 37 deletions
diff --git a/noncore/games/fifteen/fifteen.cpp b/noncore/games/fifteen/fifteen.cpp
index 506e87a..f425e06 100644
--- a/noncore/games/fifteen/fifteen.cpp
+++ b/noncore/games/fifteen/fifteen.cpp
@@ -20,8 +20,13 @@
20 20
21#include "fifteen.h" 21#include "fifteen.h"
22 22
23#include "fifteenconfigdialog.h"
24
25#include <opie2/ofileselector.h>
26
23#include <qtopia/resource.h> 27#include <qtopia/resource.h>
24#include <qtopia/config.h> 28#include <qtopia/config.h>
29#include <qtopia/qpeapplication.h>
25 30
26#include <qvbox.h> 31#include <qvbox.h>
27#include <qaction.h> 32#include <qaction.h>
@@ -29,6 +34,7 @@
29#include <qmessagebox.h> 34#include <qmessagebox.h>
30#include <qtoolbar.h> 35#include <qtoolbar.h>
31#include <qmenubar.h> 36#include <qmenubar.h>
37#include <qimage.h>
32 38
33#include <stdlib.h> 39#include <stdlib.h>
34#include <time.h> 40#include <time.h>
@@ -36,6 +42,7 @@
36FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags fl) 42FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags fl)
37 : QMainWindow( parent, name, fl ) 43 : QMainWindow( parent, name, fl )
38{ 44{
45
39 // random seed 46 // random seed
40 srand(time(0)); 47 srand(time(0));
41 setCaption( tr("Fifteen Pieces") ); 48 setCaption( tr("Fifteen Pieces") );
@@ -64,6 +71,12 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f
64 a->addTo( game ); 71 a->addTo( game );
65 a->addTo( toolbar ); 72 a->addTo( toolbar );
66 73
74
75 a = new QAction( tr("Configure"), Resource::loadPixmap( "SettingsIcon" ),
76 QString::null, 0, this, 0 );
77 connect( a, SIGNAL( activated()), table, SLOT( slotConfigure()) );
78 a->addTo( game );
79
67 /* This is pointless and confusing. 80 /* This is pointless and confusing.
68 a = new QAction( tr( "Solve" ), Resource::loadIconSet( "repeat" ), 81 a = new QAction( tr( "Solve" ), Resource::loadIconSet( "repeat" ),
69 QString::null, 0, this, 0 ); 82 QString::null, 0, this, 0 );
@@ -74,8 +87,15 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f
74 menubar->insertItem( tr( "Game" ), game ); 87 menubar->insertItem( tr( "Game" ), game );
75} 88}
76 89
90
91
92
93///////////////
94/////// Pieces table Implementation
95///////
77PiecesTable::PiecesTable(QWidget* parent, const char* name ) 96PiecesTable::PiecesTable(QWidget* parent, const char* name )
78 : QTableView(parent, name), _menu(0), _randomized(false) 97 : QTableView(parent, name), _menu(0), _randomized(false),
98 _dialog( 0l )
79{ 99{
80 // setup table view 100 // setup table view
81 setFrameStyle(StyledPanel | Sunken); 101 setFrameStyle(StyledPanel | Sunken);
@@ -90,16 +110,13 @@ PiecesTable::PiecesTable(QWidget* parent, const char* name )
90 readConfig(); 110 readConfig();
91 initColors(); 111 initColors();
92 112
93 // set font
94 QFont f = font();
95 f.setPixelSize(18);
96 f.setBold( TRUE );
97 setFont(f);
98} 113}
99 114
115
100PiecesTable::~PiecesTable() 116PiecesTable::~PiecesTable()
101{ 117{
102 writeConfig(); 118 writeConfig();
119 clear();
103} 120}
104 121
105void PiecesTable::writeConfig() 122void PiecesTable::writeConfig()
@@ -111,6 +128,7 @@ void PiecesTable::writeConfig()
111 map.append( QString::number( _map[i] ) ); 128 map.append( QString::number( _map[i] ) );
112 cfg.writeEntry("Map", map, '-'); 129 cfg.writeEntry("Map", map, '-');
113 cfg.writeEntry("Randomized", _randomized ); 130 cfg.writeEntry("Randomized", _randomized );
131 cfg.writeEntry("Image", _image );
114} 132}
115 133
116void PiecesTable::readConfig() 134void PiecesTable::readConfig()
@@ -119,62 +137,113 @@ void PiecesTable::readConfig()
119 cfg.setGroup("Game"); 137 cfg.setGroup("Game");
120 QStringList map = cfg.readListEntry("Map", '-'); 138 QStringList map = cfg.readListEntry("Map", '-');
121 _randomized = cfg.readBoolEntry( "Randomized", FALSE ); 139 _randomized = cfg.readBoolEntry( "Randomized", FALSE );
140 _image = cfg.readEntry( "Image", QString::null );
122 int i = 0; 141 int i = 0;
123 for ( QStringList::Iterator it = map.begin(); it != map.end(); ++it ) { 142 for ( QStringList::Iterator it = map.begin(); it != map.end(); ++it ) {
124 _map[i] = (*it).toInt(); 143 _map[i] = (*it).toInt();
125 i++; 144 i++;
126 if ( i > 15 ) break; 145 if ( i > 15 ) break;
127 } 146 }
147
128} 148}
129 149
130void PiecesTable::paintCell(QPainter *p, int row, int col)
131{
132 int w = cellWidth();
133 int h = cellHeight();
134 int x2 = w - 1;
135 int y2 = h - 1;
136 150
137 int number = _map[col + row * numCols()] + 1; 151void PiecesTable::clear() {
152 /* clean up and resize */
153 for (uint i = 0; i < _pixmap.count(); ++i )
154 delete _pixmap[i];
155 _pixmap.resize( 16 );
156}
138 157
139 // draw cell background 158/*
140 if(number == 16) 159 * Let us pre-render the tiles. Either we've a Custom Image as
141 p->setBrush(colorGroup().background()); 160 * background or we use the drawRect to fill the background and
161 * last we put the number on it
162 */
163void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
164 QString str = _str;
165
166
167 /* couldn't load image fall back to plain tiles*/
168 QImage img = QImage(str);
169 if(img.isNull())
170 str = QString::null;
142 else 171 else
143 p->setBrush(_colors[number-1]); 172 img = img.smoothScale( width(),height() );
144 p->setPen(NoPen); 173
145 p->drawRect(0, 0, w, h); 174 QPixmap pix;
175 pix.convertFromImage( img );
176
177 uint image=0;
178
179 clear();
180
181 /* used variables */
182 int cols = numCols();
183 int rows = numRows();
184 int cellW = cellWidth();
185 int cellH = cellHeight();
186 int x2 = cellW-1;
187 int y2 = cellH-1;
188 bool empty = str.isEmpty();
189 double bw = empty ? 0.9 : 0.98;
190 int x_offset = cellW - int(cellW * bw);// 10% should be enough
191 inty_offset = cellH - int(cellH * bw);
146 192
147 if (number == 16) return; 193 /* border polygon */
194 initPolygon(cellW, cellH, x_offset, y_offset );
195
196 if ( cellW == 0 || cellH == 0 ) {
197 _pixmap.resize( 0 );
198 return;
199 }
200
201 QFont f = font();
202 f.setPixelSize(18);
203 f.setBold( TRUE );
204
205 /* for every tile */
206 for(int row = 0; row < rows; ++row ) {
207 for(int col= 0; col < cols; ++col) {
208 QPixmap *pip = new QPixmap(cellW, cellH );
209 QPainter *p = new QPainter(pip );
210 p->setFont( f );
211
212 /* draw the tradional tile or a part of the pixmap*/
213 if(empty) {
214 p->setBrush(_colors[image]);
215 p->setPen(NoPen);
216 p->drawRect(0,0,cellW,cellH);
217 }else
218 p->drawPixmap(0, 0, pix,col*cellW, row*cellH, cellW, cellH );
148 219
149 // draw borders 220 // draw borders
150 if (height() > 40) { 221 if (height() > 40) {
151 p->setBrush(_colors[number-1].light(130)); 222 p->setBrush(_colors[image].light(130));
152 p->drawPolygon(light_border); 223 p->drawPolygon(light_border);
153 224
154 p->setBrush(_colors[number-1].dark(130)); 225 p->setBrush(_colors[image].dark(130));
155 p->drawPolygon(dark_border); 226 p->drawPolygon(dark_border);
156 } 227 }
157 228
158 // draw number 229 // draw number
159 p->setPen(black); 230 p->setPen(black);
160 p->drawText(0, 0, x2, y2, AlignHCenter | AlignVCenter, QString::number(number)); 231 p->drawText(0, 0, x2, y2, AlignHCenter | AlignVCenter, QString::number(image+1));
161}
162 232
163void PiecesTable::resizeEvent(QResizeEvent *e) 233 delete p;
164{ 234 _pixmap[image++] = pip;
165 QTableView::resizeEvent(e); 235 }
166 236 }
167 setCellWidth(contentsRect().width()/ numRows()); 237 _image = str;
168 setCellHeight(contentsRect().height() / numCols());
169 238
170 // 239 if ( upd )
171 // Calculate 3d-effect borders 240 update();
172 // 241}
173 intcell_w = cellWidth();
174 intcell_h = cellHeight();
175 int x_offset = cell_w - int(cell_w * 0.9);// 10% should be enough
176 inty_offset = cell_h - int(cell_h * 0.9);
177 242
243/*
244 * Calculate 3d-effect borders
245 */
246void PiecesTable::initPolygon(int cell_w, int cell_h, int x_offset, int y_offset ) {
178 light_border.setPoints(6, 247 light_border.setPoints(6,
179 0, 0, 248 0, 0,
180 cell_w, 0, 249 cell_w, 0,
@@ -192,6 +261,40 @@ void PiecesTable::resizeEvent(QResizeEvent *e)
192 cell_w - x_offset, y_offset); 261 cell_w - x_offset, y_offset);
193} 262}
194 263
264void PiecesTable::paintCell(QPainter *p, int row, int col)
265{
266 int w = cellWidth();
267 int h = cellHeight();
268
269 int number = _map[col + row * numCols()] + 1;
270
271 // draw cell background
272 if(number == 16) {
273 p->setBrush(colorGroup().background());
274 p->setPen(NoPen);
275 p->drawRect(0, 0, w, h);
276 return;
277 }
278
279 if( _pixmap.count() == 0 )
280 return;
281
282 p->drawPixmap(0, 0, *(_pixmap[(number-1 )]) );
283}
284
285void PiecesTable::resizeEvent(QResizeEvent *e)
286{
287 QTableView::resizeEvent(e);
288
289 setCellWidth(contentsRect().width()/ numRows());
290 setCellHeight(contentsRect().height() / numCols());
291
292
293 /* update the image and calculate border*/
294 slotCustomImage( _image );
295
296}
297
195void PiecesTable::initColors() 298void PiecesTable::initColors()
196{ 299{
197 _colors.resize(numRows() * numCols()); 300 _colors.resize(numRows() * numCols());
@@ -384,3 +487,13 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
384 checkwin(); 487 checkwin();
385 } 488 }
386} 489}
490
491void PiecesTable::slotConfigure() {
492 if ( !_dialog )
493 _dialog = new FifteenConfigDialog(this, "Fifteen Configure Dialog", true );
494
495
496 _dialog->setImageSrc( _image );
497 if ( QPEApplication::execDialog(_dialog) == QDialog::Accepted )
498 slotCustomImage( _dialog->imageSrc(), true );
499}