28#include <tdepopupmenu.h>
30#include <KoDocument.h>
34#include <KoZoomHandler.h>
36#include "KoGuideLineDia.h"
41 Popup( KoGuides * guides )
44 m_delete = insertItem( i18n(
"&Delete" ), guides, TQ_SLOT( slotRemove() ) );
45 m_seperator = insertSeparator();
46 m_pos = insertItem( i18n(
"&Set Position..." ), guides, TQ_SLOT( slotChangePosition() ) );
49 void update(
int count )
54 setItemVisible( m_seperator,
true );
55 setItemVisible( m_pos,
true );
60 setItemVisible( m_seperator,
false );
61 setItemVisible( m_pos,
false );
71const KoGuides::SnapStatus KoGuides::SNAP_NONE = 0;
72const KoGuides::SnapStatus KoGuides::SNAP_HORIZ = 1;
73const KoGuides::SnapStatus KoGuides::SNAP_VERT = 2;
74const KoGuides::SnapStatus KoGuides::SNAP_BOTH = 3;
78, m_zoomHandler( zoomHandler )
80 m_popup =
new Popup(
this );
81 m_mouseSelected =
false;
91void KoGuides::paintGuides( TQPainter &painter )
94 const KoPageLayout& pl = m_view->koDocument()->pageLayout();
95 int width = TQMAX( m_view->canvas()->width(), m_zoomHandler->zoomItX( pl.
ptWidth ) );
96 int height = TQMAX( m_view->canvas()->height(), m_zoomHandler->zoomItY( pl.
ptHeight ) );
98 for (
int i = 0; i < GL_END; ++i )
100 TQValueList<KoGuideLine *>::iterator it = m_guideLines[i].begin();
101 for ( ; it != m_guideLines[i].end(); ++it )
103 if ( !( *it )->automatic || ( *it )->snapping )
105 if ( ( *it )->snapping )
106 painter.setPen( TQPen( green, 0, DotLine ) );
107 else if ( ( *it )->selected )
108 painter.setPen( TQPen( red, 0, DotLine ) );
110 painter.setPen( TQPen( blue, 0, DotLine ) );
113 if ( ( *it )->orientation == TQt::Vertical )
115 painter.translate( m_zoomHandler->zoomItX( ( *it )->position ), 0 );
116 painter.drawLine( 0, 0, 0, height );
120 painter.translate( 0, m_zoomHandler->zoomItY( ( *it )->position ) );
121 painter.drawLine( 0, 0, width, 0 );
129bool KoGuides::mousePressEvent( TQMouseEvent *e )
131 bool eventProcessed =
true;
132 bool changed =
false;
133 m_mouseSelected =
false;
135 KoPoint p( mapFromScreen( e->pos() ) );
136 KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
139 m_lastPoint = e->pos();
140 if ( e->button() == TQt::LeftButton || e->button() == TQt::RightButton )
142 if ( e->button() == TQt::LeftButton )
144 m_mouseSelected =
true;
146 if ( e->state() & TQt::ControlButton )
148 if ( guideLine->selected )
150 unselect( guideLine );
151 m_mouseSelected =
false;
159 else if ( ! guideLine->selected )
169 if ( !( e->state() & TQt::ControlButton ) )
171 changed = unselectAll();
173 eventProcessed =
false;
176 if ( changed || hasSelected() )
178 emit moveGuides(
true );
186 if ( changed && ! hasSelected() )
188 emit moveGuides(
false );
191 if ( e->button() == TQt::RightButton && hasSelected() )
193 m_popup->update( m_guideLines[GL_SELECTED].count() );
194 m_popup->exec( TQCursor::pos() );
195 emit moveGuides(
false );
198 return eventProcessed;
202bool KoGuides::mouseMoveEvent( TQMouseEvent *e )
204 bool eventProcessed =
false;
205 if ( m_mouseSelected )
207 TQPoint p( e->pos() );
209 m_lastPoint = e->pos();
212 emit guideLinesChanged( m_view );
213 eventProcessed =
true;
215 else if ( e->state() == TQt::NoButton )
217 KoPoint p( mapFromScreen( e->pos() ) );
218 KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
221 m_view->canvas()->setCursor( guideLine->orientation == TQt::Vertical ? TQt::sizeHorCursor : TQt::sizeVerCursor );
222 eventProcessed =
true;
225 return eventProcessed;
229bool KoGuides::mouseReleaseEvent( TQMouseEvent *e )
231 bool eventProcessed =
false;
232 if ( m_mouseSelected )
234 KoPoint p( mapFromScreen( e->pos() ) );
235 if ( m_guideLines[GL_SELECTED].count() == 1 )
238 m_view->canvas()->rect().coords( &x1, &y1, &x2, &y2 );
239 TQPoint gp( m_view->canvas()->mapFromGlobal( e->globalPos() ) );
240 if ( m_guideLines[GL_SELECTED].first()->orientation == TQt::Vertical )
242 if ( gp.x() < x1 || gp.x() > x2 )
247 if ( gp.y() < y1 || gp.y() > y2 )
251 KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
254 m_view->canvas()->setCursor( guideLine->orientation == TQt::Vertical ? TQt::sizeHorCursor : TQt::sizeVerCursor );
256 m_mouseSelected =
false;
257 eventProcessed =
true;
258 emit guideLinesChanged( m_view );
260 emit moveGuides(
false );
261 return eventProcessed;
265bool KoGuides::keyPressEvent( TQKeyEvent *e )
267 bool eventProcessed =
false;
270 case TQt::Key_Delete:
275 emit guideLinesChanged( m_view );
276 eventProcessed =
true;
282 return eventProcessed;
285void KoGuides::setGuideLines(
const TQValueList<double> &horizontalPos,
const TQValueList<double> &verticalPos )
289 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL].begin();
290 for ( ; it != m_guideLines[GL].end(); ++it )
294 m_guideLines[GL].clear();
296 TQValueList<double>::ConstIterator posIt = horizontalPos.begin();
297 for ( ; posIt != horizontalPos.end(); ++posIt )
299 KoGuideLine *guideLine =
new KoGuideLine( TQt::Horizontal, *posIt,
false );
300 m_guideLines[GL].append( guideLine );
302 posIt = verticalPos.begin();
303 for ( ; posIt != verticalPos.end(); ++posIt )
305 KoGuideLine *guideLine =
new KoGuideLine( TQt::Vertical, *posIt,
false );
306 m_guideLines[GL].append( guideLine );
311void KoGuides::setAutoGuideLines(
const TQValueList<double> &horizontalPos,
const TQValueList<double> &verticalPos )
313 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_AUTOMATIC].begin();
314 for ( ; it != m_guideLines[GL_AUTOMATIC].end(); ++it )
318 m_guideLines[GL_AUTOMATIC].clear();
320 TQValueList<double>::ConstIterator posIt = horizontalPos.begin();
321 for ( ; posIt != horizontalPos.end(); ++posIt )
323 KoGuideLine *guideLine =
new KoGuideLine( TQt::Horizontal, *posIt,
true );
324 m_guideLines[GL_AUTOMATIC].append( guideLine );
326 posIt = verticalPos.begin();
327 for ( ; posIt != verticalPos.end(); ++posIt )
329 KoGuideLine *guideLine =
new KoGuideLine( TQt::Vertical, *posIt,
true );
330 m_guideLines[GL_AUTOMATIC].append( guideLine );
335void KoGuides::getGuideLines( TQValueList<double> &horizontalPos, TQValueList<double> &verticalPos )
const
337 horizontalPos.clear();
340 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[GL].begin();
341 for ( ; it != m_guideLines[GL].end(); ++it )
343 if ( ( *it )->orientation == TQt::Horizontal )
345 horizontalPos.append( ( *it )->position );
349 verticalPos.append( ( *it )->position );
352 it = m_guideLines[GL_SELECTED].begin();
353 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
355 if ( ( *it )->orientation == TQt::Horizontal )
357 horizontalPos.append( ( *it )->position );
361 verticalPos.append( ( *it )->position );
367void KoGuides::snapToGuideLines(
KoRect &rect,
int snap, SnapStatus &snapStatus,
KoPoint &diff )
369 if( !(snapStatus & SNAP_VERT))
371 if( !(snapStatus & SNAP_HORIZ))
374 for (
int i = 0; i < GL_END; ++i )
376 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
377 for ( ; it != m_guideLines[i].end(); ++it )
379 if ( ( *it )->orientation == TQt::Horizontal )
381 double tmp = (*it)->position - rect.top();
382 if ( snapStatus & SNAP_HORIZ || TQABS( tmp ) < m_zoomHandler->unzoomItY( snap ) )
384 if(TQABS( tmp ) < TQABS(diff.y()))
387 snapStatus |= SNAP_HORIZ;
390 tmp = (*it)->position - rect.bottom();
391 if ( snapStatus & SNAP_HORIZ || TQABS( tmp ) < m_zoomHandler->unzoomItY( snap ) )
393 if(TQABS( tmp ) < TQABS(diff.y()))
396 snapStatus |= SNAP_HORIZ;
402 double tmp = (*it)->position - rect.left();
403 if ( snapStatus & SNAP_VERT || TQABS( tmp ) < m_zoomHandler->unzoomItX( snap ) )
405 if(TQABS( tmp ) < TQABS(diff.x()))
408 snapStatus |= SNAP_VERT;
411 tmp = (*it)->position - rect.right();
412 if ( snapStatus & SNAP_VERT || TQABS( tmp ) < m_zoomHandler->unzoomItX( snap ) )
414 if(TQABS( tmp ) < TQABS(diff.x()))
417 snapStatus |= SNAP_VERT;
424 if(!(snapStatus & SNAP_VERT))
427 if(!(snapStatus & SNAP_HORIZ))
431void KoGuides::snapToGuideLines(
KoPoint &pos,
int snap, SnapStatus &snapStatus,
KoPoint &diff )
433 if( !(snapStatus & SNAP_VERT))
435 if( !(snapStatus & SNAP_HORIZ))
438 for (
int i = 0; i < GL_END; ++i )
440 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
441 for ( ; it != m_guideLines[i].end(); ++it )
443 if ( ( *it )->orientation == TQt::Horizontal )
445 double tmp = (*it)->position - pos.y();
446 if ( snapStatus & SNAP_HORIZ || TQABS( tmp ) < m_zoomHandler->unzoomItY( snap ) )
448 if(TQABS( tmp ) < TQABS(diff.y()))
451 snapStatus |= SNAP_HORIZ;
457 double tmp = (*it)->position - pos.x();
458 if ( snapStatus & SNAP_VERT || TQABS( tmp ) < m_zoomHandler->unzoomItX( snap ) )
460 if(TQABS( tmp ) < TQABS(diff.x()))
463 snapStatus |= SNAP_VERT;
470 if(!(snapStatus & SNAP_VERT))
473 if(!(snapStatus & SNAP_HORIZ))
478void KoGuides::repaintSnapping(
const KoRect &snappedRect )
480 bool needRepaint =
false;
481 for (
int i = 0; i < GL_END; ++i )
483 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
484 for ( ; it != m_guideLines[i].end(); ++it )
486 if ( ( *it )->orientation == TQt::Horizontal )
488 if ( virtuallyEqual( snappedRect.top(), (*it)->position )
489 || virtuallyEqual( snappedRect.bottom(), ( *it )->position ) )
491 if ( ! ( *it )->snapping )
493 ( *it )->snapping =
true;
497 else if ( ( *it )->snapping )
499 ( *it )->snapping =
false;
505 if ( virtuallyEqual( snappedRect.left(), (*it)->position )
506 || virtuallyEqual( snappedRect.right(), ( *it )->position ) )
508 if ( ! ( *it )->snapping )
510 ( *it )->snapping =
true;
514 else if ( ( *it )->snapping )
516 ( *it )->snapping =
false;
525 emit paintGuides(
true );
527 emit paintGuides(
false );
532void KoGuides::repaintSnapping(
const KoPoint &snappedPoint, SnapStatus snapStatus )
534 bool needRepaint =
false;
535 for (
int i = 0; i < GL_END; ++i )
537 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
538 for ( ; it != m_guideLines[i].end(); ++it )
540 if ( ( *it )->orientation == TQt::Horizontal && ( snapStatus & SNAP_HORIZ ) )
542 if( virtuallyEqual( snappedPoint.y(), (*it)->position ) )
544 if ( ! ( *it )->snapping )
546 ( *it )->snapping =
true;
550 else if ( ( *it )->snapping )
552 ( *it )->snapping =
false;
558 if ( snapStatus & SNAP_VERT )
560 if( virtuallyEqual( snappedPoint.x(), (*it)->position ) )
562 if ( ! ( *it )->snapping )
564 ( *it )->snapping =
true;
568 else if ( ( *it )->snapping )
570 ( *it )->snapping =
false;
580 emit paintGuides(
true );
582 emit paintGuides(
false );
587void KoGuides::repaintAfterSnapping()
589 bool needRepaint =
false;
591 for (
int i = 0; i < GL_END; ++i )
593 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
594 for ( ; it != m_guideLines[i].end(); ++it )
596 if ( ( *it )->snapping )
599 ( *it )->snapping =
false;
606 emit paintGuides(
true );
608 emit paintGuides(
false );
615 for (
int i = 0; i < GL_END; ++i )
617 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
618 for ( ; it != m_guideLines[i].end(); ++it )
620 if ( ( *it )->orientation == TQt::Horizontal )
622 double moveyl = ( *it )->position - rect.top();
623 double moveyr = ( *it )->position - rect.bottom();
626 if ( moveyl < diff.y() && moveyl > 1E-10 )
630 if ( moveyr < diff.y() && moveyr > 1E-10 )
635 else if ( diff.y() < 0 )
637 if ( moveyl > diff.y() && moveyl < -1E-10 )
641 if ( moveyr > diff.y() && moveyr < -1E-10 )
649 double movexl = ( *it )->position - rect.left();
650 double movexr = ( *it )->position - rect.right();
653 if ( movexl < diff.x() && movexl > 1E-10 )
657 if ( ( movexr < diff.x() ) && movexr > 1E-10 )
662 else if ( diff.x() < 0 )
664 if ( movexl > diff.x() && movexl < -1E-10 )
668 if ( movexr > diff.x() && movexr < -1E-10 )
679void KoGuides::moveGuide(
const TQPoint &pos,
bool horizontal,
int rulerWidth )
681 int x = pos.x() - rulerWidth;
682 int y = pos.y() - rulerWidth;
684 if ( !m_insertGuide )
686 if ( ! horizontal && x > 0 )
688 m_insertGuide =
true;
689 add( TQt::Vertical, p );
691 else if ( horizontal && y > 0 )
693 m_insertGuide =
true;
694 add( TQt::Horizontal, p );
698 TQMouseEvent e( TQEvent::MouseButtonPress, p, TQt::LeftButton, TQt::LeftButton );
699 mousePressEvent( &e );
704 TQMouseEvent e( TQEvent::MouseMove, p, TQt::NoButton, TQt::LeftButton );
705 mouseMoveEvent( &e );
710void KoGuides::addGuide(
const TQPoint &pos,
bool ,
int rulerWidth )
712 int x = pos.x() - rulerWidth;
713 int y = pos.y() - rulerWidth;
715 m_insertGuide =
false;
716 TQMouseEvent e( TQEvent::MouseButtonRelease, p, TQt::LeftButton, TQt::LeftButton );
717 mouseReleaseEvent( &e );
721void KoGuides::slotChangePosition()
723 KoPoint p( mapFromScreen( m_lastPoint ) );
724 KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
726 const KoPageLayout& pl = m_view->koDocument()->pageLayout();
728 if ( guideLine->orientation == TQt::Vertical )
730 max = TQMAX( pl.
ptWidth, m_zoomHandler->unzoomItX( m_view->canvas()->size().width() + m_view->canvasXOffset() - 1 ) );
734 max = TQMAX( pl.
ptHeight, m_zoomHandler->unzoomItY( m_view->canvas()->size().height() + m_view->canvasYOffset() - 1 ) );
737 KoGuideLineDia dia( 0, guideLine->position, 0.0, max, m_view->koDocument()->unit() );
738 if ( dia.exec() == TQDialog::Accepted )
740 guideLine->position = dia.pos();
742 emit guideLinesChanged( m_view );
747void KoGuides::slotRemove()
754void KoGuides::paint()
756 m_view->canvas()->repaint(
false );
760void KoGuides::add( TQt::Orientation o, TQPoint &pos )
762 KoPoint p( mapFromScreen( pos ) );
763 KoGuideLine *guideLine =
new KoGuideLine( o, o == TQt::Vertical ? p.x(): p.y() );
764 m_guideLines[GL].append( guideLine );
768void KoGuides::select( KoGuideLine *guideLine )
770 guideLine->selected =
true;
771 if ( m_guideLines[GL].remove( guideLine ) == 1 )
773 m_guideLines[GL_SELECTED].append( guideLine );
778void KoGuides::unselect( KoGuideLine *guideLine )
780 guideLine->selected =
false;
781 if ( m_guideLines[GL_SELECTED].remove( guideLine ) == 1 )
783 m_guideLines[GL].append( guideLine );
788bool KoGuides::unselectAll()
790 bool selected = m_guideLines[GL_SELECTED].empty() ==
false;
792 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
793 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
795 ( *it )->selected =
false;
796 m_guideLines[GL].append( *it );
798 m_guideLines[GL_SELECTED].clear();
804void KoGuides::removeSelected()
806 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
807 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
811 m_guideLines[GL_SELECTED].clear();
815bool KoGuides::hasSelected()
817 return m_guideLines[GL_SELECTED].empty() ==
false;
821KoGuides::KoGuideLine * KoGuides::find(
KoPoint &p,
double diff )
823 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
824 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
826 if ( ( *it )->orientation == TQt::Vertical && TQABS( ( *it )->position - p.x() ) < diff )
830 if ( ( *it )->orientation == TQt::Horizontal && TQABS( ( *it )->position - p.y() ) < diff )
836 it = m_guideLines[GL].begin();
837 for ( ; it != m_guideLines[GL].end(); ++it )
839 if ( ( *it )->orientation == TQt::Vertical && TQABS( ( *it )->position - p.x() ) < diff )
843 if ( ( *it )->orientation == TQt::Horizontal && TQABS( ( *it )->position - p.y() ) < diff )
852void KoGuides::moveSelectedBy( TQPoint &p )
854 KoPoint point( m_zoomHandler->unzoomPoint( p ) );
855 if ( m_guideLines[GL_SELECTED].count() > 1 )
857 const KoPageLayout& pl = m_view->koDocument()->pageLayout();
858 double right = TQMAX( pl.
ptWidth, m_zoomHandler->unzoomItX( m_view->canvas()->width() + m_view->canvasXOffset() - 1 ) );
859 double bottom = TQMAX( pl.
ptHeight, m_zoomHandler->unzoomItY( m_view->canvas()->height() + m_view->canvasYOffset() - 1 ) );
861 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
862 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
864 if ( ( *it )->orientation == TQt::Vertical )
866 double tmp = ( *it )->position + point.x();
869 point.setX( point.x() - tmp );
871 else if ( tmp > right )
873 point.setX( point.x() - ( tmp - right ) );
878 double tmp = ( *it )->position + point.y();
881 point.setY( point.y() - tmp );
883 else if ( tmp > bottom )
885 point.setY( point.y() - ( tmp - bottom ) );
890 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
891 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
893 ( *it )->snapping =
false;
895 if ( ( *it )->orientation == TQt::Vertical && p.x() != 0 )
897 ( *it )->position = ( *it )->position + point.x();
899 else if ( ( *it )->orientation == TQt::Horizontal && p.y() != 0 )
901 ( *it )->position = ( *it )->position + point.y();
907KoPoint KoGuides::mapFromScreen(
const TQPoint & pos )
909 int x = pos.x() + m_view->canvasXOffset();
910 int y = pos.y() + m_view->canvasYOffset();
911 double xf = m_zoomHandler->unzoomItX( x );
912 double yf = m_zoomHandler->unzoomItY( y );
913 return KoPoint( xf, yf );
917TQPoint KoGuides::mapToScreen(
const KoPoint & pos )
919 int x = m_zoomHandler->zoomItX( pos.x() ) - m_view->canvasXOffset();
920 int y = m_zoomHandler->zoomItY( pos.y() ) - m_view->canvasYOffset();
921 return TQPoint( x, y );
925#include "KoGuides.moc"
A point whose coordinates are floating-point values ( "double"s ).
A rect whose coordinates are floating-point values ( "double"s ).
This class is used to display a KoDocument.
This class handles the zooming and DPI stuff (conversions between pt values and pixels).
double ptHeight
Page height in pt.
double ptWidth
Page width in pt.