summaryrefslogtreecommitdiffstats
path: root/atlantik/libatlantikui/portfolioview.cpp
blob: c2a7b1a436f886383b6ab2d1baf37fc06be63423 (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
// Copyright (c) 2002-2003 Rob Kaper <[email protected]>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; see the file COPYING.LIB.  If not, write to
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.

#include <tqpainter.h>
#include <tqcursor.h>

#include <kdebug.h>
#include <kdialogbase.h>
#include <kiconeffect.h>
#include <kglobalsettings.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kstandarddirs.h>

#include <atlantic_core.h>
#include <config.h>
#include <player.h>
#include <estate.h>
#include <estategroup.h>

#include "portfolioview.moc"

#define PE_DISTW	4
#define	PE_DISTH	4
#define PE_SPACE	2
#define PE_MARGINW	5
#define PE_MARGINH	2
#define ICONSIZE	48

PortfolioView::PortfolioView(AtlanticCore *core, Player *player, TQColor activeColor, TQColor inactiveColor, TQWidget *parent, const char *name) : TQWidget(parent, name)
{
	m_atlanticCore = core;
	m_player = player;
	m_activeColor = activeColor;
	m_inactiveColor = inactiveColor;
	m_lastPE = 0;

	qpixmap = 0;
	b_recreate = true;

	m_portfolioEstates.setAutoDelete(true);
	setBackgroundColor(TQt::white);
	setMinimumHeight(ICONSIZE);

	// Init icon
	m_image = 0;
	m_imageName = "hamburger.png";
	loadIcon();
}

PortfolioView::~PortfolioView()
{
	clearPortfolio();
	delete m_image;
	delete qpixmap;
}

Player *PortfolioView::player()
{
	return m_player;
}

void PortfolioView::buildPortfolio()
{
	if ( m_portfolioEstates.count() )
		clearPortfolio();

	// Loop through estate groups in order
	TQPtrList<EstateGroup> estateGroups = m_atlanticCore->estateGroups();
	PortfolioEstate *lastPE = 0, *firstPEprevGroup = 0;

	int x = 100, y = 25, marginHint = 5, bottom;
	bottom = ICONSIZE - PE_HEIGHT - marginHint;

	EstateGroup *estateGroup;
	for (TQPtrListIterator<EstateGroup> it(estateGroups); *it; ++it)
	{
		if ((estateGroup = *it))
		{
			// New group
			lastPE = 0;

			// Loop through estates
			TQPtrList<Estate> estates = m_atlanticCore->estates();
			Estate *estate;
			for (TQPtrListIterator<Estate> it(estates); *it; ++it)
			{
				if ((estate = *it) && estate->estateGroup() == estateGroup)
				{
					// Create PE
					PortfolioEstate *portfolioEstate = new PortfolioEstate(estate, m_player, false, this, "portfolioestate");
					m_portfolioEstates.append(portfolioEstate);

 					connect(portfolioEstate, TQT_SIGNAL(estateClicked(Estate *)), this, TQT_SIGNAL(estateClicked(Estate *)));
					if (lastPE)
					{
						x = lastPE->x() + 2;
						y = lastPE->y() + 4;
						if (y > bottom)
							bottom = y;
					}
					else if (firstPEprevGroup)
					{
						x = firstPEprevGroup->x() + PE_WIDTH + 8;
						y = 20 + marginHint;
						firstPEprevGroup = portfolioEstate;
					}
					else
					{
						x = ICONSIZE + marginHint;
						y = 20 + marginHint;
						if (y > bottom)
							bottom = y;
						firstPEprevGroup = portfolioEstate;
					}

					portfolioEstate->setGeometry(x, y, portfolioEstate->width(), portfolioEstate->height());
					portfolioEstate->show();

					connect(estate, TQT_SIGNAL(changed()), portfolioEstate, TQT_SLOT(estateChanged()));

					lastPE = portfolioEstate;
				}
			}
		}
	}
	setMinimumWidth(x + PE_WIDTH + marginHint);
	int minHeight = bottom + PE_HEIGHT + marginHint;
	if (minHeight > minimumHeight())
		setMinimumHeight(minHeight);
}

void PortfolioView::clearPortfolio()
{
	m_portfolioEstates.clear();
}

void PortfolioView::loadIcon()
{
	if (m_imageName == m_player->image())
		return;
	m_imageName = m_player->image();

	delete m_image;
	m_image = 0;

	if (!m_imageName.isEmpty())
	{
		TQString filename = locate("data", "atlantik/themes/default/tokens/" + m_imageName);
		if (TDEStandardDirs::exists(filename))
			m_image = new TQPixmap(filename);
	}

	if (!m_image)
	{
		return;

/*
		m_imageName = "hamburger.png";

		TQString filename = locate("data", "atlantik/themes/default/tokens/" + m_imageName);
		if (TDEStandardDirs::exists(filename))
			m_image = new TQPixmap(filename);
*/
	}
	else if (ICONSIZE > minimumHeight())
		setMinimumHeight(ICONSIZE);

	TQWMatrix m;
	m.scale(double(ICONSIZE) / m_image->width(), double(ICONSIZE) / m_image->height());
	TQPixmap *scaledPixmap = new TQPixmap(ICONSIZE, ICONSIZE);
	*scaledPixmap = m_image->xForm(m);

	delete m_image;
	m_image = scaledPixmap;
}

void PortfolioView::paintEvent(TQPaintEvent *)
{
	if (b_recreate)
	{
		delete qpixmap;
		qpixmap = new TQPixmap(width(), height());

		TQPainter painter;
		painter.begin(TQT_TQPAINTDEVICE(qpixmap), this);

		painter.setPen(TQt::white);
		painter.setBrush(TQt::white);
		painter.drawRect(rect());

		painter.setPen(m_player->hasTurn() ? m_activeColor : TQt::black);
		painter.setBrush(m_player->hasTurn() ? m_activeColor : TQt::black);
		painter.drawRect(0, 0, width(), 20);

		if (m_image)
		{
			painter.setPen(TQt::black);
			painter.setBrush(TQt::white);
			painter.drawRect(0, 0, ICONSIZE, ICONSIZE);

			painter.drawPixmap(0, 0, *m_image);
		}

		painter.setPen(TQt::white);
		painter.setFont(TQFont(TDEGlobalSettings::generalFont().family(), TDEGlobalSettings::generalFont().pointSize(), TQFont::Bold));
		painter.drawText(ICONSIZE + KDialog::marginHint(), 15, m_player->name());

		if ( m_portfolioEstates.count() )
			painter.drawText(width() - 50, 15, TQString::number(m_player->money()));
		else
		{
			painter.setPen(TQt::black);
			painter.setBrush(TQt::white);

			painter.setFont(TQFont(TDEGlobalSettings::generalFont().family(), TDEGlobalSettings::generalFont().pointSize(), TQFont::Normal));
			painter.drawText(ICONSIZE + KDialog::marginHint(), 30, m_player->host());
		}

		b_recreate = false;
	}
	bitBlt(this, 0, 0, qpixmap);
}

void PortfolioView::resizeEvent(TQResizeEvent *)
{
	b_recreate = true;
}

void PortfolioView::playerChanged()
{
	loadIcon();

	b_recreate = true;
	update();
}

void PortfolioView::mousePressEvent(TQMouseEvent *e)
{
	Player *playerSelf = m_atlanticCore->playerSelf();

	if ( e->button()==Qt::RightButton && (m_player != playerSelf) )
	{
		KPopupMenu *rmbMenu = new KPopupMenu(this);
		rmbMenu->insertTitle(m_player->name());

		if ( m_portfolioEstates.count() )
		{
			// Start trade
			rmbMenu->insertItem(i18n("Request Trade with %1").arg(m_player->name()), 0);
		}
		else
		{
			// Kick player
			rmbMenu->insertItem(i18n("Boot Player %1 to Lounge").arg(m_player->name()), 0);
			rmbMenu->setItemEnabled( 0, m_atlanticCore->selfIsMaster() );
		}

		connect(rmbMenu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotMenuAction(int)));
		TQPoint g = TQCursor::pos();
		rmbMenu->exec(g);
	}
}

void PortfolioView::slotMenuAction(int item)
{
	switch (item)
	{
	case 0:
		if ( m_portfolioEstates.count() )
			emit newTrade(m_player);
		else
			emit kickPlayer(m_player);
		break;
	}
}
#undef PE_DISTW
#undef PE_DISTH
#undef PE_SPACE
#undef PE_MARGINW
#undef PE_MARGINH
#undef ICONSIZE