aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/objects/KvsObject_button.cpp
blob: 1b70ea90232644dd639a79fe4106578fedff6fba (about) (plain) (blame)
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
//=============================================================================
//
//   File : KvsObject_button.cpp
//   Creation date : Wed 13 Sep 2000 02:42:05 CEST by Krzysztof Godlewski
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2000 Krzysztof Godlewski
//   Copyright (C) 2000-2010 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 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 "kvi_debug.h"
#include "KviLocale.h"
#include "KviError.h"
#include "KviIconManager.h"
#include "KviFile.h"

#include "KvsObject_button.h"
#include "KvsObject_pixmap.h"

#include <QIcon>
#include <QPushButton>

/*
	@doc:button
	@title:
		button class
	@type:
		class
	@short:
		Button widget.
	@inherits:
		[class]object[/class]
		[class]button[/class]
	@description:
		This widget provides a push button
	@functions:
		!fn: $setText([<text:string>])
		Set the button text.[br]
		See also [classfnc]$text[/classfnc]().
		!fn: <string> $text()
		Return the button text.[br]
		See also [classfnc]$setText[/classfnc]().
		!fn: $setImage(<image_id_or_pixmap_object>)
		Sets the icon for this button.
		See the [doc:image_id]image identifier[/doc] documentation for the explanation	of the <image_id> parameter.
		!fn: $clickEvent()
		Called by KVIrc when the mouse button is clicked.
		The default implementation emits the [classfnc]$clicked[/classfnc]()signal.
	@signals:
		!sg: $clicked()
		This signal is emitted by the default implementation of [classfnc]$clickEvent[/classfnc]().
	@properties:
		!pr: $scaledContents()
		This property holds whether the label will scale its contents to fill all available space.
*/

KVSO_BEGIN_REGISTERCLASS(KvsObject_button,"button","widget")

	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_button,setText)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_button,text)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_button,clickEvent)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_button,setImage)

KVSO_END_REGISTERCLASS(KvsObject_button)

KVSO_BEGIN_CONSTRUCTOR(KvsObject_button,KvsObject_widget)

KVSO_END_CONSTRUCTOR(KvsObject_button)


KVSO_BEGIN_DESTRUCTOR(KvsObject_button)

KVSO_END_DESTRUCTOR(KvsObject_button)

bool KvsObject_button::init(KviKvsRunTimeContext *,KviKvsVariantList *)
{
	SET_OBJECT(QPushButton);
	connect(widget(),SIGNAL(clicked()),this,SLOT(slotClicked()));
	return true;
}

KVSO_CLASS_FUNCTION(button,text)
{
	CHECK_INTERNAL_POINTER(widget())
	c->returnValue()->setString(((QPushButton *)widget())->text());
	return true;
}

KVSO_CLASS_FUNCTION(button,setText)
{
	CHECK_INTERNAL_POINTER(widget())
	QString szText;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("text",KVS_PT_STRING,0,szText)
	KVSO_PARAMETERS_END(c)
	((QPushButton *)widget())->setText(szText);
	return true;
}

KVSO_CLASS_FUNCTION(button,setImage)
{
	CHECK_INTERNAL_POINTER(widget())
	KviKvsVariant * pIcon;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("icon_or_hobject",KVS_PT_VARIANT,0,pIcon)
	KVSO_PARAMETERS_END(c)
	if(!pIcon)
	{
		c->warning(__tr2qs_ctx("Image parameter missing","object"));
		return true;
	}
	if(pIcon->isHObject())
	{
		kvs_hobject_t hObj;
		pIcon->asHObject(hObj);
		KviKvsObject *pObject = KviKvsKernel::instance()->objectController()->lookupObject(hObj);
		if (!pObject)
		{
			c->warning(__tr2qs_ctx("Pixmap parameter is not an object!","objects"));
			return true;
		}
		if(pObject->inheritsClass("pixmap"))
			((QPushButton *)widget())->setIcon(QIcon(*((KvsObject_pixmap *)pObject)->getPixmap()));
		else
			c->warning(__tr2qs_ctx("Object pixmap required!","object"));
		return true;
	}
	QString szIcon;
	pIcon->asString(szIcon);
	QPixmap * pix = g_pIconManager->getImage(szIcon);
	if(pix)	((QPushButton *)widget())->setIcon(*pix);
	else((QPushButton *)widget())->setIcon(QIcon());
	return true;
}
KVSO_CLASS_FUNCTION(button,clickEvent)
{
	emitSignal("clicked",c);
	return true;
}

// slots
void KvsObject_button::slotClicked()
{
	KviKvsVariantList *params=0;
	callFunction(this,"clickEvent",params);
}