1
//=============================================================================
3
// File : KvsObject_pixmap.cpp
4
// Creation date : Fri Mar 18 14:30:48 CEST 2005
5
// by Tonino Imbesi(Grifisx) and Alessandro Carbone(Noldor)
7
// This file is part of the KVIrc irc client distribution
8
// Copyright (C) 2005-2008 Alessandro Carbone (elfonol at gmail dot com)
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 opinion) any later version.
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.
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.
24
//=============================================================================
26
#include "KvsObject_pixmap.h"
27
#include "KvsObject_widget.h"
29
#include "KvsObject_memoryBuffer.h"
30
#include "KviAnimatedPixmap.h"
32
#include "kvi_debug.h"
33
#include "KviLocale.h"
46
This class provide a pixmap.
51
The pixmap class is an off-screen, pixel-based paint device.
53
!fn: $fill(<widget:object>,<x_offset:integer>,<y:offset>)
54
Fills the pixmap with the widget's background color or pixmap.[br]
55
Note that x_offset, y_offest are offsets in the widget.
56
!fn: $resize(<width:integer>,<height:integer>)
57
Resizes the pixmap to w width and h height. Set wh or hg to 0, to have a null pixmap.
58
!fn: $scale(<width:integer>,<height:integer>,[<aspect_ratio:string>])
59
Scales the pixmap or the animation by sx horizontally and sy vertically.
61
- IgnoreAspectRatio:the pixmap is scaled ignoring his aspect ratio.
62
- KeepAspectRatio: pixmap is scaled to a rectangle as large as possible inside size, preserving the aspect ratio.
63
- KeepAspectRatioByExpanding, the pixmap is scaled to a rectangle as small as possible outside size, preserving the aspect ratio.
64
Default value is KeepAspectRatio.
65
!fn: $load(<file_name:string>)
66
Load a pixmap from the file <file_name>.
67
!fn: $save(<file_name:string>)
68
Save the pixmap or the current animation frame to the file <file_name>.
69
!fn: $loadAnimation(<file_name:string>)
70
Load an animation from the file <file_name> and start it.
71
!fn: $startAnimation()
75
!fn: <integer> $height()
76
Return the height of the pixmap or of the animation.
77
!fn: <integer> $width()
78
Return the width of the pixmap or of the animation.
79
!fn: $frameChangedEvent()
80
This event is triggered by KVIrc when the current animation frame changes.
83
This signal is emitted by the default implementation of [classfnc]$frameChangedEvent[/classfnc]().[br]
88
KVSO_BEGIN_REGISTERCLASS(KvsObject_pixmap,"pixmap","object")
90
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,fill)
91
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,resize)
92
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,scale)
93
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,load)
94
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,loadAnimation)
95
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,save)
96
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,startAnimation)
97
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,stopAnimation)
98
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,loadFromMemoryBuffer)
99
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,height)
100
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,width)
101
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,grabWidget)
102
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,frameChangedEvent)
104
KVSO_END_REGISTERCLASS(KvsObject_pixmap)
107
KVSO_BEGIN_CONSTRUCTOR(KvsObject_pixmap,KviKvsObject)
108
m_pAnimatedPixmap = 0;
110
KVSO_END_CONSTRUCTOR(KvsObject_pixmap)
113
KVSO_BEGIN_DESTRUCTOR(KvsObject_pixmap)
115
if (m_pPixmap)delete m_pPixmap;
116
if (m_pAnimatedPixmap) delete m_pAnimatedPixmap;
117
KVSO_END_CONSTRUCTOR(KvsObject_pixmap)
120
KVSO_CLASS_FUNCTION(pixmap,fill)
122
KviKvsObject *pObject;
123
kvs_int_t iXoffset,iYoffset;
124
kvs_hobject_t hObject;
125
KVSO_PARAMETERS_BEGIN(c)
126
KVSO_PARAMETER("widget",KVS_PT_HOBJECT,0,hObject)
127
KVSO_PARAMETER("x_offset",KVS_PT_INT,0,iXoffset)
128
KVSO_PARAMETER("y_offset",KVS_PT_INT,0,iYoffset)
129
KVSO_PARAMETERS_END(c)
131
pObject=KviKvsKernel::instance()->objectController()->lookupObject(hObject);
132
CHECK_HOBJECT_IS_WIDGET(pObject)
133
if (m_pAnimatedPixmap)
135
delete m_pAnimatedPixmap;
138
if(!m_pPixmap) m_pPixmap=new QPixmap();
139
m_pPixmap->fill(((QWidget *)(pObject->object())),iXoffset,iYoffset);
142
KVSO_CLASS_FUNCTION(pixmap,resize)
144
kvs_int_t iWidth,iHeight;
145
KVSO_PARAMETERS_BEGIN(c)
146
KVSO_PARAMETER("width",KVS_PT_INTEGER,0,iWidth)
147
KVSO_PARAMETER("height",KVS_PT_INTEGER,0,iHeight)
148
KVSO_PARAMETERS_END(c)
149
if(m_pPixmap) delete m_pPixmap;
150
if (m_pAnimatedPixmap){
151
delete m_pAnimatedPixmap;
154
m_pPixmap=new QPixmap(iWidth,iHeight);
157
KVSO_CLASS_FUNCTION(pixmap,scale)
159
kvs_int_t iWidth,iHeight;
160
QString szAspectRatio;
161
KVSO_PARAMETERS_BEGIN(c)
162
KVSO_PARAMETER("width",KVS_PT_INTEGER,0,iWidth)
163
KVSO_PARAMETER("height",KVS_PT_INTEGER,0,iHeight)
164
KVSO_PARAMETER("aspect_ratio",KVS_PT_STRING,KVS_PF_OPTIONAL,szAspectRatio)
165
KVSO_PARAMETERS_END(c)
166
Qt::AspectRatioMode ratio=Qt::KeepAspectRatio;
167
if(!m_pPixmap || m_pAnimatedPixmap) return true;
168
if(!szAspectRatio.isEmpty())
170
if(KviQString::equalCI(szAspectRatio,"IgnoreAspectRatio")) ratio=Qt::IgnoreAspectRatio;
171
else if (KviQString::equalCI(szAspectRatio,"KeepAspectRatio")) ratio=Qt::KeepAspectRatio;
172
else if (KviQString::equalCI(szAspectRatio,"KeepAspectRatioByExpanding")) ratio=Qt::KeepAspectRatioByExpanding;
173
else c->warning(__tr2qs_ctx("Unknown aspect ratio %Q - Switching to KeepAspectRatio ratio","objects"),&szAspectRatio);
175
if (m_pAnimatedPixmap) m_pAnimatedPixmap->resize(QSize(iWidth,iHeight),ratio);
176
else *m_pPixmap=m_pPixmap->scaled(iWidth,iHeight,ratio,Qt::SmoothTransformation);
179
KVSO_CLASS_FUNCTION(pixmap,loadAnimation)
183
KVSO_PARAMETERS_BEGIN(c)
184
KVSO_PARAMETER("file",KVS_PT_STRING,0,szFile)
185
KVSO_PARAMETERS_END(c)
186
if(!QFile::exists(szFile))
188
c->warning(__tr2qs_ctx("I can't find the specified file '%Q'.","objects"),&szFile);
191
if (m_pAnimatedPixmap) delete m_pAnimatedPixmap;
197
m_pAnimatedPixmap= new KviAnimatedPixmap(szFile);
198
connect(m_pAnimatedPixmap,SIGNAL(frameChanged()),this,SLOT(frameChanged()));
202
KVSO_CLASS_FUNCTION(pixmap,startAnimation)
205
if (m_pAnimatedPixmap) m_pAnimatedPixmap->start();
209
KVSO_CLASS_FUNCTION(pixmap,stopAnimation)
212
if (m_pAnimatedPixmap) m_pAnimatedPixmap->stop();
216
KVSO_CLASS_FUNCTION(pixmap,load)
219
KVSO_PARAMETERS_BEGIN(c)
220
KVSO_PARAMETER("file",KVS_PT_STRING,0,szFile)
221
KVSO_PARAMETERS_END(c)
222
if(!QFile::exists(szFile))
224
c->warning(__tr2qs_ctx("I can't find the specified file '%Q'.","objects"),&szFile);
227
if (m_pAnimatedPixmap) delete m_pAnimatedPixmap;
228
if(!m_pPixmap) m_pPixmap=new QPixmap();
229
m_pPixmap->load(szFile);
232
KVSO_CLASS_FUNCTION(pixmap,save)
235
KVSO_PARAMETERS_BEGIN(c)
236
KVSO_PARAMETER("file",KVS_PT_STRING,0,szFile)
237
KVSO_PARAMETERS_END(c)
238
if (m_pAnimatedPixmap) m_pAnimatedPixmap->pixmap()->save(szFile);
239
else if(m_pPixmap) m_pPixmap->save(szFile);
243
KVSO_CLASS_FUNCTION(pixmap,save)
245
//CHECK_INTERNAL_POINTER(m_pPixmap)
247
KVSO_PARAMETERS_BEGIN(c)
248
KVSO_PARAMETER("file",KVS_PT_STRING,0,szFile)
249
KVSO_PARAMETERS_END(c)
250
if(!QFile::exists(szFile))
252
c->warning(__tr2qs_ctx("I can't find the specified file '%Q'.","objects"),&szFile);
255
if (m_pAnimatedPixmap) delete m_pAnimatedPixmap;
256
m_pAnimatedPixmap= new KviAnimatedPixmap(szFile);
257
connect(m_pAnimatedPixmap,SIGNAL(frameChanged()),this,SLOT(frameChanged()));
258
//bPixmapModified=true;
263
KVSO_CLASS_FUNCTION(pixmap,loadFromMemoryBuffer)
265
KviKvsObject * pObject;
266
kvs_hobject_t hObject;
267
KVSO_PARAMETERS_BEGIN(c)
268
KVSO_PARAMETER("hobject",KVS_PT_HOBJECT,0,hObject)
269
KVSO_PARAMETERS_END(c)
271
pObject=KviKvsKernel::instance()->objectController()->lookupObject(hObject);
274
c->warning(__tr2qs_ctx("Buffer parameter is not an object","objects"));
277
if (!pObject->inheritsClass("memorybuffer"))
279
c->warning(__tr2qs_ctx("Buffer parameter is not a memorybuffer object","objects"));
282
if (m_pAnimatedPixmap){
283
delete m_pAnimatedPixmap;
286
if (!m_pPixmap) m_pPixmap=new QPixmap();
287
m_pPixmap->loadFromData(((KvsObject_memoryBuffer *)pObject)->dataBuffer());
291
KVSO_CLASS_FUNCTION(pixmap,grabWidget)
293
CHECK_INTERNAL_POINTER(m_pPixmap)
294
KviKvsObject * pObject;
295
kvs_hobject_t hObject;
296
KVSO_PARAMETERS_BEGIN(c)
297
KVSO_PARAMETER("widget",KVS_PT_HOBJECT,0,hObject)
298
KVSO_PARAMETERS_END(c)
299
pObject=KviKvsKernel::instance()->objectController()->lookupObject(hObject);
300
CHECK_HOBJECT_IS_WIDGET(pObject)
301
if (m_pAnimatedPixmap){
302
delete m_pAnimatedPixmap;
305
if (!m_pPixmap) m_pPixmap=new QPixmap();
306
*m_pPixmap=QPixmap::grabWidget(((KvsObject_widget *)pObject)->widget());
307
/* QString szFileName;
308
KVSO_PARAMETERS_BEGIN(c)
309
KVSO_PARAMETER("file_name",KVS_PT_STRING,0,szFileName)
310
KVSO_PARAMETERS_END(c)
311
QPrinter printer;//(QPrinter::HighResolution);
312
printer.setOutputFormat(QPrinter::PdfFormat);
313
printer.setPaperSize(QPrinter::A4);
314
printer.setOutputFileName(szFileName);
316
QPixmap pixmap=QPixmap::grabWidget(((QTableWidget *)widget()));
317
QPainter painter(&printer);
318
painter.drawPixmap(QPoint(0,0), pixmap, pixmap.rect());
323
KVSO_CLASS_FUNCTION(pixmap,height)
325
if(!m_pAnimatedPixmap && !m_pPixmap) c->returnValue()->setInteger(0);
326
else if(m_pAnimatedPixmap) c->returnValue()->setInteger(m_pAnimatedPixmap->size().height());
327
else c->returnValue()->setInteger(m_pPixmap->height());
330
KVSO_CLASS_FUNCTION(pixmap,width)
332
if(!m_pAnimatedPixmap && !m_pPixmap) c->returnValue()->setInteger(0);
333
else if(m_pAnimatedPixmap) c->returnValue()->setInteger(m_pAnimatedPixmap->size().width());
334
else c->returnValue()->setInteger(m_pPixmap->width());
338
QPixmap * KvsObject_pixmap::getPixmap()
340
if (!m_pAnimatedPixmap && !m_pPixmap) return new QPixmap();
341
else if(m_pAnimatedPixmap) return m_pAnimatedPixmap->pixmap();
342
else return m_pPixmap;
344
KVSO_CLASS_FUNCTION(pixmap,frameChangedEvent)
346
emitSignal("frameChanged",c);
351
void KvsObject_pixmap::frameChanged()
353
KviKvsVariantList *params=0;
354
callFunction(this,"frameChangedEvent",params);
357
#ifndef COMPILE_USE_STANDALONE_MOC_SOURCES
358
#include "m_KvsObject_pixmap.moc"
359
#endif //!COMPILE_USE_STANDALONE_MOC_SOURCES