File: symbol.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 (235 lines) | stat: -rw-r--r-- 6,142 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
// symbol.cpp - Symbol's implementation of functions

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

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

// XPM files defining symbols
#include "symbol_xpm.h"
// end symbol defs

Symbol::Symbol(Render2D *r1, QObject *parent, const char *name)
  : Drawable(parent, name)
{
  r = r1;
  highlighted = false;
  offset = QPoint(0,0);
  need_offset = false;
}

void Symbol::SetSymbol(QString n) { 
  which = n; 
  // load symbol
  if (which == "sym_plus") {
    originalRegular = QPixmap(sym_plus_xpm);
    rotateRegular = QPixmap(sym_plus_xpm);
    originalHighlighted = QPixmap(hl_sym_plus_xpm);
    rotateHighlighted = QPixmap(hl_sym_plus_xpm);
    return;
  }
  if (which == "sym_minus") {
    originalRegular = QPixmap(sym_minus_xpm);
    rotateRegular = QPixmap(sym_minus_xpm);
    originalHighlighted = QPixmap(hl_sym_minus_xpm);
    rotateHighlighted = QPixmap(hl_sym_minus_xpm);
    return;
  }
  if (which == "sym_delta_plus") {
    originalRegular = QPixmap(sym_delta_plus_xpm);
    rotateRegular = QPixmap(sym_delta_plus_xpm);
    originalHighlighted = QPixmap(hl_sym_delta_plus_xpm);
    rotateHighlighted = QPixmap(hl_sym_delta_plus_xpm);
    return;
  }
  if (which == "sym_delta_minus") {
    originalRegular = QPixmap(sym_delta_minus_xpm);
    rotateRegular = QPixmap(sym_delta_minus_xpm);
    originalHighlighted = QPixmap(hl_sym_delta_minus_xpm);
    rotateHighlighted = QPixmap(hl_sym_delta_minus_xpm);
    return;
  }
  if (which == "sym_2e") {
    originalRegular = QPixmap(sym_2e_xpm);
    rotateRegular = QPixmap(sym_2e_xpm);
    originalHighlighted = QPixmap(hl_sym_2e_xpm);
    rotateHighlighted = QPixmap(hl_sym_2e_xpm);
    return;
  }
  if (which == "sym_1e") {
    originalRegular = QPixmap(sym_1e_xpm);
    rotateRegular = QPixmap(sym_1e_xpm);
    originalHighlighted = QPixmap(hl_sym_1e_xpm);
    rotateHighlighted = QPixmap(hl_sym_1e_xpm);
    return;
  }
}

void Symbol::SetRotate(double d) {
  rotation = d; 
  // if text symbol, don't rotate
  if (which == QString("sym_delta_plus")) return;
  if (which == QString("sym_delta_minus")) return;
  if (which == QString("sym_minus")) return;
  // rotate symbol
  QWMatrix rm;
  rm.rotate(d);
  rotateRegular = originalRegular.xForm(rm);
  rotateHighlighted = originalHighlighted.xForm(rm);
}

QString Symbol::ToXML(QString xml_id) {
  QString s, n1;

  // begin symbol
  s.append("<symbol id=");
  s.append(xml_id);
  s.append(">\n");

  // write Start
  s.append("<Start>");
  n1.setNum(start->x);
  s.append(n1);
  s.append(" ");
  n1.setNum(start->y);
  s.append(n1);
  s.append("</Start>\n");

  // write symbol code
  s.append("<symtype>");
  s.append(which);
  s.append("</symtype>\n");

  // write color
  s.append("<color>");
  n1.setNum(color.red());
  s.append(n1);
  s.append(" ");
  n1.setNum(color.green());
  s.append(n1);
  s.append(" ");
  n1.setNum(color.blue());
  s.append(n1);
  s.append("</color>\n");

  // end symbol
  s.append("</symbol>\n");

  return s;
}

// set Symbol from XDrawChem-format XML
void Symbol::FromXML(QString xml_tag) {
  int i1, i2;

  i1 = xml_tag.find("<Start>");
  i2 = xml_tag.find("</Start>") + 8;
  SetStartFromXML(xml_tag.mid(i1, i2 - i1));
  i1 = xml_tag.find("<symtype>") + 9;
  i2 = xml_tag.find("</symtype>");
  SetSymbol(xml_tag.mid(i1, i2 - i1));
  i1 = xml_tag.find("<color>");
  i2 = xml_tag.find("</color>") + 8;
  SetColorFromXML(xml_tag.mid(i1, i2 - i1));
}

void Symbol::Render(void)
{
  QPoint p1 = start->toQPoint();
  p1.setX(p1.x() + offset.x() - 8);
  p1.setY(p1.y() + offset.y() - 8);
  if (highlighted)
  r->drawPixmap(p1, rotateHighlighted);
  else
  r->drawPixmap(p1, rotateRegular);
}

void Symbol::Edit() {
  int lsty;
  if (which == "sym_plus") lsty = SYM_PLUS;
  if (which == "sym_minus") lsty = SYM_MINUS;
  if (which == "sym_delta_plus") lsty = SYM_DELTA_PLUS;
  if (which == "sym_minus") lsty = SYM_DELTA_MINUS;
  if (which == "sym_2e") lsty = SYM_2E;
  if (which == "sym_1e") lsty = SYM_1E;
  BondEditDialog be(r, "symbol editor", start, end, TYPE_SYMBOL, 0, 0, 0, 
		    lsty, color);
  if ( !be.exec() ) return;
  cout << "change" << endl;
  lsty = be.Style();
  if (lsty == SYM_PLUS) SetSymbol("sym_plus");
  if (lsty == SYM_MINUS) SetSymbol("sym_minus");
  if (lsty == SYM_DELTA_PLUS) SetSymbol("sym_delta_plus");
  if (lsty == SYM_DELTA_MINUS) SetSymbol("sym_delta_minus");
  if (lsty == SYM_2E) SetSymbol("sym_2e");
  if (lsty == SYM_1E) SetSymbol("sym_1e");
}

int Symbol::Type(void)
{
  return TYPE_SYMBOL;
}

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

// Do not allow connections to this object.
// Simplest way to do this, I think, is to disallow this function
DPoint * Symbol::FindNearestPoint(DPoint *target, double &dist) {
  dist = 99999.0;
  return 0;
}

Drawable * Symbol::FindNearestObject(DPoint *target, double &dist) {
  if (WithinBounds(target))
    dist = 0.01;
  else
    dist = 99999.0;
  return this;
}

bool Symbol::WithinBounds(DPoint *target) {
  tmp_pt = new DPoint(start->x, start->y);
  tmp_pt->x = tmp_pt->x + offset.x();
  tmp_pt->y = tmp_pt->y + offset.y();
  if (tmp_pt->distanceTo(target) < 8.0)
    return true;
  else
    return false;
}

void Symbol::setPoint(DPoint *s) {
  start = s;
}

QRect Symbol::BoundingBox() {
  if (highlighted == false)
    return QRect( QPoint(999,999), QPoint(0,0) ); 
  int top, bottom, left, right, swp;
  top = (int)start->y + offset.y() - 8;
  left = (int)start->x + offset.x() - 8;
  bottom = (int)start->y + offset.y() + 8;
  right = (int)start->x + offset.x() + 8;
  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) );
}

bool Symbol::WithinRect(QRect n, bool shiftdown) {
  tmp_pt = new DPoint(start->x, start->y);
  tmp_pt->x = tmp_pt->x + offset.x();
  tmp_pt->y = tmp_pt->y + offset.y();
  if (n.contains(tmp_pt->toQPoint(), true))
    highlighted = true;
  else
    highlighted = false;
  return highlighted;
}