1 //=============================================================================
2 //
3 // File : KvsObject_label.cpp
4 // Creation date : Mon Sep 18 14:21:48 CEST 2000 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 // Copyright (C) 2014 OmegaPhil (OmegaPhil at startmail dot com)
10 //
11 // This program is FREE software. You can redistribute it and/or
12 // modify it under the terms of the GNU General Public License
13 // as published by the Free Software Foundation; either version 2
14 // of the License, or (at your option) any later version.
15 //
16 // This program is distributed in the HOPE that it will be USEFUL,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 // See the GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, write to the Free Software Foundation,
23 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 //=============================================================================
26
27 #include "KviError.h"
28 #include "kvi_debug.h"
29 #include "KvsObject_label.h"
30 #include "KviLocale.h"
31 #include "KviIconManager.h"
32
33 #include <QFont>
34 #include <QPixmap>
35 #include <QMessageBox>
36 #include <QLabel>
37
38 // Tables used in $setAlignment & $alignment
39 const char * const align_tbl[] = {
40 "Left",
41 "Right",
42 "HCenter",
43 "VCenter",
44 "Center",
45 "Top",
46 "Bottom",
47 "Justify"
48 };
49
50 const int align_cod[] = {
51 Qt::AlignLeft,
52 Qt::AlignRight,
53 Qt::AlignHCenter,
54 Qt::AlignVCenter,
55 Qt::AlignCenter,
56 Qt::AlignTop,
57 Qt::AlignBottom,
58 Qt::AlignJustify,
59 };
60
61 #define align_num (sizeof(align_tbl) / sizeof(align_tbl[0]))
62
63 // used in $frameStyle & $setFrameStyle
64 const char * const frame_tbl[] = {
65 "NoFrame",
66 "Box",
67 "Panel",
68 "WinPanel",
69 "Hline",
70 // shadow styles
71 "Plain",
72 "Raised",
73 "Sunken"
74 };
75
76 const int frame_cod[] = {
77 QFrame::NoFrame,
78 QFrame::Box,
79 QFrame::Panel,
80 QFrame::WinPanel,
81 QFrame::HLine,
82 QFrame::Plain,
83 QFrame::Raised,
84 QFrame::Sunken
85 };
86
87 #define frame_num (sizeof(frame_tbl) / sizeof(frame_tbl[0]))
88
89 /*
90 @doc: label
91 @keyterms:
92 label object class, show image
93 @title:
94 label class
95 @type:
96 class
97 @short:
98 Displays text or an image
99 @inherits:
100 [class]object[/class]
101 [class]widget[/class]
102 @description:
103 This widget can be used to display a text or an image.[br]
104 It can have different frame styles and text/image alignment.
105 @functions:
106 !fn: $setText(<text:string>)
107 Sets the text to be displayed by the label.
108 The text can contain limited HTML tags.
109 See also [classfnc]$text[/classfnc]().
110 !fn: <string> $text()
111 Returns the text currently displayed by the label.
112 See also [classfnc]$setText[/classfnc]().
113 !fn: <integer> $margin()
114 Returns current value of margin for this label (default is 0).
115 See also [classfnc]$setMargin[/classfnc]().
116 !fn: $setMargin(<margin:uint>)
117 Sets margin width / height to <margin>.
118 See also [classfnc]$margin[/classfnc]().
119 !fn: <string> $alignment()
120 Returns a string containing alignment flags that are set for
121 this label. The flags are separated by commas.[br]
122 An example output could look like this:[br]
123 [pre]Bottom, Right[/pre]
124 See [classfnc]$setAlignment[/classfnc]() for explanation of all
125 alignment flags.
126 !fn: $setAlignment(<flag1:string>, <flag2:string>, ...)
127 This function sets alignment flags, given as parameters, for
128 this label. Valid flags are:
129 [pre]
130 Right - Text is aligned to right border
131 Left - Text is aligned to left border
132 Top - Text is aligned to the top border
133 Bottom - Text is aligned to the bottom border
134 HCenter - Text is horizontally centered
135 VCenter - Text is vertically centered
136 Center - Equals HCenter + VCenter
137 Justify - Text is spaced apart to cover available room
138 [/pre]
139 It is obvious that you can not set [i]Right[/i]
140 and [i]Left[/i] simultaneously - this will [b]not[/b]
141 result in an error message - it will simply not work :)
142 !fn: $clear()
143 Clears the label. Equal to calling [classfnc]$setText[/classfnc]("")
144 !fn: <string> $frameStyle()
145 Returns a string containing this label's frame-style flags,
146 separated with commas. Output from this function could look like
147 this:
148 [pre]Panel, Raised[/pre]
149 See [classfnc]$setFrameStyle[/classfnc]() for a list of all
150 frame-style flags and their explanation.
151 !fn: $setFrameStyle(<flag1>, <flag2>, ...)
152 Sets the frame-style flags to the ones passed as arguments.
153 The flags determine the shape or shadow of the
154 label's frame. Valid shape flags are:[br]
155 [pre]
156 NoFrame - Draw no frame. You shouldn't specify a shadow when using this.
157 Box - Draws a rectangular box. Its borders can be [i]Raised[/i] or [i]Sunken[/i]
158 Panel - Draws a rectangular panel which can be [i]Raised[/i] or [i]Sunken[/i]
159 WinPanel - Similar to [i]Panel[/i], but is more in Win95 style
160 Hline - Draws a horizontal line that frames nothing (useful as separator)
161 [/pre]
162 Valid shadow flags are:[br]
163 [pre]
164 Plain - No 3D effect (draws using foreground color)
165 Raised - Makes the label look like it was raised above the parent widget
166 Sunken - Makes the label look like it was [i]pushed[/i] inside the parent widget
167 [/pre]
168 !fn: $setImage(<image_id>)
169 Sets the image to be displayed on this label.
170 Giving empty argument clears the pixmap.[br]
171 See the [doc:image_id]image identifier[/doc] documentation for
172 the explanation of the <image_id> parameter.
173
174 */
175
176 KVSO_BEGIN_REGISTERCLASS(KvsObject_label, "label", "widget")
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_label,setText)177 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_label, setText)
178 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_label, text)
179 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_label, margin)
180 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_label, setMargin)
181 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_label, alignment)
182 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_label, setAlignment)
183 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_label, clear)
184 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_label, frameStyle)
185 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_label, setFrameStyle)
186 KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_label, setImage)
187 KVSO_END_REGISTERCLASS(KvsObject_label)
188
189 KVSO_BEGIN_CONSTRUCTOR(KvsObject_label, KvsObject_widget)
190
191 KVSO_END_CONSTRUCTOR(KvsObject_label)
192
193 KVSO_BEGIN_DESTRUCTOR(KvsObject_label)
194
195 KVSO_END_CONSTRUCTOR(KvsObject_label)
196
197 bool KvsObject_label::init(KviKvsRunTimeContext *, KviKvsVariantList *)
198 {
199 SET_OBJECT(QLabel)
200 ((QLabel *)widget())->setWordWrap(true);
201 ((QLabel *)widget())->setTextInteractionFlags(Qt::TextSelectableByMouse);
202 return true;
203 }
204
KVSO_CLASS_FUNCTION(label,setText)205 KVSO_CLASS_FUNCTION(label, setText)
206 {
207 CHECK_INTERNAL_POINTER(widget())
208 QString szText;
209 KVSO_PARAMETERS_BEGIN(c)
210 KVSO_PARAMETER("text", KVS_PT_STRING, 0, szText)
211 KVSO_PARAMETERS_END(c)
212 ((QLabel *)widget())->setText(szText);
213 return true;
214 }
215
KVSO_CLASS_FUNCTION(label,text)216 KVSO_CLASS_FUNCTION(label, text)
217 {
218 CHECK_INTERNAL_POINTER(widget())
219 c->returnValue()->setString(((QLabel *)widget())->text());
220 return true;
221 }
222
KVSO_CLASS_FUNCTION(label,setMargin)223 KVSO_CLASS_FUNCTION(label, setMargin)
224 {
225 CHECK_INTERNAL_POINTER(widget())
226 kvs_uint_t iMargin;
227 KVSO_PARAMETERS_BEGIN(c)
228 KVSO_PARAMETER("margin", KVS_PT_UNSIGNEDINTEGER, 0, iMargin)
229 KVSO_PARAMETERS_END(c)
230 ((QLabel *)widget())->setMargin(iMargin);
231 return true;
232 }
KVSO_CLASS_FUNCTION(label,margin)233 KVSO_CLASS_FUNCTION(label, margin)
234 {
235 CHECK_INTERNAL_POINTER(widget())
236 c->returnValue()->setInteger(((QLabel *)widget())->margin());
237 return true;
238 }
239
KVSO_CLASS_FUNCTION(label,setAlignment)240 KVSO_CLASS_FUNCTION(label, setAlignment)
241 {
242 CHECK_INTERNAL_POINTER(widget())
243 QStringList alignment;
244 KVSO_PARAMETERS_BEGIN(c)
245 KVSO_PARAMETER("alignment", KVS_PT_STRINGLIST, KVS_PF_OPTIONAL, alignment)
246 KVSO_PARAMETERS_END(c)
247 int align, sum = 0;
248 for(auto & it : alignment)
249 {
250
251 align = 0;
252 for(unsigned int j = 0; j < align_num; j++)
253 {
254 if(KviQString::equalCI(it, align_tbl[j]))
255 {
256 align = align_cod[j];
257 break;
258 }
259 }
260 if(align)
261 sum = sum | align;
262 else
263 c->warning(__tr2qs_ctx("Unknown alignment '%Q'", "objects"), &it);
264 }
265 ((QLabel *)widget())->setAlignment((Qt::Alignment)sum);
266 return true;
267 }
268
KVSO_CLASS_FUNCTION(label,alignment)269 KVSO_CLASS_FUNCTION(label, alignment)
270 {
271 CHECK_INTERNAL_POINTER(widget())
272 int mode = ((QLabel *)widget())->alignment();
273 QString szAlignment = "";
274 for(unsigned int i = 0; i < align_num; i++)
275 {
276 if(mode == align_cod[i])
277 {
278 szAlignment = align_tbl[i];
279 break;
280 }
281 }
282 c->returnValue()->setString(szAlignment);
283 return true;
284 }
285
KVSO_CLASS_FUNCTION(label,clear)286 KVSO_CLASS_FUNCTION(label, clear)
287 {
288 CHECK_INTERNAL_POINTER(widget())
289 ((QLabel *)widget())->clear();
290 return true;
291 }
292
KVSO_CLASS_FUNCTION(label,setFrameStyle)293 KVSO_CLASS_FUNCTION(label, setFrameStyle)
294 {
295 CHECK_INTERNAL_POINTER(widget())
296 QStringList style;
297 KVSO_PARAMETERS_BEGIN(c)
298 KVSO_PARAMETER("style", KVS_PT_STRINGLIST, KVS_PF_OPTIONAL, style)
299 KVSO_PARAMETERS_END(c)
300 if(!widget())
301 return true;
302 int framestyle, sum = 0;
303 for(auto & it : style)
304 {
305 framestyle = 0;
306 for(unsigned int j = 0; j < align_num; j++)
307 {
308 if(KviQString::equalCI(it, frame_tbl[j]))
309 {
310 framestyle = frame_cod[j];
311 break;
312 }
313 }
314 if(framestyle)
315 sum = sum | framestyle;
316 else
317 c->warning(__tr2qs_ctx("Unknown style '%Q'", "objects"), &it);
318 }
319 ((QLabel *)widget())->setFrameStyle(sum);
320 return true;
321 }
322
KVSO_CLASS_FUNCTION(label,frameStyle)323 KVSO_CLASS_FUNCTION(label, frameStyle)
324 {
325 CHECK_INTERNAL_POINTER(widget())
326 int mode = ((QLabel *)widget())->frameStyle();
327 QString szStyle = "";
328 for(unsigned int i = 0; i < frame_num; i++)
329 {
330 if(mode == frame_cod[i])
331 {
332 szStyle = frame_tbl[i];
333 break;
334 }
335 }
336 c->returnValue()->setString(szStyle);
337 return true;
338 }
339
KVSO_CLASS_FUNCTION(label,setImage)340 KVSO_CLASS_FUNCTION(label, setImage)
341 {
342 CHECK_INTERNAL_POINTER(widget())
343 QString icon;
344 KVSO_PARAMETERS_BEGIN(c)
345 KVSO_PARAMETER("icon", KVS_PT_STRING, 0, icon)
346 KVSO_PARAMETERS_END(c)
347 QPixmap * pix = g_pIconManager->getImage(icon);
348 if(pix)
349 ((QLabel *)widget())->setPixmap(*pix);
350 return true;
351 }
352