karbon

vpattern.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002, The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqdom.h>
21
22#include "vpattern.h"
23#include <tqpixmap.h>
24#define THUMB_SIZE 30
25
26VPattern::VPattern()
27{
28 m_valid = false;
29 validThumb = false;
30}
31
32VPattern::VPattern( const TQString &tilename )
33{
34 load( tilename );
35}
36
37void
38VPattern::load( const TQString &tilename )
39{
40 m_tilename = tilename;
41 bool ok = m_image.load( tilename );
42
43 if( !ok )
44 {
45 m_valid = false;
46 return;
47 }
48
49 m_image = m_image.convertDepth( 32 );
50 m_pixmap.convertFromImage(m_image, TQPixmap::AutoColor);
51 if( m_image.width() > THUMB_SIZE || m_image.height() > THUMB_SIZE )
52 {
53 int xsize = THUMB_SIZE;
54 int ysize = THUMB_SIZE;
55 int picW = m_image.width();
56 int picH = m_image.height();
57 if( picW > picH )
58 {
59 float yFactor = (float)((float)(float)picH/(float)picW);
60 ysize = (int)(yFactor * (float)THUMB_SIZE);
61 if(ysize > 30) ysize = 30;
62 }
63 else if( picW < picH )
64 {
65 float xFactor = (float)((float)picW/(float)picH);
66 xsize = (int)(xFactor * (float)THUMB_SIZE);
67 if(xsize > 30) xsize = 30;
68 }
69
70 TQImage thumbImg = m_image.smoothScale( xsize, ysize );
71 m_pixmapThumb.convertFromImage( thumbImg );
72 validThumb = true;
73 }
74 m_valid = !m_image.isNull();
75}
76
77unsigned char *
78VPattern::pixels()
79{
80 return m_image.bits();
81}
82
83unsigned int
84VPattern::tileWidth() const
85{
86 return m_image.width();
87}
88
89unsigned int
90VPattern::tileHeight() const
91{
92 return m_image.height();
93}
94
95void
96VPattern::save( TQDomElement& element ) const
97{
98 TQDomElement me = element.ownerDocument().createElement( "PATTERN" );
99
100 me.setAttribute( "originX", m_origin.x() );
101 me.setAttribute( "originY", m_origin.y() );
102 me.setAttribute( "vectorX", m_vector.x() );
103 me.setAttribute( "vectorY", m_vector.y() );
104
105 me.setAttribute( "tilename", m_tilename );
106
107 element.appendChild( me );
108}
109
110void
111VPattern::load( const TQDomElement& element )
112{
113 m_origin.setX( element.attribute( "originX", "0.0" ).toDouble() );
114 m_origin.setY( element.attribute( "originY", "0.0" ).toDouble() );
115 m_vector.setX( element.attribute( "vectorX", "0.0" ).toDouble() );
116 m_vector.setY( element.attribute( "vectorY", "0.0" ).toDouble() );
117
118 m_tilename = element.attribute( "tilename" );
119 load( m_tilename );
120}
121
122void
123VPattern::transform( const TQWMatrix &m )
124{
125 m_origin = m_origin.transform( m );
126 m_vector = m_vector.transform( m );
127}
128
129TQPixmap& VPattern::pixmap() const
130{
131 return (TQPixmap&)m_pixmap;
132}
133
134TQPixmap& VPattern::thumbPixmap() const
135{
136 return (TQPixmap&)m_pixmapThumb;
137}
138
139
140