map.cpp 4.93 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
39
40
	const division *divByName = getDivision(d -> getUntranslatedName());
	const division *divByColor = getDivision(d -> getRGB());
	if ( ( divByName == NULL && divByColor == NULL ) || 
	     ( divByName != NULL && divByColor == NULL && !d->canAsk(division::eClick) && !divByName->canAsk(division::eClick) ) )
Albert Astals Cid's avatar
Albert Astals Cid committed
41
	{
42
		p_divisions.append(d);
Albert Astals Cid's avatar
Albert Astals Cid committed
43
44
45
46
47
48
		b = true;
	}
	else b = false;
	return b;
}

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

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

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

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

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

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;
}

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

105
KLocalizedString KGmap::getCapitalToDivisionQuestionPattern() const
106
{
107
	return ki18nc("@title:group", p_capitalToDivisionPattern.toUtf8());
108
109
}

110
KLocalizedString KGmap::getDivisionToCapitalQuestionPattern() const
111
{
112
	return ki18nc("@title:group", p_divisionToCapitalPattern.toUtf8());
113
114
115
116
}

QString KGmap::getCapitalToDivisionTitle() const
{
117
118
	if (p_capitalToDivisionTitle.isNull()) return i18n("%1 by Capital", getDivisionsString());
	else return p_capitalToDivisionTitle;
119
120
121
122
}

QString KGmap::getDivisionToCapitalTitle() const
{
123
124
	if (p_divisionToCapitalTitle.isNull()) return i18n("Capitals of %1", getDivisionsString());
	else return p_divisionToCapitalTitle;
125
126
}

Albert Astals Cid's avatar
Albert Astals Cid committed
127
QString KGmap::getFile() const
Albert Astals Cid's avatar
Albert Astals Cid committed
128
129
130
131
{
	return p_file;
}

132
133
134
135
136
137
QString KGmap::getFileName() const
{
	QFileInfo fi(p_file);
	return fi.fileName();
}

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

Albert Astals Cid's avatar
Albert Astals Cid committed
143
QString KGmap::getName() const
Albert Astals Cid's avatar
Albert Astals Cid committed
144
{
145
	return p_name;
Albert Astals Cid's avatar
Albert Astals Cid committed
146
147
}

Albert Astals Cid's avatar
Albert Astals Cid committed
148
149
150
151
152
153
QString KGmap::getDivisionsString() const
{
	if (p_divisionsString.isNull()) return i18n("Regions");
	else return p_divisionsString;
}

154
division *KGmap::getRandomDivision(division::askMode am) const
Albert Astals Cid's avatar
Albert Astals Cid committed
155
{
156
	QList<division*> aux;
157
	int i = (int)((float)p_divisions.size() * KRandom::random() / (RAND_MAX + 1.0));
158
	if (!p_divisions[i] -> canAsk(am)) return getRandomDivision(am);
159
	else return p_divisions[i];
Albert Astals Cid's avatar
Albert Astals Cid committed
160
161
}

Albert Astals Cid's avatar
Albert Astals Cid committed
162
QString KGmap::getWhatIs(QRgb c, bool all) const
Albert Astals Cid's avatar
Albert Astals Cid committed
163
{
164
	// this is only asked from mapasker.cpp hence the division::eClick in canAsk
165
	division *d = getDivision(c);
Laurent Montel's avatar
Laurent Montel committed
166
	if (d == NULL) return QStringLiteral("nothing");
Albert Astals Cid's avatar
Albert Astals Cid committed
167
	else
Albert Astals Cid's avatar
Albert Astals Cid committed
168
	{
169
170
		if (all) return d -> getUntranslatedName();
		else if (d -> canAsk(division::eClick)) return d -> getUntranslatedName();
Laurent Montel's avatar
Laurent Montel committed
171
		else return QLatin1String("");
Albert Astals Cid's avatar
Albert Astals Cid committed
172
173
	}
}
Albert Astals Cid's avatar
Albert Astals Cid committed
174

175
176
177
178
179
QString KGmap::getAuthor() const
{
	return p_author;
}

Albert Astals Cid's avatar
Albert Astals Cid committed
180
181
QColor KGmap::getColor(const QString &s) const
{
182
	return QColor(getDivision(s)->getRGB());
Albert Astals Cid's avatar
Albert Astals Cid committed
183
}
184

185
QList<const division*> KGmap::getIgnoredDivisions(division::askMode am) const
186
{
187
	QList<const division*> ignoredDivisions;
188
	foreach (division* div, p_divisions)
189
		if (!div->canAsk(am))
190
			ignoredDivisions << div;
191
192
	return ignoredDivisions;
}
193

Laurent G's avatar
Laurent G committed
194
195
196
197
198
199
200
201
QList<const division*> KGmap::getAllDivisionsOrdered() const
{
	QList<const division*> constDivisions;
	foreach (const division* div, p_divisions)
		constDivisions << div;
	return constDivisions;
}

202
203
204
205
division *KGmap::getDivision(const QString &divisionName) const
{
	foreach (division* div, p_divisions)
	{
206
		if (div->getUntranslatedName() == divisionName) return div;
207
208
209
210
211
212
213
214
215
216
217
218
	}
	return NULL;
}

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