summaryrefslogtreecommitdiffstats
path: root/src/modules/objects/class_urllabel.cpp
blob: 544153c2cb3a547289c966ed129be00f765c0a77 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//vim: ts=8
//   File : class_urllabel.cpp
//   Creation date : Thu Feb 18 1:27:44 CEST 2001 by Krzysztof Godlewski
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-2000 Krzysztof Godlewski
//   Copyright (C) 1999-2000 Szymon Stefanek (pragma at kvirc dot net)
//
//   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 opinion) 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 "class_urllabel.h"

#include "kvi_debug.h"
#include "kvi_locale.h"
#include "kvi_error.h"

#include "kvi_app.h"

#include "kvi_window.h"
#include "kvi_kvs_script.h"
#ifdef COMPILE_USE_QT4
#include <QMouseEvent>
#define QT_LEFT_BUTTON Qt::LeftButton
#else
#define QT_LEFT_BUTTON QEvent::LeftButton
#endif
//#warning "Functions to change colors for different states"
//#warning "$setUnderline(), $underline()"

//extern KviApp * g_pApp;
/*static KviScriptObjectClass * g_pUrlLabelClass = 0;

static KviScriptObject * urlLabelClassCreateInstance(KviScriptObjectClass * c,
	KviScriptObject * p, const char * n)
{
	return new KviScriptUrlLabelObject(c, p, n);
}

KviScriptUrlLabelObject::KviScriptUrlLabelObject(KviScriptObjectClass * c, \
KviScriptObject * p, const char *n) : KviScriptLabelObject(c, p, n)
{
	m_normalClr = QColor("black");
	m_enterClr = QColor("blue");
	m_activeClr = QColor("red");
	m_useSingleClick = true;
//	((QLabel *)widget())->setAutoResize(true);
	//widget()->setCursor(Qt::pointingHandCursor);
	//((KviScriptWidgetObject: * )p)->widget()->setCursor(Qt::pointingHandCursor);
}

KviScriptUrlLabelObject::~KviScriptUrlLabelObject()
{
}

bool KviScriptUrlLabelObject::eventFilter(QObject * o, QEvent * e)
{
	QPalette pal = ((QLabel *)widget())->palette();

	switch(e->type())
	{
		case QEvent::Enter:
			pal.setColor(QColorGroup::Foreground, m_enterClr);
			break;
		case QEvent::Leave:
			// this doesn't work...
			if(((QMouseEvent *)e)->state() & QEvent::LeftButton)
				pal.setColor(QColorGroup::Foreground, \
					m_activeClr);
			else
				pal.setColor(QColorGroup::Foreground, \
					m_normalClr);
			break;
		case QEvent::MouseButtonRelease:
			pal.setColor(QColorGroup::Foreground, m_enterClr);
			// perform actions here
			if(!m_lastClickWasDbl && !m_useSingleClick)
				break;
			if(!m_action.isEmpty())
			{
					
			}
			break;
		case QEvent::MouseButtonPress:
			m_lastClickWasDbl = false;
			if(!((QMouseEvent * )e)->button() & \
			  QEvent::LeftButton || !m_useSingleClick)
				break;
			pal.setColor(QColorGroup::Foreground, m_activeClr);
			break;
		case QEvent::MouseButtonDblClick:
			m_lastClickWasDbl = true;
			if(m_useSingleClick)
				break;
			pal.setColor(QColorGroup::Foreground, m_activeClr);
			break;
		default: // make gcc happy
		break;
	}
	
	((QLabel *)widget())->setPalette(pal);

	return false;
}
*/
//#warning "Docs for urllabel"

/*
	@doc:	urllabel
	@keyterms:
		display url, link
	@title:
		urllabel class
	@type:
		class
	@short:
		Displays an URI allowing to perform actions on click
	@inherits:
		[class]object[/class]
		[class]widget[/class]
		[class]label[/class]
	@description:
		This widget allows you to bind specific action on click. It can
		open an url, or execute any KVI++ code. The text color reacts on
		cursor movements and clicks.
	@functions:
		!fn: $setUrl(<text:string>)
		Sets the url to be opened when the links is activated. The way
		the label opens the url is defined in KVirc config dialog.
		!fn: <string> $url()
		Returns the currently set url.
		!fn: $setAction(<text:string>)
		Sets the action to be performed when the link is triggered. It
		can be any valid KVI++ code. Note that all KVirc commands must
		be prefixed with a '/', just as if you typed them in the input
		widget.
		!fn: <string> $action()
		Returns currently set action string.
		!fn: $setText(<text>)
		Sets the label's text. Reimplemented from internal reasons.
		!fn: $setCursorChange(<bEnabled:boolean>)
		Tells the label wheather to change or not the shape of cursor,
		when it is above the widget. Default is false.
		!fn: <boolean> $cursorChange()
		Returns true if the cursor changes over the label, false if not.
		!fn: $setUseSingleClick(<bEnabled:boolean>)
		If <bEnabled> is true, the label will react on single clicks,
		otherwise only double clicks will trigger the link.
		!fn: <boolean> $useSingleClick()
		Returns true if the label reacts on single clicks, false if it
		reacts only on double clicks.
*/

KVSO_BEGIN_REGISTERCLASS(KviKvsObject_urlabel,"urllabel","label")

	KVSO_REGISTER_HANDLER(KviKvsObject_urlabel,"setUrl",functionsetUrl)
	KVSO_REGISTER_HANDLER(KviKvsObject_urlabel,"url",functionurl)
	KVSO_REGISTER_HANDLER(KviKvsObject_urlabel,"setAction",functionsetAction)
	KVSO_REGISTER_HANDLER(KviKvsObject_urlabel,"action",functionaction)
	KVSO_REGISTER_HANDLER(KviKvsObject_urlabel,"setText",functionsetText)
	KVSO_REGISTER_HANDLER(KviKvsObject_urlabel,"setCursorChange",functionsetCursorChange)
	KVSO_REGISTER_HANDLER(KviKvsObject_urlabel,"cursorChange",functioncursorChange)
	KVSO_REGISTER_HANDLER(KviKvsObject_urlabel,"setUseSingleClick",functionsetUseSingleClick)
	KVSO_REGISTER_HANDLER(KviKvsObject_urlabel,"useSingleClick",functionuseSingleClick)
	
KVSO_END_REGISTERCLASS(KviKvsObject_urlabel)


KVSO_BEGIN_CONSTRUCTOR(KviKvsObject_urlabel,KviKvsObject_label)

	m_normalClr = QColor("black");
	m_enterClr = QColor("blue");
	m_activeClr = QColor("red");
	m_useSingleClick = true;
	
KVSO_END_CONSTRUCTOR(KviKvsObject_urlabel)

KVSO_BEGIN_DESTRUCTOR(KviKvsObject_urlabel)

KVSO_END_CONSTRUCTOR(KviKvsObject_urlabel)

bool KviKvsObject_urlabel::eventFilter(QObject * o, QEvent * e)
{
	QPalette pal = ((QLabel *)widget())->palette();

	switch(e->type())
	{
		case QEvent::Enter:
			pal.setColor(QColorGroup::Foreground, m_enterClr);
			break;
		case QEvent::Leave:
			// this doesn't work...
			if(((QMouseEvent *)e)->state() & QT_LEFT_BUTTON)
				pal.setColor(QColorGroup::Foreground, \
					m_activeClr);
			else
				pal.setColor(QColorGroup::Foreground, \
					m_normalClr);
			break;
		case QEvent::MouseButtonRelease:
			pal.setColor(QColorGroup::Foreground, m_enterClr);
			// perform actions here
			if(!m_lastClickWasDbl && !m_useSingleClick)
				break;
			if(!m_action.isEmpty())
			{
				KviKvsScript kvs("commandline",m_action);
				kvs.run(g_pActiveWindow,0,0/*,KviKvsScript::AssumeLocals*/);
					
			} else if(!m_url.isEmpty()) {
				KviKvsScript kvs("commandline","openurl "+m_url);
				kvs.run(g_pActiveWindow,0,0/*,KviKvsScript::AssumeLocals*/);
			}
			break;
		case QEvent::MouseButtonPress:
			m_lastClickWasDbl = false;
			if(!((QMouseEvent * )e)->button() & \
			  QT_LEFT_BUTTON || !m_useSingleClick)
				break;
			pal.setColor(QColorGroup::Foreground, m_activeClr);
			break;
		case QEvent::MouseButtonDblClick:
			m_lastClickWasDbl = true;
			if(m_useSingleClick)
				break;
			pal.setColor(QColorGroup::Foreground, m_activeClr);
			break;
		default: // make gcc happy
		break;
	}
	
	((QLabel *)widget())->setPalette(pal);

	return false;
}
bool KviKvsObject_urlabel::functionsetUrl(KviKvsObjectFunctionCall *c)
{
	QString szUrl;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("url",KVS_PT_STRING,0,szUrl)
	KVSO_PARAMETERS_END(c)
	if(!widget()) return true;
	if(szUrl.isEmpty()){
		c->error(__tr2qs("Not Enough Parameters"));
		return false;
		}
	m_url = szUrl;
	return true;
}
bool KviKvsObject_urlabel::functionurl(KviKvsObjectFunctionCall *c)
{
	c->returnValue()->setString(m_url);
	return true;
}
bool KviKvsObject_urlabel::functionsetAction(KviKvsObjectFunctionCall *c)
{
	QString szAction;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("action",KVS_PT_STRING,0,szAction)
	KVSO_PARAMETERS_END(c)
	if(!widget()) return true;
	if(szAction.isEmpty()){
		c->error(__tr2qs("Not Enough Parameters"));
		return false;
		}
	m_action = szAction;
	return true;
}
bool KviKvsObject_urlabel::functionsetText(KviKvsObjectFunctionCall *c)
{
	QString szText;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("text",KVS_PT_STRING,0,szText)
	KVSO_PARAMETERS_END(c)
	if(!widget()) return true;
	((QLabel *)widget())->setText(szText);
	((QLabel *)widget())->setFixedSize(((QLabel *)widget())->sizeHint());
	return true;
}
bool KviKvsObject_urlabel::functionaction(KviKvsObjectFunctionCall *c)
{
	c->returnValue()->setString(m_action);
	return true;
}
bool KviKvsObject_urlabel::functionsetCursorChange(KviKvsObjectFunctionCall *c)
{
	bool bFlag;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("bEnabled",KVS_PT_BOOLEAN,0,bFlag)
	KVSO_PARAMETERS_END(c)
	if(!widget())
		return true;
	if(m_changeCursor = bFlag)
		widget()->setCursor(Qt::pointingHandCursor);
	else
		widget()->setCursor(Qt::arrowCursor);
	return true;
}
bool KviKvsObject_urlabel::functionsetUseSingleClick(KviKvsObjectFunctionCall *c)
{
	bool bFlag;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("bEnabled",KVS_PT_BOOLEAN,0,bFlag)
	KVSO_PARAMETERS_END(c)
	m_useSingleClick = bFlag;
	return true;
}
bool KviKvsObject_urlabel::functionuseSingleClick(KviKvsObjectFunctionCall *c)
{
	c->returnValue()->setBoolean(m_useSingleClick);
	return true;
}
bool KviKvsObject_urlabel::functioncursorChange(KviKvsObjectFunctionCall *c)
{
	c->returnValue()->setBoolean(m_changeCursor);
	return true;
}