kformulaview.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
3 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <iostream>
22
23#include <tqpainter.h>
24#include <tqtimer.h>
25
26#include <tdeapplication.h>
27#include <kdebug.h>
28
29#include "basicelement.h"
30#include "formulacursor.h"
31#include "formulaelement.h"
32#include "kformulacontainer.h"
33#include "kformuladocument.h"
34#include "kformulaview.h"
35
36KFORMULA_NAMESPACE_BEGIN
37
38struct View::View_Impl {
39
40 View_Impl(Container* doc, View* view)
41 : smallCursor(false), activeCursor(true), cursorHasChanged(true),
42 document(doc)
43 {
44 connect(document, TQ_SIGNAL(elementWillVanish(BasicElement*)),
45 view, TQ_SLOT(slotElementWillVanish(BasicElement*)));
46 connect(document, TQ_SIGNAL(formulaLoaded(FormulaElement*)),
47 view, TQ_SLOT(slotFormulaLoaded(FormulaElement*)));
48 connect(document, TQ_SIGNAL(cursorMoved(FormulaCursor*)),
49 view, TQ_SLOT(slotCursorMoved(FormulaCursor*)));
50
51 cursor = document->createCursor();
52 blinkTimer = new TQTimer( view );
53 connect( blinkTimer, TQ_SIGNAL( timeout() ),
54 view, TQ_SLOT( slotBlinkCursor() ) );
55 if ( TQApplication::cursorFlashTime() > 0 )
56 blinkTimer->start( TQApplication::cursorFlashTime() / 2 );
57 }
58
59 void startTimer()
60 {
61 if ( TQApplication::cursorFlashTime() > 0 )
62 blinkTimer->start( TQApplication::cursorFlashTime() / 2 );
63 }
64
65 void stopTimer()
66 {
67 blinkTimer->stop();
68 }
69
70 ~View_Impl()
71 {
72 if ( document->activeCursor() == cursor ) {
73 document->setActiveCursor( 0 );
74 }
75 delete cursor;
76 delete blinkTimer;
77 }
78
82 bool smallCursor;
83
87 bool activeCursor;
88
93 bool cursorHasChanged;
94
98 TQTimer *blinkTimer;
99
103 Container* document;
104
108 FormulaCursor* cursor;
109};
110
111
112FormulaCursor* View::cursor() const { return impl->cursor; }
113bool& View::cursorHasChanged() { return impl->cursorHasChanged; }
114bool& View::smallCursor() { return impl->smallCursor; }
115bool& View::activeCursor() { return impl->activeCursor; }
116Container* View::container() const { return impl->document; }
117void View::startCursorTimer() { impl->startTimer(); }
118void View::stopCursorTimer() { impl->stopTimer(); }
119
120
121View::View(Container* doc)
122{
123 impl = new View_Impl(doc, this);
124 cursor()->calcCursorSize( contextStyle(), smallCursor() );
125}
126
127View::~View()
128{
129 delete impl;
130}
131
132
133TQPoint View::getCursorPoint() const
134{
135 return contextStyle().layoutUnitToPixel( cursor()->getCursorPoint() );
136}
137
138void View::setReadOnly(bool ro)
139{
140 cursor()->setReadOnly(ro);
141}
142
143
144void View::calcCursor()
145{
146 cursor()->calcCursorSize( contextStyle(), smallCursor() );
147}
148
149
150void View::draw(TQPainter& painter, const TQRect& rect, const TQColorGroup& cg)
151{
152// kdDebug( DEBUGID ) << "View::draw: " << rect.x() << " " << rect.y() << " "
153// << rect.width() << " " << rect.height() << endl;
154 container()->draw( painter, rect, cg, true );
155 if ( cursorVisible() ) {
156 StyleAttributes style;
157 cursor()->draw( painter, contextStyle(), style, smallCursor(), activeCursor() );
158 }
159}
160
161void View::draw(TQPainter& painter, const TQRect& rect)
162{
163 container()->draw( painter, rect, true );
164 if ( cursorVisible() ) {
165 StyleAttributes style;
166 cursor()->draw( painter, contextStyle(), style, smallCursor(), activeCursor() );
167 }
168}
169
170void View::keyPressEvent( TQKeyEvent* event )
171{
172 container()->input( event );
173}
174
175
176void View::focusInEvent(TQFocusEvent*)
177{
178 //cursor()->calcCursorSize( contextStyle(), smallCursor() );
179 container()->setActiveCursor(cursor());
180 activeCursor() = true;
181 startCursorTimer();
182 smallCursor() = false;
183 emitCursorChanged();
184}
185
186void View::focusOutEvent(TQFocusEvent*)
187{
188 //container()->setActiveCursor(0);
189 activeCursor() = false;
190 stopCursorTimer();
191 smallCursor() = true;
192 emitCursorChanged();
193}
194
195void View::mousePressEvent( TQMouseEvent* event )
196{
197 const ContextStyle& context = contextStyle();
198 mousePressEvent( event, context.pixelToLayoutUnit( event->pos() ) );
199}
200
201void View::mouseReleaseEvent( TQMouseEvent* event )
202{
203 const ContextStyle& context = contextStyle();
204 mouseReleaseEvent( event, context.pixelToLayoutUnit( event->pos() ) );
205}
206
207void View::mouseDoubleClickEvent( TQMouseEvent* event )
208{
209 const ContextStyle& context = contextStyle();
210 mouseDoubleClickEvent( event, context.pixelToLayoutUnit( event->pos() ) );
211}
212
213void View::mouseMoveEvent( TQMouseEvent* event )
214{
215 const ContextStyle& context = contextStyle();
216 mouseMoveEvent( event, context.pixelToLayoutUnit( event->pos() ) );
217}
218
219void View::wheelEvent( TQWheelEvent* event )
220{
221 const ContextStyle& context = contextStyle();
222 wheelEvent( event, context.pixelToLayoutUnit( event->pos() ) );
223}
224
225void View::mousePressEvent( TQMouseEvent* event, const PtPoint& pos )
226{
227 const ContextStyle& context = contextStyle();
228 mousePressEvent( event, context.ptToLayoutUnitPix( pos ) );
229}
230
231void View::mouseReleaseEvent( TQMouseEvent* event, const PtPoint& pos )
232{
233 const ContextStyle& context = contextStyle();
234 mouseReleaseEvent( event, context.ptToLayoutUnitPix( pos ) );
235}
236
237void View::mouseDoubleClickEvent( TQMouseEvent* event, const PtPoint& pos )
238{
239 const ContextStyle& context = contextStyle();
240 mouseDoubleClickEvent( event, context.ptToLayoutUnitPix( pos ) );
241}
242
243void View::mouseMoveEvent( TQMouseEvent* event, const PtPoint& pos )
244{
245 const ContextStyle& context = contextStyle();
246 mouseMoveEvent( event, context.ptToLayoutUnitPix( pos ) );
247}
248
249void View::wheelEvent( TQWheelEvent* event, const PtPoint& pos )
250{
251 const ContextStyle& context = contextStyle();
252 wheelEvent( event, context.ptToLayoutUnitPix( pos ) );
253}
254
255
256void View::mousePressEvent( TQMouseEvent* event, const LuPixelPoint& pos )
257{
258 int flags = movementFlag( event->state() );
259 cursor()->mousePress( pos, flags );
260 emitCursorChanged();
261}
262
263void View::mouseReleaseEvent( TQMouseEvent* event, const LuPixelPoint& pos )
264{
265 int flags = movementFlag( event->state() );
266 cursor()->mouseRelease( pos, flags );
267 emitCursorChanged();
268}
269
270void View::mouseDoubleClickEvent( TQMouseEvent*, const LuPixelPoint& )
271{
272 cursor()->moveRight( WordMovement );
273 cursor()->moveLeft( SelectMovement | WordMovement );
274 emitCursorChanged();
275}
276
277void View::mouseMoveEvent( TQMouseEvent* event, const LuPixelPoint& pos )
278{
279 int flags = movementFlag( event->state() );
280 cursor()->mouseMove( pos, flags );
281 emitCursorChanged();
282}
283
284void View::wheelEvent( TQWheelEvent*, const LuPixelPoint& )
285{
286}
287
288
290{
291 if (c == cursor()) {
292 cursorHasChanged() = true;
293 emitCursorChanged();
294 }
295}
296
298{
299 cursor()->formulaLoaded(formula);
300}
301
303{
304 cursor()->elementWillVanish(element);
305 emitCursorChanged();
306}
307
309{
310 activeCursor() = ! activeCursor();
311 emitCursorChanged();
312}
313
314void View::slotSelectAll()
315{
316 cursor()->moveHome(WordMovement);
317 cursor()->moveEnd(SelectMovement | WordMovement);
318 emitCursorChanged();
319}
320
321
322void View::moveLeft( int flag )
323{
324 cursor()->moveLeft( flag );
325 emitCursorChanged();
326}
327
328void View::moveRight( int flag )
329{
330 cursor()->moveRight( flag );
331 emitCursorChanged();
332}
333
334void View::moveUp( int flag )
335{
336 cursor()->moveUp( flag );
337 emitCursorChanged();
338}
339
340void View::moveDown( int flag )
341{
342 cursor()->moveDown( flag );
343 emitCursorChanged();
344}
345
346
347void View::moveHome( int flag )
348{
349 cursor()->moveHome( flag );
350 emitCursorChanged();
351}
352
353void View::moveEnd( int flag )
354{
355 cursor()->moveEnd( flag );
356 emitCursorChanged();
357}
358
359
360void View::setSmallCursor(bool small)
361{
362 smallCursor() = small;
363}
364
365bool View::isHome() const
366{
367 return cursor()->isHome();
368}
369
370bool View::isEnd() const
371{
372 return cursor()->isEnd();
373}
374
375void View::eraseSelection( Direction direction )
376{
377 DirectedRemove r( req_remove, direction );
378 container()->performRequest( &r );
379}
380
381void View::addText( TQString str )
382{
383 TextRequest r( str );
384 container()->performRequest( &r );
385}
386
387void View::emitCursorChanged()
388{
389 if (cursor()->hasChanged() || cursorHasChanged()) {
391 cursor()->clearChangedFlag();
392 cursorHasChanged() = false;
393 cursor()->calcCursorSize( contextStyle(), smallCursor() );
394 activeCursor() = true;
395 startCursorTimer();
396 }
397 emit cursorChanged(cursorVisible(), cursor()->isSelection());
398}
399
400const ContextStyle& View::contextStyle() const
401{
402 return container()->document()->getContextStyle();
403}
404
405bool View::cursorVisible()
406{
407 return !cursor()->isReadOnly() || cursor()->isSelection();
408}
409
410KFORMULA_NAMESPACE_END
411
412using namespace KFormula;
413#include "kformulaview.moc"
Basis of every formula element.
The document.
virtual Document * document() const
void setActiveCursor(FormulaCursor *cursor)
Sets the cursor that is to be used for any editing.
void updateMatrixActions()
If the cursor is in a matrix, the matrix actions will be enabled, otherwise disabled.
void input(TQKeyEvent *event)
General input.
void draw(TQPainter &painter, const TQRect &r, const TQColorGroup &cg, bool edit=false)
Draws the whole thing.
Contains all the style information for the formela.
ContextStyle & getContextStyle(bool edit=false)
The selection.
void setReadOnly(bool ro)
Puts the widget in read only mode.
bool isSelection() const
Returns wether there actually is a selection.
void clearChangedFlag()
Resets the cursor's change flag.
bool isEnd() const
void calcCursorSize(const ContextStyle &context, bool smallCursor)
Calculates the size of the cursor.
void elementWillVanish(BasicElement *element)
The element is going to leave the formula with and all its children.
bool isHome() const
bool isReadOnly() const
void draw(TQPainter &, const ContextStyle &context, StyleAttributes &style, bool smallCursor, bool activeCursor)
Draws the cursor at its current position.
void formulaLoaded(FormulaElement *rootElement)
A new formula has been loaded.
The main element.
A point whose coordinates are floating-point values ( "double"s ).
Definition KoPoint.h:30
The view that shows the formula.
void cursorChanged(bool visible, bool selecting)
Is emitted every time the cursor might have changed.
bool isEnd() const
TQPoint getCursorPoint() const
void slotElementWillVanish(BasicElement *)
There is an element that will disappear from the tree.
bool isHome() const
void slotFormulaLoaded(FormulaElement *)
A new formula has been loaded.
void slotBlinkCursor()
Tell the cursor to change its visibility status.
void setReadOnly(bool ro)
Puts the widget in read only mode.
virtual Container * getDocument() const
The document we show.
void slotCursorMoved(FormulaCursor *cursor)
The cursor has been moved by the container.