File: render2d_select.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 (220 lines) | stat: -rw-r--r-- 6,753 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
#include <iostream.h>
#include <math.h>
#include <qwidget.h>
#include <qpainter.h>

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

void Render2D::Select_mouseMoveEvent(QMouseEvent *e1) {
  //bool update;
  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, dx, dy;
  // Get DPoint of nearest point
  np = c->FindNearestPoint(e, dist);
  // get Drawable of nearest object
  Drawable *no = c->FindNearestObject(e, distobj);

  //cout << "np = " << dist << " no = " << distobj;
  //if (np == 0) cout << " (np = 0)";
  //cout << endl;
  // no buttons down
  if ( (mouse1down == false) && (mouse3down == false) ) {
    if (mode == MODE_SELECT_MULTIPLE_SELECTED) {
      // check if near rotate or resize handle
      if (DistanceBetween(resizeHandle, e1->pos()) < 4.0) {
	setCursor(sizeFDiagCursor);
	mode = MODE_SELECT_MULTIPLE_RESIZE;
	return;
      }
      if (DistanceBetween(rotateHandle, e1->pos()) < 4.0) {
	setCursor(rotatecursor);
	mode = MODE_SELECT_MULTIPLE_ROTATE;
	return;
      }
      // else, check if in selection box (for move)
      if (selectionBox.contains( e1->pos() ))
	setCursor(sizeAllCursor);
      else
	setCursor(arrowCursor);
      return;
    }
    if (mode == MODE_SELECT_MULTIPLE_RESIZE) {
      if (DistanceBetween(resizeHandle, e1->pos()) > 4.0) {
	mode = MODE_SELECT_MULTIPLE_SELECTED;
	if (selectionBox.contains( e1->pos() ))
	  setCursor(sizeAllCursor);
	else
	  setCursor(arrowCursor);
	return;
      }
      return; // do nothing if still on resize handle
    }
    if (mode == MODE_SELECT_MULTIPLE_ROTATE) {
      if (DistanceBetween(rotateHandle, e1->pos()) > 4.0) {
	mode = MODE_SELECT_MULTIPLE_SELECTED;
	if (selectionBox.contains( e1->pos() ))
	  setCursor(sizeAllCursor);
	else
	  setCursor(arrowCursor);
	return;
      }
      return; // do nothing if still on resize handle
    }
    if (no != 0) {
      // highlight text and symbol objects preferentially when MODE_SELECT...
      if ( (no->Type() == TYPE_TEXT) || (no->Type() == TYPE_SYMBOL) ){
	highlightpoint = 0;
	highlightobject = no;
	if ( (prevhighlightobject != highlightobject) &&
	     (prevhighlightobject != 0) )
	  prevhighlightobject->Highlight(false);
	highlightobject->Highlight(true);
	if (prevhighlightobject != highlightobject) repaint();
	setCursor(sizeAllCursor);
	// return since no need to check points
	return;
      }
      // highlight object if object close enough and no point close
      if ( (distobj < 6.0) && (dist >= 8.0) ) {
	highlightpoint = 0;
	highlightobject = no;
	if (prevhighlightobject != 0) prevhighlightobject->Highlight(false);
	highlightobject->Highlight(true);
	if (prevhighlightobject != highlightobject) repaint();
	setCursor(sizeAllCursor);
	// return since no need to check points
	return;
      }
      // unhighlight object if no object close
      if (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 (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) {
      highlightpoint = 0;
      highlightobject = 0;
      if (prevhighlightobject != 0) {
	prevhighlightobject->Highlight(false);
	repaint();
      }
      setCursor(arrowCursor);
      return;
    }
    if (np != 0) {
      //cout << dist << " to (" << np->x << "," << np->y << ")" << endl;
      if (dist < 8.0) {
	highlightpoint = np;
	if (prevhighlight != highlightpoint) repaint();
	setCursor(sizeAllCursor);
	return;
      } else {
	// Clear object, if any
	if (prevhighlightobject != 0) {
	  prevhighlightobject->Highlight(false);
	  highlightobject = 0;
	  repaint();
	}
	// Clear highlighted point
	highlightpoint = 0;
	if (prevhighlight != highlightpoint) repaint();
	setCursor(arrowCursor);
	return;
      }
    }
    return; // rest of function only if mouse button down
  }

  if ( ( mode == MODE_SELECT_MULTIPLE) && (mouse1down) ) {
    drawBox(selectOrigin, selectPrev, QColor(255,255,255));
    selectPrev = e1->pos();
    c->NewSelectRect(QRect(selectOrigin, selectPrev), false);
    repaint();
    drawBox(selectOrigin, selectPrev, QColor(0,0,0));
  }

  if ( (mode == MODE_SELECT_RESIZE_MULTIPLE) && (mouse1down) ) {
    //double dh, dw;
    DPoint *dp1 = new DPoint( selectionBox.topLeft() );
    // disallow moves to the left or above selection box
    if ( (e->x < (dp1->x + 2.0) ) || (e->y < (dp1->y + 2.0) ) ) {
      cout << "Warning!" << endl;
      QPoint gin((int)prevpos->x, (int)prevpos->y), gout;
      gout = mapToGlobal(gin);
      cursor().setPos(gout);
      return;
    }
    dx = dp1->distanceTo(e);
    dy = dp1->distanceTo(prevpos);
    //cout << "resize factor " << dx / dy << endl;
    c->Resize(dp1, dx / dy);
    selectionBox = c->selectionBox();
    repaint();
  }

  if ( (mode == MODE_SELECT_ROTATE_MULTIPLE) && (mouse1down) ) {
    DPoint *dp1 = new DPoint( rotateOrigin );
    double da = getAngle( rotateOrigin, e1->pos() );
    double db = prevangle - da;
    if (db < -300.0) db += 360.0;
    if (db > 300.0) db -= 360.0;
    db = db * M_PI / 180.0;
    //cout << db << endl;
    prevangle = da;
    c->Rotate(dp1, db);
    selectionBox = c->selectionBox();
    repaint();
  }

  if ( (mode == MODE_SELECT_MOVE_MULTIPLE) && (mouse1down) ) {
    dx = e->x - prevpos->x; dy = e->y - prevpos->y;
    //cout << "Move by " << dx << "," << dy << endl;
    c->Move(dx, dy);
    selectionBox.moveBy((int)dx, (int)dy);
    repaint();
  }

  if ( (mode == MODE_SELECT_MOVE_OBJECT) && (mouse1down) ) {
    dx = e->x - prevpos->x; dy = e->y - prevpos->y;
    //cout << "Move by " << dx << "," << dy << endl;
    c->Move(dx, dy);
    selectionBox.moveBy((int)dx, (int)dy);
    repaint();
  }

  if ( (mode == MODE_SELECT_MOVE_POINT) && (mouse1down) ) {
    dx = e->x - prevpos->x; dy = e->y - prevpos->y;
    //cout << "Move by " << dx << "," << dy << endl;
    highlightpoint->x += dx;
    highlightpoint->y += dy;
    repaint();
  }
}

// Get angle between -BA> and -BD> (Law of Cosines)
double Render2D::GetAngle(DPoint *a, DPoint *b, DPoint *d) {
  double a1 = b->distanceTo(a);
  double a2 = b->distanceTo(d);
  double a3 = a->distanceTo(d);
  double cos1 = (a2*a2 + a1*a1 - a3*a3) / (2*a1*a2);
  return acos(cos1);
}