aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/objects/KvsObject_pixmap.cpp
diff options
context:
space:
mode:
authorGravatar Noldor2011-04-28 16:21:09 +0000
committerGravatar Noldor2011-04-28 16:21:09 +0000
commit6ef086201df1e347590198f843c87ce320453176 (patch)
tree0846f136ec43f7ef5b91b3734e94462a60afa989 /src/modules/objects/KvsObject_pixmap.cpp
parentAdded alpha component in color options (diff)
downloadKVIrc-6ef086201df1e347590198f843c87ce320453176.tar.gz
KVIrc-6ef086201df1e347590198f843c87ce320453176.tar.bz2
KVIrc-6ef086201df1e347590198f843c87ce320453176.zip
Code improvements in kvs classes
git-svn-id: https://svn.kvirc.de/svn/trunk/kvirc@5805 17fca916-40b9-46aa-a4ea-0a15b648b75c
Diffstat (limited to 'src/modules/objects/KvsObject_pixmap.cpp')
-rw-r--r--src/modules/objects/KvsObject_pixmap.cpp414
1 files changed, 365 insertions, 49 deletions
diff --git a/src/modules/objects/KvsObject_pixmap.cpp b/src/modules/objects/KvsObject_pixmap.cpp
index a84798f3f..02e8110b7 100644
--- a/src/modules/objects/KvsObject_pixmap.cpp
+++ b/src/modules/objects/KvsObject_pixmap.cpp
@@ -50,9 +50,8 @@
@description:
The pixmap class is an off-screen, pixel-based paint device.
@functions:
- !fn: $fill(<widget:object>,<x_offset:integer>,<y:offset>)
- Fills the pixmap with the widget's background color or pixmap.[br]
- Note that x_offset, y_offest are offsets in the widget.
+ !fn: $fill(<colorname, rgb or hsv array value or [<red>,<green>,<blue>][<hue>,<saturation>,<value>],[opacity],[system color:RGB or HSV])
+ Fills the pixmap with color and opacity.[br]
!fn: $resize(<width:integer>,<height:integer>)
Resizes the pixmap to w width and h height. Set wh or hg to 0, to have a null pixmap.
!fn: $scale(<width:integer>,<height:integer>,[<aspect_ratio:string>])
@@ -98,6 +97,11 @@ KVSO_BEGIN_REGISTERCLASS(KvsObject_pixmap,"pixmap","object")
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,loadFromMemoryBuffer)
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,height)
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,width)
+ KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,rotate)
+
+ KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,mirrored)
+ KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,setPixel)
+ KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,toGrayScale)
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,grabWidget)
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_pixmap,frameChangedEvent)
@@ -106,7 +110,9 @@ KVSO_END_REGISTERCLASS(KvsObject_pixmap)
KVSO_BEGIN_CONSTRUCTOR(KvsObject_pixmap,KviKvsObject)
m_pAnimatedPixmap = 0;
+ m_currentType = Image;
m_pPixmap = 0;
+ m_pImage = 0;
KVSO_END_CONSTRUCTOR(KvsObject_pixmap)
@@ -114,44 +120,297 @@ KVSO_BEGIN_DESTRUCTOR(KvsObject_pixmap)
emit aboutToDie();
if (m_pPixmap)delete m_pPixmap;
if (m_pAnimatedPixmap) delete m_pAnimatedPixmap;
+ if (m_pImage) delete m_pImage;
KVSO_END_CONSTRUCTOR(KvsObject_pixmap)
KVSO_CLASS_FUNCTION(pixmap,fill)
{
- KviKvsObject *pObject;
- kvs_int_t iXoffset,iYoffset;
- kvs_hobject_t hObject;
+ kvs_int_t iCol1,iCol2,iCol3,iOpacity;
+ QString szColorMode,szColor;
+ KviKvsVariant *var1,*var2,*var3;
+ KVSO_PARAMETERS_BEGIN(c)
+ KVSO_PARAMETER("Color_1_Or_Colorname",KVS_PT_VARIANT,0,var1)
+ KVSO_PARAMETER("Color_2",KVS_PT_VARIANT,KVS_PF_OPTIONAL,var2)
+ KVSO_PARAMETER("Colo3_3",KVS_PT_VARIANT,KVS_PF_OPTIONAL,var3)
+ KVSO_PARAMETER("color_mode",KVS_PT_STRING,KVS_PF_OPTIONAL,szColorMode)
+ KVSO_PARAMETER("opacity",KVS_PT_INT,KVS_PF_OPTIONAL,iOpacity)
+ KVSO_PARAMETERS_END(c)
+ QColor col;
+ if (!var1->asInteger(iCol1))
+ {
+ var1->asString(szColor);
+ if (c->paramCount()<2) iOpacity=255;
+ else
+ {
+ if (!var2->asInteger(iOpacity))
+ {
+ c->warning(__tr2qs_ctx("The opacity parameter didn't evaluate to integer","objects"));
+ return true;
+ }
+ }
+ col.setNamedColor(szColor);
+ col.setAlpha(iOpacity);
+ }
+ else
+ {
+ if(c->paramCount()<3)
+ {
+ c->error(__tr2qs_ctx("Color name or triplette rgb/hsv value required","objects"));
+ return true;
+ }
+ if (!var2->asInteger(iCol2)||!var3->asInteger(iCol3))
+ {
+ c->error(__tr2qs_ctx("One of the triplette parameters didn't evaluate to an integer","objects"));\
+ return true;
+ }
+ if (c->paramCount()<5) iOpacity=255;
+ if(KviQString::equalCI(szColorMode, "HSV"))
+ col.setHsv(iCol1,iCol2,iCol3,iOpacity);
+ else
+ col.setRgb(iCol1,iCol2,iCol3,iOpacity);
+ }
+ if(m_currentType==Image)
+ {
+ if(m_pImage)
+ {
+ if(!m_pPixmap) m_pPixmap = new QPixmap();
+ *m_pPixmap = QPixmap::fromImage(*m_pImage);
+ delete m_pImage;
+ m_pImage = 0;
+ }
+ else m_pPixmap = 0;
+ }
+ else if(m_currentType==AnimatedPixmap)
+ {
+ delete m_pAnimatedPixmap;
+ m_pAnimatedPixmap=0;
+ }
+ m_currentType = Pixmap;
+ if(!m_pPixmap) m_pPixmap=new QPixmap();
+ m_pPixmap->fill(col);
+ return true;
+}
+
+KVSO_CLASS_FUNCTION(pixmap,resize)
+{
+ kvs_int_t iWidth,iHeight;
KVSO_PARAMETERS_BEGIN(c)
- KVSO_PARAMETER("widget",KVS_PT_HOBJECT,0,hObject)
- KVSO_PARAMETER("x_offset",KVS_PT_INT,0,iXoffset)
- KVSO_PARAMETER("y_offset",KVS_PT_INT,0,iYoffset)
+ KVSO_PARAMETER("width",KVS_PT_INTEGER,0,iWidth)
+ KVSO_PARAMETER("height",KVS_PT_INTEGER,0,iHeight)
KVSO_PARAMETERS_END(c)
+ if(m_currentType==Image)
+ {
+ if(m_pImage)
+ delete m_pImage;
+ m_pImage = 0;
+ }
+ else if(m_currentType==AnimatedPixmap)
+ {
+ if (m_pAnimatedPixmap)
+ delete m_pAnimatedPixmap;
+ m_pAnimatedPixmap=0;
+ }
+ else if(m_pPixmap)
+ {
+ delete m_pPixmap;
+ }
+ m_currentType = Image;
+ m_pImage=new QImage(iWidth,iHeight,QImage::Format_ARGB32_Premultiplied);
+ m_pImage->fill(Qt::transparent);
+ return true;
+}
- pObject=KviKvsKernel::instance()->objectController()->lookupObject(hObject);
- CHECK_HOBJECT_IS_WIDGET(pObject)
- if (m_pAnimatedPixmap)
+KVSO_CLASS_FUNCTION(pixmap,setPixel)
+{
+ kvs_int_t iCol1,iCol2,iCol3,iOpacity,iX,iY;
+ QString szColorMode,szColor;
+ KviKvsVariant *var1,*var2,*var3;
+ KVSO_PARAMETERS_BEGIN(c)
+ KVSO_PARAMETER("x",KVS_PT_INTEGER,0,iX)
+ KVSO_PARAMETER("y",KVS_PT_INTEGER,0,iY)
+ KVSO_PARAMETER("Color_1_Or_Colorname",KVS_PT_VARIANT,0,var1)
+ KVSO_PARAMETER("Color_2",KVS_PT_VARIANT,KVS_PF_OPTIONAL,var2)
+ KVSO_PARAMETER("Colo3_3",KVS_PT_VARIANT,KVS_PF_OPTIONAL,var3)
+ KVSO_PARAMETER("color_mode",KVS_PT_STRING,KVS_PF_OPTIONAL,szColorMode)
+ KVSO_PARAMETER("opacity",KVS_PT_INT,KVS_PF_OPTIONAL,iOpacity)
+ KVSO_PARAMETERS_END(c)
+ QColor col;
+ if (!var1->asInteger(iCol1))
+ {
+ var1->asString(szColor);
+ if (c->paramCount()<4) iOpacity=255;
+ else
+ {
+ if (!var2->asInteger(iOpacity))
+ {
+ c->warning(__tr2qs_ctx("The opacity parameter didn't evaluate to integer","objects"));
+ return true;
+ }
+ }
+ col.setNamedColor(szColor);
+ col.setAlpha(iOpacity);
+ }
+ else
+ {
+ if(c->paramCount()<5)
+ {
+ c->error(__tr2qs_ctx("Color name or triplette rgb/hsv value required","objects"));
+ return true;
+ }
+ if (!var2->asInteger(iCol2)||!var3->asInteger(iCol3))
+ {
+ c->error(__tr2qs_ctx("One of the triplette parameters didn't evaluate to an integer","objects"));\
+ return true;
+ }
+ if (c->paramCount()<7) iOpacity=255;
+ if(KviQString::equalCI(szColorMode, "HSV"))
+ col.setHsv(iCol1,iCol2,iCol3,iOpacity);
+ else
+ col.setRgb(iCol1,iCol2,iCol3,iOpacity);
+ }
+
+
+ if(m_currentType==AnimatedPixmap)
{
- delete m_pAnimatedPixmap;
- m_pAnimatedPixmap=0;
+ c->warning(__tr2qs_ctx("AnimatedPixmap not supported","objects"));
+ return true;
}
- if(!m_pPixmap) m_pPixmap=new QPixmap();
- m_pPixmap->fill(((QWidget *)(pObject->object())),iXoffset,iYoffset);
+ else if((m_currentType==Image && !m_pImage) || (m_currentType==Pixmap && !m_pPixmap))
+ {
+ c->error(__tr2qs_ctx("The pixmap is null","objects"));
+ return false;
+ }
+ else if(m_currentType==Pixmap)
+ {
+ if(m_pImage) delete m_pImage;
+ *m_pImage = m_pPixmap->toImage();
+ delete m_pPixmap;
+ }
+ m_currentType = Image;
+ m_pImage->setPixel(iX,iY,col.rgba());
return true;
}
-KVSO_CLASS_FUNCTION(pixmap,resize)
+
+KVSO_CLASS_FUNCTION(pixmap,rotate)
{
- kvs_int_t iWidth,iHeight;
+ kvs_real_t dAngle;
+ QString szAxis;
KVSO_PARAMETERS_BEGIN(c)
- KVSO_PARAMETER("width",KVS_PT_INTEGER,0,iWidth)
- KVSO_PARAMETER("height",KVS_PT_INTEGER,0,iHeight)
+ KVSO_PARAMETER("angle",KVS_PT_DOUBLE,0,dAngle)
+ KVSO_PARAMETER("axis",KVS_PT_STRING,KVS_PF_OPTIONAL,szAxis)
KVSO_PARAMETERS_END(c)
- if(m_pPixmap) delete m_pPixmap;
- if (m_pAnimatedPixmap){
- delete m_pAnimatedPixmap;
- m_pAnimatedPixmap=0;
+ Qt::Axis axis = Qt::ZAxis;
+ if(!szAxis.isEmpty())
+ {
+ if (KviQString::equalCI(szAxis,"XAxis"))axis=Qt::XAxis;
+ else if (KviQString::equalCI(szAxis,"YAxis")) axis=Qt::YAxis;
+ else if (KviQString::equalCI(szAxis,"ZAxis")) axis=Qt::ZAxis;
+ else c->warning(__tr2qs_ctx("Unknown axis '%Q' switching to default ZAxis","objects"),&szAxis);
+ }
+ if(m_currentType==Pixmap)
+ {
+ if(m_pPixmap){
+ if(!m_pImage) m_pImage = new QImage();
+ *m_pImage = m_pPixmap->toImage();
+ }
+ else{
+ c->error(__tr2qs_ctx("The pixmap is null","objects"));
+ return false;
+ }
+ }
+ else if(m_currentType==AnimatedPixmap)
+ {
+ c->warning(__tr2qs_ctx("AnimatedPixmap not supported","objects"));
+ return true;
+ }
+ if(!m_pImage)
+ {
+ c->error(__tr2qs_ctx("The pixmap is null","objects"));
+ return false;
+ }
+ m_currentType = Image;
+ QTransform transform;
+ transform.rotate(dAngle,axis);
+ *m_pImage=m_pImage->transformed(transform);
+ return true;
+}
+
+
+KVSO_CLASS_FUNCTION(pixmap,mirrored)
+{
+ kvs_real_t dAngle;
+ bool bHorizontal,bVertical;
+ KVSO_PARAMETERS_BEGIN(c)
+ KVSO_PARAMETER("bHorizontal",KVS_PT_BOOLEAN,0,bHorizontal)
+ KVSO_PARAMETER("bvertical",KVS_PT_BOOLEAN,0,bVertical)
+
+ KVSO_PARAMETERS_END(c)
+ if(m_currentType==Pixmap)
+ {
+ if(m_pPixmap){
+ if(!m_pImage) m_pImage = new QImage();
+ *m_pImage = m_pPixmap->toImage();
+ }
+ else{
+ c->error(__tr2qs_ctx("The pixmap is null","objects"));
+ return false;
+ }
+ }
+ else if(m_currentType==AnimatedPixmap)
+ {
+ c->warning(__tr2qs_ctx("AnimatedPixmap not supported","objects"));
+ return true;
+ }
+ if(!m_pImage)
+ {
+ c->error(__tr2qs_ctx("The pixmap is null","objects"));
+ return false;
+ }
+ m_currentType = Image;
+ *m_pImage=m_pImage->mirrored(bHorizontal,bVertical);
+ return true;
+}
+
+
+KVSO_CLASS_FUNCTION(pixmap,toGrayScale)
+{
+ if(m_currentType==Pixmap)
+ {
+ if(m_pPixmap){
+ if(!m_pImage) m_pImage = new QImage();
+ *m_pImage = m_pPixmap->toImage();
+ }
+ else{
+ c->error(__tr2qs_ctx("The pixmap is null","objects"));
+ return false;
+ }
+ }
+ else if(m_currentType==AnimatedPixmap)
+ {
+ c->warning(__tr2qs_ctx("AnimatedPixmap not supported","objects"));
+ return true;
+ }
+ if(!m_pImage)
+ {
+ c->error(__tr2qs_ctx("The pixmap is null","objects"));
+ return false;
+ }
+ m_currentType = Image;
+ QRgb col;
+ int w=m_pImage->width();
+ int h=m_pImage->height();
+ int res;
+ for(int y=0; y<h; y++)
+ {
+ QRgb *data = (QRgb *) m_pImage->scanLine(y);
+ for (int x=0; x<w; x++)
+ {
+ col = *data;
+ res = (qRed(col)+qGreen(col)+qBlue(col))/3;
+ *data++ = qRgba(res, res, res,qAlpha(col));
+ }
}
- m_pPixmap=new QPixmap(iWidth,iHeight);
return true;
}
KVSO_CLASS_FUNCTION(pixmap,scale)
@@ -164,7 +423,6 @@ KVSO_CLASS_FUNCTION(pixmap,scale)
KVSO_PARAMETER("aspect_ratio",KVS_PT_STRING,KVS_PF_OPTIONAL,szAspectRatio)
KVSO_PARAMETERS_END(c)
Qt::AspectRatioMode ratio=Qt::KeepAspectRatio;
- if(!m_pPixmap || m_pAnimatedPixmap) return true;
if(!szAspectRatio.isEmpty())
{
if(KviQString::equalCI(szAspectRatio,"IgnoreAspectRatio")) ratio=Qt::IgnoreAspectRatio;
@@ -172,8 +430,23 @@ KVSO_CLASS_FUNCTION(pixmap,scale)
else if (KviQString::equalCI(szAspectRatio,"KeepAspectRatioByExpanding")) ratio=Qt::KeepAspectRatioByExpanding;
else c->warning(__tr2qs_ctx("Unknown aspect ratio %Q - Switching to KeepAspectRatio ratio","objects"),&szAspectRatio);
}
- if (m_pAnimatedPixmap) m_pAnimatedPixmap->resize(QSize(iWidth,iHeight),ratio);
- else *m_pPixmap=m_pPixmap->scaled(iWidth,iHeight,ratio,Qt::SmoothTransformation);
+ if(m_currentType==Pixmap)
+ {
+ if(m_pPixmap)
+ *m_pPixmap=m_pPixmap->scaled(iWidth,iHeight,ratio,Qt::SmoothTransformation);
+ else{
+ c->error(__tr2qs_ctx("The pixmap is null","objects"));
+ return false;
+ }
+ }
+ else if (m_currentType==AnimatedPixmap)
+ m_pAnimatedPixmap->resize(QSize(iWidth,iHeight),ratio);
+ else if(m_pImage)
+ *m_pImage=m_pImage->scaled(iWidth,iHeight,ratio,Qt::SmoothTransformation);
+ else{
+ c->error(__tr2qs_ctx("The pixmap is null","objects"));
+ return false;
+ }
return true;
}
KVSO_CLASS_FUNCTION(pixmap,loadAnimation)
@@ -185,14 +458,17 @@ KVSO_CLASS_FUNCTION(pixmap,loadAnimation)
KVSO_PARAMETERS_END(c)
if(!QFile::exists(szFile))
{
- c->warning(__tr2qs_ctx("I can't find the specified file '%Q'.","objects"),&szFile);
- return true;
+ c->warning(__tr2qs_ctx("I can't find the specified file '%Q'.","objects"),&szFile);
+ return true;
+ }
+ if (m_pAnimatedPixmap){
+ delete m_pAnimatedPixmap;
+ m_pAnimatedPixmap = 0;
}
- if (m_pAnimatedPixmap) delete m_pAnimatedPixmap;
if (m_pPixmap)
{
- delete m_pPixmap;
- m_pPixmap=0;
+ delete m_pPixmap;
+ m_pPixmap=0;
}
m_pAnimatedPixmap= new KviAnimatedPixmap(szFile);
connect(m_pAnimatedPixmap,SIGNAL(frameChanged()),this,SLOT(frameChanged()));
@@ -221,12 +497,21 @@ KVSO_CLASS_FUNCTION(pixmap,load)
KVSO_PARAMETERS_END(c)
if(!QFile::exists(szFile))
{
- c->warning(__tr2qs_ctx("I can't find the specified file '%Q'.","objects"),&szFile);
- return true;
+ c->warning(__tr2qs_ctx("I can't find the specified file '%Q'.","objects"),&szFile);
+ return true;
}
- if (m_pAnimatedPixmap) delete m_pAnimatedPixmap;
+ if(!m_pImage) m_pImage=new QImage();
+
+ m_pImage->load(szFile);
+ return true;
+ if (m_currentType==Image && m_pImage)
+ delete m_pImage;
+ else if (m_currentType==AnimatedPixmap && m_pAnimatedPixmap)
+ delete m_pAnimatedPixmap;
if(!m_pPixmap) m_pPixmap=new QPixmap();
+ m_currentType=Pixmap;
m_pPixmap->load(szFile);
+ qDebug("alpha %i",m_pPixmap->hasAlpha());
return true;
}
KVSO_CLASS_FUNCTION(pixmap,save)
@@ -235,15 +520,41 @@ KVSO_CLASS_FUNCTION(pixmap,save)
KVSO_PARAMETERS_BEGIN(c)
KVSO_PARAMETER("file",KVS_PT_STRING,0,szFile)
KVSO_PARAMETERS_END(c)
- if (m_pAnimatedPixmap) m_pAnimatedPixmap->pixmap()->save(szFile);
- else if(m_pPixmap) m_pPixmap->save(szFile);
+ if(m_currentType==Pixmap)
+ {
+ if(m_pPixmap)
+ m_pPixmap->save(szFile);
+ else{
+ c->error(__tr2qs_ctx("The pixmap is null","objects"));
+ return false;
+ }
+ }
+ else if(m_currentType==Image)
+ {
+ if(m_pImage)
+ m_pImage->save(szFile);
+ else{
+ c->error(__tr2qs_ctx("The pixmap is null","objects"));
+ return false;
+ }
+ }
+ else m_pAnimatedPixmap->pixmap()->save(szFile);
return true;
}
void KvsObject_pixmap::setInternalPixmap(QPixmap *pPixmap)
{
- delete m_pPixmap;
- m_pPixmap=pPixmap;
+ if(m_currentType==Image && m_pImage) delete m_pImage;
+ else if(m_currentType==Pixmap && m_pPixmap) delete m_pPixmap;
+ m_currentType=Pixmap;
+ m_pPixmap=pPixmap;
+}
+void KvsObject_pixmap::setInternalImage(QImage *pImage)
+{
+ if(m_currentType==Image && m_pImage) delete m_pImage;
+ else if(m_currentType==Pixmap && m_pPixmap) delete m_pPixmap;
+ m_currentType=Image;
+ m_pImage=pImage;
}
KVSO_CLASS_FUNCTION(pixmap,loadFromMemoryBuffer)
{
@@ -307,25 +618,30 @@ KVSO_CLASS_FUNCTION(pixmap,grabWidget)
}
KVSO_CLASS_FUNCTION(pixmap,height)
{
- if(!m_pAnimatedPixmap && !m_pPixmap) c->returnValue()->setInteger(0);
- else if(m_pAnimatedPixmap) c->returnValue()->setInteger(m_pAnimatedPixmap->size().height());
- else c->returnValue()->setInteger(m_pPixmap->height());
+ if (m_currentType==Image && m_pImage) c->returnValue()->setInteger(m_pImage->height());
+ else if(m_currentType==Pixmap && m_pPixmap) c->returnValue()->setInteger(m_pPixmap->height());
+ else c->returnValue()->setInteger(0);
+ //else if(m_pAnimatedPixmap) c->returnValue()->setInteger(m_pAnimatedPixmap->size().height());
+ //else c->returnValue()->setInteger(m_pPixmap->height());
return true;
}
KVSO_CLASS_FUNCTION(pixmap,width)
{
- if(!m_pAnimatedPixmap && !m_pPixmap) c->returnValue()->setInteger(0);
- else if(m_pAnimatedPixmap) c->returnValue()->setInteger(m_pAnimatedPixmap->size().width());
- else c->returnValue()->setInteger(m_pPixmap->width());
- return true;
+
+ if (m_currentType==Image && m_pImage) c->returnValue()->setInteger(m_pImage->width());
+ else if(m_currentType==Pixmap && m_pPixmap) c->returnValue()->setInteger(m_pPixmap->width());
+ else c->returnValue()->setInteger(0);
+ //else if(m_pAnimatedPixmap) c->returnValue()->setInteger(m_pAnimatedPixmap->size().height());
+ //else c->returnValue()->setInteger(m_pPixmap->height());
+ return true;
}
-QPixmap * KvsObject_pixmap::getPixmap()
+/*QPixmap * KvsObject_pixmap::getPixmap()
{
if (!m_pAnimatedPixmap && !m_pPixmap) return new QPixmap();
else if(m_pAnimatedPixmap) return m_pAnimatedPixmap->pixmap();
else return m_pPixmap;
-}
+}*/
KVSO_CLASS_FUNCTION(pixmap,frameChangedEvent)
{
emitSignal("frameChanged",c);