~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to karbon/core/vselection.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2001, The Karbon Developers
 
3
   Copyright (C) 2002, The Karbon Developers
 
4
 
 
5
   This library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Library General Public
 
7
   License as published by the Free Software Foundation; either
 
8
   version 2 of the License, or (at your option) any later version.
 
9
 
 
10
   This library is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
   Library General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU Library General Public License
 
16
   along with this library; see the file COPYING.LIB.  If not, write to
 
17
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
   Boston, MA 02111-1307, USA.
 
19
*/
 
20
 
 
21
 
 
22
#include "vdocument.h"
 
23
#include "vdrawselection.h"
 
24
#include "vpainter.h"
 
25
#include "vselection.h"
 
26
#include "vselectnodes.h"
 
27
#include "vselectobjects.h"
 
28
#include "vvisitor.h"
 
29
#include "vcolor.h"
 
30
#include "vfill.h"
 
31
#include "vstroke.h"
 
32
 
 
33
VSelection::VSelection( VObject* parent )
 
34
        : VObject( parent ), m_showhandle( true )
 
35
{
 
36
        m_handleRect = new KoRect[ 10 ];
 
37
        setStroke( VStroke( VColor( Qt::black ) ) );
 
38
        setFill( VFill() );
 
39
 
 
40
        m_selectObjects = true;
 
41
}
 
42
 
 
43
VSelection::VSelection( const VSelection& selection )
 
44
        : VObject( selection ), VVisitor()
 
45
{
 
46
        m_handleRect = new KoRect[ 10 ];
 
47
 
 
48
        VObjectListIterator itr = selection.m_objects;
 
49
        for ( ; itr.current() ; ++itr )
 
50
                append( itr.current() );        // Don't clone objects here.
 
51
 
 
52
        m_showhandle = true;
 
53
        m_selectObjects = selection.m_selectObjects;
 
54
}
 
55
 
 
56
VSelection::~VSelection()
 
57
{
 
58
        //clear();
 
59
        delete[]( m_handleRect );
 
60
}
 
61
 
 
62
VSelection*
 
63
VSelection::clone() const
 
64
{
 
65
        return new VSelection( *this );
 
66
}
 
67
 
 
68
void
 
69
VSelection::accept( VVisitor& visitor )
 
70
{
 
71
        visitor.visitVSelection( *this );
 
72
}
 
73
 
 
74
void
 
75
VSelection::take( VObject& object )
 
76
{
 
77
        m_objects.removeRef( &object );
 
78
        if( object.state() >= selected )
 
79
                object.setState( normal );
 
80
        invalidateBoundingBox();
 
81
}
 
82
 
 
83
void
 
84
VSelection::append()
 
85
{
 
86
        clear();
 
87
 
 
88
        VSelectObjects op( m_objects );
 
89
        op.visit( *static_cast<VDocument*>( parent() ) );
 
90
        selectNodes();
 
91
 
 
92
        invalidateBoundingBox();
 
93
}
 
94
 
 
95
void
 
96
VSelection::append( VObject* object )
 
97
{
 
98
        if( object->state() != deleted )
 
99
        {
 
100
                m_objects.append( object );
 
101
                object->setState( selected );
 
102
                invalidateBoundingBox();
 
103
        }
 
104
}
 
105
 
 
106
void
 
107
VSelection::append( const VObjectList &objects )
 
108
{
 
109
        VObjectListIterator itr = objects;
 
110
        for( ; itr.current(); ++itr )
 
111
                append( itr.current() );
 
112
}
 
113
 
 
114
bool
 
115
VSelection::append( const KoRect& rect, bool selectObjects, bool exclusive )
 
116
{
 
117
        bool success = false;
 
118
 
 
119
        if( selectObjects )
 
120
        {
 
121
                m_objects.clear();
 
122
                VSelectObjects op( m_objects, rect );
 
123
                if( op.visit( *static_cast<VDocument*>( parent() ) ) )
 
124
                {
 
125
                        selectNodes();
 
126
                        success = true;
 
127
                }
 
128
        }
 
129
        else
 
130
        {
 
131
                VObjectListIterator itr( m_objects );
 
132
                VObjectList notSelected;
 
133
 
 
134
                // Try to select all that have at least one node contained in the rect
 
135
                for ( ; itr.current(); ++itr )
 
136
                {
 
137
                        VSelectNodes op( rect, true, exclusive );
 
138
 
 
139
                        if( op.visit( *itr.current() ) )
 
140
                                success = true;
 
141
                        else
 
142
                                notSelected.append( itr.current());
 
143
                }
 
144
                // Remove all that were not selected from this selection
 
145
                VObjectListIterator jtr( notSelected );
 
146
                for ( ; jtr.current(); ++jtr )
 
147
                        take( *( jtr.current() ) );
 
148
        }
 
149
 
 
150
        invalidateBoundingBox();
 
151
 
 
152
        return success;
 
153
}
 
154
 
 
155
void
 
156
VSelection::clear()
 
157
{
 
158
        VSelectNodes op( true );
 
159
 
 
160
        VObjectListIterator itr = m_objects;
 
161
        for( ; itr.current(); ++itr )
 
162
        {
 
163
                op.visit( *itr.current() );
 
164
 
 
165
                //if( itr.current()->state() != deleted )
 
166
                //      itr.current()->setState( normal );
 
167
        }
 
168
 
 
169
        m_objects.clear();
 
170
        invalidateBoundingBox();
 
171
}
 
172
 
 
173
void
 
174
VSelection::draw( VPainter* painter, double zoomFactor ) const
 
175
{
 
176
        if( objects().count() == 0 || state() == VObject::edit )
 
177
                return;
 
178
 
 
179
        VDrawSelection op( m_objects, painter, !m_selectObjects );
 
180
        op.visitVSelection( (VSelection &)*this );
 
181
 
 
182
        // get bounding box:
 
183
        const KoRect& rect = boundingBox();
 
184
 
 
185
        // calculate displaycoords of big handle rect:
 
186
        m_handleRect[ 0 ].setCoords(    qRound( rect.left() ), qRound( rect.top() ),
 
187
                                                                        qRound( rect.right() ), qRound( rect.bottom() ) );
 
188
 
 
189
        KoPoint center = m_handleRect[ 0 ].center();
 
190
 
 
191
        double handleNodeSize = m_handleNodeSize / zoomFactor;
 
192
 
 
193
        // calculate displaycoords of nodes:
 
194
        m_handleRect[ node_lb ].setRect( m_handleRect[0].left() - handleNodeSize, m_handleRect[0].top() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
 
195
        m_handleRect[ node_mb ].setRect( center.x() - handleNodeSize, m_handleRect[0].top() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
 
196
        m_handleRect[ node_rb ].setRect( m_handleRect[0].right() - handleNodeSize - (1 / zoomFactor), m_handleRect[0].top() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
 
197
        m_handleRect[ node_rm ].setRect( m_handleRect[0].right() - handleNodeSize - (1 / zoomFactor), center.y() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
 
198
        m_handleRect[ node_rt ].setRect( m_handleRect[0].right() - handleNodeSize - (1 / zoomFactor) , m_handleRect[0].bottom() - handleNodeSize - (1 / zoomFactor), 2 * handleNodeSize, 2 * handleNodeSize );
 
199
        m_handleRect[ node_mt ].setRect( center.x() - handleNodeSize, m_handleRect[0].bottom() - handleNodeSize - (1 / zoomFactor), 2 * handleNodeSize, 2 * handleNodeSize );
 
200
        m_handleRect[ node_lt ].setRect( m_handleRect[0].left() - handleNodeSize, m_handleRect[0].bottom() - handleNodeSize - (1 / zoomFactor), 2 * handleNodeSize, 2 * handleNodeSize );
 
201
        m_handleRect[ node_lm ].setRect( m_handleRect[0].left() - handleNodeSize, center.y() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
 
202
 
 
203
        if( !m_showhandle ) return;
 
204
 
 
205
        // draw handle rect:
 
206
        painter->setPen( Qt::blue.light() );
 
207
        painter->setBrush( Qt::NoBrush );
 
208
 
 
209
        painter->drawRect( KoRect( m_handleRect[ 0 ].x() * zoomFactor, m_handleRect[ 0 ].y() * zoomFactor,
 
210
                                                          m_handleRect[ 0 ].width() * zoomFactor, m_handleRect[ 0 ].height() * zoomFactor ) );
 
211
        painter->setPen( Qt::blue.light() );
 
212
 
 
213
        // draw nodes:
 
214
        if( state() == VObject::selected )
 
215
        {
 
216
                painter->setPen( Qt::blue.light() );
 
217
                painter->setBrush( Qt::white );
 
218
 
 
219
                KoRect temp;
 
220
                for( uint i = node_lt; i <= node_rb; ++i )
 
221
                {
 
222
                        if( i != node_mm )
 
223
                        {
 
224
                                temp.setRect(   zoomFactor * m_handleRect[ i ].left(),
 
225
                                                                zoomFactor * m_handleRect[ i ].top(),
 
226
                                                                2 * m_handleNodeSize + 1, 2 * m_handleNodeSize + 1 );
 
227
                                painter->drawRect( temp );
 
228
                        }
 
229
                }
 
230
        }
 
231
}
 
232
 
 
233
const KoRect&
 
234
VSelection::boundingBox() const
 
235
{
 
236
// disable bbox caching for selection since there is no reliable
 
237
// way to get notified of a bbox change:
 
238
//      if( m_boundingBoxIsInvalid )
 
239
//      {
 
240
                // clear:
 
241
                m_boundingBox = KoRect();
 
242
 
 
243
                VObjectListIterator itr = m_objects;
 
244
                for( ; itr.current(); ++itr )
 
245
                        m_boundingBox |= itr.current()->boundingBox();
 
246
 
 
247
//              m_boundingBoxIsInvalid = false;
 
248
//      }
 
249
 
 
250
        return m_boundingBox;
 
251
}
 
252
 
 
253
 
 
254
VHandleNode
 
255
VSelection::handleNode( const KoPoint &point ) const
 
256
{
 
257
        for( uint i = node_lt; i <= node_rb; ++i )
 
258
        {
 
259
                if( m_handleRect[i].contains( point ) )
 
260
                        return static_cast<VHandleNode>( i );
 
261
        }
 
262
 
 
263
        return node_none;
 
264
}
 
265
 
 
266
QPtrList<VSegment>
 
267
VSelection::getSegments( const KoRect& rect )
 
268
{
 
269
        VTestNodes op( rect );
 
270
 
 
271
        VObjectListIterator itr = m_objects;
 
272
        for( ; itr.current(); ++itr )
 
273
                op.visit( *itr.current() );
 
274
 
 
275
        return op.result();
 
276
}
 
277
 
 
278
void
 
279
VSelection::selectNodes( bool select )
 
280
{
 
281
        VSelectNodes op( select );
 
282
 
 
283
        VObjectListIterator itr = m_objects;
 
284
        for( ; itr.current(); ++itr )
 
285
        {
 
286
                op.visit( *itr.current() );
 
287
        }
 
288
}
 
289