KoChild.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
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 <KoChild.h>
21
22#include <tqpainter.h>
23
24#include <kdebug.h>
25
26class KoChild::KoChildPrivate
27{
28public:
29 KoChildPrivate()
30 {
31 m_contentsX = m_contentsY = 0;
32 }
33 ~KoChildPrivate()
34 {
35 }
36
37 TQRect m_geometry;
38
39 double m_rotation;
40 double m_shearX;
41 double m_shearY;
42 TQPoint m_rotationPoint;
43 double m_scaleX;
44 double m_scaleY;
45 TQWMatrix m_matrix;
46 bool m_lock;
47 TQPointArray m_old;
48 bool m_transparent;
49 int m_contentsX;
50 int m_contentsY;
51};
52
53KoChild::KoChild( TQObject *parent, const char *name )
54: TQObject( parent, name )
55{
56 d = new KoChildPrivate;
57
58 d->m_scaleX = d->m_scaleY = 1.0;
59 d->m_shearX = d->m_shearY = 0.0;
60 d->m_rotation = 0.0;
61 d->m_lock = false;
62 d->m_transparent = false;
63
64 updateMatrix();
65}
66
67KoChild::~KoChild()
68{
69 delete d;
70}
71
72void KoChild::setGeometry( const TQRect &rect, bool noEmit )
73{
74 if ( !d->m_lock )
75 d->m_old = framePointArray();
76
77 d->m_geometry = rect;
78
79 // Embedded objects should have a minimum size of 3, so they can be selected
80 if( d->m_geometry.width() < 3 )
81 d->m_geometry.setWidth( 3 );
82
83 if( d->m_geometry.height() < 3 )
84 d->m_geometry.setHeight( 3 );
85
87
88 if ( !d->m_lock && !noEmit )
89 emit changed( this );
90}
91
92TQRect KoChild::geometry() const
93{
94 return d->m_geometry;
95}
96
97TQRegion KoChild::region( const TQWMatrix &matrix ) const
98{
99 return TQRegion( pointArray( matrix ) );
100}
101
102TQPointArray KoChild::pointArray( const TQWMatrix &matrix ) const
103{
104 return pointArray( TQRect( 0, 0, d->m_geometry.width(), d->m_geometry.height() ), matrix );
105}
106
107//bool KoChild::contains( const TQPoint &point ) const
108//{
109// return region().contains( point );
110//}
111
113{
114 return pointArray().boundingRect();
115}
116
118{
119 return !( d->m_shearX != 0.0 || d->m_shearY != 0.0 || d->m_rotation != 0.0 );
120}
121
122void KoChild::setClipRegion( TQPainter &painter, bool combine )
123{
124 painter.setClipping( true );
125 if ( combine && !painter.clipRegion().isEmpty() )
126 painter.setClipRegion( region( painter.worldMatrix() ).intersect( painter.clipRegion() ) );
127 else
128 painter.setClipRegion( region( painter.worldMatrix() ) );
129}
130
131void KoChild::setScaling( double x, double y )
132{
133 if ( !d->m_lock )
134 d->m_old = framePointArray();
135
136 d->m_scaleX = x;
137 d->m_scaleY = y;
138
139 // why is that commented out? (Simon)
140 // This is commented out, because KoChild::transform() scales
141 // the world matrix explicitly and updateMatrix() doesn't even
142 // handle scaling (Werner)
143 //updateMatrix()
144
145 if ( !d->m_lock )
146 emit changed( this );
147}
148
149double KoChild::xScaling() const
150{
151 return d->m_scaleX;
152}
153
154double KoChild::yScaling() const
155{
156 return d->m_scaleY;
157}
158
159void KoChild::setShearing( double x, double y )
160{
161 if ( !d->m_lock )
162 d->m_old = framePointArray();
163
164 d->m_shearX = x;
165 d->m_shearY = y;
166
167 updateMatrix();
168
169 if ( !d->m_lock )
170 emit changed( this );
171}
172
173double KoChild::xShearing() const
174{
175 return d->m_shearX;
176}
177
178double KoChild::yShearing() const
179{
180 return d->m_shearY;
181}
182
183void KoChild::setRotation( double rot )
184{
185 if ( !d->m_lock )
186 d->m_old = framePointArray();
187
188 d->m_rotation = rot;
189 updateMatrix();
190
191 if ( !d->m_lock )
192 emit changed( this );
193}
194
195double KoChild::rotation() const
196{
197 return d->m_rotation;
198}
199
200void KoChild::setRotationPoint( const TQPoint &pos )
201{
202 if ( !d->m_lock )
203 d->m_old = framePointArray();
204
205 d->m_rotationPoint = pos;
206 updateMatrix();
207
208 if ( !d->m_lock )
209 emit changed( this );
210}
211
213{
214 return d->m_rotationPoint;
215}
216
217void KoChild::transform( TQPainter &painter )
218{
219 setClipRegion( painter, true );
220
221 TQWMatrix m = painter.worldMatrix();
222 m = d->m_matrix * m;
223 m.scale( d->m_scaleX, d->m_scaleY );
224 painter.setWorldMatrix( m );
225}
226
227void KoChild::setContentsPos( int x, int y )
228{
229 d->m_contentsX = x;
230 d->m_contentsY = y;
231}
232
234{
235 return TQRect( d->m_contentsX, d->m_contentsY, int(d->m_geometry.width() / d->m_scaleX),
236 int(d->m_geometry.height() / d->m_scaleY) );
237}
238
239TQPointArray KoChild::framePointArray( const TQWMatrix &matrix ) const
240{
241 return pointArray( TQRect( -6, -6, d->m_geometry.width() + 12, d->m_geometry.height() + 12 ), matrix );
242}
243
244TQRegion KoChild::frameRegion( const TQWMatrix &matrix, bool solid ) const
245{
246 const TQPointArray arr = framePointArray( matrix );
247 const TQRegion frameReg( arr );
248
249 if ( solid )
250 return frameReg;
251
252 const TQRegion reg = region( matrix );
253 return frameReg.subtract( reg );
254}
255
256TQPointArray KoChild::pointArray( const TQRect &r, const TQWMatrix &matrix ) const
257{
258 TQPoint topleft = d->m_matrix.map( TQPoint( r.left(), r.top() ) );
259 TQPoint topright = d->m_matrix.map( TQPoint( r.right(), r.top() ) );
260 TQPoint bottomleft = d->m_matrix.map( TQPoint( r.left(), r.bottom() ) );
261 TQPoint bottomright = d->m_matrix.map( TQPoint( r.right(), r.bottom() ) );
262
263 TQPointArray arr( 4 );
264 arr.setPoint( 0, topleft );
265 arr.setPoint( 1, topright );
266 arr.setPoint( 2, bottomright );
267 arr.setPoint( 3, bottomleft );
268
269 for( int i = 0; i < 4; ++i )
270 arr.setPoint( i, matrix.map( arr.point( i ) ) );
271
272 return arr;
273}
274
276{
277 TQWMatrix r;
278 r.rotate( - d->m_rotation );
279 TQPoint p = r.map( TQPoint( d->m_rotationPoint.x(),
280 d->m_rotationPoint.y() ) );
281
282 TQWMatrix m;
283 m.rotate( d->m_rotation );
284 m.translate( -d->m_rotationPoint.x() + d->m_geometry.x(), -d->m_rotationPoint.y() + d->m_geometry.y() );
285 m.translate( p.x(), p.y() );
286 m.shear( d->m_shearX, d->m_shearY );
287
288 d->m_matrix = m;
289}
290
291TQWMatrix KoChild::matrix() const
292{
293 return d->m_matrix;
294}
295
297{
298 if ( d->m_lock )
299 return;
300
301 d->m_old = framePointArray();
302 d->m_lock = true;
303}
304
306{
307 if ( !d->m_lock )
308 return;
309
310 d->m_lock = false;
311 emit changed( this );
312}
313
314bool KoChild::locked() const
315{
316 return d->m_lock;
317}
318
319TQPointArray KoChild::oldPointArray( const TQWMatrix &matrix )
320{
321 TQPointArray arr = d->m_old;
322
323 for( int i = 0; i < 4; ++i )
324 arr.setPoint( i, matrix.map( arr.point( i ) ) );
325
326 return arr;
327}
328
329void KoChild::setTransparent( bool transparent )
330{
331 d->m_transparent = transparent;
332}
333
335{
336 return d->m_transparent;
337}
338
340{
341 if ( !frameRegion().contains( p ) )
342 return NoGadget;
343
344 if ( TQRegion( pointArray( TQRect( -5, -5, 5, 5 ) ) ).contains( p ) )
345 return TopLeft;
346 if ( TQRegion( pointArray( TQRect( d->m_geometry.width() / 2 - 3, -5, 5, 5 ) ) ).contains( p ) )
347 return TopMid;
348 if ( TQRegion( pointArray( TQRect( d->m_geometry.width(), -5, 5, 5 ) ) ).contains( p ) )
349 return TopRight;
350 if ( TQRegion( pointArray( TQRect( -5, d->m_geometry.height() / 2 - 3, 5, 5 ) ) ).contains( p ) )
351 return MidLeft;
352 if ( TQRegion( pointArray( TQRect( -5, d->m_geometry.height(), 5, 5 ) ) ).contains( p ) )
353 return BottomLeft;
354 if ( TQRegion( pointArray( TQRect( d->m_geometry.width() / 2 - 3,
355 d->m_geometry.height(), 5, 5 ) ) ).contains( p ) )
356 return BottomMid;
357 if ( TQRegion( pointArray( TQRect( d->m_geometry.width(), d->m_geometry.height(), 5, 5 ) ) ).contains( p ) )
358 return BottomRight;
359 if ( TQRegion( pointArray( TQRect( d->m_geometry.width(),
360 d->m_geometry.height() / 2 - 3, 5, 5 ) ) ).contains( p ) )
361 return MidRight;
362
363 return Move;
364}
365
366#include <KoChild.moc>
virtual bool isTransparent() const
It might be interesting for view updates and repainting in general whether a child is transparent or ...
Definition KoChild.cpp:334
void setGeometry(const TQRect &rect, bool noEmit=false)
Sets a new geometry for this child document.
Definition KoChild.cpp:72
virtual double yShearing() const
Definition KoChild.cpp:178
void changed(KoChild *thisChild)
Emitted every time this child changes, but only if this child is not locked.
virtual TQPoint rotationPoint() const
Definition KoChild.cpp:212
virtual Gadget gadgetHitTest(const TQPoint &p)
Different actions are taken depending on where a child frame is hit.
Definition KoChild.cpp:339
virtual double rotation() const
Definition KoChild.cpp:195
bool isRectangle() const
Definition KoChild.cpp:117
void lock()
Locks this child and stores the current transformation.
Definition KoChild.cpp:296
virtual TQWMatrix matrix() const
Definition KoChild.cpp:291
virtual TQRegion region(const TQWMatrix &=TQWMatrix()) const
Definition KoChild.cpp:97
void unlock()
Unlocks this child and emits a changed signal.
Definition KoChild.cpp:305
virtual void updateMatrix()
Stores the current transformation of this child into a matrix.
Definition KoChild.cpp:275
TQRect geometry() const
Definition KoChild.cpp:92
virtual void setRotationPoint(const TQPoint &pos)
Sets the center of the rotation to the point pos.
Definition KoChild.cpp:200
virtual void transform(TQPainter &painter)
Transforms the painter (its worldmatrix and the clipping) in such a way that the painter can be passe...
Definition KoChild.cpp:217
virtual double xScaling() const
Definition KoChild.cpp:149
virtual TQPointArray framePointArray(const TQWMatrix &matrix=TQWMatrix()) const
Definition KoChild.cpp:239
virtual void setClipRegion(TQPainter &painter, bool combine=true)
Sets the clip region of the painter, so that only pixels of the child part can be drawn.
Definition KoChild.cpp:122
virtual void setScaling(double x, double y)
Scales the content of the child part.
Definition KoChild.cpp:131
virtual TQRect contentRect() const
Definition KoChild.cpp:233
virtual void setRotation(double)
Sets the angle of rotation.
Definition KoChild.cpp:183
TQRect boundingRect() const
Tests whether the part contains a certain point.
Definition KoChild.cpp:112
virtual double yScaling() const
Definition KoChild.cpp:154
virtual double xShearing() const
Definition KoChild.cpp:173
virtual TQRegion frameRegion(const TQWMatrix &matrix=TQWMatrix(), bool solid=false) const
Definition KoChild.cpp:244
virtual TQPointArray oldPointArray(const TQWMatrix &matrix)
Definition KoChild.cpp:319
virtual TQPointArray pointArray(const TQWMatrix &matrix=TQWMatrix()) const
Definition KoChild.cpp:102
virtual void setTransparent(bool transparent)
Marks this child as either transparent or not.
Definition KoChild.cpp:329
virtual void setShearing(double x, double y)
Shears the content of the child part.
Definition KoChild.cpp:159
virtual void setContentsPos(int x, int y)
Sets the position of the content relative to the child frame.
Definition KoChild.cpp:227
Gadget
The gadget generally identifies where a child has been hit (generally by the mouse pointer).
Definition KoChild.h:56
bool locked() const
If the child is locked, geometry changes (including scaling, rotation, ...) are not backed up.
Definition KoChild.cpp:314
const char * name(StdAction id)