summaryrefslogtreecommitdiffstats
path: root/kimagemapeditor/drawzone.cpp
blob: 152f5300016f981b78ed832394adfaabad76c47f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/***************************************************************************
                          drawzone.cpp  -  description
                             -------------------
    begin                : Wed Apr 4 2001
    copyright            : (C) 2001 by Jan Sch�er
    email                : j_schaef@informatik.uni-kl.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

// QT
#include <tqbitmap.h>
#include <tqpainter.h>
#include <tqdragobject.h>
#include <tqpixmap.h>

// KDE
#include <kdebug.h>
#include <kurldrag.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kapplication.h>
#include <kmimetype.h>

// Local
#include "drawzone.h"
#include "kimagemapeditor.h"
#include "kimecommands.h"
#include "areacreator.h"

#include "kimecommon.h"

DrawZone::DrawZone(TQWidget *tqparent,KImageMapEditor* _imageMapEditor)
	: TQScrollView(tqparent)
{
	imageMapEditor=_imageMapEditor;
//	setPicture(TQImage());
	currentAction=None;
	currentArea=0L;
	oldArea=0L;
	_zoom=1;
  if (imageMapEditor->isReadWrite()) {
	    viewport()->setMouseTracking(true);
    	viewport()->setAcceptDrops(true);
      this->setAcceptDrops(true);
  }
  else
      viewport()->setMouseTracking(false);

	setDragAutoScroll(true);

	// The cross rectangle cursor
	TQBitmap b(32,32,true);
	TQBitmap b2(32,32,true);
	TQPainter p(&b);
	// the cross
  p.drawLine(0,8,6,8);
  p.drawLine(10,8,16,8);
	p.drawLine(8,0,8,6);
	p.drawLine(8,10,8,16);
	// the rectangle
	p.drawRect(17,17,8,6);

	p.end();

	p.begin(&b2);
	// the cross black lines
  p.drawLine(0,8,6,8);
  p.drawLine(10,8,16,8);
	p.drawLine(8,0,8,6);
	p.drawLine(8,10,8,16);

	// the cross white lines
  p.drawLine(0,7,6,7);
  p.drawLine(10,7,16,7);
	p.drawLine(7,0,7,6);
	p.drawLine(7,10,7,16);

	// the cross white lines
  p.drawLine(0,9,6,9);
  p.drawLine(10,9,16,9);
	p.drawLine(9,0,9,6);
	p.drawLine(9,10,9,16);

	// the rectangles
	p.drawRect(17,17,8,6);	// black
	p.drawRect(18,18,6,4);  // white
	p.drawRect(16,16,10,8); // white

	p.end();

	RectangleCursor = TQCursor(b,b2,8,8);


	// The cross circle cursor
	b = TQBitmap(32,32,true);
	b2 = TQBitmap(32,32,true);
	p.begin(&b);
	// the cross
  p.drawLine(0,8,6,8);
  p.drawLine(10,8,16,8);
	p.drawLine(8,0,8,6);
	p.drawLine(8,10,8,16);
	// the circle
	p.drawEllipse(17,17,8,8);

	p.end();

	p.begin(&b2);
	// the cross black lines
  p.drawLine(0,8,6,8);
  p.drawLine(10,8,16,8);
	p.drawLine(8,0,8,6);
	p.drawLine(8,10,8,16);

	// the cross white lines
  p.drawLine(0,7,6,7);
  p.drawLine(10,7,16,7);
	p.drawLine(7,0,7,6);
	p.drawLine(7,10,7,16);

	// the cross white lines
  p.drawLine(0,9,6,9);
  p.drawLine(10,9,16,9);
	p.drawLine(9,0,9,6);
	p.drawLine(9,10,9,16);

	// the circles
	p.drawEllipse(17,17,8,8);  // black
	p.drawEllipse(16,16,10,10);  // white
	p.drawEllipse(18,18,6,6);  // white

	p.end();

	CircleCursor = TQCursor(b,b2,8,8);

	TQString path = KGlobal::dirs()->findResourceDir( "data", "kimagemapeditor/polygoncursor.png" ) + "kimagemapeditor/polygoncursor.png";
	PolygonCursor = TQCursor(TQPixmap(path),8,8);

	path = KGlobal::dirs()->findResourceDir( "data", "kimagemapeditor/freehandcursor.png" ) + "kimagemapeditor/freehandcursor.png";
	FreehandCursor = TQCursor(TQPixmap(path),8,8);

	path = KGlobal::dirs()->findResourceDir( "data", "kimagemapeditor/addpointcursor.png" ) + "kimagemapeditor/addpointcursor.png";
	AddPointCursor = TQCursor(TQPixmap(path),8,8);

	path = KGlobal::dirs()->findResourceDir( "data", "kimagemapeditor/removepointcursor.png" ) + "kimagemapeditor/removepointcursor.png";
	RemovePointCursor = TQCursor(TQPixmap(path),8,8);
}

DrawZone::~DrawZone(){
}

void DrawZone::setPicture(const TQImage &_image) {
	image=_image;
//-	zoomedImage.convertFromImage(image);
	setZoom(_zoom);
}

void DrawZone::setZoom(double z)
{
	_zoom=z;
	imageRect.setHeight(myround(image.height()*_zoom));
	imageRect.setWidth(myround(image.width()*_zoom));
	zoomedImage=TQPixmap(imageRect.width(),imageRect.height());
	TQPainter p(&zoomedImage);
	p.scale(z,z);
	TQPixmap pix;
	pix.convertFromImage(image);
	// if the picture has transparent areas,
	// fill them with Gimp like background
	if (pix.tqmask()) {
  	TQPixmap backPix(32,32);
  	TQPainter p2(&backPix);
  	p2.fillRect(0,0,32,32,TQColor(156,149,156));
  	p2.fillRect(0,16,16,16,TQColor(98,105,98));
  	p2.fillRect(16,0,16,16,TQColor(98,105,98));
  	p2.flush();
  	p.setPen(TQPen());
  	p.fillRect(imageRect.left(),imageRect.top(),imageRect.width(),imageRect.height(),TQBrush(TQColor("black"),backPix));
	}
	p.drawPixmap(imageRect.left(),imageRect.top(),pix);
	p.flush();
	resizeContents(visibleWidth()>imageRect.width() ? visibleWidth() : imageRect.width(),
								 visibleHeight()>imageRect.height() ? visibleHeight() : imageRect.height());
	tqrepaintContents(0,0,contentsWidth(),contentsHeight(),true);
}

TQPoint DrawZone::translateFromZoom(const TQPoint & p) const {
	return TQPoint((int)(p.x()/_zoom),(int)(p.y()/_zoom));
}

TQRect DrawZone::translateFromZoom(const TQRect & p) const {
	return TQRect((int)(p.x()/_zoom),(int) (p.y()/_zoom),
							 (int)(p.width()/_zoom),(int)(p.height()/_zoom));
}

TQPoint DrawZone::translateToZoom(const TQPoint & p) const {
	return TQPoint(myround(p.x()*_zoom),myround(p.y()*_zoom));
}

TQRect DrawZone::translateToZoom(const TQRect & r) const {
//	return TQRect(round(r.x()*_zoom),round(r.y()*_zoom),
//							 round(r.width()*_zoom),round(r.height()*_zoom));
	return TQRect((int)(r.x()*_zoom),(int)(r.y()*_zoom),
							 (int)(r.width()*_zoom+2),(int)(r.height()*_zoom+2));
}

void DrawZone::contentsMouseDoubleClickEvent(TQMouseEvent* e) {
  if ( ! imageMapEditor->isReadWrite())
     return;

	TQPoint point=e->pos();
	point-=imageRect.topLeft();
	point=translateFromZoom(point);
	if ( currentAction==None &&
		(currentArea=imageMapEditor->onArea(point)))
	{
		imageMapEditor->deselectAll();
		imageMapEditor->select(currentArea);
		currentArea=imageMapEditor->selected();
		imageMapEditor->showTagEditor(imageMapEditor->selected());
	}

}

void DrawZone::contentsMousePressEvent(TQMouseEvent* e)
{
  if ( ! imageMapEditor->isReadWrite())
     return;

	drawStart=e->pos();
	// Check if it's on picture if not
	// move it to the picture's border
	if (!imageRect.tqcontains(drawStart)) {
		if (drawStart.x()>imageRect.right())
			drawStart.setX(imageRect.right());
		if (drawStart.x()<imageRect.left())
			drawStart.setX(imageRect.left());
		if (drawStart.y()>imageRect.bottom())
			drawStart.setY(imageRect.bottom());
		if (drawStart.y()<imageRect.top())
			drawStart.setY(imageRect.top());
	}

	// Translate it to picture coordinates
	drawStart-=imageRect.topLeft();
  TQPoint zoomedPoint = drawStart;
	drawStart=translateFromZoom(drawStart);
	delete oldArea;
	oldArea=0L;

	if (currentArea)
	{
		oldArea=currentArea->clone();
	}

	if ( currentAction==None ) {
		if (e->button()==Qt::RightButton)
		{
			if ( (currentArea=imageMapEditor->onArea(drawStart)) )
			{
  			if ( ! currentArea->isSelected())
  			{
  				imageMapEditor->deselectAll();
  				imageMapEditor->select(currentArea);
  			}
 				currentArea=imageMapEditor->selected();
  		}

  		imageMapEditor->slotShowMainPopupMenu(e->globalPos());

		}
		else
		if (e->button()==Qt::MidButton) {
			contentsMouseDoubleClickEvent(e);
		}
		else  // LeftClick on selectionpoint
		if ((currentArea=imageMapEditor->selected()) &&
			(currentSelectionPoint=currentArea->onSelectionPoint(zoomedPoint,_zoom)))
		{
			oldArea=currentArea->clone();

		  if ( (imageMapEditor->currentToolType() == KImageMapEditor::RemovePoint) &&
		       (imageMapEditor->selected()->selectionPoints()->count()>3) )
		  {
      	currentAction=RemovePoint;
		  }
		  else
		  {
  			currentAction=MoveSelectionPoint;
        currentArea->setMoving(true);
      }

		} else // leftclick not on selectionpoint but on area
		if ((currentArea=imageMapEditor->onArea(drawStart)))
		{
		  if ( imageMapEditor->currentToolType() == KImageMapEditor::AddPoint )
		  {
		    currentAction=AddPoint;
		    viewport()->setCursor(AddPointCursor);
   			oldArea=currentArea->clone();
		  }
		  else
		  {
  			currentAction=MoveArea;
  			viewport()->setCursor(sizeAllCursor);

  			if ( currentArea->isSelected() ) {
  				if ( (e->state() & ControlButton) )
   					imageMapEditor->deselect(currentArea);
   			} else
  			{
  				if ( (e->state() & ControlButton) )
  					imageMapEditor->select( currentArea );
  				else {
  					imageMapEditor->deselectAll();
  					imageMapEditor->select( currentArea );
  				}
  			}

  			currentArea = imageMapEditor->selected();
        currentArea->setMoving(true);

  			oldArea=currentArea->clone();
      }
  	}
		else  // leftclick on the background
		if ( (imageMapEditor->currentToolType()==KImageMapEditor::Rectangle) ||
		     (imageMapEditor->currentToolType()==KImageMapEditor::Circle) ||
		     (imageMapEditor->currentToolType()==KImageMapEditor::Polygon) ||
		     (imageMapEditor->currentToolType()==KImageMapEditor::Freehand))
		{
			currentArea=AreaCreator::create(imageMapEditor->currentToolType());

 		  currentArea->setRect(TQRect(drawStart,drawStart));
 		  currentArea->setSelected(false);
 		  imageMapEditor->deselectAll();

			switch (imageMapEditor->currentToolType())	{
				case KImageMapEditor::Rectangle : currentAction=DrawRectangle; break;
				case KImageMapEditor::Circle : currentAction=DrawCircle; break;
				case KImageMapEditor::Polygon :
  					currentAction=DrawPolygon;
      			currentArea->addCoord(drawStart);
      			currentSelectionPoint=currentArea->selectionPoints()->last();
      			break;
        case KImageMapEditor::Freehand :
            currentAction=DrawFreehand;
      			//currentArea->addCoord(drawStart);
      			currentArea->setFinished(false);
 			  		break;
				default: break;
			}
		}
		else
  	// leftclicked with the arrow at an areafree position
		if (imageMapEditor->currentToolType()==KImageMapEditor::Selection)
  	{
  	  	currentArea=0L;
  	  	imageMapEditor->deselectAll();
  	  	// Start drawing a selection rectangle
  	  	currentAction=DoSelect;
  	  	oldSelectionRect = imageRect;
  	}
	} else
	if ( currentAction==DrawPolygon) {

	}

	TQRect r;
	if (oldArea)
		r=oldArea->selectionRect();
	if (currentArea) {
		r= r | currentArea->selectionRect();
		tqrepaintContents(translateToZoom(r),false);
	}


}

void DrawZone::contentsMouseReleaseEvent(TQMouseEvent *e) {
  if ( ! imageMapEditor->isReadWrite())
     return;

  TQPoint drawEnd=e->pos();

	// Check if it's on picture if not
	// move it to the picture's border
	if (!imageRect.tqcontains(drawEnd)) {
		if (drawEnd.x()>imageRect.right())
			drawEnd.setX(imageRect.right());
		if (drawEnd.x()<imageRect.left())
			drawEnd.setX(imageRect.left());
		if (drawEnd.y()>imageRect.bottom())
			drawEnd.setY(imageRect.bottom());
		if (drawEnd.y()<imageRect.top())
			drawEnd.setY(imageRect.top());
	}
	// Translate it to picture coordinates
	drawEnd-=imageRect.topLeft();
  TQPoint zoomedPoint=drawEnd;

	drawEnd=translateFromZoom(drawEnd);

	if (currentAction==DrawCircle || currentAction==DrawRectangle) {
  		currentAction=None;
   		imageMapEditor->commandHistory()->addCommand(
   			new CreateCommand( imageMapEditor, currentArea ), true);
  } else
	if (currentAction==DrawPolygon) {
		// If the number of Polygonpoints is more than 2
		// and clicked on the first PolygonPoint or
		// the right Button was pressed the Polygon is finished
  	if ((currentArea->selectionPoints()->count()>2)
  		&& (currentArea->selectionPoints()->first()->tqcontains(drawEnd)
  		 || (e->button()==Qt::RightButton)))
  	{
			currentArea->setFinished(true);
  		currentAction=None;
   		imageMapEditor->commandHistory()->addCommand(
   			new CreateCommand( imageMapEditor, currentArea ), true);
		} else
		{
   		currentArea->insertCoord(currentArea->countSelectionPoints()-1, drawEnd);
   		currentSelectionPoint=currentArea->selectionPoints()->last();
   	}
	} else
	if (currentAction==DrawFreehand)
	{
			currentArea->setFinished(true);
			currentArea->simplifyCoords();
  		currentAction=None;
   		imageMapEditor->commandHistory()->addCommand(
   			new CreateCommand( imageMapEditor, currentArea ), true);
	} else
	if (currentAction==MoveArea) {
	    TQPoint p1 = oldArea->rect().topLeft();
	    TQPoint p2 = imageMapEditor->selected()->rect().topLeft();

	    if (p1 != p2)
	    {
  			imageMapEditor->commandHistory()->addCommand(
	  			new MoveCommand( imageMapEditor, imageMapEditor->selected(), oldArea->rect().topLeft()),true);
  	  	imageMapEditor->slotAreaChanged(currentArea);
  	  } else
     	  imageMapEditor->updateSelection();

  		currentAction=None;
  } else
  if (currentAction==MoveSelectionPoint) {
			imageMapEditor->commandHistory()->addCommand(
				new ResizeCommand( imageMapEditor, imageMapEditor->selected(), oldArea),true);
  		imageMapEditor->slotAreaChanged(currentArea);
  		currentAction=None;
  } else
  if (currentAction==RemovePoint) {
     if (currentSelectionPoint==currentArea->onSelectionPoint(zoomedPoint,_zoom))
     {
       currentArea->removeSelectionPoint(currentSelectionPoint);

       imageMapEditor->commandHistory()->addCommand(
         new RemovePointCommand( imageMapEditor, imageMapEditor->selected(), oldArea),true);
    	 imageMapEditor->slotAreaChanged(currentArea);
     }
 		 currentAction=None;
  } else
  if (currentAction==AddPoint)
  {
      if (currentArea==imageMapEditor->onArea(drawEnd))
      {
        imageMapEditor->commandHistory()->addCommand(
          new AddPointCommand( imageMapEditor, imageMapEditor->selected(), drawEnd),true);
    		imageMapEditor->slotAreaChanged(currentArea);
  		}
   		currentAction=None;
  } else
  if (currentAction==DoSelect) {
  	currentAction=None;

   	TQRect r(drawStart.x(),drawStart.y(),drawCurrent.x()-drawStart.x(),drawCurrent.y()-drawStart.y());
   	r = r.normalize();

    	AreaListIterator it=imageMapEditor->areaList();
  		for ( ; it.current() != 0L ; ++it )	{
    		if ( it.current()->rect().intersects(r) )
    		{
   				if (!it.current()->isSelected() )
    				imageMapEditor->selectWithoutUpdate( it.current() );
    		}
    		else
     		  if (it.current()->isSelected())
      			imageMapEditor->deselectWithoutUpdate( it.current() );
			}

    imageMapEditor->updateActionAccess();
  	imageMapEditor->updateSelection();
    tqrepaintContents(imageRect,false);
  } else {
  	currentAction=None;
  }
	imageMapEditor->slotChangeStatusCoords(drawEnd.x(),drawEnd.y());
	if (currentArea)
	{
	  currentArea->setMoving(false);
		tqrepaintArea(*currentArea);
	}
	delete oldArea;
	oldArea=0L;
//	tqrepaintContents(0,0,contentsWidth(),contentsHeight(),false);
  imageMapEditor->slotUpdateSelectionCoords();
}


void DrawZone::contentsMouseMoveEvent(TQMouseEvent *e)
{
  if ( ! imageMapEditor->isReadWrite())
     return;


		drawCurrent=e->pos();

		// If outside the image
		// set it to the border
		if (!imageRect.tqcontains(drawCurrent)) {
  		if (drawCurrent.x()>imageRect.right())
  			drawCurrent.setX(imageRect.right());
  		if (drawCurrent.x()<imageRect.left())
  			drawCurrent.setX(imageRect.left());
  		if (drawCurrent.y()>imageRect.bottom())
  			drawCurrent.setY(imageRect.bottom());
  		if (drawCurrent.y()<imageRect.top())
  			drawCurrent.setY(imageRect.top());
		}

		// Translate to image coordinates
		drawCurrent-=imageRect.topLeft();
    TQPoint zoomedPoint=drawCurrent;
		drawCurrent=translateFromZoom(drawCurrent);

		if (currentAction==DrawRectangle) {
			// To avoid flicker, only tqrepaint the minimum rect
			TQRect oldRect=translateToZoom(currentArea->rect());
			currentArea->setRect(TQRect(drawStart,drawCurrent).normalize());
			TQRect newRect=translateToZoom(currentArea->selectionRect());
			TQRect r=oldRect | newRect;
			tqrepaintContents(r,false);
			imageMapEditor->slotUpdateSelectionCoords( currentArea->rect() );
		} else
		if (currentAction==DrawCircle) {
			TQRect oldRect=translateToZoom(currentArea->rect());

			// We don't want ellipses
			int maxDistance=myabs(drawStart.x()-drawCurrent.x()) >
											myabs(drawStart.y()-drawCurrent.y()) ?
											myabs(drawStart.x()-drawCurrent.x()) :
											myabs(drawStart.y()-drawCurrent.y()) ;

			int xDiff=maxDistance;
			int yDiff=maxDistance;

			if ( drawStart.x()-drawCurrent.x() > 0)
				xDiff=-xDiff;

			if ( drawStart.y()-drawCurrent.y() > 0)
				yDiff=-yDiff;

			TQPoint endPoint( drawStart.x()+xDiff, drawStart.y()+yDiff);

			currentArea->setRect(TQRect(drawStart,endPoint).normalize());
			TQRect newRect=translateToZoom(currentArea->rect());
			TQRect r=oldRect | newRect;
			tqrepaintContents(r,false);
			imageMapEditor->slotUpdateSelectionCoords( currentArea->rect() );
		} else
		if ( currentAction==DrawPolygon ) {
			TQRect oldRect=translateToZoom(currentArea->rect());
			currentArea->moveSelectionPoint(currentSelectionPoint,drawCurrent);
			TQRect newRect=translateToZoom(currentArea->rect());
			TQRect r=oldRect | newRect;
			tqrepaintContents(r,false);
		} else
		if ( currentAction==DrawFreehand) {
			TQRect oldRect=translateToZoom(currentArea->rect());
		  currentArea->insertCoord(currentArea->countSelectionPoints(), drawCurrent);
			TQRect newRect=translateToZoom(currentArea->rect());
			TQRect r=oldRect | newRect;
			tqrepaintContents(r,false);
		} else
		if ( currentAction==MoveArea ) {
			TQRect oldRect=translateToZoom(currentArea->selectionRect());
			currentArea->moveBy((drawCurrent-drawStart).x(),(drawCurrent-drawStart).y());
			TQRect newRect=translateToZoom(currentArea->selectionRect());
			TQRect r=oldRect | newRect;
			currentArea->setMoving(true);
			tqrepaintContents(r,false);
			drawStart=drawCurrent;
			imageMapEditor->slotUpdateSelectionCoords();
		} else
		if ( currentAction==MoveSelectionPoint ) {
			TQRect oldRect=translateToZoom(currentArea->selectionRect());
			currentArea->moveSelectionPoint(currentSelectionPoint,drawCurrent);
			TQRect newRect=translateToZoom(currentArea->selectionRect());
			TQRect r=oldRect | newRect;
			tqrepaintContents(r,false);
			imageMapEditor->slotUpdateSelectionCoords();
		} else
	  if (currentAction==DoSelect) {

    	TQRect r(drawStart.x(),drawStart.y(),drawCurrent.x()-drawStart.x(),drawCurrent.y()-drawStart.y());
    	r = r.normalize();
//    	r = translateFromZoom(r);
/*
    	AreaListIterator it=imageMapEditor->areaList();
  		for ( ; it.current() != 0L ; ++it )	{
    		if ( it.current()->rect().intersects(r) )
    		{
   				if (!it.current()->isSelected() )
    				imageMapEditor->selectWithoutUpdate( it.current() );
    		}
    		else
     		  if (it.current()->isSelected())
      			imageMapEditor->deselectWithoutUpdate( it.current() );
			}
*/
      // We don't have to tqrepaint the hole selection rectangle
      // only the borders have to be tqrepainted.
      // So we have to create 4 rectangles for every rectangle
      // which represent the borders and then tqrepaint them.

      TQRect lb,rb,tb,bb;
      createBorderRectangles(translateToZoom(r),lb,rb,tb,bb);
      tqrepaintContents(lb,false);
      tqrepaintContents(rb,false);
      tqrepaintContents(tb,false);
      tqrepaintContents(bb,false);

      createBorderRectangles(translateToZoom(oldSelectionRect),lb,rb,tb,bb);
      tqrepaintContents(lb,false);
      tqrepaintContents(rb,false);
      tqrepaintContents(tb,false);
      tqrepaintContents(bb,false);

//    	tqrepaintContents(oldSelectionRect | r,false);
    	oldSelectionRect = r;
//    	tqrepaintContents(translateToZoom(r),false);
//+			imageMapEditor->updateSelection();


//			TQRect r(drawStart.x(),drawStart.y(),drawCurrent.x()-drawStart.x(),drawCurrent.y()-drawStart.y());
//			r = r.normalize();
//			TQRect r2(drawStart.x(),drawStart.y(),drawOld.x()-drawStart.x(),drawOld.y()-drawStart.y());
//			r2 = r2.normalize();
//			r = translateToZoom(r | r2);
//			tqrepaintContents(r,false);
  	} else
		if ( currentAction==None )
		{
			if ( imageMapEditor->selected() &&
					 imageMapEditor->selected()->onSelectionPoint(zoomedPoint,_zoom ))
			{
 				if (imageMapEditor->selected()->type()==Area::Polygon)
 				{
			    if ((imageMapEditor->currentToolType()==KImageMapEditor::RemovePoint) &&
			        (imageMapEditor->selected()->selectionPoints()->count()>3) )
			    {
		        viewport()->setCursor(RemovePointCursor);
			    }
			    else
			    {
 						viewport()->setCursor(pointingHandCursor);
 				  }
 				}
 				else
 				{
   				TQPoint center=imageMapEditor->selected()->rect().center();
   				if (drawCurrent.x() < center.x()) {
   					if (drawCurrent.y() < center.y())
   						viewport()->setCursor(sizeFDiagCursor);
   					else
   						viewport()->setCursor(sizeBDiagCursor);
   				}
   				else {
   					if (drawCurrent.y() < center.y())
   						viewport()->setCursor(sizeBDiagCursor);
   					else
   						viewport()->setCursor(sizeFDiagCursor);
 					}
 				}
			} else
			if ( imageMapEditor->onArea(drawCurrent) )
			{
  			if (imageMapEditor->currentToolType()==KImageMapEditor::AddPoint)
  			{
			    viewport()->setCursor(AddPointCursor);
			  }
			  else
        {
				  viewport()->setCursor(sizeAllCursor);
        }
			}
			else
			if (imageMapEditor->currentToolType()==KImageMapEditor::Rectangle) {
			    viewport()->setCursor(RectangleCursor);
//          kdDebug() << "KImageMapEditor::DrawZone: viewport()->setCursor to Rectangle" << endl;
      }
			else
			if (imageMapEditor->currentToolType()==KImageMapEditor::Circle)
			    viewport()->setCursor(CircleCursor);
			else
			if (imageMapEditor->currentToolType()==KImageMapEditor::Polygon)
			    viewport()->setCursor(PolygonCursor);
			else
			if (imageMapEditor->currentToolType()==KImageMapEditor::Freehand)
			    viewport()->setCursor(FreehandCursor);
			else
		 		viewport()->setCursor(arrowCursor);

		}
		imageMapEditor->slotChangeStatusCoords(drawCurrent.x(),drawCurrent.y());
}

void DrawZone::createBorderRectangles(const TQRect & r,TQRect & rb,TQRect & lb,TQRect & tb,TQRect & bb)
{
  int bw;
  bw = (int) (2+2*_zoom); // Border width

  rb.setX(r.x()+r.width()-bw);
  rb.setY(r.y());
  rb.setWidth(bw+1);
  rb.setHeight(r.height());

  lb.setX(r.x());
  lb.setY(r.y());
  lb.setWidth(bw);
  lb.setHeight(r.height());

  tb.setX(r.x());
  tb.setY(r.y());
  tb.setWidth(r.width());
  tb.setHeight(bw);

  bb.setX(r.x());
  bb.setY(r.y()+r.height()-bw);
  bb.setWidth(r.width());
  bb.setHeight(bw+1);
}


void DrawZone::resizeEvent(TQResizeEvent* e) {
	TQScrollView::resizeEvent(e);
	int width=(int) (image.width()*_zoom);
	int height=(int) (image.height()*_zoom);
	if (visibleWidth()>width)
		width=visibleWidth();
	if (visibleHeight()>height)
		height=visibleHeight();

	resizeContents(width,height);

	imageRect.setLeft(0);
	imageRect.setTop(0);
	imageRect.setHeight((int)(image.height()*_zoom));
	imageRect.setWidth((int)(image.width()*_zoom));

}

void DrawZone::cancelDrawing()
{
  if (   (currentAction == DrawPolygon )
      || (currentAction == DrawRectangle )
      || (currentAction == DrawCircle )
     )
  {
    currentAction = None;
    TQRect r = translateToZoom(currentArea->selectionRect());
    delete currentArea;
    currentArea = 0L;
    tqrepaintContents(r,false);
    imageMapEditor->slotUpdateSelectionCoords();
  }
}

void DrawZone::tqrepaintArea(const Area & a) {
	tqrepaintContents(translateToZoom(a.selectionRect()),false);
}

void DrawZone::tqrepaintRect(const TQRect & r) {
	tqrepaintContents(translateToZoom(r),false);
}

void DrawZone::drawContents(TQPainter* p,int clipx,int clipy,int clipw,int cliph)
{

// Erase background without flicker
	TQRect updateRect(clipx,clipy,clipw,cliph);

	// Pixmap for double-buffering
  TQPixmap doubleBuffer(updateRect.size());
  if (doubleBuffer.isNull())
  	return;

  TQPainter p2(&doubleBuffer);
	p2.drawPixmap(0,0,zoomedImage,clipx,clipy,clipw,cliph);
	p2.setBackgroundColor(p->backgroundColor());

	if (zoomedImage.width() < (clipw+clipx) ) {
		int eraseWidth = clipw+clipx - zoomedImage.width();
		p2.eraseRect( TQRect(clipw-eraseWidth,0,eraseWidth,cliph) );
	}

	if (zoomedImage.height() < (cliph+clipy) ) {
		int eraseHeight = cliph+clipy - zoomedImage.height();
		p2.eraseRect( TQRect(0,cliph-eraseHeight,clipw,eraseHeight) );
	}

	p2.translate(-clipx, -clipy);
	p2.scale(_zoom,_zoom);

	TQRect areaUpdateRect;
	areaUpdateRect.setX(myround(clipx/_zoom)-1);
	areaUpdateRect.setY(myround(clipy/_zoom)-1);
	areaUpdateRect.setWidth(myround(clipw/_zoom)+2);
	areaUpdateRect.setHeight(myround(cliph/_zoom)+2);

	AreaListIterator it=imageMapEditor->areaList();
	for ( it.toLast();it.current() != 0L; --it)
	{
     if (it.current()->rect().intersects(areaUpdateRect))
       it.current()->draw(p2);
	}

	// Draw the current drawing Area
	if (currentAction != MoveArea &&
			currentAction != MoveSelectionPoint &&
			currentAction != None &&
			currentAction != DoSelect)
	{
		currentArea->draw(p2);
	}

	if (currentAction == DoSelect )
  {
		TQPen pen = TQPen(TQColor("white"),1);
		p2.setRasterOp(TQt::XorROP);
		pen.setStyle(Qt::DotLine);
		p2.setPen(pen);

		TQRect r( drawStart.x(),drawStart.y(),drawCurrent.x()-drawStart.x(),drawCurrent.y()-drawStart.y());
		r = r.normalize();
		p2.drawRect(r);
	}



  p2.end();

  // Copy the double buffer into the widget
  p->drawPixmap(clipx,clipy,doubleBuffer);


}

void DrawZone::contentsDragEnterEvent(TQDragEnterEvent*e) {
  if (!KURLDrag::canDecode(e))
    return;

//   bool accept = false;
  KURL::List uris;
  KURLDrag::decode(e,uris);
  KMimeType::Ptr ptr = KMimeType::findByURL(uris.first());
//  kdDebug() << "***** " << ptr.data()->name() << endl;
  if ((ptr.data()->name() == "text/html")
      || (ptr.data()->name().left(6) == "image/"))
    e->accept();
}

void DrawZone::contentsDropEvent( TQDropEvent* e) {
  viewportDropEvent(e);
}



void DrawZone::viewportDropEvent( TQDropEvent* e) {
	KURL::List urlList;
	// A file from konqueror was dropped
	if (KURLDrag::decode(e,urlList)) {
		imageMapEditor->openFile(urlList.first());
	}
}