karbon
vpattern.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <qdom.h>
00021
00022 #include "vpattern.h"
00023 #include <qpixmap.h>
00024 #define THUMB_SIZE 30
00025
00026 VPattern::VPattern()
00027 {
00028 m_valid = false;
00029 validThumb = false;
00030 }
00031
00032 VPattern::VPattern( const QString &tilename )
00033 {
00034 load( tilename );
00035 }
00036
00037 void
00038 VPattern::load( const QString &tilename )
00039 {
00040 m_tilename = tilename;
00041 bool ok = m_image.load( tilename );
00042
00043 if( !ok )
00044 {
00045 m_valid = false;
00046 return;
00047 }
00048
00049 m_image = m_image.convertDepth( 32 );
00050 m_pixmap.convertFromImage(m_image, QPixmap::AutoColor);
00051 if( m_image.width() > THUMB_SIZE || m_image.height() > THUMB_SIZE )
00052 {
00053 int xsize = THUMB_SIZE;
00054 int ysize = THUMB_SIZE;
00055 int picW = m_image.width();
00056 int picH = m_image.height();
00057 if( picW > picH )
00058 {
00059 float yFactor = (float)((float)(float)picH/(float)picW);
00060 ysize = (int)(yFactor * (float)THUMB_SIZE);
00061 if(ysize > 30) ysize = 30;
00062 }
00063 else if( picW < picH )
00064 {
00065 float xFactor = (float)((float)picW/(float)picH);
00066 xsize = (int)(xFactor * (float)THUMB_SIZE);
00067 if(xsize > 30) xsize = 30;
00068 }
00069
00070 QImage thumbImg = m_image.smoothScale( xsize, ysize );
00071 m_pixmapThumb.convertFromImage( thumbImg );
00072 validThumb = true;
00073 }
00074 m_valid = !m_image.isNull();
00075 }
00076
00077 unsigned char *
00078 VPattern::pixels()
00079 {
00080 return m_image.bits();
00081 }
00082
00083 unsigned int
00084 VPattern::tileWidth() const
00085 {
00086 return m_image.width();
00087 }
00088
00089 unsigned int
00090 VPattern::tileHeight() const
00091 {
00092 return m_image.height();
00093 }
00094
00095 void
00096 VPattern::save( QDomElement& element ) const
00097 {
00098 QDomElement me = element.ownerDocument().createElement( "PATTERN" );
00099
00100 me.setAttribute( "originX", m_origin.x() );
00101 me.setAttribute( "originY", m_origin.y() );
00102 me.setAttribute( "vectorX", m_vector.x() );
00103 me.setAttribute( "vectorY", m_vector.y() );
00104
00105 me.setAttribute( "tilename", m_tilename );
00106
00107 element.appendChild( me );
00108 }
00109
00110 void
00111 VPattern::load( const QDomElement& element )
00112 {
00113 m_origin.setX( element.attribute( "originX", "0.0" ).toDouble() );
00114 m_origin.setY( element.attribute( "originY", "0.0" ).toDouble() );
00115 m_vector.setX( element.attribute( "vectorX", "0.0" ).toDouble() );
00116 m_vector.setY( element.attribute( "vectorY", "0.0" ).toDouble() );
00117
00118 m_tilename = element.attribute( "tilename" );
00119 load( m_tilename );
00120 }
00121
00122 void
00123 VPattern::transform( const QWMatrix &m )
00124 {
00125 m_origin = m_origin.transform( m );
00126 m_vector = m_vector.transform( m );
00127 }
00128
00129 QPixmap& VPattern::pixmap() const
00130 {
00131 return (QPixmap&)m_pixmap;
00132 }
00133
00134 QPixmap& VPattern::thumbPixmap() const
00135 {
00136 return (QPixmap&)m_pixmapThumb;
00137 }
00138
00139
00140
|