map.cpp 5.18 KB
Newer Older
Albert Astals Cid's avatar
Albert Astals Cid committed
1
/***************************************************************************
2
 *   Copyright (C) 2004-2007 by Albert Astals Cid                          *
Albert Astals Cid's avatar
Albert Astals Cid committed
3
 *   aacid@kde.org                                                         *
Albert Astals Cid's avatar
Albert Astals Cid committed
4
5
6
7
8
9
10
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

Albert Astals Cid's avatar
Albert Astals Cid committed
11
12
#include "map.h"

13
14
#include <stdlib.h> // for RAND_MAX

David Gil's avatar
David Gil committed
15
#include <klocalizedstring.h>
16
#include <krandom.h>
Albert Astals Cid's avatar
Albert Astals Cid committed
17

Albert Astals Cid's avatar
Albert Astals Cid committed
18
#include <qfile.h>
19
#include <qfileinfo.h>
Albert Astals Cid's avatar
Albert Astals Cid committed
20

Albert Astals Cid's avatar
Albert Astals Cid committed
21
22
#include "division.h"

Albert Astals Cid's avatar
Albert Astals Cid committed
23
KGmap::KGmap()
Albert Astals Cid's avatar
Albert Astals Cid committed
24
{
25
26
	p_capitalToDivisionPattern = I18N_NOOP2("@title:group", "%1 is the capital of...");
	p_divisionToCapitalPattern = I18N_NOOP2("@title:group", "The capital of %1 is...");
Albert Astals Cid's avatar
Albert Astals Cid committed
27
28
}

Albert Astals Cid's avatar
Albert Astals Cid committed
29
KGmap::~KGmap()
Albert Astals Cid's avatar
Albert Astals Cid committed
30
{
31
	qDeleteAll(p_divisions);
Albert Astals Cid's avatar
Albert Astals Cid committed
32
}
Albert Astals Cid's avatar
Albert Astals Cid committed
33

34
bool KGmap::addDivision(division *d)
Albert Astals Cid's avatar
Albert Astals Cid committed
35
36
{
	bool b;
37
38
	if ( ( getDivision(d -> getUntranslatedName()) == NULL && getDivision(d -> getRGB()) == NULL ) || 
	     ( getDivision(d -> getUntranslatedName()) != NULL && getDivision(d -> getRGB()) == NULL && !d->canAsk(division::eClick) && !getDivision(d -> getUntranslatedName())->canAsk(division::eClick) ) )
Albert Astals Cid's avatar
Albert Astals Cid committed
39
	{
40
		p_divisions.append(d);
Albert Astals Cid's avatar
Albert Astals Cid committed
41
42
43
44
45
46
		b = true;
	}
	else b = false;
	return b;
}

Albert Astals Cid's avatar
Albert Astals Cid committed
47
void KGmap::setFile(const QString &s)
Albert Astals Cid's avatar
Albert Astals Cid committed
48
49
50
51
{
	p_file = s;
}

Albert Astals Cid's avatar
Albert Astals Cid committed
52
bool KGmap::setMapFile(const QString &s)
53
54
{
	p_mapFile = s;
Albert Astals Cid's avatar
Albert Astals Cid committed
55
	return QFile::exists(s);
56
57
}

Albert Astals Cid's avatar
Albert Astals Cid committed
58
void KGmap::setName(const QString &s)
Albert Astals Cid's avatar
Albert Astals Cid committed
59
60
61
62
{
	p_name = s;
}

Albert Astals Cid's avatar
Albert Astals Cid committed
63
64
65
66
67
void KGmap::setDivisionsString(const QString &s)
{
	p_divisionsString = s;
}

68
69
70
71
72
void KGmap::setAuthor(const QString &s)
{
	p_author = s;
}

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
void KGmap::setCapitalToDivisionQuestionPattern(const QString &str)
{
	p_capitalToDivisionPattern = str;
}

void KGmap::setDivisionToCapitalQuestionPattern(const QString &str)
{
	p_divisionToCapitalPattern = str;
}

void KGmap::setCapitalToDivisionTitle(const QString &str)
{
	p_capitalToDivisionTitle = str;
}

void KGmap::setDivisionToCapitalTitle(const QString &str)
{
	p_divisionToCapitalTitle = str;
}

93
uint KGmap::count(division::askMode am) const
Albert Astals Cid's avatar
Albert Astals Cid committed
94
{
95
	uint count = 0;
96
	foreach(const division *d, p_divisions)
97
	{
98
		if (d->canAsk(am)) count++;
99
100
	}
	return count;
Albert Astals Cid's avatar
Albert Astals Cid committed
101
102
}

Albert Astals Cid's avatar
Albert Astals Cid committed
103
QString KGmap::getDivisionFlagFile(const QString &s) const
Albert Astals Cid's avatar
Albert Astals Cid committed
104
{
105
	return getDivision(s) -> getFlagFile();
Albert Astals Cid's avatar
Albert Astals Cid committed
106
107
}

Albert Astals Cid's avatar
Albert Astals Cid committed
108
QString KGmap::getDivisionCapital(const QString &s) const
109
{
110
	return getDivision(s) -> getCapital();
111
112
}

113
114
115
116
117
QStringList KGmap::getDivisionFalseCapitals(const QString &divisionName) const
{
	return getDivision(divisionName) -> getFalseCapitals();
}

118
KLocalizedString KGmap::getCapitalToDivisionQuestionPattern() const
119
{
120
	return ki18nc("@title:group", p_capitalToDivisionPattern.toUtf8());
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
}

QString KGmap::getDivisionToCapitalQuestionPattern() const
{
	return p_divisionToCapitalPattern;
}

QString KGmap::getCapitalToDivisionTitle() const
{
	return p_capitalToDivisionTitle;
}

QString KGmap::getDivisionToCapitalTitle() const
{
	return p_divisionToCapitalTitle;
}

Albert Astals Cid's avatar
Albert Astals Cid committed
138
QString KGmap::getFile() const
Albert Astals Cid's avatar
Albert Astals Cid committed
139
140
141
142
{
	return p_file;
}

143
144
145
146
147
148
QString KGmap::getFileName() const
{
	QFileInfo fi(p_file);
	return fi.fileName();
}

Albert Astals Cid's avatar
Albert Astals Cid committed
149
QString KGmap::getMapFile() const
150
151
152
153
{
	return p_mapFile;
}

Albert Astals Cid's avatar
Albert Astals Cid committed
154
QString KGmap::getName() const
Albert Astals Cid's avatar
Albert Astals Cid committed
155
{
156
	return p_name;
Albert Astals Cid's avatar
Albert Astals Cid committed
157
158
}

Albert Astals Cid's avatar
Albert Astals Cid committed
159
160
161
162
163
164
QString KGmap::getDivisionsString() const
{
	if (p_divisionsString.isNull()) return i18n("Regions");
	else return p_divisionsString;
}

165
QString KGmap::getRandomDivision(division::askMode am) const
Albert Astals Cid's avatar
Albert Astals Cid committed
166
{
167
	QList<division*> aux;
168
	int i = (int)((float)p_divisions.size() * KRandom::random() / (RAND_MAX + 1.0));
169
	if (!p_divisions[i] -> canAsk(am)) return getRandomDivision(am);
170
	else return p_divisions[i] -> getUntranslatedName();
Albert Astals Cid's avatar
Albert Astals Cid committed
171
172
}

Albert Astals Cid's avatar
Albert Astals Cid committed
173
QString KGmap::getWhatIs(QRgb c, bool all) const
Albert Astals Cid's avatar
Albert Astals Cid committed
174
{
175
	// this is only asked from mapasker.cpp hence the division::eClick in canAsk
176
	division *d = getDivision(c);
Laurent Montel's avatar
Laurent Montel committed
177
	if (d == NULL) return QStringLiteral("nothing");
Albert Astals Cid's avatar
Albert Astals Cid committed
178
	else
Albert Astals Cid's avatar
Albert Astals Cid committed
179
	{
180
181
		if (all) return d -> getUntranslatedName();
		else if (d -> canAsk(division::eClick)) return d -> getUntranslatedName();
Laurent Montel's avatar
Laurent Montel committed
182
		else return QLatin1String("");
Albert Astals Cid's avatar
Albert Astals Cid committed
183
184
	}
}
Albert Astals Cid's avatar
Albert Astals Cid committed
185

186
187
188
189
190
QString KGmap::getAuthor() const
{
	return p_author;
}

Albert Astals Cid's avatar
Albert Astals Cid committed
191
192
QColor KGmap::getColor(const QString &s) const
{
193
	return QColor(getDivision(s)->getRGB());
Albert Astals Cid's avatar
Albert Astals Cid committed
194
}
195

196
QList<const division*> KGmap::getIgnoredDivisions(division::askMode am) const
197
{
198
	QList<const division*> ignoredDivisions;
199
	foreach (division* div, p_divisions)
200
		if (!div->canAsk(am))
201
			ignoredDivisions << div;
202
203
	return ignoredDivisions;
}
204

Laurent G's avatar
Laurent G committed
205
206
207
208
209
210
211
212
QList<const division*> KGmap::getAllDivisionsOrdered() const
{
	QList<const division*> constDivisions;
	foreach (const division* div, p_divisions)
		constDivisions << div;
	return constDivisions;
}

213
214
215
216
division *KGmap::getDivision(const QString &divisionName) const
{
	foreach (division* div, p_divisions)
	{
217
		if (div->getUntranslatedName() == divisionName) return div;
218
219
220
221
222
223
224
225
226
227
228
229
	}
	return NULL;
}

division *KGmap::getDivision(QRgb c) const
{
	foreach (division* div, p_divisions)
	{
		if (div->getRGB() == c) return div;
	}
	return NULL;
}
230
231
232
233
234

bool KGmap::getDivisionCanAsk(const QString &div, division::askMode am) const
{
	return getDivision(div) -> canAsk(am);
}