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
@@ -22,4 +22,9 @@
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
@@ -31,2 +36,3 @@
31#include <qmenubar.h> 36#include <qmenubar.h>
37#include <qimage.h>
32 38
@@ -38,2 +44,3 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f
38{ 44{
45
39 // random seed 46 // random seed
@@ -66,2 +73,8 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f
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.
@@ -76,4 +89,11 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f
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{
@@ -92,9 +112,5 @@ PiecesTable::PiecesTable(QWidget* parent, const char* name )
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()
@@ -102,2 +118,3 @@ PiecesTable::~PiecesTable()
102 writeConfig(); 118 writeConfig();
119 clear();
103} 120}
@@ -113,2 +130,3 @@ void PiecesTable::writeConfig()
113 cfg.writeEntry("Randomized", _randomized ); 130 cfg.writeEntry("Randomized", _randomized );
131 cfg.writeEntry("Image", _image );
114} 132}
@@ -121,2 +139,3 @@ void PiecesTable::readConfig()
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;
@@ -127,22 +146,74 @@ void PiecesTable::readConfig()
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
@@ -150,6 +221,6 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
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);
@@ -159,20 +230,18 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
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,
@@ -194,2 +263,36 @@ void PiecesTable::resizeEvent(QResizeEvent *e)
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()
@@ -386 +489,11 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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}