lib

kformulaview.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
00003                   Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
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 //     kdDebug( DEBUGID ) << "View::draw: " << rect.x() << " " << rect.y() << " "
00153 //                      << rect.width() << " " << rect.height() << endl;
00154     container()->draw( painter, rect, cg, true );
00155     if ( cursorVisible() ) {
00156         cursor()->draw( painter, contextStyle(), smallCursor(), activeCursor() );
00157     }
00158 }
00159 
00160 void View::draw(QPainter& painter, const QRect& rect)
00161 {
00162     container()->draw( painter, rect, true );
00163     if ( cursorVisible() ) {
00164         cursor()->draw( painter, contextStyle(), smallCursor(), activeCursor() );
00165     }
00166 }
00167 
00168 void View::keyPressEvent( QKeyEvent* event )
00169 {
00170     container()->input( event );
00171 }
00172 
00173 
00174 void View::focusInEvent(QFocusEvent*)
00175 {
00176     //cursor()->calcCursorSize( contextStyle(), smallCursor() );
00177     container()->setActiveCursor(cursor());
00178     activeCursor() = true;
00179     startCursorTimer();
00180     smallCursor() = false;
00181     emitCursorChanged();
00182 }
00183 
00184 void View::focusOutEvent(QFocusEvent*)
00185 {
00186     //container()->setActiveCursor(0);
00187     activeCursor() = false;
00188     stopCursorTimer();
00189     smallCursor() = true;
00190     emitCursorChanged();
00191 }
00192 
00193 void View::mousePressEvent( QMouseEvent* event )
00194 {
00195     const ContextStyle& context = contextStyle();
00196     mousePressEvent( event, context.pixelToLayoutUnit( event->pos() ) );
00197 }
00198 
00199 void View::mouseReleaseEvent( QMouseEvent* event )
00200 {
00201     const ContextStyle& context = contextStyle();
00202     mouseReleaseEvent( event, context.pixelToLayoutUnit( event->pos() ) );
00203 }
00204 
00205 void View::mouseDoubleClickEvent( QMouseEvent* event )
00206 {
00207     const ContextStyle& context = contextStyle();
00208     mouseDoubleClickEvent( event, context.pixelToLayoutUnit( event->pos() ) );
00209 }
00210 
00211 void View::mouseMoveEvent( QMouseEvent* event )
00212 {
00213     const ContextStyle& context = contextStyle();
00214     mouseMoveEvent( event, context.pixelToLayoutUnit( event->pos() ) );
00215 }
00216 
00217 void View::wheelEvent( QWheelEvent* event )
00218 {
00219     const ContextStyle& context = contextStyle();
00220     wheelEvent( event, context.pixelToLayoutUnit( event->pos() ) );
00221 }
00222 
00223 void View::mousePressEvent( QMouseEvent* event, const PtPoint& pos )
00224 {
00225     const ContextStyle& context = contextStyle();
00226     mousePressEvent( event, context.ptToLayoutUnitPix( pos ) );
00227 }
00228 
00229 void View::mouseReleaseEvent( QMouseEvent* event, const PtPoint& pos )
00230 {
00231     const ContextStyle& context = contextStyle();
00232     mouseReleaseEvent( event, context.ptToLayoutUnitPix( pos ) );
00233 }
00234 
00235 void View::mouseDoubleClickEvent( QMouseEvent* event, const PtPoint& pos )
00236 {
00237     const ContextStyle& context = contextStyle();
00238     mouseDoubleClickEvent( event, context.ptToLayoutUnitPix( pos ) );
00239 }
00240 
00241 void View::mouseMoveEvent( QMouseEvent* event, const PtPoint& pos )
00242 {
00243     const ContextStyle& context = contextStyle();
00244     mouseMoveEvent( event, context.ptToLayoutUnitPix( pos ) );
00245 }
00246 
00247 void View::wheelEvent( QWheelEvent* event, const PtPoint& pos )
00248 {
00249     const ContextStyle& context = contextStyle();
00250     wheelEvent( event, context.ptToLayoutUnitPix( pos ) );
00251 }
00252 
00253 
00254 void View::mousePressEvent( QMouseEvent* event, const LuPixelPoint& pos )
00255 {
00256     int flags = movementFlag( event->state() );
00257     cursor()->mousePress( pos, flags );
00258     emitCursorChanged();
00259 }
00260 
00261 void View::mouseReleaseEvent( QMouseEvent* event, const LuPixelPoint& pos )
00262 {
00263     int flags = movementFlag( event->state() );
00264     cursor()->mouseRelease( pos, flags );
00265     emitCursorChanged();
00266 }
00267 
00268 void View::mouseDoubleClickEvent( QMouseEvent*, const LuPixelPoint& )
00269 {
00270     cursor()->moveRight( WordMovement );
00271     cursor()->moveLeft( SelectMovement | WordMovement );
00272     emitCursorChanged();
00273 }
00274 
00275 void View::mouseMoveEvent( QMouseEvent* event, const LuPixelPoint& pos )
00276 {
00277     int flags = movementFlag( event->state() );
00278     cursor()->mouseMove( pos, flags );
00279     emitCursorChanged();
00280 }
00281 
00282 void View::wheelEvent( QWheelEvent*, const LuPixelPoint& )
00283 {
00284 }
00285 
00286 
00287 void View::slotCursorMoved(FormulaCursor* c)
00288 {
00289     if (c == cursor()) {
00290         cursorHasChanged() = true;
00291         emitCursorChanged();
00292     }
00293 }
00294 
00295 void View::slotFormulaLoaded(FormulaElement* formula)
00296 {
00297     cursor()->formulaLoaded(formula);
00298 }
00299 
00300 void View::slotElementWillVanish(BasicElement* element)
00301 {
00302     cursor()->elementWillVanish(element);
00303     emitCursorChanged();
00304 }
00305 
00306 void View::slotBlinkCursor()
00307 {
00308     activeCursor() = ! activeCursor();
00309     emitCursorChanged();
00310 }
00311 
00312 void View::slotSelectAll()
00313 {
00314     cursor()->moveHome(WordMovement);
00315     cursor()->moveEnd(SelectMovement | WordMovement);
00316     emitCursorChanged();
00317 }
00318 
00319 
00320 void View::moveLeft( int flag )
00321 {
00322     cursor()->moveLeft( flag );
00323     emitCursorChanged();
00324 }
00325 
00326 void View::moveRight( int flag )
00327 {
00328     cursor()->moveRight( flag );
00329     emitCursorChanged();
00330 }
00331 
00332 void View::moveUp( int flag )
00333 {
00334     cursor()->moveUp( flag );
00335     emitCursorChanged();
00336 }
00337 
00338 void View::moveDown( int flag )
00339 {
00340     cursor()->moveDown( flag );
00341     emitCursorChanged();
00342 }
00343 
00344 
00345 void View::moveHome( int flag )
00346 {
00347     cursor()->moveHome( flag );
00348     emitCursorChanged();
00349 }
00350 
00351 void View::moveEnd( int flag )
00352 {
00353     cursor()->moveEnd( flag );
00354     emitCursorChanged();
00355 }
00356 
00357 
00358 void View::setSmallCursor(bool small)
00359 {
00360     smallCursor() = small;
00361 }
00362 
00363 bool View::isHome() const
00364 {
00365     return cursor()->isHome();
00366 }
00367 
00368 bool View::isEnd() const
00369 {
00370     return cursor()->isEnd();
00371 }
00372 
00373 void View::eraseSelection( Direction direction )
00374 {
00375     DirectedRemove r( req_remove, direction );
00376     container()->performRequest( &r );
00377 }
00378 
00379 void View::addText( QString str )
00380 {
00381     TextRequest r( str );
00382     container()->performRequest( &r );
00383 }
00384 
00385 void View::emitCursorChanged()
00386 {
00387     if (cursor()->hasChanged() || cursorHasChanged()) {
00388         getDocument()->updateMatrixActions();
00389         cursor()->clearChangedFlag();
00390         cursorHasChanged() = false;
00391         cursor()->calcCursorSize( contextStyle(), smallCursor() );
00392         activeCursor() = true;
00393         startCursorTimer();
00394     }
00395     emit cursorChanged(cursorVisible(), cursor()->isSelection());
00396 }
00397 
00398 const ContextStyle& View::contextStyle() const
00399 {
00400     return container()->document()->getContextStyle();
00401 }
00402 
00403 bool View::cursorVisible()
00404 {
00405     return !cursor()->isReadOnly() || cursor()->isSelection();
00406 }
00407 
00408 KFORMULA_NAMESPACE_END
00409 
00410 using namespace KFormula;
00411 #include "kformulaview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys