lib

KoGuides.cpp

00001 // -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
00002 /* This file is part of the KDE project
00003    Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
00004    Copyright (C) 2005 Casper Boemann Rasmussen <cbr@boemann.dk>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "KoGuides.h"
00023 
00024 #include <qcursor.h>
00025 #include <qpainter.h>
00026 #include <qpixmap.h>
00027 
00028 #include <klocale.h>
00029 #include <kpopupmenu.h>
00030 
00031 #include <KoDocument.h>
00032 #include <KoPoint.h>
00033 #include <KoRect.h>
00034 #include <KoView.h>
00035 #include <KoZoomHandler.h>
00036 
00037 #include "KoGuideLineDia.h"
00038 
00039 class KoGuides::Popup : public KPopupMenu
00040 {
00041 public: 
00042     Popup( KoGuides * guides )
00043     {
00044         m_title = insertTitle( i18n( "Guide Line" ) );
00045         m_delete = insertItem( i18n( "&Delete" ), guides, SLOT( slotRemove() ) );
00046         m_seperator = insertSeparator();
00047         m_pos = insertItem( i18n( "&Set Position..." ), guides, SLOT( slotChangePosition() ) );
00048     }
00049 
00050     void update( int count )
00051     {
00052         if ( count == 1 )
00053         {
00054             changeTitle( m_title, i18n( "Guide Line" ) );
00055             setItemVisible( m_seperator, true );
00056             setItemVisible( m_pos, true );
00057         }
00058         else
00059         {
00060             changeTitle( m_title, i18n( "Guide Lines" ) );
00061             setItemVisible( m_seperator, false );
00062             setItemVisible( m_pos, false );
00063         }
00064     }
00065 private:
00066     int m_title;
00067     int m_delete;
00068     int m_seperator;
00069     int m_pos;
00070 };
00071 
00072 const KoGuides::SnapStatus KoGuides::SNAP_NONE = 0;
00073 const KoGuides::SnapStatus KoGuides::SNAP_HORIZ = 1;
00074 const KoGuides::SnapStatus KoGuides::SNAP_VERT = 2;
00075 const KoGuides::SnapStatus KoGuides::SNAP_BOTH = 3;
00076 
00077 KoGuides::KoGuides( KoView *view, KoZoomHandler *zoomHandler )
00078 : m_view( view )
00079 , m_zoomHandler( zoomHandler )
00080 {
00081     m_popup = new Popup( this );
00082     m_mouseSelected = false;
00083 }
00084 
00085 
00086 KoGuides::~KoGuides()
00087 {
00088     delete m_popup;
00089 }
00090 
00091 
00092 void KoGuides::paintGuides( QPainter &painter )
00093 {
00094     //painter.setRasterOp( NotROP );
00095     const KoPageLayout& pl = m_view->koDocument()->pageLayout();
00096     int width = QMAX( m_view->canvas()->width(), m_zoomHandler->zoomItX( pl.ptWidth ) );
00097     int height = QMAX( m_view->canvas()->height(), m_zoomHandler->zoomItY( pl.ptHeight ) );
00098 
00099     for ( int i = 0; i < GL_END; ++i )
00100     {
00101         QValueList<KoGuideLine *>::iterator it = m_guideLines[i].begin();
00102         for ( ; it != m_guideLines[i].end(); ++it )
00103         {
00104             if ( !( *it )->automatic || ( *it )->snapping ) // dont paint autoStyle guides when they are not snapping
00105             {
00106                 if ( ( *it )->snapping )
00107                     painter.setPen( QPen( green, 0, DotLine ) );
00108                 else if ( ( *it )->selected )
00109                     painter.setPen( QPen( red, 0, DotLine ) );
00110                 else
00111                     painter.setPen( QPen( blue, 0, DotLine ) );
00112 
00113                 painter.save();
00114                 if ( ( *it )->orientation == Qt::Vertical )
00115                 {
00116                     painter.translate( m_zoomHandler->zoomItX( ( *it )->position ), 0 );
00117                     painter.drawLine( 0, 0, 0, height );
00118                 }
00119                 else
00120                 {
00121                     painter.translate( 0, m_zoomHandler->zoomItY( ( *it )->position ) );
00122                     painter.drawLine( 0, 0, width, 0 );
00123                 }
00124                 painter.restore();
00125             }
00126         }
00127     }
00128 }
00129 
00130 bool KoGuides::mousePressEvent( QMouseEvent *e )
00131 {
00132     bool eventProcessed = true;
00133     bool changed = false;
00134     m_mouseSelected = false;
00135     
00136     KoPoint p( mapFromScreen( e->pos() ) );
00137     KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
00138     if ( guideLine )
00139     {
00140         m_lastPoint = e->pos();
00141         if ( e->button() == Qt::LeftButton || e->button() == Qt::RightButton )
00142         {
00143             if ( e->button() == Qt::LeftButton )
00144             {
00145                 m_mouseSelected = true;
00146             }
00147             if ( e->state() & Qt::ControlButton )
00148             {
00149                 if ( guideLine->selected )
00150                 {
00151                     unselect( guideLine );
00152                     m_mouseSelected = false;
00153                 }
00154                 else
00155                 {
00156                     select( guideLine );
00157                 }
00158                 changed = true;
00159             }
00160             else if ( ! guideLine->selected )
00161             {
00162                 unselectAll();
00163                 select( guideLine );
00164                 changed = true;
00165             }
00166         }
00167     }
00168     else 
00169     {   
00170         if ( !( e->state() & Qt::ControlButton ) )
00171         {
00172             changed = unselectAll();
00173         }
00174         eventProcessed = false;
00175     }
00176     
00177     if ( changed || hasSelected() )
00178     {
00179         emit moveGuides( true );
00180     }
00181 
00182     if ( changed )
00183     {
00184         paint();
00185     }
00186 
00187     if ( changed && ! hasSelected() )
00188     {
00189         emit moveGuides( false );
00190     }
00191 
00192     if ( e->button() == Qt::RightButton && hasSelected() )
00193     {
00194         m_popup->update( m_guideLines[GL_SELECTED].count() );
00195         m_popup->exec( QCursor::pos() );
00196         emit moveGuides( false );
00197     }
00198     
00199     return eventProcessed;
00200 }
00201 
00202 
00203 bool KoGuides::mouseMoveEvent( QMouseEvent *e )
00204 {
00205     bool eventProcessed = false;
00206     if ( m_mouseSelected )
00207     {
00208         QPoint p( e->pos() );
00209         p -= m_lastPoint;
00210         m_lastPoint = e->pos();
00211         moveSelectedBy( p );
00212         paint();
00213         emit guideLinesChanged( m_view );
00214         eventProcessed = true;
00215     }
00216     else if ( e->state() == Qt::NoButton )
00217     {
00218         KoPoint p( mapFromScreen( e->pos() ) );
00219         KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
00220         if ( guideLine )
00221         {
00222             m_view->canvas()->setCursor( guideLine->orientation == Qt::Vertical ? Qt::sizeHorCursor : Qt::sizeVerCursor );
00223             eventProcessed = true;
00224         }
00225     }
00226     return eventProcessed;
00227 }
00228 
00229 
00230 bool KoGuides::mouseReleaseEvent( QMouseEvent *e )
00231 {
00232     bool eventProcessed = false;
00233     if ( m_mouseSelected )
00234     {
00235         KoPoint p( mapFromScreen( e->pos() ) );
00236         if ( m_guideLines[GL_SELECTED].count() == 1 )
00237         {
00238             int x1, y1, x2, y2;
00239             m_view->canvas()->rect().coords( &x1, &y1, &x2, &y2 );
00240             QPoint gp( m_view->canvas()->mapFromGlobal( e->globalPos() ) );
00241             if ( m_guideLines[GL_SELECTED].first()->orientation == Qt::Vertical )
00242             {
00243                 if ( gp.x() < x1 || gp.x() > x2 )
00244                     removeSelected();
00245             }
00246             else
00247             {
00248                 if ( gp.y() < y1 || gp.y() > y2 )
00249                     removeSelected();
00250             }
00251         }
00252         KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
00253         if ( guideLine )
00254         {
00255             m_view->canvas()->setCursor( guideLine->orientation == Qt::Vertical ? Qt::sizeHorCursor : Qt::sizeVerCursor );
00256         }
00257         m_mouseSelected = false;
00258         eventProcessed = true;
00259         emit guideLinesChanged( m_view );
00260     }
00261     emit moveGuides( false );
00262     return eventProcessed;
00263 }
00264 
00265 
00266 bool KoGuides::keyPressEvent( QKeyEvent *e )
00267 {
00268     bool eventProcessed = false;
00269     switch( e->key() )
00270     {
00271         case Qt::Key_Delete: 
00272             if ( hasSelected() )
00273             {
00274                 removeSelected();
00275                 paint();
00276                 emit guideLinesChanged( m_view );
00277                 eventProcessed = true;
00278             }
00279             break;
00280         default:
00281             break;
00282     }
00283     return eventProcessed;
00284 }
00285 
00286 void KoGuides::setGuideLines( const QValueList<double> &horizontalPos, const QValueList<double> &verticalPos )
00287 {
00288     removeSelected();
00289 
00290     QValueList<KoGuideLine *>::iterator it = m_guideLines[GL].begin();
00291     for ( ; it != m_guideLines[GL].end(); ++it )
00292     {
00293         delete ( *it );
00294     }
00295     m_guideLines[GL].clear();
00296 
00297     QValueList<double>::ConstIterator posIt = horizontalPos.begin();
00298     for ( ; posIt != horizontalPos.end(); ++posIt )
00299     {
00300         KoGuideLine *guideLine = new KoGuideLine( Qt::Horizontal, *posIt, false );
00301         m_guideLines[GL].append( guideLine );
00302     }
00303     posIt = verticalPos.begin();
00304     for ( ; posIt != verticalPos.end(); ++posIt )
00305     {
00306         KoGuideLine *guideLine = new KoGuideLine( Qt::Vertical, *posIt, false );
00307         m_guideLines[GL].append( guideLine );
00308     }
00309     paint();
00310 }
00311 
00312 void KoGuides::setAutoGuideLines( const QValueList<double> &horizontalPos, const QValueList<double> &verticalPos )
00313 {
00314     QValueList<KoGuideLine *>::iterator it = m_guideLines[GL_AUTOMATIC].begin();
00315     for ( ; it != m_guideLines[GL_AUTOMATIC].end(); ++it )
00316     {
00317         delete ( *it );
00318     }
00319     m_guideLines[GL_AUTOMATIC].clear();
00320 
00321     QValueList<double>::ConstIterator posIt = horizontalPos.begin();
00322     for ( ; posIt != horizontalPos.end(); ++posIt )
00323     {
00324         KoGuideLine *guideLine = new KoGuideLine( Qt::Horizontal, *posIt, true );
00325         m_guideLines[GL_AUTOMATIC].append( guideLine );
00326     }
00327     posIt = verticalPos.begin();
00328     for ( ; posIt != verticalPos.end(); ++posIt )
00329     {
00330         KoGuideLine *guideLine = new KoGuideLine( Qt::Vertical, *posIt, true );
00331         m_guideLines[GL_AUTOMATIC].append( guideLine );
00332     }
00333 }
00334 
00335 
00336 void KoGuides::getGuideLines( QValueList<double> &horizontalPos, QValueList<double> &verticalPos ) const
00337 {
00338     horizontalPos.clear();
00339     verticalPos.clear();
00340 
00341     QValueList<KoGuideLine *>::const_iterator it = m_guideLines[GL].begin();
00342     for ( ; it != m_guideLines[GL].end(); ++it )
00343     {
00344         if ( ( *it )->orientation == Qt::Horizontal )
00345         {
00346             horizontalPos.append( ( *it )->position );
00347         }
00348         else
00349         {
00350             verticalPos.append( ( *it )->position );
00351         }
00352     }
00353     it = m_guideLines[GL_SELECTED].begin();
00354     for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
00355     {
00356         if ( ( *it )->orientation == Qt::Horizontal )
00357         {
00358             horizontalPos.append( ( *it )->position );
00359         }
00360         else
00361         {
00362             verticalPos.append( ( *it )->position );
00363         }
00364     }
00365 }
00366 
00367 
00368 void KoGuides::snapToGuideLines( KoRect &rect, int snap, SnapStatus &snapStatus, KoPoint &diff )
00369 {
00370     if( !(snapStatus & SNAP_VERT))
00371         diff.setX(10000);
00372     if( !(snapStatus & SNAP_HORIZ))
00373         diff.setY(10000);
00374 
00375     for ( int i = 0; i < GL_END; ++i )
00376     {
00377         QValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
00378         for ( ; it != m_guideLines[i].end(); ++it )
00379         {
00380             if ( ( *it )->orientation == Qt::Horizontal )
00381             {
00382                 double tmp = (*it)->position - rect.top();
00383                 if ( snapStatus & SNAP_HORIZ || QABS( tmp ) < m_zoomHandler->unzoomItY( snap ) )
00384                 {
00385                     if(QABS( tmp ) < QABS(diff.y()))
00386                     {
00387                         diff.setY( tmp );
00388                         snapStatus |= SNAP_HORIZ;
00389                     }
00390                 }
00391                 tmp = (*it)->position - rect.bottom();
00392                 if ( snapStatus & SNAP_HORIZ || QABS( tmp ) < m_zoomHandler->unzoomItY( snap ) )
00393                 {
00394                     if(QABS( tmp ) < QABS(diff.y()))
00395                     {
00396                         diff.setY( tmp );
00397                         snapStatus |= SNAP_HORIZ;
00398                     }
00399                 }
00400             }
00401             else
00402             {
00403                 double tmp = (*it)->position - rect.left();
00404                 if ( snapStatus & SNAP_VERT || QABS( tmp ) < m_zoomHandler->unzoomItX( snap ) )
00405                 {
00406                     if(QABS( tmp ) < QABS(diff.x()))
00407                     {
00408                         diff.setX( tmp );
00409                         snapStatus |= SNAP_VERT;
00410                     }
00411                 }
00412                 tmp = (*it)->position - rect.right();
00413                 if ( snapStatus & SNAP_VERT || QABS( tmp ) < m_zoomHandler->unzoomItX( snap ) )
00414                 {
00415                     if(QABS( tmp ) < QABS(diff.x()))
00416                     {
00417                         diff.setX( tmp );
00418                         snapStatus |= SNAP_VERT;
00419                     }
00420                 }
00421             }
00422         }
00423     }
00424 
00425     if(!(snapStatus & SNAP_VERT))
00426         diff.setX( 0 );
00427 
00428     if(!(snapStatus & SNAP_HORIZ))
00429         diff.setY( 0 );
00430 }
00431 
00432 void KoGuides::snapToGuideLines( KoPoint &pos, int snap, SnapStatus &snapStatus, KoPoint &diff )
00433 {
00434     if( !(snapStatus & SNAP_VERT))
00435         diff.setX(10000);
00436     if( !(snapStatus & SNAP_HORIZ))
00437         diff.setY(10000);
00438 
00439     for ( int i = 0; i < GL_END; ++i )
00440     {
00441         QValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
00442         for ( ; it != m_guideLines[i].end(); ++it )
00443         {
00444             if ( ( *it )->orientation == Qt::Horizontal )
00445             {
00446                 double tmp = (*it)->position - pos.y();
00447                 if ( snapStatus & SNAP_HORIZ || QABS( tmp ) < m_zoomHandler->unzoomItY( snap ) )
00448                 {
00449                     if(QABS( tmp ) < QABS(diff.y()))
00450                     {
00451                         diff.setY( tmp );
00452                         snapStatus |= SNAP_HORIZ;
00453                     }
00454                 }
00455             }
00456             else
00457             {
00458                 double tmp = (*it)->position - pos.x();
00459                 if ( snapStatus & SNAP_VERT || QABS( tmp ) < m_zoomHandler->unzoomItX( snap ) )
00460                 {
00461                     if(QABS( tmp ) < QABS(diff.x()))
00462                     {
00463                         diff.setX( tmp );
00464                         snapStatus |= SNAP_VERT;
00465                     }
00466                 }
00467             }
00468         }
00469     }
00470 
00471     if(!(snapStatus & SNAP_VERT))
00472         diff.setX( 0 );
00473 
00474     if(!(snapStatus & SNAP_HORIZ))
00475         diff.setY( 0 );
00476 }
00477 
00478 
00479 void KoGuides::repaintSnapping( const KoRect &snappedRect )
00480 {
00481     bool needRepaint = false;
00482     for ( int i = 0; i < GL_END; ++i )
00483     {
00484         QValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
00485         for ( ; it != m_guideLines[i].end(); ++it )
00486         {
00487             if ( ( *it )->orientation == Qt::Horizontal )
00488             {
00489                 if ( virtuallyEqual( snappedRect.top(), (*it)->position ) 
00490                         || virtuallyEqual( snappedRect.bottom(), ( *it )->position ) )
00491                 {
00492                     if ( ! ( *it )->snapping )
00493                     {
00494                         ( *it )->snapping = true;
00495                         needRepaint = true;
00496                     }
00497                 }
00498                 else if ( ( *it )->snapping )
00499                 {
00500                     ( *it )->snapping = false;
00501                     needRepaint = true;
00502                 }
00503             }
00504             else
00505             {
00506                 if ( virtuallyEqual( snappedRect.left(), (*it)->position )
00507                         || virtuallyEqual( snappedRect.right(), ( *it )->position ) )
00508                 {
00509                     if ( ! ( *it )->snapping )
00510                     {
00511                         ( *it )->snapping = true;
00512                         needRepaint = true;
00513                     }
00514                 }
00515                 else if ( ( *it )->snapping )
00516                 {
00517                     ( *it )->snapping = false;
00518                     needRepaint = true;
00519                 }
00520             }
00521         }
00522     }
00523 
00524     if ( needRepaint )
00525     {
00526         emit paintGuides( true );
00527         paint();
00528         emit paintGuides( false );
00529     }
00530 }
00531 
00532 
00533 void KoGuides::repaintSnapping( const KoPoint &snappedPoint, SnapStatus snapStatus )
00534 {
00535     bool needRepaint = false;
00536     for ( int i = 0; i < GL_END; ++i )
00537     {
00538         QValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
00539         for ( ; it != m_guideLines[i].end(); ++it )
00540         {
00541             if ( ( *it )->orientation == Qt::Horizontal && ( snapStatus & SNAP_HORIZ ) )
00542             {
00543                 if( virtuallyEqual( snappedPoint.y(), (*it)->position ) )
00544                 {
00545                     if ( ! ( *it )->snapping )
00546                     {
00547                         ( *it )->snapping = true;
00548                         needRepaint = true;
00549                     }
00550                 }
00551                 else if ( ( *it )->snapping )
00552                 {
00553                     ( *it )->snapping = false;
00554                     needRepaint = true;
00555                 }
00556             }
00557             else
00558             {
00559                 if ( snapStatus & SNAP_VERT )
00560                 {
00561                     if( virtuallyEqual( snappedPoint.x(), (*it)->position ) )
00562                     {
00563                         if ( ! ( *it )->snapping )
00564                         {
00565                             ( *it )->snapping = true;
00566                             needRepaint = true;
00567                         }
00568                     }
00569                     else if ( ( *it )->snapping )
00570                     {
00571                         ( *it )->snapping = false;
00572                         needRepaint = true;
00573                     }
00574                 }
00575             }
00576         }
00577     }
00578 
00579     if ( needRepaint )
00580     {
00581         emit paintGuides( true );
00582         paint();
00583         emit paintGuides( false );
00584     }
00585 }
00586 
00587 
00588 void KoGuides::repaintAfterSnapping()
00589 {
00590     bool needRepaint = false;
00591 
00592     for ( int i = 0; i < GL_END; ++i )
00593     {
00594         QValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
00595         for ( ; it != m_guideLines[i].end(); ++it )
00596         {
00597             if ( ( *it )->snapping )
00598             {
00599                 needRepaint = true;
00600                 ( *it )->snapping = false;
00601             }
00602         }
00603     }
00604 
00605     if ( needRepaint )
00606     {
00607         emit paintGuides( true );
00608         paint();
00609         emit paintGuides( false );
00610     }
00611 }
00612 
00613 
00614 void KoGuides::diffNextGuide( KoRect &rect, KoPoint &diff )
00615 {
00616     for ( int i = 0; i < GL_END; ++i )
00617     {
00618         QValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
00619         for ( ; it != m_guideLines[i].end(); ++it )
00620         {
00621             if ( ( *it )->orientation == Qt::Horizontal )
00622             {
00623                 double moveyl = ( *it )->position - rect.top();
00624                 double moveyr = ( *it )->position - rect.bottom();
00625                 if ( diff.y() > 0 )
00626                 {
00627                     if ( moveyl < diff.y() && moveyl > 1E-10 )
00628                     {
00629                         diff.setY( moveyl );
00630                     }
00631                     if ( moveyr < diff.y() && moveyr > 1E-10 )
00632                     {
00633                         diff.setY( moveyr );
00634                     }
00635                 }
00636                 else if ( diff.y() < 0 )
00637                 {
00638                     if ( moveyl > diff.y() && moveyl < -1E-10 )
00639                     {
00640                         diff.setY( moveyl );
00641                     }
00642                     if ( moveyr > diff.y() && moveyr < -1E-10 )
00643                     {
00644                         diff.setY( moveyr );
00645                     }
00646                 }
00647             }
00648             else
00649             {
00650                 double movexl = ( *it )->position - rect.left();
00651                 double movexr = ( *it )->position - rect.right();
00652                 if ( diff.x() > 0 )
00653                 {
00654                     if ( movexl < diff.x() && movexl > 1E-10 )
00655                     {
00656                         diff.setX( movexl );
00657                     }
00658                     if ( ( movexr < diff.x() ) && movexr > 1E-10 )
00659                     {
00660                         diff.setX( movexr );
00661                     }
00662                 }
00663                 else if ( diff.x() < 0 )
00664                 {
00665                     if ( movexl > diff.x() && movexl < -1E-10 )
00666                     {
00667                         diff.setX( movexl );
00668                     }
00669                     if ( movexr > diff.x() && movexr < -1E-10 )
00670                     {
00671                         diff.setX( movexr );
00672                     }
00673                 }
00674             }
00675         }
00676     }
00677 }
00678 
00679 
00680 void KoGuides::moveGuide( const QPoint &pos, bool horizontal, int rulerWidth )
00681 {
00682     int x = pos.x() - rulerWidth;
00683     int y = pos.y() - rulerWidth;
00684     QPoint p( x, y );
00685     if ( !m_insertGuide )
00686     {
00687         if ( ! horizontal && x > 0 )
00688         {
00689             m_insertGuide = true;
00690             add( Qt::Vertical, p );
00691         }
00692         else if ( horizontal && y > 0 )
00693         {
00694             m_insertGuide = true;
00695             add( Qt::Horizontal, p );
00696         }
00697         if ( m_insertGuide )
00698         {
00699             QMouseEvent e( QEvent::MouseButtonPress, p, Qt::LeftButton, Qt::LeftButton );
00700             mousePressEvent( &e );
00701         }
00702     }
00703     else
00704     {
00705         QMouseEvent e( QEvent::MouseMove, p, Qt::NoButton, Qt::LeftButton );
00706         mouseMoveEvent( &e );
00707     }
00708 }
00709 
00710 
00711 void KoGuides::addGuide( const QPoint &pos, bool /* horizontal */, int rulerWidth )
00712 {
00713     int x = pos.x() - rulerWidth;
00714     int y = pos.y() - rulerWidth;
00715     QPoint p( x, y );
00716     m_insertGuide = false;
00717     QMouseEvent e( QEvent::MouseButtonRelease, p, Qt::LeftButton, Qt::LeftButton );
00718     mouseReleaseEvent( &e );
00719 }
00720 
00721 
00722 void KoGuides::slotChangePosition()
00723 {
00724     KoPoint p( mapFromScreen( m_lastPoint ) );
00725     KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
00726 
00727     const KoPageLayout& pl = m_view->koDocument()->pageLayout();
00728     double max = 0.0;
00729     if ( guideLine->orientation == Qt::Vertical )
00730     {
00731         max = QMAX( pl.ptWidth, m_zoomHandler->unzoomItX( m_view->canvas()->size().width() + m_view->canvasXOffset() - 1 ) );
00732     }
00733     else
00734     {
00735         max = QMAX( pl.ptHeight, m_zoomHandler->unzoomItY( m_view->canvas()->size().height() + m_view->canvasYOffset() - 1 ) );
00736     }
00737 
00738     KoGuideLineDia dia( 0, guideLine->position, 0.0, max, m_view->koDocument()->unit() );
00739     if ( dia.exec() == QDialog::Accepted )
00740     {
00741         guideLine->position = dia.pos();
00742         paint();
00743         emit guideLinesChanged( m_view );
00744     }
00745 }
00746 
00747 
00748 void KoGuides::slotRemove()
00749 {
00750     removeSelected();
00751     paint();
00752 }
00753 
00754 
00755 void KoGuides::paint()
00756 {
00757     m_view->canvas()->repaint( false );
00758 }
00759 
00760 
00761 void KoGuides::add( Qt::Orientation o, QPoint &pos )
00762 {
00763     KoPoint p( mapFromScreen( pos ) );
00764     KoGuideLine *guideLine = new KoGuideLine( o, o == Qt::Vertical ? p.x(): p.y() );
00765     m_guideLines[GL].append( guideLine );
00766 }
00767 
00768 
00769 void KoGuides::select( KoGuideLine *guideLine )
00770 {
00771     guideLine->selected = true;
00772     if ( m_guideLines[GL].remove( guideLine ) == 1 )
00773     {
00774         m_guideLines[GL_SELECTED].append( guideLine );
00775     }
00776 }
00777 
00778 
00779 void KoGuides::unselect( KoGuideLine *guideLine )
00780 {
00781     guideLine->selected = false;
00782     if ( m_guideLines[GL_SELECTED].remove( guideLine ) == 1 )
00783     {
00784         m_guideLines[GL].append( guideLine );
00785     }
00786 }
00787 
00788 
00789 bool KoGuides::unselectAll()
00790 {
00791     bool selected = m_guideLines[GL_SELECTED].empty() == false;
00792     
00793     QValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
00794     for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
00795     {
00796         ( *it )->selected = false;
00797         m_guideLines[GL].append( *it );
00798     }
00799     m_guideLines[GL_SELECTED].clear();
00800     
00801     return selected;
00802 }
00803 
00804 
00805 void KoGuides::removeSelected()
00806 {
00807     QValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
00808     for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
00809     {
00810         delete ( *it );
00811     }
00812     m_guideLines[GL_SELECTED].clear();
00813 }
00814 
00815 
00816 bool KoGuides::hasSelected()
00817 {
00818     return m_guideLines[GL_SELECTED].empty() == false;
00819 }
00820 
00821 
00822 KoGuides::KoGuideLine * KoGuides::find( KoPoint &p, double diff )
00823 {
00824     QValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
00825     for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
00826     {
00827         if ( ( *it )->orientation == Qt::Vertical && QABS( ( *it )->position - p.x() ) < diff )
00828         {
00829             return *it;
00830         }
00831         if ( ( *it )->orientation == Qt::Horizontal && QABS( ( *it )->position - p.y() ) < diff )
00832         {
00833             return *it;
00834         }
00835     }
00836 
00837     it = m_guideLines[GL].begin();
00838     for ( ; it != m_guideLines[GL].end(); ++it )
00839     {
00840         if ( ( *it )->orientation == Qt::Vertical && QABS( ( *it )->position - p.x() ) < diff )
00841         {
00842             return *it;
00843         }
00844         if ( ( *it )->orientation == Qt::Horizontal && QABS( ( *it )->position - p.y() ) < diff )
00845         {
00846             return *it;
00847         }
00848     }
00849     return 0;
00850 }
00851 
00852 
00853 void KoGuides::moveSelectedBy( QPoint &p )
00854 {
00855     KoPoint point( m_zoomHandler->unzoomPoint( p ) );
00856     if ( m_guideLines[GL_SELECTED].count() > 1 )
00857     {
00858         const KoPageLayout& pl = m_view->koDocument()->pageLayout();
00859         double right = QMAX( pl.ptWidth, m_zoomHandler->unzoomItX( m_view->canvas()->width() + m_view->canvasXOffset() - 1 ) );
00860         double bottom = QMAX( pl.ptHeight, m_zoomHandler->unzoomItY( m_view->canvas()->height() + m_view->canvasYOffset() - 1 ) );
00861 
00862         QValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
00863         for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
00864         {
00865             if ( ( *it )->orientation == Qt::Vertical )
00866             {
00867                 double tmp = ( *it )->position + point.x();
00868                 if ( tmp < 0 )
00869                 {
00870                     point.setX( point.x() - tmp );
00871                 }
00872                 else if ( tmp > right )
00873                 {
00874                     point.setX( point.x() - ( tmp - right ) );
00875                 }
00876             }
00877             else
00878             {
00879                 double tmp = ( *it )->position + point.y();
00880                 if ( tmp < 0 )
00881                 {
00882                     point.setY( point.y() - tmp );
00883                 }
00884                 else if ( tmp > bottom )
00885                 {
00886                     point.setY( point.y() - ( tmp - bottom ) );
00887                 }
00888             }
00889         }
00890     }
00891     QValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
00892     for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
00893     {
00894         ( *it )->snapping = false;
00895 
00896         if ( ( *it )->orientation == Qt::Vertical && p.x() != 0 )
00897         {
00898             ( *it )->position = ( *it )->position + point.x();
00899         }
00900         else if ( ( *it )->orientation == Qt::Horizontal && p.y() != 0 )
00901         {
00902             ( *it )->position = ( *it )->position + point.y();
00903         }
00904     }
00905 }
00906 
00907 
00908 KoPoint KoGuides::mapFromScreen( const QPoint & pos )
00909 {
00910     int x = pos.x() + m_view->canvasXOffset();
00911     int y = pos.y() + m_view->canvasYOffset();
00912     double xf = m_zoomHandler->unzoomItX( x );
00913     double yf = m_zoomHandler->unzoomItY( y );
00914     return KoPoint( xf, yf );
00915 }
00916 
00917 
00918 QPoint KoGuides::mapToScreen( const KoPoint & pos )
00919 {
00920     int x = m_zoomHandler->zoomItX( pos.x() ) - m_view->canvasXOffset();
00921     int y = m_zoomHandler->zoomItY( pos.y() ) - m_view->canvasYOffset();
00922     return QPoint( x, y );
00923 }
00924 
00925 
00926 #include "KoGuides.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys