summaryrefslogtreecommitdiffstats
path: root/kgeography/src/map.cpp
blob: 6c54db692bcfa1b0f6d550737dc706723154ce9e (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
/***************************************************************************
 *   Copyright (C) 2004 by Albert Astals Cid                               *
 *   [email protected]                                                      *
 *                                                                         *
 *   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 <stdlib.h> // for RAND_MAX

#include <tdeapplication.h>
#include <tdelocale.h>

#include <tqfile.h>
#include <tqfileinfo.h>

#include "division.h"
#include "map.h"

KGmap::KGmap()
{
	p_hasAllFlags = true;
}

KGmap::~KGmap()
{
	TQMap<TQRgb, division*>::iterator it;
	it = p_colorMap.begin();
	while (it != p_colorMap.end())
	{
		delete it.data();
		p_colorMap.remove(it);
		it = p_colorMap.begin();
	}
}

bool KGmap::addDivision(division *c)
{
	bool b;
	if (p_nameMap.find(c -> getName()) == p_nameMap.end() && 
		p_colorMap.find(c -> getRGB()) == p_colorMap.end())
	{
		p_colorMap.insert(c -> getRGB(), c);
		p_nameMap.insert(c -> getName(), c);
		b = true;
		if (c -> canAsk(false)) p_hasAllFlags = p_hasAllFlags && !c -> getFlagFile().isNull();
	}
	else b = false;
	return b;
}

void KGmap::setFile(const TQString &s)
{
	p_file = s;
}

bool KGmap::setMapFile(const TQString &s)
{
	p_mapFile = s;
	return TQFile::exists(s);
}

void KGmap::setName(const TQString &s)
{
	p_name = s;
}

uint KGmap::count(bool clickDivisionMode) const
{
	TQValueList<division*> aux = p_nameMap.values();
	TQValueList<division*>::const_iterator it = aux.begin();
	TQValueList<division*>::const_iterator end = aux.end();
	uint count = 0;
	for( ; it != end; ++it)
	{
		if ((*it)->canAsk(clickDivisionMode)) count++;
	}
	return count;
}

bool KGmap::hasAllFlags() const
{
	return p_hasAllFlags;
}

TQString KGmap::getDivisionFlagFile(const TQString &s) const
{
	return p_nameMap[s] -> getFlagFile();
}

TQString KGmap::getDivisionCapital(const TQString &s) const
{
	return p_nameMap[s] -> getCapital();
}

TQString KGmap::getFile() const
{
	return p_file;
}

TQString KGmap::getFileName() const
{
	TQFileInfo fi(p_file);
	return fi.fileName();
}

TQString KGmap::getMapFile() const
{
	return p_mapFile;
}

TQString KGmap::getName() const
{
	return p_name;
}

TQString KGmap::getRandomDivision(bool clickDivisionMode) const
{
	TQValueList<division*> aux;
	int i = (int)((float)p_nameMap.size() * kapp -> random() / (RAND_MAX + 1.0));
	aux = p_nameMap.values();
	if (!aux[i] -> canAsk(clickDivisionMode)) return getRandomDivision(clickDivisionMode);
	else return aux[i] -> getName();
}

TQString KGmap::getWhatIs(TQRgb c, bool all) const
{
	// this is only asked from mapasker.cpp hence the true in canAsk
	TQMap<TQRgb, division*>::const_iterator it;
	it = p_colorMap.find(c);
	if (it == p_colorMap.end()) return "nothing";
	else
	{
		if (all) return it.data() -> getName();
		else if (it.data() -> canAsk(true)) return it.data() -> getName();
		else return "";
	}
}

TQColor KGmap::getColor(const TQString &s) const
{
	TQValueList<division*> divisions;
	TQValueList<TQRgb> colors;
	division *d;
	int i;
	
	d = p_nameMap[s];
	divisions = p_colorMap.values();
	colors = p_colorMap.keys();
	
	i = 0;
	while(divisions[i] != d) i++;
	return TQColor(colors[i]);
}