summaryrefslogtreecommitdiffstats
path: root/blinken/src/highscoredialog.cpp
blob: 1121eb306f6e1e329d3d9413f7211af22a88a323 (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
/***************************************************************************
 *   Copyright (C) 2005 by Albert Astals Cid <tsdgeos@terra.es>            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqtabbar.h>
#include <tqtabwidget.h>

#include <kapplication.h>
#include <kconfig.h>
#include <klocale.h>

#include "highscoredialog.h"
#include "counter.h"
#include "fontutils.h"
#include "settings.h"

static const int margin = 15;
static const int smallMargin = 5;
static const int namesFontSize = 25;

/* scoresWidget */

class scoresWidget : public TQWidget
{
	public:
		scoresWidget(TQWidget *parent, const TQValueList< TQPair<int, TQString> > &scores);
		TQSize calcSize();

	protected:
		void paintEvent(TQPaintEvent *);
	
	private:
		const TQValueList< TQPair<int, TQString> > &m_scores;
};

scoresWidget::scoresWidget(TQWidget *parent, const TQValueList< TQPair<int, TQString> > &scores) : TQWidget(parent, 0, WStaticContents | WNoAutoErase), m_scores(scores)
{
}

void scoresWidget::paintEvent(TQPaintEvent *)
{
	int w = width();
	int h = height();
	TQFont f;
	TQPixmap buf(w, h);
	TQPainter p(&buf);
	TQRect r;
	TQColor bg = paletteBackgroundColor();
 
	// bg color
	p.fillRect(0, 0, w, h, bg);
	
	p.setPen(black);
	
	if (blinkenSettings::customFont()) f = TQFont("Steve");
	p.setFont(f);
	f.setPointSize(fontUtils::fontSize(p, "A", 1000, namesFontSize));
	p.setFont(f);
	
	p.translate(margin, margin);
	
	TQValueList< TQPair<int, TQString> >::const_iterator it;
	for (it = m_scores.begin(); it != m_scores.end(); ++it)
	{
		counter::paint(p, !(*it).second.isEmpty(), (*it).first, false, TQColor(), TQColor(), TQColor());
		p.setPen(black);
		p.drawText(counter::width(false) + 2 * smallMargin, 30, (*it).second);
		p.translate(0, counter::height() + smallMargin);
	}
	
	bitBlt(this, 0, 0, &buf);
}

TQSize scoresWidget::calcSize()
{
	int mw, mh, lt;
	TQRect r;
	TQPainter p(this);
	TQFont f;
	
	if (blinkenSettings::customFont()) f = TQFont("Steve");
	p.setFont(f);
	f.setPointSize(fontUtils::fontSize(p, "A", 1000, namesFontSize));
	p.setFont(f);
	for (int i = 0; i < 3; i++)
	{
		lt = 0;
		TQValueList< TQPair<int, TQString> >::const_iterator it;
		for (it = m_scores.begin(); it != m_scores.end(); ++it)
		{
			r = p.boundingRect(TQRect(), TQt::AlignAuto, (*it).second);
			lt = TQMAX(lt, r.width());
		}
	}
	
	mw = margin + counter::width(false) + 2 * smallMargin + lt + margin;
	mh = margin * 2 + counter::height() * 5 + smallMargin * 4;
	
	TQSize size(mw, mh);
	setMinimumSize(size);
	resize(size);
	
	return size;
}

/* myTabWidget */

class myTabWidget : public TQTabWidget
{
	public:
		myTabWidget(TQWidget *parent) : TQTabWidget(parent) {}
		
		TQSize tabBarSizeHint() const
		{
			return tabBar() -> sizeHint();
		}
};

/* highScoreDialog */

highScoreDialog::highScoreDialog(TQWidget *parent) : KDialogBase(parent, 0, true, i18n("Highscores"), KDialogBase::Close)
{
	m_tw = new myTabWidget(this);
	setMainWidget(m_tw);
	
	TDEConfig *cfg = kapp -> config();
	for (int i = 1; i <= 3; i++)
	{
		cfg -> setGroup(TQString("Level%1").arg(i));
		for (int j = 1; j <= 5; j++)
		{
			m_scores[i-1].append(tqMakePair(cfg->readNumEntry(TQString("Score%1").arg(j)), cfg->readEntry(TQString("Name%1").arg(j))));
		}
	}
	
	m_tw -> addTab(new scoresWidget(m_tw, m_scores[0]), i18n("Level 1"));
	m_tw -> addTab(new scoresWidget(m_tw, m_scores[1]), i18n("Level 2"));
	m_tw -> addTab(new scoresWidget(m_tw, m_scores[2]), i18n("Level ?"));
}

bool highScoreDialog::scoreGoodEnough(int level, int score)
{
	level--;
	TQValueList< TQPair<int, TQString> >::iterator it, itEnd;
	it = m_scores[level].begin();
	itEnd = m_scores[level].end();
	while (it != itEnd && (*it).first >= score) it++;
	
	return (it != itEnd);
}

void highScoreDialog::addScore(int level, int score, const TQString &name)
{
	level--;
	TQValueList< TQPair<int, TQString> >::iterator it, itEnd;
	it = m_scores[level].begin();
	itEnd = m_scores[level].end();
	while (it != itEnd && (*it).first >= score) it++;
	
	if (it != itEnd)
	{
		m_scores[level].insert(it, tqMakePair(score, name));
		m_scores[level].remove(--m_scores[level].end());
		
		TDEConfig *cfg = kapp -> config();
		cfg -> setGroup(TQString("Level%1").arg(level + 1));
		int j;
		for (it = m_scores[level].begin(), j = 1; it != m_scores[level].end(); ++it, j++)
		{
			cfg->writeEntry(TQString("Score%1").arg(j), (*it).first);
			cfg->writeEntry(TQString("Name%1").arg(j), (*it).second);
		}
		cfg -> sync();
	}
}

void highScoreDialog::showLevel(int level)
{
	TQSize max, aux;
	m_tw -> setCurrentPage(level -1);
	
	for (int i = 0; i < 3; i++)
	{
		aux = static_cast<scoresWidget*>(m_tw -> page(i)) -> calcSize();
		max = max.expandedTo(aux);
	}
	if (max.width() < m_tw -> tabBarSizeHint().width() + 5) m_tw -> setMinimumSize(m_tw -> tabBarSizeHint().width() + 5, max.height() + m_tw -> tabBarSizeHint().height() + 5);
	
	exec();
	delete this;
}