00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <iostream>
00022
00023 #include <qpainter.h>
00024 #include <qtimer.h>
00025
00026 #include <kapplication.h>
00027 #include <kdebug.h>
00028
00029 #include "basicelement.h"
00030 #include "formulacursor.h"
00031 #include "formulaelement.h"
00032 #include "kformulacontainer.h"
00033 #include "kformuladocument.h"
00034 #include "kformulaview.h"
00035
00036 KFORMULA_NAMESPACE_BEGIN
00037
00038 struct View::View_Impl {
00039
00040 View_Impl(Container* doc, View* view)
00041 : smallCursor(false), activeCursor(true), cursorHasChanged(true),
00042 document(doc)
00043 {
00044 connect(document, SIGNAL(elementWillVanish(BasicElement*)),
00045 view, SLOT(slotElementWillVanish(BasicElement*)));
00046 connect(document, SIGNAL(formulaLoaded(FormulaElement*)),
00047 view, SLOT(slotFormulaLoaded(FormulaElement*)));
00048 connect(document, SIGNAL(cursorMoved(FormulaCursor*)),
00049 view, SLOT(slotCursorMoved(FormulaCursor*)));
00050
00051 cursor = document->createCursor();
00052 blinkTimer = new QTimer( view );
00053 connect( blinkTimer, SIGNAL( timeout() ),
00054 view, SLOT( slotBlinkCursor() ) );
00055 if ( QApplication::cursorFlashTime() > 0 )
00056 blinkTimer->start( QApplication::cursorFlashTime() / 2 );
00057 }
00058
00059 void startTimer()
00060 {
00061 if ( QApplication::cursorFlashTime() > 0 )
00062 blinkTimer->start( QApplication::cursorFlashTime() / 2 );
00063 }
00064
00065 void stopTimer()
00066 {
00067 blinkTimer->stop();
00068 }
00069
00070 ~View_Impl()
00071 {
00072 if ( document->activeCursor() == cursor ) {
00073 document->setActiveCursor( 0 );
00074 }
00075 delete cursor;
00076 delete blinkTimer;
00077 }
00078
00082 bool smallCursor;
00083
00087 bool activeCursor;
00088
00093 bool cursorHasChanged;
00094
00098 QTimer *blinkTimer;
00099
00103 Container* document;
00104
00108 FormulaCursor* cursor;
00109 };
00110
00111
00112 FormulaCursor* View::cursor() const { return impl->cursor; }
00113 bool& View::cursorHasChanged() { return impl->cursorHasChanged; }
00114 bool& View::smallCursor() { return impl->smallCursor; }
00115 bool& View::activeCursor() { return impl->activeCursor; }
00116 Container* View::container() const { return impl->document; }
00117 void View::startCursorTimer() { impl->startTimer(); }
00118 void View::stopCursorTimer() { impl->stopTimer(); }
00119
00120
00121 View::View(Container* doc)
00122 {
00123 impl = new View_Impl(doc, this);
00124 cursor()->calcCursorSize( contextStyle(), smallCursor() );
00125 }
00126
00127 View::~View()
00128 {
00129 delete impl;
00130 }
00131
00132
00133 QPoint View::getCursorPoint() const
00134 {
00135 return contextStyle().layoutUnitToPixel( cursor()->getCursorPoint() );
00136 }
00137
00138 void View::setReadOnly(bool ro)
00139 {
00140 cursor()->setReadOnly(ro);
00141 }
00142
00143
00144 void View::calcCursor()
00145 {
00146 cursor()->calcCursorSize( contextStyle(), smallCursor() );
00147 }
00148
00149
00150 void View::draw(QPainter& painter, const QRect& rect, const QColorGroup& cg)
00151 {
00152
00153
00154 container()->draw( painter, rect, cg, true );
00155 if ( cursorVisible() ) {
00156 StyleAttributes style;
00157 cursor()->draw( painter, contextStyle(), style, smallCursor(), activeCursor() );
00158 }
00159 }
00160
00161 void View::draw(QPainter& painter, const QRect& rect)
00162 {
00163 container()->draw( painter, rect, true );
00164 if ( cursorVisible() ) {
00165 StyleAttributes style;
00166 cursor()->draw( painter, contextStyle(), style, smallCursor(), activeCursor() );
00167 }
00168 }
00169
00170 void View::keyPressEvent( QKeyEvent* event )
00171 {
00172 container()->input( event );
00173 }
00174
00175
00176 void View::focusInEvent(QFocusEvent*)
00177 {
00178
00179 container()->setActiveCursor(cursor());
00180 activeCursor() = true;
00181 startCursorTimer();
00182 smallCursor() = false;
00183 emitCursorChanged();
00184 }
00185
00186 void View::focusOutEvent(QFocusEvent*)
00187 {
00188
00189 activeCursor() = false;
00190 stopCursorTimer();
00191 smallCursor() = true;
00192 emitCursorChanged();
00193 }
00194
00195 void View::mousePressEvent( QMouseEvent* event )
00196 {
00197 const ContextStyle& context = contextStyle();
00198 mousePressEvent( event, context.pixelToLayoutUnit( event->pos() ) );
00199 }
00200
00201 void View::mouseReleaseEvent( QMouseEvent* event )
00202 {
00203 const ContextStyle& context = contextStyle();
00204 mouseReleaseEvent( event, context.pixelToLayoutUnit( event->pos() ) );
00205 }
00206
00207 void View::mouseDoubleClickEvent( QMouseEvent* event )
00208 {
00209 const ContextStyle& context = contextStyle();
00210 mouseDoubleClickEvent( event, context.pixelToLayoutUnit( event->pos() ) );
00211 }
00212
00213 void View::mouseMoveEvent( QMouseEvent* event )
00214 {
00215 const ContextStyle& context = contextStyle();
00216 mouseMoveEvent( event, context.pixelToLayoutUnit( event->pos() ) );
00217 }
00218
00219 void View::wheelEvent( QWheelEvent* event )
00220 {
00221 const ContextStyle& context = contextStyle();
00222 wheelEvent( event, context.pixelToLayoutUnit( event->pos() ) );
00223 }
00224
00225 void View::mousePressEvent( QMouseEvent* event, const PtPoint& pos )
00226 {
00227 const ContextStyle& context = contextStyle();
00228 mousePressEvent( event, context.ptToLayoutUnitPix( pos ) );
00229 }
00230
00231 void View::mouseReleaseEvent( QMouseEvent* event, const PtPoint& pos )
00232 {
00233 const ContextStyle& context = contextStyle();
00234 mouseReleaseEvent( event, context.ptToLayoutUnitPix( pos ) );
00235 }
00236
00237 void View::mouseDoubleClickEvent( QMouseEvent* event, const PtPoint& pos )
00238 {
00239 const ContextStyle& context = contextStyle();
00240 mouseDoubleClickEvent( event, context.ptToLayoutUnitPix( pos ) );
00241 }
00242
00243 void View::mouseMoveEvent( QMouseEvent* event, const PtPoint& pos )
00244 {
00245 const ContextStyle& context = contextStyle();
00246 mouseMoveEvent( event, context.ptToLayoutUnitPix( pos ) );
00247 }
00248
00249 void View::wheelEvent( QWheelEvent* event, const PtPoint& pos )
00250 {
00251 const ContextStyle& context = contextStyle();
00252 wheelEvent( event, context.ptToLayoutUnitPix( pos ) );
00253 }
00254
00255
00256 void View::mousePressEvent( QMouseEvent* event, const LuPixelPoint& pos )
00257 {
00258 int flags = movementFlag( event->state() );
00259 cursor()->mousePress( pos, flags );
00260 emitCursorChanged();
00261 }
00262
00263 void View::mouseReleaseEvent( QMouseEvent* event, const LuPixelPoint& pos )
00264 {
00265 int flags = movementFlag( event->state() );
00266 cursor()->mouseRelease( pos, flags );
00267 emitCursorChanged();
00268 }
00269
00270 void View::mouseDoubleClickEvent( QMouseEvent*, const LuPixelPoint& )
00271 {
00272 cursor()->moveRight( WordMovement );
00273 cursor()->moveLeft( SelectMovement | WordMovement );
00274 emitCursorChanged();
00275 }
00276
00277 void View::mouseMoveEvent( QMouseEvent* event, const LuPixelPoint& pos )
00278 {
00279 int flags = movementFlag( event->state() );
00280 cursor()->mouseMove( pos, flags );
00281 emitCursorChanged();
00282 }
00283
00284 void View::wheelEvent( QWheelEvent*, const LuPixelPoint& )
00285 {
00286 }
00287
00288
00289 void View::slotCursorMoved(FormulaCursor* c)
00290 {
00291 if (c == cursor()) {
00292 cursorHasChanged() = true;
00293 emitCursorChanged();
00294 }
00295 }
00296
00297 void View::slotFormulaLoaded(FormulaElement* formula)
00298 {
00299 cursor()->formulaLoaded(formula);
00300 }
00301
00302 void View::slotElementWillVanish(BasicElement* element)
00303 {
00304 cursor()->elementWillVanish(element);
00305 emitCursorChanged();
00306 }
00307
00308 void View::slotBlinkCursor()
00309 {
00310 activeCursor() = ! activeCursor();
00311 emitCursorChanged();
00312 }
00313
00314 void View::slotSelectAll()
00315 {
00316 cursor()->moveHome(WordMovement);
00317 cursor()->moveEnd(SelectMovement | WordMovement);
00318 emitCursorChanged();
00319 }
00320
00321
00322 void View::moveLeft( int flag )
00323 {
00324 cursor()->moveLeft( flag );
00325 emitCursorChanged();
00326 }
00327
00328 void View::moveRight( int flag )
00329 {
00330 cursor()->moveRight( flag );
00331 emitCursorChanged();
00332 }
00333
00334 void View::moveUp( int flag )
00335 {
00336 cursor()->moveUp( flag );
00337 emitCursorChanged();
00338 }
00339
00340 void View::moveDown( int flag )
00341 {
00342 cursor()->moveDown( flag );
00343 emitCursorChanged();
00344 }
00345
00346
00347 void View::moveHome( int flag )
00348 {
00349 cursor()->moveHome( flag );
00350 emitCursorChanged();
00351 }
00352
00353 void View::moveEnd( int flag )
00354 {
00355 cursor()->moveEnd( flag );
00356 emitCursorChanged();
00357 }
00358
00359
00360 void View::setSmallCursor(bool small)
00361 {
00362 smallCursor() = small;
00363 }
00364
00365 bool View::isHome() const
00366 {
00367 return cursor()->isHome();
00368 }
00369
00370 bool View::isEnd() const
00371 {
00372 return cursor()->isEnd();
00373 }
00374
00375 void View::eraseSelection( Direction direction )
00376 {
00377 DirectedRemove r( req_remove, direction );
00378 container()->performRequest( &r );
00379 }
00380
00381 void View::addText( QString str )
00382 {
00383 TextRequest r( str );
00384 container()->performRequest( &r );
00385 }
00386
00387 void View::emitCursorChanged()
00388 {
00389 if (cursor()->hasChanged() || cursorHasChanged()) {
00390 getDocument()->updateMatrixActions();
00391 cursor()->clearChangedFlag();
00392 cursorHasChanged() = false;
00393 cursor()->calcCursorSize( contextStyle(), smallCursor() );
00394 activeCursor() = true;
00395 startCursorTimer();
00396 }
00397 emit cursorChanged(cursorVisible(), cursor()->isSelection());
00398 }
00399
00400 const ContextStyle& View::contextStyle() const
00401 {
00402 return container()->document()->getContextStyle();
00403 }
00404
00405 bool View::cursorVisible()
00406 {
00407 return !cursor()->isReadOnly() || cursor()->isSelection();
00408 }
00409
00410 KFORMULA_NAMESPACE_END
00411
00412 using namespace KFormula;
00413 #include "kformulaview.moc"