F:/KPlato/koffice/libs/kofficeui/KoGuides.cpp

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

Généré le Wed Nov 22 23:41:04 2006 pour KPlato par  doxygen 1.5.1-p1