summaryrefslogtreecommitdiffstats
path: root/libktorrent/labelview.cpp
blob: b488271dee7a89a7edcddc88729fb86f02a725dc (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
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   [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.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/
#include <algorithm>
#include <tqlayout.h>
#include <tqlabel.h>
#include <kiconloader.h>
#include <tdeglobalsettings.h>
#include <util/log.h>
#include "labelview.h"

using namespace bt;

namespace kt
{
	LabelViewItem::LabelViewItem(const TQString & icon,const TQString & title,const TQString & description,LabelView* view)
		: LabelViewItemBase(view),odd(false),selected(false)
	{
		icon_lbl->setPixmap(DesktopIcon(icon));
		title_lbl->setText(title);
		description_lbl->setText(description);
		setOdd(false);
	}
	
	LabelViewItem::~LabelViewItem()
	{
	}
	
	void LabelViewItem::setTitle(const TQString & title)
	{
		title_lbl->setText(title);
	}
	
	void LabelViewItem::setDescription(const TQString & d)
	{
		description_lbl->setText(d);
	}
	
	void LabelViewItem::setIcon(const TQString & icon)
	{
		icon_lbl->setPixmap(DesktopIcon(icon));
	}
	
	void LabelViewItem::setOdd(bool o)
	{
		odd = o;
		setSelected(selected);
	}
	
	void LabelViewItem::setSelected(bool sel)
	{
		selected = sel;

		if (selected)
		{
			setPaletteBackgroundColor(TDEGlobalSettings::highlightColor());
			setPaletteForegroundColor(TDEGlobalSettings::highlightedTextColor());
		}
		else if (odd)
		{
			setPaletteBackgroundColor(TDEGlobalSettings::baseColor());
			setPaletteForegroundColor(TDEGlobalSettings::textColor());
		}
		else
		{
			setPaletteBackgroundColor(TDEGlobalSettings::alternateBackgroundColor());
			setPaletteForegroundColor(TDEGlobalSettings::textColor());
		}
	}
	
	bool LabelViewItem::operator < (const LabelViewItem & item)
	{
		return title_lbl->text() < item.title_lbl->text();
	}
	
	void LabelViewItem::mousePressEvent(TQMouseEvent *e)
	{
		if (e->button() == TQt::LeftButton)
		{
			clicked(this);
		}

		setFocus();
		TQWidget::mousePressEvent(e);
	}
	
	typedef std::list<LabelViewItem*>::iterator LabelViewItr;
	typedef std::list<LabelViewItem*>::const_iterator LabelViewCItr;
	
	class LabelViewBox : public TQWidget
	{
		TQVBoxLayout* layout;
	public:
		LabelViewBox(TQWidget* parent) : TQWidget(parent)
		{
			setPaletteBackgroundColor(TDEGlobalSettings::baseColor());
			layout = new TQVBoxLayout(this);
			layout->setMargin(0);
		}
		
		virtual ~LabelViewBox()
		{}
		
		void add(LabelViewItem* item)
		{
			item->reparent(this,TQPoint(0,0));
			layout->add(item);
			item->show();
		}
		
		void remove(LabelViewItem* item)
		{
			item->hide();
			layout->remove(item);
			item->reparent(0,TQPoint(0,0));
		}
		
		void sorted(const std::list<LabelViewItem*> items)
		{
			for (LabelViewCItr i = items.begin();i != items.end();i++)
				layout->remove(*i);
			
			for (LabelViewCItr i = items.begin();i != items.end();i++)
				layout->add(*i);
		}
	};
	

	
	///////////////////////////////////////

	LabelView::LabelView ( TQWidget *parent, const char *name )
			: TQScrollView ( parent, name ),selected(0)
	{
		item_box = new LabelViewBox(this->viewport());
		setResizePolicy(TQScrollView::AutoOneFit);
	
		addChild(item_box, 0, 0);
		item_box->show();
	}


	LabelView::~LabelView()
	{}

	void LabelView::addItem(LabelViewItem* item)
	{
		item_box->add(item);
		items.push_back(item);
		item->setOdd(items.size() % 2 == 1);
		
		connect(item, TQ_SIGNAL(clicked(LabelViewItem*)),
				this, TQ_SLOT(onItemClicked(LabelViewItem*)));
	}
	
	void LabelView::removeItem(LabelViewItem* item)
	{
		LabelViewItr i = std::find(items.begin(),items.end(),item);
		if (i != items.end())
		{
			item_box->remove(item);
			items.erase(i);
			disconnect(item, TQ_SIGNAL(clicked(LabelViewItem*)),
					this, TQ_SLOT(onItemClicked(LabelViewItem*)));
			
			// check for selected being equal to item
			if (item == selected)
				selected = 0;
			
			// update odd status of each item
			updateOddStatus();	
		}
	}
	
	void LabelView::updateOddStatus()
	{
		bool odd = true;
		LabelViewItr i = items.begin();
		while (i != items.end())
		{
			LabelViewItem* item = *i;
			item->setOdd(odd);
			odd = !odd;
			i++;
		}
	}
	
	void LabelView::onItemClicked(LabelViewItem* it)
	{
		if (selected == it)
			return;
		
		if (selected)
			selected->setSelected(false);
		
		selected = it;
		selected->setSelected(true);
		currentChanged(selected);
	}
	
	void LabelView::clear()
	{
		LabelViewItr i = items.begin();
		while (i != items.end())
		{
			LabelViewItem* item = *i;
			item_box->remove(item);
			i = items.erase(i);
			delete item;
		}
		selected = 0;
	}
	
	void LabelView::update()
	{
		LabelViewItr i = items.begin();
		while (i != items.end())
		{
			LabelViewItem* item = *i;
			item->update();
			i++;
		}
	}
	
	struct LabelViewItemCmp
	{
		bool operator() (LabelViewItem* a,LabelViewItem* b)
		{
			return *a < *b;
		}
	};
	
	void LabelView::sort()
	{
		items.sort(LabelViewItemCmp());
		item_box->sorted(items);
		updateOddStatus();
	}

}
#include "labelview.moc"