lib

KoGuides.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
3 Copyright (C) 2005 Casper Boemann Rasmussen <cbr@boemann.dk>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "KoGuides.h"
22
23#include <tqcursor.h>
24#include <tqpainter.h>
25#include <tqpixmap.h>
26
27#include <tdelocale.h>
28#include <tdepopupmenu.h>
29
30#include <KoDocument.h>
31#include <KoPoint.h>
32#include <KoRect.h>
33#include <KoView.h>
34#include <KoZoomHandler.h>
35
36#include "KoGuideLineDia.h"
37
38class KoGuides::Popup : public TDEPopupMenu
39{
40public:
41 Popup( KoGuides * guides )
42 {
43 m_title = insertTitle( i18n( "Guide Line" ) );
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() ) );
47 }
48
49 void update( int count )
50 {
51 if ( count == 1 )
52 {
53 changeTitle( m_title, i18n( "Guide Line" ) );
54 setItemVisible( m_seperator, true );
55 setItemVisible( m_pos, true );
56 }
57 else
58 {
59 changeTitle( m_title, i18n( "Guide Lines" ) );
60 setItemVisible( m_seperator, false );
61 setItemVisible( m_pos, false );
62 }
63 }
64private:
65 int m_title;
66 int m_delete;
67 int m_seperator;
68 int m_pos;
69};
70
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;
75
76KoGuides::KoGuides( KoView *view, KoZoomHandler *zoomHandler )
77: m_view( view )
78, m_zoomHandler( zoomHandler )
79{
80 m_popup = new Popup( this );
81 m_mouseSelected = false;
82}
83
84
85KoGuides::~KoGuides()
86{
87 delete m_popup;
88}
89
90
91void KoGuides::paintGuides( TQPainter &painter )
92{
93 //painter.setRasterOp( NotROP );
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 ) );
97
98 for ( int i = 0; i < GL_END; ++i )
99 {
100 TQValueList<KoGuideLine *>::iterator it = m_guideLines[i].begin();
101 for ( ; it != m_guideLines[i].end(); ++it )
102 {
103 if ( !( *it )->automatic || ( *it )->snapping ) // dont paint autoStyle guides when they are not snapping
104 {
105 if ( ( *it )->snapping )
106 painter.setPen( TQPen( green, 0, DotLine ) );
107 else if ( ( *it )->selected )
108 painter.setPen( TQPen( red, 0, DotLine ) );
109 else
110 painter.setPen( TQPen( blue, 0, DotLine ) );
111
112 painter.save();
113 if ( ( *it )->orientation == TQt::Vertical )
114 {
115 painter.translate( m_zoomHandler->zoomItX( ( *it )->position ), 0 );
116 painter.drawLine( 0, 0, 0, height );
117 }
118 else
119 {
120 painter.translate( 0, m_zoomHandler->zoomItY( ( *it )->position ) );
121 painter.drawLine( 0, 0, width, 0 );
122 }
123 painter.restore();
124 }
125 }
126 }
127}
128
129bool KoGuides::mousePressEvent( TQMouseEvent *e )
130{
131 bool eventProcessed = true;
132 bool changed = false;
133 m_mouseSelected = false;
134
135 KoPoint p( mapFromScreen( e->pos() ) );
136 KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
137 if ( guideLine )
138 {
139 m_lastPoint = e->pos();
140 if ( e->button() == TQt::LeftButton || e->button() == TQt::RightButton )
141 {
142 if ( e->button() == TQt::LeftButton )
143 {
144 m_mouseSelected = true;
145 }
146 if ( e->state() & TQt::ControlButton )
147 {
148 if ( guideLine->selected )
149 {
150 unselect( guideLine );
151 m_mouseSelected = false;
152 }
153 else
154 {
155 select( guideLine );
156 }
157 changed = true;
158 }
159 else if ( ! guideLine->selected )
160 {
161 unselectAll();
162 select( guideLine );
163 changed = true;
164 }
165 }
166 }
167 else
168 {
169 if ( !( e->state() & TQt::ControlButton ) )
170 {
171 changed = unselectAll();
172 }
173 eventProcessed = false;
174 }
175
176 if ( changed || hasSelected() )
177 {
178 emit moveGuides( true );
179 }
180
181 if ( changed )
182 {
183 paint();
184 }
185
186 if ( changed && ! hasSelected() )
187 {
188 emit moveGuides( false );
189 }
190
191 if ( e->button() == TQt::RightButton && hasSelected() )
192 {
193 m_popup->update( m_guideLines[GL_SELECTED].count() );
194 m_popup->exec( TQCursor::pos() );
195 emit moveGuides( false );
196 }
197
198 return eventProcessed;
199}
200
201
202bool KoGuides::mouseMoveEvent( TQMouseEvent *e )
203{
204 bool eventProcessed = false;
205 if ( m_mouseSelected )
206 {
207 TQPoint p( e->pos() );
208 p -= m_lastPoint;
209 m_lastPoint = e->pos();
210 moveSelectedBy( p );
211 paint();
212 emit guideLinesChanged( m_view );
213 eventProcessed = true;
214 }
215 else if ( e->state() == TQt::NoButton )
216 {
217 KoPoint p( mapFromScreen( e->pos() ) );
218 KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
219 if ( guideLine )
220 {
221 m_view->canvas()->setCursor( guideLine->orientation == TQt::Vertical ? TQt::sizeHorCursor : TQt::sizeVerCursor );
222 eventProcessed = true;
223 }
224 }
225 return eventProcessed;
226}
227
228
229bool KoGuides::mouseReleaseEvent( TQMouseEvent *e )
230{
231 bool eventProcessed = false;
232 if ( m_mouseSelected )
233 {
234 KoPoint p( mapFromScreen( e->pos() ) );
235 if ( m_guideLines[GL_SELECTED].count() == 1 )
236 {
237 int x1, y1, x2, y2;
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 )
241 {
242 if ( gp.x() < x1 || gp.x() > x2 )
243 removeSelected();
244 }
245 else
246 {
247 if ( gp.y() < y1 || gp.y() > y2 )
248 removeSelected();
249 }
250 }
251 KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
252 if ( guideLine )
253 {
254 m_view->canvas()->setCursor( guideLine->orientation == TQt::Vertical ? TQt::sizeHorCursor : TQt::sizeVerCursor );
255 }
256 m_mouseSelected = false;
257 eventProcessed = true;
258 emit guideLinesChanged( m_view );
259 }
260 emit moveGuides( false );
261 return eventProcessed;
262}
263
264
265bool KoGuides::keyPressEvent( TQKeyEvent *e )
266{
267 bool eventProcessed = false;
268 switch( e->key() )
269 {
270 case TQt::Key_Delete:
271 if ( hasSelected() )
272 {
273 removeSelected();
274 paint();
275 emit guideLinesChanged( m_view );
276 eventProcessed = true;
277 }
278 break;
279 default:
280 break;
281 }
282 return eventProcessed;
283}
284
285void KoGuides::setGuideLines( const TQValueList<double> &horizontalPos, const TQValueList<double> &verticalPos )
286{
287 removeSelected();
288
289 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL].begin();
290 for ( ; it != m_guideLines[GL].end(); ++it )
291 {
292 delete ( *it );
293 }
294 m_guideLines[GL].clear();
295
296 TQValueList<double>::ConstIterator posIt = horizontalPos.begin();
297 for ( ; posIt != horizontalPos.end(); ++posIt )
298 {
299 KoGuideLine *guideLine = new KoGuideLine( TQt::Horizontal, *posIt, false );
300 m_guideLines[GL].append( guideLine );
301 }
302 posIt = verticalPos.begin();
303 for ( ; posIt != verticalPos.end(); ++posIt )
304 {
305 KoGuideLine *guideLine = new KoGuideLine( TQt::Vertical, *posIt, false );
306 m_guideLines[GL].append( guideLine );
307 }
308 paint();
309}
310
311void KoGuides::setAutoGuideLines( const TQValueList<double> &horizontalPos, const TQValueList<double> &verticalPos )
312{
313 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_AUTOMATIC].begin();
314 for ( ; it != m_guideLines[GL_AUTOMATIC].end(); ++it )
315 {
316 delete ( *it );
317 }
318 m_guideLines[GL_AUTOMATIC].clear();
319
320 TQValueList<double>::ConstIterator posIt = horizontalPos.begin();
321 for ( ; posIt != horizontalPos.end(); ++posIt )
322 {
323 KoGuideLine *guideLine = new KoGuideLine( TQt::Horizontal, *posIt, true );
324 m_guideLines[GL_AUTOMATIC].append( guideLine );
325 }
326 posIt = verticalPos.begin();
327 for ( ; posIt != verticalPos.end(); ++posIt )
328 {
329 KoGuideLine *guideLine = new KoGuideLine( TQt::Vertical, *posIt, true );
330 m_guideLines[GL_AUTOMATIC].append( guideLine );
331 }
332}
333
334
335void KoGuides::getGuideLines( TQValueList<double> &horizontalPos, TQValueList<double> &verticalPos ) const
336{
337 horizontalPos.clear();
338 verticalPos.clear();
339
340 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[GL].begin();
341 for ( ; it != m_guideLines[GL].end(); ++it )
342 {
343 if ( ( *it )->orientation == TQt::Horizontal )
344 {
345 horizontalPos.append( ( *it )->position );
346 }
347 else
348 {
349 verticalPos.append( ( *it )->position );
350 }
351 }
352 it = m_guideLines[GL_SELECTED].begin();
353 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
354 {
355 if ( ( *it )->orientation == TQt::Horizontal )
356 {
357 horizontalPos.append( ( *it )->position );
358 }
359 else
360 {
361 verticalPos.append( ( *it )->position );
362 }
363 }
364}
365
366
367void KoGuides::snapToGuideLines( KoRect &rect, int snap, SnapStatus &snapStatus, KoPoint &diff )
368{
369 if( !(snapStatus & SNAP_VERT))
370 diff.setX(10000);
371 if( !(snapStatus & SNAP_HORIZ))
372 diff.setY(10000);
373
374 for ( int i = 0; i < GL_END; ++i )
375 {
376 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
377 for ( ; it != m_guideLines[i].end(); ++it )
378 {
379 if ( ( *it )->orientation == TQt::Horizontal )
380 {
381 double tmp = (*it)->position - rect.top();
382 if ( snapStatus & SNAP_HORIZ || TQABS( tmp ) < m_zoomHandler->unzoomItY( snap ) )
383 {
384 if(TQABS( tmp ) < TQABS(diff.y()))
385 {
386 diff.setY( tmp );
387 snapStatus |= SNAP_HORIZ;
388 }
389 }
390 tmp = (*it)->position - rect.bottom();
391 if ( snapStatus & SNAP_HORIZ || TQABS( tmp ) < m_zoomHandler->unzoomItY( snap ) )
392 {
393 if(TQABS( tmp ) < TQABS(diff.y()))
394 {
395 diff.setY( tmp );
396 snapStatus |= SNAP_HORIZ;
397 }
398 }
399 }
400 else
401 {
402 double tmp = (*it)->position - rect.left();
403 if ( snapStatus & SNAP_VERT || TQABS( tmp ) < m_zoomHandler->unzoomItX( snap ) )
404 {
405 if(TQABS( tmp ) < TQABS(diff.x()))
406 {
407 diff.setX( tmp );
408 snapStatus |= SNAP_VERT;
409 }
410 }
411 tmp = (*it)->position - rect.right();
412 if ( snapStatus & SNAP_VERT || TQABS( tmp ) < m_zoomHandler->unzoomItX( snap ) )
413 {
414 if(TQABS( tmp ) < TQABS(diff.x()))
415 {
416 diff.setX( tmp );
417 snapStatus |= SNAP_VERT;
418 }
419 }
420 }
421 }
422 }
423
424 if(!(snapStatus & SNAP_VERT))
425 diff.setX( 0 );
426
427 if(!(snapStatus & SNAP_HORIZ))
428 diff.setY( 0 );
429}
430
431void KoGuides::snapToGuideLines( KoPoint &pos, int snap, SnapStatus &snapStatus, KoPoint &diff )
432{
433 if( !(snapStatus & SNAP_VERT))
434 diff.setX(10000);
435 if( !(snapStatus & SNAP_HORIZ))
436 diff.setY(10000);
437
438 for ( int i = 0; i < GL_END; ++i )
439 {
440 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
441 for ( ; it != m_guideLines[i].end(); ++it )
442 {
443 if ( ( *it )->orientation == TQt::Horizontal )
444 {
445 double tmp = (*it)->position - pos.y();
446 if ( snapStatus & SNAP_HORIZ || TQABS( tmp ) < m_zoomHandler->unzoomItY( snap ) )
447 {
448 if(TQABS( tmp ) < TQABS(diff.y()))
449 {
450 diff.setY( tmp );
451 snapStatus |= SNAP_HORIZ;
452 }
453 }
454 }
455 else
456 {
457 double tmp = (*it)->position - pos.x();
458 if ( snapStatus & SNAP_VERT || TQABS( tmp ) < m_zoomHandler->unzoomItX( snap ) )
459 {
460 if(TQABS( tmp ) < TQABS(diff.x()))
461 {
462 diff.setX( tmp );
463 snapStatus |= SNAP_VERT;
464 }
465 }
466 }
467 }
468 }
469
470 if(!(snapStatus & SNAP_VERT))
471 diff.setX( 0 );
472
473 if(!(snapStatus & SNAP_HORIZ))
474 diff.setY( 0 );
475}
476
477
478void KoGuides::repaintSnapping( const KoRect &snappedRect )
479{
480 bool needRepaint = false;
481 for ( int i = 0; i < GL_END; ++i )
482 {
483 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
484 for ( ; it != m_guideLines[i].end(); ++it )
485 {
486 if ( ( *it )->orientation == TQt::Horizontal )
487 {
488 if ( virtuallyEqual( snappedRect.top(), (*it)->position )
489 || virtuallyEqual( snappedRect.bottom(), ( *it )->position ) )
490 {
491 if ( ! ( *it )->snapping )
492 {
493 ( *it )->snapping = true;
494 needRepaint = true;
495 }
496 }
497 else if ( ( *it )->snapping )
498 {
499 ( *it )->snapping = false;
500 needRepaint = true;
501 }
502 }
503 else
504 {
505 if ( virtuallyEqual( snappedRect.left(), (*it)->position )
506 || virtuallyEqual( snappedRect.right(), ( *it )->position ) )
507 {
508 if ( ! ( *it )->snapping )
509 {
510 ( *it )->snapping = true;
511 needRepaint = true;
512 }
513 }
514 else if ( ( *it )->snapping )
515 {
516 ( *it )->snapping = false;
517 needRepaint = true;
518 }
519 }
520 }
521 }
522
523 if ( needRepaint )
524 {
525 emit paintGuides( true );
526 paint();
527 emit paintGuides( false );
528 }
529}
530
531
532void KoGuides::repaintSnapping( const KoPoint &snappedPoint, SnapStatus snapStatus )
533{
534 bool needRepaint = false;
535 for ( int i = 0; i < GL_END; ++i )
536 {
537 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
538 for ( ; it != m_guideLines[i].end(); ++it )
539 {
540 if ( ( *it )->orientation == TQt::Horizontal && ( snapStatus & SNAP_HORIZ ) )
541 {
542 if( virtuallyEqual( snappedPoint.y(), (*it)->position ) )
543 {
544 if ( ! ( *it )->snapping )
545 {
546 ( *it )->snapping = true;
547 needRepaint = true;
548 }
549 }
550 else if ( ( *it )->snapping )
551 {
552 ( *it )->snapping = false;
553 needRepaint = true;
554 }
555 }
556 else
557 {
558 if ( snapStatus & SNAP_VERT )
559 {
560 if( virtuallyEqual( snappedPoint.x(), (*it)->position ) )
561 {
562 if ( ! ( *it )->snapping )
563 {
564 ( *it )->snapping = true;
565 needRepaint = true;
566 }
567 }
568 else if ( ( *it )->snapping )
569 {
570 ( *it )->snapping = false;
571 needRepaint = true;
572 }
573 }
574 }
575 }
576 }
577
578 if ( needRepaint )
579 {
580 emit paintGuides( true );
581 paint();
582 emit paintGuides( false );
583 }
584}
585
586
587void KoGuides::repaintAfterSnapping()
588{
589 bool needRepaint = false;
590
591 for ( int i = 0; i < GL_END; ++i )
592 {
593 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
594 for ( ; it != m_guideLines[i].end(); ++it )
595 {
596 if ( ( *it )->snapping )
597 {
598 needRepaint = true;
599 ( *it )->snapping = false;
600 }
601 }
602 }
603
604 if ( needRepaint )
605 {
606 emit paintGuides( true );
607 paint();
608 emit paintGuides( false );
609 }
610}
611
612
613void KoGuides::diffNextGuide( KoRect &rect, KoPoint &diff )
614{
615 for ( int i = 0; i < GL_END; ++i )
616 {
617 TQValueList<KoGuideLine *>::const_iterator it = m_guideLines[i].begin();
618 for ( ; it != m_guideLines[i].end(); ++it )
619 {
620 if ( ( *it )->orientation == TQt::Horizontal )
621 {
622 double moveyl = ( *it )->position - rect.top();
623 double moveyr = ( *it )->position - rect.bottom();
624 if ( diff.y() > 0 )
625 {
626 if ( moveyl < diff.y() && moveyl > 1E-10 )
627 {
628 diff.setY( moveyl );
629 }
630 if ( moveyr < diff.y() && moveyr > 1E-10 )
631 {
632 diff.setY( moveyr );
633 }
634 }
635 else if ( diff.y() < 0 )
636 {
637 if ( moveyl > diff.y() && moveyl < -1E-10 )
638 {
639 diff.setY( moveyl );
640 }
641 if ( moveyr > diff.y() && moveyr < -1E-10 )
642 {
643 diff.setY( moveyr );
644 }
645 }
646 }
647 else
648 {
649 double movexl = ( *it )->position - rect.left();
650 double movexr = ( *it )->position - rect.right();
651 if ( diff.x() > 0 )
652 {
653 if ( movexl < diff.x() && movexl > 1E-10 )
654 {
655 diff.setX( movexl );
656 }
657 if ( ( movexr < diff.x() ) && movexr > 1E-10 )
658 {
659 diff.setX( movexr );
660 }
661 }
662 else if ( diff.x() < 0 )
663 {
664 if ( movexl > diff.x() && movexl < -1E-10 )
665 {
666 diff.setX( movexl );
667 }
668 if ( movexr > diff.x() && movexr < -1E-10 )
669 {
670 diff.setX( movexr );
671 }
672 }
673 }
674 }
675 }
676}
677
678
679void KoGuides::moveGuide( const TQPoint &pos, bool horizontal, int rulerWidth )
680{
681 int x = pos.x() - rulerWidth;
682 int y = pos.y() - rulerWidth;
683 TQPoint p( x, y );
684 if ( !m_insertGuide )
685 {
686 if ( ! horizontal && x > 0 )
687 {
688 m_insertGuide = true;
689 add( TQt::Vertical, p );
690 }
691 else if ( horizontal && y > 0 )
692 {
693 m_insertGuide = true;
694 add( TQt::Horizontal, p );
695 }
696 if ( m_insertGuide )
697 {
698 TQMouseEvent e( TQEvent::MouseButtonPress, p, TQt::LeftButton, TQt::LeftButton );
699 mousePressEvent( &e );
700 }
701 }
702 else
703 {
704 TQMouseEvent e( TQEvent::MouseMove, p, TQt::NoButton, TQt::LeftButton );
705 mouseMoveEvent( &e );
706 }
707}
708
709
710void KoGuides::addGuide( const TQPoint &pos, bool /* horizontal */, int rulerWidth )
711{
712 int x = pos.x() - rulerWidth;
713 int y = pos.y() - rulerWidth;
714 TQPoint p( x, y );
715 m_insertGuide = false;
716 TQMouseEvent e( TQEvent::MouseButtonRelease, p, TQt::LeftButton, TQt::LeftButton );
717 mouseReleaseEvent( &e );
718}
719
720
721void KoGuides::slotChangePosition()
722{
723 KoPoint p( mapFromScreen( m_lastPoint ) );
724 KoGuideLine * guideLine = find( p, m_zoomHandler->unzoomItY( 2 ) );
725
726 const KoPageLayout& pl = m_view->koDocument()->pageLayout();
727 double max = 0.0;
728 if ( guideLine->orientation == TQt::Vertical )
729 {
730 max = TQMAX( pl.ptWidth, m_zoomHandler->unzoomItX( m_view->canvas()->size().width() + m_view->canvasXOffset() - 1 ) );
731 }
732 else
733 {
734 max = TQMAX( pl.ptHeight, m_zoomHandler->unzoomItY( m_view->canvas()->size().height() + m_view->canvasYOffset() - 1 ) );
735 }
736
737 KoGuideLineDia dia( 0, guideLine->position, 0.0, max, m_view->koDocument()->unit() );
738 if ( dia.exec() == TQDialog::Accepted )
739 {
740 guideLine->position = dia.pos();
741 paint();
742 emit guideLinesChanged( m_view );
743 }
744}
745
746
747void KoGuides::slotRemove()
748{
749 removeSelected();
750 paint();
751}
752
753
754void KoGuides::paint()
755{
756 m_view->canvas()->repaint( false );
757}
758
759
760void KoGuides::add( TQt::Orientation o, TQPoint &pos )
761{
762 KoPoint p( mapFromScreen( pos ) );
763 KoGuideLine *guideLine = new KoGuideLine( o, o == TQt::Vertical ? p.x(): p.y() );
764 m_guideLines[GL].append( guideLine );
765}
766
767
768void KoGuides::select( KoGuideLine *guideLine )
769{
770 guideLine->selected = true;
771 if ( m_guideLines[GL].remove( guideLine ) == 1 )
772 {
773 m_guideLines[GL_SELECTED].append( guideLine );
774 }
775}
776
777
778void KoGuides::unselect( KoGuideLine *guideLine )
779{
780 guideLine->selected = false;
781 if ( m_guideLines[GL_SELECTED].remove( guideLine ) == 1 )
782 {
783 m_guideLines[GL].append( guideLine );
784 }
785}
786
787
788bool KoGuides::unselectAll()
789{
790 bool selected = m_guideLines[GL_SELECTED].empty() == false;
791
792 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
793 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
794 {
795 ( *it )->selected = false;
796 m_guideLines[GL].append( *it );
797 }
798 m_guideLines[GL_SELECTED].clear();
799
800 return selected;
801}
802
803
804void KoGuides::removeSelected()
805{
806 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
807 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
808 {
809 delete ( *it );
810 }
811 m_guideLines[GL_SELECTED].clear();
812}
813
814
815bool KoGuides::hasSelected()
816{
817 return m_guideLines[GL_SELECTED].empty() == false;
818}
819
820
821KoGuides::KoGuideLine * KoGuides::find( KoPoint &p, double diff )
822{
823 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
824 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
825 {
826 if ( ( *it )->orientation == TQt::Vertical && TQABS( ( *it )->position - p.x() ) < diff )
827 {
828 return *it;
829 }
830 if ( ( *it )->orientation == TQt::Horizontal && TQABS( ( *it )->position - p.y() ) < diff )
831 {
832 return *it;
833 }
834 }
835
836 it = m_guideLines[GL].begin();
837 for ( ; it != m_guideLines[GL].end(); ++it )
838 {
839 if ( ( *it )->orientation == TQt::Vertical && TQABS( ( *it )->position - p.x() ) < diff )
840 {
841 return *it;
842 }
843 if ( ( *it )->orientation == TQt::Horizontal && TQABS( ( *it )->position - p.y() ) < diff )
844 {
845 return *it;
846 }
847 }
848 return 0;
849}
850
851
852void KoGuides::moveSelectedBy( TQPoint &p )
853{
854 KoPoint point( m_zoomHandler->unzoomPoint( p ) );
855 if ( m_guideLines[GL_SELECTED].count() > 1 )
856 {
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 ) );
860
861 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
862 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
863 {
864 if ( ( *it )->orientation == TQt::Vertical )
865 {
866 double tmp = ( *it )->position + point.x();
867 if ( tmp < 0 )
868 {
869 point.setX( point.x() - tmp );
870 }
871 else if ( tmp > right )
872 {
873 point.setX( point.x() - ( tmp - right ) );
874 }
875 }
876 else
877 {
878 double tmp = ( *it )->position + point.y();
879 if ( tmp < 0 )
880 {
881 point.setY( point.y() - tmp );
882 }
883 else if ( tmp > bottom )
884 {
885 point.setY( point.y() - ( tmp - bottom ) );
886 }
887 }
888 }
889 }
890 TQValueList<KoGuideLine *>::iterator it = m_guideLines[GL_SELECTED].begin();
891 for ( ; it != m_guideLines[GL_SELECTED].end(); ++it )
892 {
893 ( *it )->snapping = false;
894
895 if ( ( *it )->orientation == TQt::Vertical && p.x() != 0 )
896 {
897 ( *it )->position = ( *it )->position + point.x();
898 }
899 else if ( ( *it )->orientation == TQt::Horizontal && p.y() != 0 )
900 {
901 ( *it )->position = ( *it )->position + point.y();
902 }
903 }
904}
905
906
907KoPoint KoGuides::mapFromScreen( const TQPoint & pos )
908{
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 );
914}
915
916
917TQPoint KoGuides::mapToScreen( const KoPoint & pos )
918{
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 );
922}
923
924
925#include "KoGuides.moc"
A point whose coordinates are floating-point values ( "double"s ).
Definition KoPoint.h:30
A rect whose coordinates are floating-point values ( "double"s ).
Definition KoRect.h:31
This class is used to display a KoDocument.
Definition KoView.h:58
This class handles the zooming and DPI stuff (conversions between pt values and pixels).
void changeTitle(int id, const TQString &text)
int insertTitle(const TQString &text, int id=-1, int index=-1)
double ptHeight
Page height in pt.
double ptWidth
Page width in pt.