1 //=============================================================================
2 //
3 // File : KvsObject_button.cpp
4 // Creation date : Wed 13 Sep 2000 02:42:05 CEST by Krzysztof Godlewski
5 //
6 // This file is part of the KVIrc IRC client distribution
7 // Copyright (C) 2000 Krzysztof Godlewski
8 // Copyright (C) 2000-2010 Szymon Stefanek (pragma at kvirc dot net)
9 //
10 // This program is FREE software. You can redistribute it and/or
11 // modify it under the terms of the GNU General Public License
12 // as published by the Free Software Foundation; either version 2
13 // of the License, or (at your option) any later version.
14 //
15 // This program is distributed in the HOPE that it will be USEFUL,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 // See the GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, write to the Free Software Foundation,
22 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 //
24 //=============================================================================
25
26 #include "kvi_debug.h"
27 #include "KviLocale.h"
28 #include "KviError.h"
29 #include "KviIconManager.h"
30 #include "KviFile.h"
31
32 #include "KvsObject_button.h"
33 #include "KvsObject_pixmap.h"
34
35 #include <QIcon>
36 #include <QPushButton>
37
38 /*
39 @doc:button
40 @title:
41 button class
42 @type:
43 class
44 @short:
45 Button widget.
46 @inherits:
47 [class]object[/class]
48 [class]button[/class]
49 @description:
50 This widget provides a push button
51 @functions:
52 !fn: $setText([<text:string>])
53 Set the button text.[br]
54 See also [classfnc]$text[/classfnc]().
55 !fn: <string> $text()
56 Return the button text.[br]
57 See also [classfnc]$setText[/classfnc]().
58 !fn: $setImage(<image_id_or_pixmap_object>)
59 Sets the icon for this button.
60 See the [doc:image_id]image identifier[/doc] documentation for the explanation of the <image_id> parameter.
61 !fn: $clickEvent()
62 Called by KVIrc when the mouse button is clicked.
63 The default implementation emits the [classfnc]$clicked[/classfnc]()signal.
64 @signals:
65 !sg: $clicked()
66 This signal is emitted by the default implementation of [classfnc]$clickEvent[/classfnc]().
67 @properties:
68 !pr: $scaledContents()
69 This property holds whether the label will scale its contents to fill all available space.
70 */
71
72 KVSO_BEGIN_REGISTERCLASS(KvsObject_button, "button", "widget")
73
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_button,setText)74 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_button, setText)
75 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_button, text)
76 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_button, clickEvent)
77 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_button, setImage)
78
79 KVSO_END_REGISTERCLASS(KvsObject_button)
80
81 KVSO_BEGIN_CONSTRUCTOR(KvsObject_button, KvsObject_widget)
82
83 KVSO_END_CONSTRUCTOR(KvsObject_button)
84
85 KVSO_BEGIN_DESTRUCTOR(KvsObject_button)
86
87 KVSO_END_DESTRUCTOR(KvsObject_button)
88
89 bool KvsObject_button::init(KviKvsRunTimeContext *, KviKvsVariantList *)
90 {
91 SET_OBJECT(QPushButton);
92 connect(widget(), SIGNAL(clicked()), this, SLOT(slotClicked()));
93 return true;
94 }
95
KVSO_CLASS_FUNCTION(button,text)96 KVSO_CLASS_FUNCTION(button, text)
97 {
98 CHECK_INTERNAL_POINTER(widget())
99 c->returnValue()->setString(((QPushButton *)widget())->text());
100 return true;
101 }
102
KVSO_CLASS_FUNCTION(button,setText)103 KVSO_CLASS_FUNCTION(button, setText)
104 {
105 CHECK_INTERNAL_POINTER(widget())
106 QString szText;
107 KVSO_PARAMETERS_BEGIN(c)
108 KVSO_PARAMETER("text", KVS_PT_STRING, 0, szText)
109 KVSO_PARAMETERS_END(c)
110 ((QPushButton *)widget())->setText(szText);
111 return true;
112 }
113
KVSO_CLASS_FUNCTION(button,setImage)114 KVSO_CLASS_FUNCTION(button, setImage)
115 {
116 CHECK_INTERNAL_POINTER(widget())
117 KviKvsVariant * pIcon;
118 KVSO_PARAMETERS_BEGIN(c)
119 KVSO_PARAMETER("icon_or_hobject", KVS_PT_VARIANT, 0, pIcon)
120 KVSO_PARAMETERS_END(c)
121 if(!pIcon)
122 {
123 c->warning(__tr2qs_ctx("Image parameter missing", "object"));
124 return true;
125 }
126 if(pIcon->isHObject())
127 {
128 kvs_hobject_t hObj;
129 pIcon->asHObject(hObj);
130 KviKvsObject * pObject = KviKvsKernel::instance()->objectController()->lookupObject(hObj);
131 if(!pObject)
132 {
133 c->warning(__tr2qs_ctx("Pixmap parameter is not an object!", "objects"));
134 return true;
135 }
136 if(pObject->inheritsClass("pixmap"))
137 ((QPushButton *)widget())->setIcon(QIcon(*((KvsObject_pixmap *)pObject)->getPixmap()));
138 else
139 c->warning(__tr2qs_ctx("Object pixmap required!", "object"));
140 return true;
141 }
142 QString szIcon;
143 pIcon->asString(szIcon);
144 QPixmap * pix = g_pIconManager->getImage(szIcon);
145 if(pix)
146 ((QPushButton *)widget())->setIcon(*pix);
147 else
148 ((QPushButton *)widget())->setIcon(QIcon());
149 return true;
150 }
KVSO_CLASS_FUNCTION(button,clickEvent)151 KVSO_CLASS_FUNCTION(button, clickEvent)
152 {
153 emitSignal("clicked", c);
154 return true;
155 }
156
157 // slots
slotClicked()158 void KvsObject_button::slotClicked()
159 {
160 KviKvsVariantList * params = nullptr;
161 callFunction(this, "clickEvent", params);
162 }
163