File: render2d_event.cpp

package info (click to toggle)
xdrawchem 1.0-0.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,820 kB
  • ctags: 2,389
  • sloc: cpp: 17,801; makefile: 263; ansic: 168
file content (819 lines) | stat: -rw-r--r-- 25,020 bytes parent folder | download
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
#include <iostream.h>
#include <math.h>
#include <qwidget.h>
#include <qpainter.h>
#include <qdragobject.h>
#include <qglobal.h>

#include "render2d.h"
#include "chemdata.h"
#include "text.h"
#include "defs.h"

void Render2D::mousePressEvent(QMouseEvent *e1) {  
  DPoint *tmp_pt;
  bool allowedit = false;
  moved = false;

  tmp_pt = new DPoint(e1->x(), e1->y());
  if (mode > 100) c->StartUndo(mode, tmp_pt);
  //cout << "Click" << endl; 
  // update button state
  if (e1->button() == LeftButton) mouse1down = true;
  if (e1->button() == RightButton) {
    mouse3down = true;
  }
  //cout << "start at " << e1->x() << "," << e1->y() << endl;

  // MODE_TEXT: call special function to handle text events
  if (mode == MODE_TEXT) {
    DrawText_mousePressEvent(e1);
    return;
  }

  // MODE_SELECT: start multiple selection
  if ( (mode == MODE_SELECT) && (mouse1down) ) {
    if ( ( highlightpoint == 0) && (highlightobject == 0) ) {
      selectOrigin = e1->pos();
      mode = MODE_SELECT_MULTIPLE;
      return;
    } else {
      cout << "Should move." << endl;
      if (highlightpoint == 0)
	mode = MODE_SELECT_MOVE_OBJECT;
      else
	mode = MODE_SELECT_MOVE_POINT;
    }
    return;
  }

  // set allowedit = true for appropriate modes
  if (mode == MODE_SELECT) allowedit = true;
  // get Drawable of nearest object
  DPoint *e = new DPoint;
  e->x = e1->x(); e->y = e1->y();
  double distobj;
  Drawable *no = c->FindNearestObject(e, distobj);
  if (distobj > 5.0) no = 0;
  if (no != 0) {
    if ( (mode == MODE_DRAWLINE) && 
	 (no->Type() == TYPE_BOND) ) allowedit = true;
    if ( (mode == MODE_DRAWLINE_DASH) && 
	 (no->Type() == TYPE_BOND) ) allowedit = true;
    if ( (mode == MODE_DRAWLINE_UP) && 
	 (no->Type() == TYPE_BOND) ) allowedit = true;
    if ( (mode == MODE_DRAWLINE_DOWN) && 
	 (no->Type() == TYPE_BOND) ) allowedit = true;
  }
  // allowedit and BUTTON3: edit item under mouse
  if ( (allowedit) && (mouse3down) ) {
    if (highlightobject == 0) highlightobject = no;
    if (highlightobject != 0) {
      if (highlightobject->Type() == TYPE_TEXT) {
	setCursor(ibeamCursor);
	mode = MODE_TEXT;
	text_exists = true;
	localtext = (Text *)highlightobject;
	emit TextOn(localtext->getFont());
	repaint();
	return;
      }
      cout << "Will edit" << endl;
      highlightobject->Edit();
      highlightobject->Highlight(false);
      highlightobject = 0;
      if (mode == MODE_SELECT)
	setCursor(arrowCursor);
      else
	setCursor(crossCursor);
      mouse3down = false;
      repaint();
    }
    return;
  }

  // MODE_SELECT_MULTIPLE_SELECTED: either move, or deselect all
  if ( (mode == MODE_SELECT_MULTIPLE_SELECTED) && (mouse1down) ) {
    // check for move
    if (selectionBox.contains( e1->pos() )) {
      c->StartUndo(0,0);
      mode = MODE_SELECT_MOVE_MULTIPLE;
    } else {
      c->DeselectAll();
      mode = MODE_SELECT;
      repaint();
    }
  }

  // MODE_SELECT_MULTIPLE_SELECTED and Button3: Drag and Drop!
  if ( (mode == MODE_SELECT_MULTIPLE_SELECTED) && (mouse3down) ) {
    // check for move
    if (selectionBox.contains( e1->pos() )) {
      dragging = true;
    } else {
      c->DeselectAll();
      mode = MODE_SELECT;
      repaint();
    }
  } 

  // MODE_SELECT_MULTIPLE_RESIZE: resize drawing
  if ( (mode == MODE_SELECT_MULTIPLE_RESIZE) && (mouse1down) ) {
    mode = MODE_SELECT_RESIZE_MULTIPLE;
  }

  // MODE_SELECT_MULTIPLE_ROTATE: rotate drawing
  if ( (mode == MODE_SELECT_MULTIPLE_ROTATE) && (mouse1down) ) {
    mode = MODE_SELECT_ROTATE_MULTIPLE;
    rotateOrigin.setX( (selectionBox.right() + selectionBox.left() - 2) / 2);
    rotateOrigin.setY( (selectionBox.bottom() + selectionBox.top()) / 2);
    prevangle = getAngle( rotateOrigin, e1->pos() );
  }

  // MODE_DRAWLINE (draw line tool selected), and left button.
  // This means we start drawing a line
  if ( (mode == MODE_DRAWLINE) && (mouse1down) ) {
    startpoint = highlightpoint; // both NULL if nothing selected
    highlightpoint = 0; // stop drawing highlight box
    if (startpoint == NULL) {
      startpoint = new DPoint;
      startpoint->set(e1->pos());
      endpoint = NULL;
    }
    mode = MODE_DRAWLINE_DRAWING;
  }

  // MODE_DRAWLINE_DASH (draw dashed line tool selected), and left button.
  // This means we start drawing a dashed line
  if ( (mode == MODE_DRAWLINE_DASH) && (mouse1down) ) {
    startpoint = highlightpoint; // both NULL if nothing selected
    highlightpoint = 0; // stop drawing highlight box
    if (startpoint == NULL) {
      startpoint = new DPoint;
      startpoint->set(e1->pos());
      endpoint = NULL;
    }
    mode = MODE_DRAWLINE_DASH_DRAWING;
  }

  // MODE_DRAWLINE_UP (draw stereo-up line tool selected), and left button.
  // This means we start drawing a line
  if ( (mode == MODE_DRAWLINE_UP) && (mouse1down) ) {
    startpoint = highlightpoint; // both NULL if nothing selected
    highlightpoint = 0; // stop drawing highlight box
    if (startpoint == NULL) {
      startpoint = new DPoint;
      startpoint->set(e1->pos());
      endpoint = NULL;
    }
    mode = MODE_DRAWLINE_UP_DRAWING;
  }

  // MODE_DRAWLINE_DOWN (draw stereo-down line tool selected), and left button.
  // This means we start drawing a line
  if ( (mode == MODE_DRAWLINE_DOWN) && (mouse1down) ) {
    startpoint = highlightpoint; // both NULL if nothing selected
    highlightpoint = 0; // stop drawing highlight box
    if (startpoint == NULL) {
      startpoint = new DPoint;
      startpoint->set(e1->pos());
      endpoint = NULL;
    }
    mode = MODE_DRAWLINE_DOWN_DRAWING;
  }

  // MODE_DRAWARROW (draw arrow tool selected), and left button.
  // This means we start drawing an arrow
  if ( (mode == MODE_DRAWARROW) && (mouse1down) ) {
    startpoint = new DPoint;
    startpoint->set(e1->pos());
    endpoint = NULL;
    mode = MODE_DRAWARROW_DRAWING;
  }

  // MODE_DRAWCURVEARROW (draw curve arrow tool selected), and left button.
  // This means we start drawing an arrow
  if ( (mode == MODE_DRAWCURVEARROW) && (mouse1down) ) {
    startpoint = new DPoint;
    startpoint->set(e1->pos());
    endpoint = NULL;
    mode = MODE_DRAWCURVEARROW_DRAWING;
  }

  // MODE_DRAWBRACKET (draw bracket tool selected), and left button.
  // This means we start drawing an bracket
  if ( (mode == MODE_DRAWBRACKET) && (mouse1down) ) {
    startpoint = new DPoint;
    startpoint->set(e1->pos());
    endpoint = NULL;
    mode = MODE_DRAWBRACKET_DRAWING;
  }

  // MODE_ERASE, and left button.  Erase highlighted object.
  if ( (mode == MODE_ERASE) && (mouse1down) ) {
    if (highlightobject == 0) return;
    c->Erase(highlightobject);
    highlightobject = 0;
    repaint();
  }
}

void Render2D::mouseReleaseEvent(QMouseEvent *e1) { 
  DPoint *tmp_pt;

  //cout << "Unclick" << endl; 
  // update button state
  if (e1->button() == LeftButton) mouse1down = false;
  if (e1->button() == RightButton) mouse3down = false;
  //cout << "end at " << e1->x() << "," << e1->y() << endl;

  // MODE_TEXT: call special function to handle text events
  if (mode == MODE_TEXT) {
    DrawText_mouseReleaseEvent(e1);
    return;
  }

  // MODE_TOOL_*: ChemData handles this, mostly
  if ( (mode > 600) && (mode < 699) ) {
    tmp_pt = new DPoint(e1->pos());
    c->Tool(tmp_pt, mode);
    repaint();
    if (mode == MODE_TOOL_CLEANUPMOL) {
      setMode_Select();
    }
    return;
  }

  // MODE_SELECT_MULTIPLE: finish multiple selection
  if ( (mode == MODE_SELECT_MULTIPLE) && (!mouse1down) ) {
    int tmp1;
    if (selectOrigin.x() > selectPrev.x()) {
      tmp1 = selectOrigin.x();
      selectOrigin.setX(selectPrev.x());
      selectPrev.setX(tmp1);
    }
    if (selectOrigin.y() > selectPrev.y()) {
      tmp1 = selectOrigin.y();
      selectOrigin.setY(selectPrev.y());
      selectPrev.setY(tmp1);
    }
    drawBox(selectOrigin, selectPrev, QColor(255,255,255));
    bool shiftdown = false;
    if ((e1->state() & ShiftButton) == ShiftButton)
      shiftdown = true;
    c->NewSelectRect(QRect(selectOrigin, selectPrev), shiftdown);
    selectionBox = c->selectionBox();
    if (selectionBox.left() < 999)
      mode = MODE_SELECT_MULTIPLE_SELECTED;
    else
      mode = MODE_SELECT;
    repaint();
  }

  // MODE_SELECT_MOVE_MULTIPLE: finish moving
  if ( (mode == MODE_SELECT_MOVE_MULTIPLE) && (!mouse1down) ) {
    mode = MODE_SELECT_MULTIPLE_SELECTED;
    repaint();
  }

  // MODE_SELECT_MOVE_OBJECT: finish moving
  if ( (mode == MODE_SELECT_MOVE_OBJECT) && (!mouse1down) ) {
    mode = MODE_SELECT;
    repaint();
  }

  // MODE_SELECT_MOVE_POINT: finish moving
  if ( (mode == MODE_SELECT_MOVE_POINT) && (!mouse1down) ) {
    mode = MODE_SELECT;
    repaint();
  }

  // MODE_SELECT_RESIZE_MULTIPLE: finish resizing
  if ( (mode == MODE_SELECT_RESIZE_MULTIPLE) && (!mouse1down) ) {
    mode = MODE_SELECT_MULTIPLE_SELECTED;
  }

  // MODE_SELECT_ROTATE_MULTIPLE: finish rotating
  if ( (mode == MODE_SELECT_ROTATE_MULTIPLE) && (!mouse1down) ) {
    mode = MODE_SELECT_MULTIPLE_SELECTED;
  }

  // MODE_DRAWLINE_DRAWING (drawing line), and left button.
  // Finish line
  if ( (mode == MODE_DRAWLINE_DRAWING) && (!mouse1down) ) {
    if (moved) { 
      if (endpoint == NULL) {
	endpoint = new DPoint;
	endpoint->set(e1->pos());
      }
      // submit this line/bond to ChemData
      if (startpoint != endpoint)
	c->addBond(startpoint, endpoint, thick, 1, currentColor);
    }
    startpoint = NULL; endpoint = NULL;
    mode = MODE_DRAWLINE;
    repaint();
    return;
  }

  // MODE_DRAWLINE_DASH_DRAWING (drawing line), and left button.
  // Finish line
  if ( (mode == MODE_DRAWLINE_DASH_DRAWING) && (!mouse1down) ) {
    if (moved) {
      if (endpoint == NULL) {
	endpoint = new DPoint;
	endpoint->set(e1->pos());
      }
      // submit this line/bond to ChemData
      c->addBond(startpoint, endpoint, thick, 99, currentColor);
    }
    startpoint = NULL; endpoint = NULL;
    mode = MODE_DRAWLINE_DASH;
    repaint();
    return;
  }

  // MODE_DRAWLINE_UP_DRAWING (drawing line), and left button.
  // Finish line
  if ( (mode == MODE_DRAWLINE_UP_DRAWING) && (!mouse1down) ) {
    if (moved) {
      if (endpoint == NULL) {
	endpoint = new DPoint;
	endpoint->set(e1->pos());
      }
      // submit this line/bond to ChemData
      c->addBond(startpoint, endpoint, 1, 5, currentColor);
    }
    startpoint = NULL; endpoint = NULL;
    mode = MODE_DRAWLINE_UP;
    repaint();
    return;
  }

  // MODE_DRAWLINE_DOWN_DRAWING (drawing line), and left button.
  // Finish line
  if ( (mode == MODE_DRAWLINE_DOWN_DRAWING) && (!mouse1down) ) {
    if (moved) {
      if (endpoint == NULL) {
	endpoint = new DPoint;
	endpoint->set(e1->pos());
      }
      // submit this line/bond to ChemData
      c->addBond(startpoint, endpoint, 1, 7, currentColor);
    }
    startpoint = NULL; endpoint = NULL;
    mode = MODE_DRAWLINE_DOWN;
    repaint();
    return;
  }

  // MODE_DRAWARROW_DRAWING (drawing arrow), and left button.
  // Finish arrow
  if ( (mode == MODE_DRAWARROW_DRAWING) && (!mouse1down) ) {
    if (moved) {
      if (endpoint == NULL) {
	endpoint = new DPoint;
	endpoint->set(e1->pos());
      }
      // submit this arrow to ChemData
      c->addArrow(startpoint, endpoint, currentColor, bracket_type);
    }
    startpoint = NULL; endpoint = NULL;
    mode = MODE_DRAWARROW;
    repaint();
  }

  // MODE_DRAWCURVEARROW_DRAWING (drawing curved arrow), and left button.
  // Finish arrow
  if ( (mode == MODE_DRAWCURVEARROW_DRAWING) && (!mouse1down) ) {
    endpoint = new DPoint;
    endpoint->set(e1->pos());
    // submit this arrow to ChemData
    c->addCurveArrow(startpoint, endpoint, currentColor, symbolfile);
    startpoint = NULL; endpoint = NULL;
    mode = MODE_DRAWCURVEARROW;
    repaint();
  }

  // MODE_DRAWBRACKET_DRAWING (drawing bracket), and left button.
  // Finish arrow
  if ( (mode == MODE_DRAWBRACKET_DRAWING) && (!mouse1down) ) {
    if (endpoint == NULL) {
      endpoint = new DPoint;
      endpoint->set(e1->pos());
    }
    // submit this bracket to ChemData
    c->addBracket(startpoint, endpoint, currentColor, bracket_type);
    startpoint = NULL; endpoint = NULL;
    mode = MODE_DRAWBRACKET;
    repaint();
  }

  // MODE_SYMBOL: create Symbol
  if ( (mode == MODE_SYMBOL) && (!mouse1down) ) {
    endpoint = new DPoint;
    endpoint->set(e1->pos());
    if (highlightpoint != 0) {
      c->addSymbol(highlightpoint, symbolfile);  // add to existing point
    } else {
      c->addSymbol(endpoint, symbolfile);  // add at current mouse position
    }
    endpoint = NULL;
    repaint();
  }
}

void Render2D::mouseMoveEvent(QMouseEvent *e1) {
  moved = true;
  
  // Dragging
  if (dragging && mouse3down) {
    cout << "Start drag" << endl;
    // make pixmap of selection
    QRect savebox = selectionBox;
    int prevmode = mode;
    mode = MODE_SELECT;
    c->DeselectAll();
    repaint();
    QPixmap pm( savebox.size() );
    bitBlt( &pm, 0, 0, this, savebox.left(), savebox.top(), 
	    savebox.width(), savebox.height() );
    c->NewSelectRect(savebox, false);
    selectionBox = c->selectionBox();
    mode = prevmode;
    drag1 = new QImageDrag( pm.convertToImage(), this );
    drag1->dragCopy();
    dragging = false;
    repaint();
    setCursor(arrowCursor);
    mouse3down = false;
    return;
  }

  // MODE_TEXT: call special function to handle text events
  if (mode == MODE_TEXT) {
    DrawText_mouseMoveEvent(e1);
    prevpos = new DPoint;
    prevpos->x = e1->x(); prevpos->y = e1->y();
    return;
  }
  // MODE_SELECT: call special function to handle select events
  if (mode < 199) {
    Select_mouseMoveEvent(e1);
    prevpos = new DPoint;
    prevpos->x = e1->x(); prevpos->y = e1->y();
    return;
  }

  // MODE_TOOL_*: return
  if ((mode >= 600) && (mode < 699)) return;

  //bool update;
  double ang, len;
  QString sang, slen;
  DPoint *prevhighlight = highlightpoint;
  Drawable *prevhighlightobject = highlightobject;
  // Create DPoint of current pointer position
  DPoint *e = new DPoint;
  DPoint *np = 0;
  e->x = e1->x(); e->y = e1->y();
  double dist, distobj;
  // Get DPoint of nearest point
  np = c->FindNearestPoint(e, dist);
  // get Drawable of nearest object
  Drawable *no = c->FindNearestObject(e, distobj);

  // no buttons down
  if ( (mouse1down == false) && (mouse3down == false) ) {
    if (mode == MODE_DRAWARROW) return;

    // if (no != 0) {
      // highlight object if object close enough
      if ( (no != 0) && (mode == MODE_ERASE) && (distobj < 6.0) ) {
	highlightobject = no;
	if (prevhighlightobject != 0) prevhighlightobject->Highlight(false);
	highlightobject->Highlight(true);
	if (prevhighlightobject != highlightobject) repaint();
      }
      // unhighlight object if no object close
      if ( (mode == MODE_ERASE) && (no == 0 || distobj >= 6.0) ) {
	// Clear highlighted object
	highlightobject = 0;
	if (prevhighlightobject != 0) prevhighlightobject->Highlight(false);
	if (prevhighlightobject != highlightobject) repaint();
	return;
      }
      // highlight text object preferentially when MODE_SELECT...
      if ((no != 0) && (mode == MODE_SELECT) && (no->Type() == TYPE_TEXT) ) {
	highlightpoint = 0;
	highlightobject = no;
	if ( (prevhighlightobject != highlightobject) &&
	     (prevhighlightobject != 0) )
	  prevhighlightobject->Highlight(false);
	highlightobject->Highlight(true);
	if (prevhighlightobject != highlightobject) repaint();
	// return since no need to check points
	return;
      }
      // highlight object if object close enough and no point close
      if ((no != 0) && (mode == MODE_SELECT) && (distobj < 6.0) && (dist >= 8.0) ) {
	highlightobject = no;
	if (prevhighlightobject != 0) prevhighlightobject->Highlight(false);
	highlightobject->Highlight(true);
	if (prevhighlightobject != highlightobject) repaint();
	// return since no need to check points
	return;
      }
      // unhighlight object if no object close
      if ( (mode == MODE_SELECT) && (no == 0 || distobj >= 6.0) ) {
	// Clear highlighted object
	highlightobject = 0;
	if (prevhighlightobject != 0) prevhighlightobject->Highlight(false);
	if (prevhighlightobject != highlightobject) repaint();
	// don't return; go on to check for points
      }
      // unhighlight object if point close
      if ( (mode == MODE_SELECT) && (dist < 8.0) ) {
	// Clear highlighted object
	highlightobject = 0;
	if (prevhighlightobject != 0) prevhighlightobject->Highlight(false);
	if (prevhighlightobject != highlightobject) repaint();
	// don't return; go on to check for points
      }
    // }
    if (np != 0) {
      //cout << dist << " to (" << np->x << "," << np->y << ")" << endl;
      if ( (mode == MODE_TEXT) && (dist < 8.0) ) {
	highlightpoint = np;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_TEXT) && (dist >= 8.0) ) {
	// Clear highlighted point
	highlightpoint = 0;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_SYMBOL) && (dist < 8.0) ) {
	highlightpoint = np;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_SYMBOL) && (dist >= 8.0) ) {
	// Clear highlighted point
	highlightpoint = 0;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_SELECT) && (dist < 8.0) ) {
	highlightpoint = np;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_SELECT) && (dist >= 8.0) ) {
	// Clear highlighted point
	highlightpoint = 0;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_DRAWLINE) && (dist < 6.0) ) {
	highlightpoint = np;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_DRAWLINE) && (dist >= 6.0) ) {
	// Clear highlighted point
	highlightpoint = 0;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_DRAWLINE_DASH) && (dist < 6.0) ) {
	highlightpoint = np;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_DRAWLINE_DASH) && (dist >= 6.0) ) {
	// Clear highlighted point
	highlightpoint = 0;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_DRAWLINE_UP) && (dist < 6.0) ) {
	highlightpoint = np;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_DRAWLINE_UP) && (dist >= 6.0) ) {
	// Clear highlighted point
	highlightpoint = 0;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_DRAWLINE_DOWN) && (dist < 6.0) ) {
	highlightpoint = np;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
      if ( (mode == MODE_DRAWLINE_DOWN) && (dist >= 6.0) ) {
	// Clear highlighted point
	highlightpoint = 0;
	if (prevhighlight != highlightpoint) repaint();
	return;
      }
    }
    return; // rest of function only if mouse button down
  }

  // we're probably going to draw something...
  repaint();

  // MODE_DRAWLINE_DRAWING: draw temporary line
  if ( (mode == MODE_DRAWLINE_DRAWING) && mouse1down ) {
    // if within range of existing point AND the end point is not the same 
    // as the start point, snap to that point
    if ( (dist < 6.0) && (startpoint != np) ) {
      highlightpoint = np;
      endpoint = np;
    } else {
      endpoint = new DPoint;
      endpoint->x = e1->x();
      endpoint->y = e1->y();
      highlightpoint = 0;  // stop highlighting
      if (fixed_bond) { // fix endpoint if fixed set
	CorrectEndpoint_bond();
	np = c->FindNearestPoint(endpoint, dist);
	if (dist < 2.0) {
	  highlightpoint = np;
	  endpoint = np;
	}
      }
    }
    ang = Drawable::getAngle(startpoint, endpoint);
    len = startpoint->distanceTo(endpoint);
    sang.setNum(ang);
    slen.setNum(len);
    emit SignalSetStatusBar("Draw Line - Length = " + slen + ", Angle = " + 
			    sang);
    drawLine( startpoint->toQPoint(), endpoint->toQPoint(), thick, 
	      currentColor );
    return;
  }

  // MODE_DRAWLINE_DASH_DRAWING: draw temporary line
  if ( (mode == MODE_DRAWLINE_DASH_DRAWING) && mouse1down ) {
    // if within range of existing point AND the end point is not the same 
    // as the start point, snap to that point
    if ( (dist < 6.0) && (startpoint != np) ) {
      highlightpoint = np;
      endpoint = np;
    } else {
      endpoint = new DPoint;
      endpoint->x = e1->x();
      endpoint->y = e1->y();
      highlightpoint = 0;  // stop highlighting
      if (fixed_bond) { // fix endpoint if fixed set
	CorrectEndpoint_bond();
	np = c->FindNearestPoint(endpoint, dist);
	if (dist < 2.0) {
	  highlightpoint = np;
	  endpoint = np;
	}
      }
    }
    drawLine( startpoint->toQPoint(), endpoint->toQPoint(), 1, 
	      currentColor, 1 );
    return;
  }

  // MODE_DRAWLINE_UP_DRAWING: draw temporary line
  if ( (mode == MODE_DRAWLINE_UP_DRAWING) && mouse1down ) {
    // if within range of existing point AND the end point is not the same 
    // as the start point, snap to that point
    if ( (dist < 6.0) && (startpoint != np) ) {
      highlightpoint = np;
      endpoint = np;
    } else {
      endpoint = new DPoint;
      endpoint->x = e1->x();
      endpoint->y = e1->y();
      highlightpoint = 0;  // stop highlighting
      if (fixed_bond) { // fix endpoint if fixed set
	CorrectEndpoint_bond();
	np = c->FindNearestPoint(endpoint, dist);
	if (dist < 2.0) {
	  highlightpoint = np;
	  endpoint = np;
	}
      }
    }
    drawUpLine( startpoint->toQPoint(), endpoint->toQPoint(), QColor(0,0,0) );
    return;
  }
  // MODE_DRAWLINE_DOWN_DRAWING: draw temporary line
  if ( (mode == MODE_DRAWLINE_DOWN_DRAWING) && mouse1down ) {
    // if within range of existing point AND the end point is not the same 
    // as the start point, snap to that point
    if ( (dist < 6.0) && (startpoint != np) ) {
      highlightpoint = np;
      endpoint = np;
    } else {
      endpoint = new DPoint;
      endpoint->x = e1->x();
      endpoint->y = e1->y();
      highlightpoint = 0;  // stop highlighting
      if (fixed_bond) { // fix endpoint if fixed set
	CorrectEndpoint_bond();
	np = c->FindNearestPoint(endpoint, dist);
	if (dist < 2.0) {
	  highlightpoint = np;
	  endpoint = np;
	}
      }
    }
    drawDownLine(startpoint->toQPoint(), endpoint->toQPoint(), QColor(0,0,0));
    return;
  }

  // MODE_DRAWARROW_DRAWING: draw temporary line
  if ( (mode == MODE_DRAWARROW_DRAWING) && mouse1down ) {
    endpoint = new DPoint;
    endpoint->x = e1->x();
    endpoint->y = e1->y();
    if (fixed_arrow) { // fix endpoint if fixed set
      CorrectEndpoint_arrow();
    }
    drawArrow( startpoint->toQPoint(), endpoint->toQPoint(), QColor(0,0,0),
	       bracket_type );
    return;
  }

  // MODE_DRAWARROW_DRAWING: draw temporary line
  if ( (mode == MODE_DRAWCURVEARROW_DRAWING) && mouse1down ) {
    endpoint = new DPoint;
    endpoint->x = e1->x();
    endpoint->y = e1->y();
    // should not draw curve arrows fixed, even if selected
    //if (fixed) { // fix endpoint if fixed set
    //  CorrectEndpoint();
    //}
    drawCurveArrow( startpoint->toQPoint(), endpoint->toQPoint(), 
		    QColor(0,0,0), symbolfile );
    return;
  }

  // MODE_DRAWBRACKET_DRAWING: draw temporary bracket
  if ( (mode == MODE_DRAWBRACKET_DRAWING) && mouse1down ) {
    endpoint = new DPoint;
    endpoint->x = e1->x();
    endpoint->y = e1->y();
    drawBracket( startpoint->toQPoint(), endpoint->toQPoint(), QColor(0,0,0),
		 bracket_type );
    return;
  }
}

void Render2D::paintEvent(QPaintEvent *) { 
  // draw background
  //drawFillBox(QPoint(0,0), QPoint(renderWidth, renderHeight), bgcolor);
  // draw all data
  c->drawAll();
  // local/temporary draw operations
  if (highlightpoint != 0) {
    drawBox(QPoint(highlightpoint->x - 2, highlightpoint->y - 2),
	    QPoint(highlightpoint->x + 2, highlightpoint->y + 2),
	    QColor(0,0,0));
  }
  if ( (mode == MODE_TEXT) && (localtext != 0) ) localtext->EditRender();
  // if multiple selection, draw rotate and resize handles
  if ( (mode > 120) && (mode < 129) ) {
    // draw selection box
    drawBox(selectionBox.topLeft(), selectionBox.bottomRight(),
	    QColor(0,0,0));
    // draw rotate handle
    rotateHandle = selectionBox.topRight();
    drawFillBox( QPoint(rotateHandle.x() - 5, rotateHandle.y()),
		 QPoint(rotateHandle.x(), rotateHandle.y() + 5),
		 QColor(0,0,0) );
    // draw resize handle
    resizeHandle = selectionBox.bottomRight();
    drawFillBox( QPoint(resizeHandle.x() - 5, resizeHandle.y() - 5),
		 resizeHandle,
		 QColor(0,0,0) );
  }
  if (mode == 91) {
    // draw selection box
    drawBox(selectionBox.topLeft(), selectionBox.bottomRight(),
	    QColor(0,0,0));
    // draw rotate handle
    rotateHandle = selectionBox.topRight();
    drawFillBox( QPoint(rotateHandle.x() - 5, rotateHandle.y()),
		 QPoint(rotateHandle.x(), rotateHandle.y() + 5),
		 QColor(0,0,0) );
    // draw resize handle
    resizeHandle = selectionBox.bottomRight();
    drawFillBox( QPoint(resizeHandle.x() - 5, resizeHandle.y() - 5),
		 resizeHandle,
		 QColor(0,0,0) );
  }
}