summaryrefslogtreecommitdiffstats
path: root/kmahjongg/Preview.cpp
blob: 725e0a93416dc483978241a8128067b8db900ab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include <tdeapplication.h>
#include <tdefiledialog.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kpushbutton.h>
#include <kstandarddirs.h>
#include <kstdguiitem.h>
#include <kimageio.h>

#include <tqcombobox.h>
#include <tqhgroupbox.h>
#include <tqimage.h>
#include <tqregexp.h>
#include <tqpainter.h>
#include <tqvbox.h>

#include "prefs.h"
#include "Preview.h"

static const char * themeMagicV1_0= "kmahjongg-theme-v1.0";

Preview::Preview(TQWidget* parent) : KDialogBase(parent), m_tiles(true)
{
	KPushButton *loadButton;
	TQGroupBox *group;
	TQVBox *page;
	 
	page = new TQVBox(this);

	group = new TQHGroupBox(page);
	
	m_combo = new TQComboBox(false, group);
	connect(m_combo, TQT_SIGNAL(activated(int)), TQT_SLOT(selectionChanged(int)));

	loadButton = new KPushButton(i18n("Load..."), group);
	connect( loadButton, TQT_SIGNAL(clicked()), TQT_SLOT(load()) );
	
	m_drawFrame = new FrameImage(page);
	m_drawFrame->setFixedSize(310, 236);

	m_changed = false;
	
	setMainWidget(page);
	setFixedSize(sizeHint());
}

Preview::~Preview()
{
}

void Preview::selectionChanged(int which)
{
	m_selectedFile = m_fileList[which];
	drawPreview();
	m_drawFrame->repaint(0,0,-1,-1,false);
	markChanged();
}

bool Preview::isChanged()
{
	return m_changed;
}

void Preview::markChanged()
{
	m_changed = true;
}

void Preview::markUnchanged()
{
	m_changed = false;
}

void Preview::initialise(const PreviewType type)
{
	TQString extension;
	TQString tile = Prefs::tileSet();
	TQString back = Prefs::background();
	TQString layout = Prefs::layout();

	// set up the concept of the current file. Initialised to the preferences
	// value initially. Set the caption to indicate what we are doing
	switch (type)
	{
		case background:
			setCaption(i18n("Change Background Image"));
			m_selectedFile = back;
			m_fileSelector = i18n("*.bgnd|Background Image (*.bgnd)\n");
			m_fileSelector += KImageIO::pattern()+"\n";
			extension = "*.bgnd";
		break;
		
		case tileset:
			setCaption(i18n("Change Tile Set"));
			m_fileSelector = i18n("*.tileset|Tile Set File (*.tileset)\n");
			m_selectedFile = tile;
			extension = "*.tileset";
		break;
		
		case board:
			m_fileSelector = i18n("*.layout|Board Layout File (*.layout)\n");
			setCaption(i18n("Change Board Layout"));
			m_selectedFile = layout;
			extension = "*.layout";
		break;
		
		case theme:
			m_fileSelector = i18n("*.theme|KMahjongg Theme File (*.theme)\n");
			setCaption(i18n("Choose Theme"));
			m_selectedFile="";
			extension = "*.theme";
			
			m_themeLayout="";
			m_themeBack="";
			m_themeTileset="";
		
		default:
		break;
	}
	
	m_fileSelector += i18n("*|All Files");
	enableButtonApply(type != board);

	m_previewType = type;
	// we start with no change indicated
	markUnchanged();

	m_fileList = kapp->dirs()->findAllResources("appdata",  "pics/*"+extension, false, true);

	// get rid of files from the last invocation
	m_combo->clear();

	TQStringList names;
	TQStringList::const_iterator it, itEnd;
	it = m_fileList.begin();
	itEnd = m_fileList.end();
	for ( ; it != itEnd; ++it)
	{
		TQFileInfo fi(*it);
		names << fi.baseName();
	}
	
	m_combo->insertStringList(names);
	m_combo->setEnabled(m_fileList.count());
	drawPreview();
}

void Preview::slotApply() {
	if (isChanged()) {
		applyChange();
		markUnchanged();
	}
}

void Preview::slotOk() {
	slotApply();
	accept();
}

void Preview::load() {
    KURL url = KFileDialog::getOpenURL(TQString(), m_fileSelector, this, i18n("Open Board Layout" ));
    if ( !url.isEmpty() ) {
        m_selectedFile = url.path();
        drawPreview();
        m_drawFrame->repaint(0,0,-1,-1,false);
        markChanged();
    }
}

// Top level preview drawing method. Background, tileset and layout
// are initialised from the preferences. Depending on the type
// of preview dialog we pick up the selected file for one of these
// chaps.

void Preview::drawPreview()
{
	TQString tile = Prefs::tileSet();
	TQString back = Prefs::background();
	TQString layout = Prefs::layout();
	
	switch (m_previewType)
	{
		case background:
			back = m_selectedFile;
		break;
		
		case tileset:
			tile = m_selectedFile;
		break;
		
		case board:
			layout = m_selectedFile;
		break;
		
		case theme:
			// a theme is quite a bit of work. We load the
			// specified bits in (layout, background and tileset
			if (!m_selectedFile.isEmpty())
			{
				TQString backRaw, layoutRaw, tilesetRaw, magic;
				
				TQFile in(m_selectedFile);
				if (in.open(IO_ReadOnly))
				{
					TQTextStream stream(&in);
					magic = stream.readLine();
					if (magic != themeMagicV1_0)
					{
						in.close();
						KMessageBox::sorry(this, i18n("That is not a valid theme file."));
						break;
					}
					tilesetRaw = stream.readLine();
					backRaw = stream.readLine();
					layoutRaw = stream.readLine();
					in.close();
					
					tile = tilesetRaw;
					tile.replace(":", "/kmahjongg/pics/");
					if (!TQFile::exists(tile))
					{
						tile = tilesetRaw;
						tile = "pics/" + tile.right(tile.length() - tile.find(":") - 1 );
						tile = locate("appdata", tile);
					}
					
					back = backRaw;
					back.replace(":", "/kmahjongg/pics/");
					if (!TQFile::exists(back))
					{
						back = backRaw;
						back = "pics/" + back.right(back.length() - back.find(":") - 1);
						back = locate("appdata", back);
					}
					
					layout = layoutRaw;
					layout.replace(":", "/kmahjongg/pics/");
					if (!TQFile::exists(layout))
					{
						layout = layoutRaw;
						layout = "pics/" + layout.right(layout.length() - layout.find(":") - 1);
						layout = locate("appdata", layout);
					}
					
					m_themeBack=back;
					m_themeLayout=layout;
					m_themeTileset=tile;
				}
			}
		break;
	}
	
	renderBackground(back);
	renderTiles(tile, layout);
}

void Preview::paintEvent( TQPaintEvent*  ){
  m_drawFrame->repaint(false);
}

// the user selected ok, or apply. This method passes the changes
// across to the game widget and if necessary forces a board redraw
// (unnecessary on layout changes since it only effects the next game)
void Preview::applyChange()
{
	switch (m_previewType)
	{
		case background:
			loadBackground(m_selectedFile, false);
		break;
		
		case tileset:
			loadTileset(m_selectedFile);
		break;
		
		case board:
			loadBoard(m_selectedFile);
		break;
		
		case theme:
			if (!m_themeLayout.isEmpty() && !m_themeBack.isEmpty() && !m_themeTileset.isEmpty())
			{
				loadBackground(m_themeBack, false);
				loadTileset(m_themeTileset);
				loadBoard(m_themeLayout);
			}
		break;
    }

	// don't redraw for a layout change
	if (m_previewType == board  || m_previewType == theme) layoutChange();
	else boardRedraw(true);

	// either way we mark the current value as unchanged
	markUnchanged();
}

// Render the background to the pixmap.
void Preview::renderBackground(const TQString &bg) {
   TQImage img;
   TQImage tmp;
   TQPixmap *p;
   TQPixmap *b;
   p = m_drawFrame->getPreviewPixmap();
   m_back.load(bg, p->width(), p->height());
   b = m_back.getBackground();
   bitBlt( p, 0,0,
            b,0,0, b->width(), b->height(), CopyROP );
}

// This method draws a mini-tiled board with no tiles missing.

void Preview::renderTiles(const TQString &file, const TQString &layout) {
    m_tiles.loadTileset(file, true);
    m_boardLayout.loadBoardLayout(layout);

    TQPixmap *dest = m_drawFrame->getPreviewPixmap();
    int xOffset = m_tiles.width()/2;
    int yOffset = m_tiles.height()/2;
    short tile = 0;

    // we iterate over the depth stacking order. Each successive level is
    // drawn one indent up and to the right. The indent is the width
    // of the 3d relief on the tile left (tile shadow width)
    for (int z=0; z<BoardLayout::depth; z++) {
        // we draw down the board so the tile below over rights our border
        for (int y = 0; y < BoardLayout::height; y++) {
            // drawing right to left to prevent border overwrite
            for (int x=BoardLayout::width-1; x>=0; x--) {
                int sx = x*(m_tiles.qWidth()  )+xOffset;
                int sy = y*(m_tiles.qHeight()  )+yOffset;
                if (m_boardLayout.getBoardData(z, y, x) != '1') {
                    continue;
                }
                TQPixmap *t = m_tiles.unselectedPixmaps(tile);

                // Only one compilcation. Since we render top to bottom , left
                // to right situations arise where...:
                // there exists a tile one q height above and to the left
                // in this situation we would draw our top left border over it
                // we simply split the tile draw so the top half is drawn
                // minus border

                if ((x>1) && (y>0) && m_boardLayout.getBoardData(z,y-1,x-2)=='1'){
                    bitBlt( dest, sx+2, sy,
                        t, 2,0, t->width(), t->height()/2, CopyROP );
                    bitBlt( dest, sx, sy+t->height()/2,
			t, 0,t->height()/2,t->width(),t->height()/2,CopyROP);
                } else {

                bitBlt( dest, sx, sy,
                    t, 0,0, t->width(), t->height(), CopyROP );
                }
                tile++;
                if (tile == 35)
                    tile++;
                tile = tile % 43;
            }
        }
        xOffset +=m_tiles.shadowSize();
        yOffset -=m_tiles.shadowSize();
    }
}

// this really does not belong here. It will be fixed in v1.1 onwards
void Preview::saveTheme() {
    TQString tile = Prefs::tileSet();
    TQString back = Prefs::background();
    TQString layout = Prefs::layout();
    
    TQString with = ":";
    // we want to replace any path in the default store
    // with a +
    TQRegExp p(locate("data_dir", "/kmahjongg/pics/"));

    back.replace(p,with);
    tile.replace(p,with);
    layout.replace(p,with);


    // Get the name of the file to save
    KURL url = KFileDialog::getSaveURL(
        NULL,
        "*.theme",
        parentWidget(),
        i18n("Save Theme" ));
    if ( url.isEmpty() )
        return;

   if( !url.isLocalFile() )
   {
      KMessageBox::sorry( this, i18n( "Only saving to local files currently supported." ) );
      return;
   }

    // Are we over writing an existin file, or was a directory selected?
    TQFileInfo f( url.path() );
    if( f.isDir() )
        return;
    if (f.exists()) {
        // if it already exists, querie the user for replacement
        int res=KMessageBox::warningContinueCancel(this,
                        i18n("A file with that name "
                                           "already exists. Do you "
                                           "wish to overwrite it?"),TQString(),i18n("Overwrite"));
        if (res != KMessageBox::Continue)
                return ;
    }
    FILE *outFile = fopen( TQFile::encodeName(url.path()), "w" );
    if (outFile == NULL) {
        KMessageBox::sorry(this,
                i18n("Could not write to file. Aborting."));
        return;
    }

    fprintf(outFile,"%s\n%s\n%s\n%s\n",
		themeMagicV1_0,
		tile.utf8().data(),
		back.utf8().data(),
		layout.utf8().data());
    fclose(outFile);
}

FrameImage::FrameImage (TQWidget *parent, const char *name)
  : TQFrame(parent, name)
{
	rx = -1;
	thePixmap = new TQPixmap();
}

FrameImage::~FrameImage()
{
	delete thePixmap;
}

void FrameImage::setGeometry(int x, int y, int w, int h) {
    TQFrame::setGeometry(x,y,w,h);

    thePixmap->resize(size());

}

void FrameImage::paintEvent( TQPaintEvent* pa )
{
    TQFrame::paintEvent(pa);

    TQPainter p(this);


    TQPen line;
    line.setStyle(Qt::DotLine);
    line.setWidth(2);
    line.setColor(yellow);
    p.setPen(line);
    p.setBackgroundMode(Qt::OpaqueMode);
    p.setBackgroundColor(black);

    int x = pa->rect().left();
    int y = pa->rect().top();
    int h = pa->rect().height();
    int w  = pa->rect().width();

    p.drawPixmap(x+frameWidth(),y+frameWidth(),*thePixmap,x+frameWidth(),y+frameWidth(),w-(2*frameWidth()),h-(2*frameWidth()));
    if (rx >=0) {

	p.drawRect(rx, ry, rw, rh);
	p.drawRect(rx+rs, ry, rw-rs, rh-rs);
	p.drawLine(rx, ry+rh, rx+rs, ry+rh-rs);

	int midX = rx+rs+((rw-rs)/2);
	int midY = ry+((rh-rs)/2);
	switch (rt) {
		case 0:  // delete mode cursor
			p.drawLine(rx+rs, ry, rx+rw, ry+rh-rs);
			p.drawLine(rx+rw, ry, rx+rs, ry+rh-rs);
		break;
		case 1: // insert cursor
			p.drawLine(midX, ry, midX, ry+rh-rs);
			p.drawLine(rx+rs, midY, rx+rw, midY);
		break;
		case 2: // move mode cursor
			p.drawLine(midX, ry, rx+rw, midY);
			p.drawLine(rx+rw, midY, midX, ry+rh-rs);
			p.drawLine(midX, ry+rh-rs, rx+rs, midY);
			p.drawLine(rx+rs, midY, midX, ry);

		break;
	}

    }
}

void FrameImage::setRect(int x,int y,int w,int h, int s, int t)
{
	rx = x;
	ry = y;
	rw = w;
	rh = h;
	rt = t;
	rs = s;
}

// Pass on the mouse presed event to our owner

void FrameImage::mousePressEvent(TQMouseEvent *m) {
	mousePressed(m);
}

void FrameImage::mouseMoveEvent(TQMouseEvent *e) {
	mouseMoved(e);
}

#include "Preview.moc"