File: bond.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 (199 lines) | stat: -rw-r--r-- 5,110 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
// bond.cpp - Bond's implementation of functions

#include <qobject.h>
#include <qrect.h>
#include <iostream.h>
#include <math.h>

#include "render2d.h"
#include "drawable.h"
#include "bond.h"
#include "defs.h"
#include "bondedit.h"

Bond::Bond(Render2D *r1, QObject *parent, const char *name)
  : Drawable(parent, name)
{
  r = r1;
  order = 1;
  dashed = 0;
  highlighted = false;
  thick = 1;
  wside = 0;
}

bool Bond::Equals(Bond *a) {
  if ( (start == a->Start()) && (end == a->End()) ) return true;
  if ( (start == a->End()) && (end == a->Start()) ) return true;
  return false;
}

bool Bond::WithinRect(QRect n, bool shiftdown) {
  if ( DPointInRect(start, n) && DPointInRect(end, n) )
    highlighted = true;
  else
    highlighted = false;
  return highlighted;
}

void Bond::Edit() {
  cout << "edit bond" << endl;
  BondEditDialog be(r, "bond editor", start, end, TYPE_BOND, order, dashed, thick, 0, color);
  if ( !be.exec() ) return;
  cout << "change" << endl;
  color = be.Color();
  order = be.Order();
  dashed = be.Dash();
  thick = be.Thick();
}

void Bond::Render()
{
  double myangle, x2, y2, rise, run, lx, ly, cf = 0.02;
  QColor c1;
  if (highlighted)
    c1 = QColor(255,0,0);
  else
    c1 = color;
  // for single bonds
  if (order == 1) {
    if (dashed > 0)
      r->drawLine(start->toQPoint(), end->toQPoint(), 1, c1, 1);
    else
      r->drawLine(start->toQPoint(), end->toQPoint(), thick, c1);
    return;
  }
  // for stereo_up bonds
  if (order == 5) {
    r->drawUpLine(start->toQPoint(), end->toQPoint(), c1);
    return;
  }
  // for stereo_down bonds
  if (order == 7) {
    r->drawDownLine(start->toQPoint(), end->toQPoint(), c1);
    return;
  }
  // for double and triple bonds
  myangle = getAngle(start, end);
  myangle += 90.0;
  myangle = myangle * (M_PI / 180.0);
  double offs = start->distanceTo(end) / 25.0;
  if (offs > 2.0) offs = 2.0;
  //x1 = (int)(cos(myangle) * 2.5 * offs);
  //y1 = (int)(sin(myangle) * 2.5 * offs);
  x2 = (int)(cos(myangle) * 3.0 * offs);
  y2 = (int)(sin(myangle) * 3.0 * offs);
  //QPoint sp1(start->x + x2, start->y + y2);
  //QPoint ep1(end->x + x2, end->y + y2); 
  //QPoint sp2(start->x - x2, start->y - y2); 
  //QPoint ep2(end->x - x2, end->y - y2);
  // shorten lines by removing from each end
  double bl = start->distanceTo(end);
  if (bl < 100.0) cf = 0.03;
  if (bl < 51.0) cf = 0.05;
  if (bl < 27.0) cf = 0.07;
  // find sp1 and ep1
  rise = (end->y + y2) - (start->y + y2);
  run = (end->x + x2) - (start->x + x2);
  lx = start->x + x2 + run * cf;
  ly = start->y + y2 + rise * cf;
  QPoint sp1((int)lx, (int)ly);
  lx = start->x + x2 + run * (1.0 - cf);
  ly = start->y + y2 + rise * (1.0 - cf);
  QPoint ep1((int)lx, (int)ly);
  // find sp2 and ep2
  rise = (end->y - y2) - (start->y - y2);
  run = (end->x - x2) - (start->x - x2);
  lx = start->x - x2 + run * cf;
  ly = start->y - y2 + rise * cf;
  QPoint sp2((int)lx, (int)ly);
  lx = start->x - x2 + run * (1.0 - cf);
  ly = start->y - y2 + rise * (1.0 - cf);
  QPoint ep2((int)lx, (int)ly);

  if (order == 2) {
    QPoint sp, ep;
    if (wside == 1) {
      sp = sp1;
      ep = ep1;
    } else {
      sp = sp2;
      ep = ep2;
    }
    if (dashed > 1)
      r->drawLine(start->toQPoint(), end->toQPoint(), 1, c1, 1);
    else 
      r->drawLine(start->toQPoint(), end->toQPoint(), 1, c1);
    if (dashed > 0)
      r->drawLine(sp, ep, 1, c1, 1);
    else
      r->drawLine(sp, ep, 1, c1);
    return;
  }
  if (order == 3) {
    if (dashed > 2)
      r->drawLine(start->toQPoint(), end->toQPoint(), 1, c1, 1);
    else 
      r->drawLine(start->toQPoint(), end->toQPoint(), 1, c1);
    if (dashed > 1)
      r->drawLine(sp1, ep1, 1, c1, 1);
    else
      r->drawLine(sp1, ep1, 1, c1);
    if (dashed > 0) 
      r->drawLine(sp2, ep2, 1, c1, 1);
    else
      r->drawLine(sp2, ep2, 1, c1);
    return;
  }
}

int Bond::Type(void)
{
  return TYPE_BOND;
}

bool Bond::Find(DPoint *target) {
  if (start == target) return true;
  if (end == target) return true;
  return false;
}

// You *did* call Find() first to make sure target is in this Bond...?
DPoint *Bond::otherPoint(DPoint *target) {
  if (target == start) return end;
  if (target == end) return start;
  return 0;
}

DPoint * Bond::FindNearestPoint(DPoint *target, double &dist) {
  if (target->distanceTo(start) < target->distanceTo(end)) {
    dist = target->distanceTo(start);
    return start;
  } else {
    dist = target->distanceTo(end);
    return end;
  }
}

Drawable * Bond::FindNearestObject(DPoint *target, double &dist) {
  dist = DistanceToLine(start, end, target);
  return this;
}

void Bond::setPoints(DPoint *s, DPoint *e) {
  start = s;
  end = e;
}

QRect Bond::BoundingBox() {
  if (highlighted == false)
    return QRect( QPoint(999,999), QPoint(0,0) ); 
  int top, bottom, left, right, swp;
  top = (int)start->y;
  left = (int)start->x;
  bottom = (int)end->y;
  right = (int)end->x;
  if (bottom < top) { swp = top; top = bottom; bottom = swp; }
  if (right < left) { swp = left; left = right; right = swp; }
  return QRect( QPoint(left,top), QPoint(right,bottom) );
}