File: chemdata.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 (358 lines) | stat: -rw-r--r-- 10,011 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
#include <iostream.h>
#include <qobject.h>
#include <qlist.h>

#include "drawable.h"
#include "molecule.h"
#include "bond.h"
#include "arrow.h"
#include "curvearrow.h"
#include "bracket.h"
#include "text.h"
#include "symbol.h"
#include "chemdata.h"
#include "defs.h"

ChemData::ChemData(Clipboard *clipin, QObject *parent, const char *name)
  : QObject(parent, name)
{
  clip = clipin;
}

void ChemData::drawAll() {
  // draw all objects in ChemData
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) {
    tmp_draw->Render();
  }
}

void ChemData::addArrow(DPoint *s, DPoint *e, QColor c, int t, bool hl) {
  Arrow *a1 = new Arrow(r);
  a1->setPoints(s, e);
  a1->SetColor(c);
  a1->SetStyle(t);
  if (hl) a1->Highlight(true);
  drawlist.append(a1);
}

void ChemData::addCurveArrow(DPoint *s, DPoint *e, QColor c, QString s1,
			     bool hl) {
  CurveArrow *a1 = new CurveArrow(r);
  a1->setPoints(s, e);
  a1->SetColor(c);
  a1->SetCurve(s1);
  if (hl) a1->Highlight(true);
  drawlist.append(a1);
}

void ChemData::addBracket(DPoint *s, DPoint *e, QColor c, int type, bool hl) {
  Bracket *a1 = new Bracket(r);
  a1->setPoints(s, e);
  a1->SetColor(c);
  a1->SetStyle(type);
  if (hl) a1->Highlight(true);
  drawlist.append(a1);
}

void ChemData::addText(Text *t) {
  if (t->Justify() == JUSTIFY_TOPLEFT) { // add to drawing
    drawlist.append(t);
  } else { // add label to specific Molecule
    for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
	 tmp_draw = drawlist.next()) {
      if (tmp_draw->Find(t->Start()) == true) {
	Molecule *tm = (Molecule *)tmp_draw;  // this is cheating, I know!
	tm->addText(t);
	return;
      }
    }
  }
}

void ChemData::addBond(DPoint *s, DPoint *e, int thick, int order, 
		       QColor c, bool hl) {
  //cout << "Request to add bond:" << endl;
  //cout << "(" << s->x << "," << s->y << ")-(" << e->x << "," << e->y << ")";
  //cout << endl;
  Drawable *m1 = 0, *m2 = 0;
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) {
    if (tmp_draw->Find(s) == true) m1 = tmp_draw;
    if (tmp_draw->Find(e) == true) m2 = tmp_draw;
  }
  // neither point exists -- create new Molecule
  if ( (m1 == 0) && (m2 == 0) ) {
    Molecule *m = new Molecule(r);
    m->SetChemdata(this);
    m->addBond(s, e, thick, order, c, hl);
    drawlist.append(m);
    return;
  }
  // one point exists, or both in same molecule
  if ( (m1 == 0) && (m2 != 0) ) {
    m1 = m2; m2 = 0;
  }
  if ( ( (m1 != 0) && (m2 == 0) ) || (m1 == m2) ) {
    m1->addBond(s, e, thick, order, c, hl);
    return;
  }
  // both points exist in different molecules
  if (m1 != m2) {
    m1->addBond(s, e, thick, order, c, hl);
    m1->addMolecule(m2);
    drawlist.remove(m2);
    delete m2;
  }
}

void ChemData::addSymbol(DPoint *a, QString symbolfile) {
  Molecule *m1;
  Symbol *s1 = new Symbol(r);
  s1->setPoint(a);
  s1->SetSymbol(symbolfile);
  // determine whether point exists or not; if exists, add to Molecule
  for (tmp_draw = drawlist.first(); tmp_draw != NULL;
       tmp_draw = drawlist.next()) {
    if ( (tmp_draw->Find(a) == true) && (tmp_draw->Type() == TYPE_MOLECULE) ) {
      m1 = (Molecule *)tmp_draw;
      m1->addSymbol(s1);
      return;
    }
  }
  drawlist.append(s1);
}

DPoint * ChemData::FindNearestPoint(DPoint *target, double &dist) {
  DPoint *nearest = 0, *d1;
  double mindist = 9999.0, d1dist = 999999.0;
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) {
    d1 = tmp_draw->FindNearestPoint(target, d1dist);
    if (d1dist < mindist) { mindist = d1dist; nearest = d1; }
  }
  dist = mindist;
  return nearest;
}

Drawable * ChemData::FindNearestObject(DPoint *target, double &dist) {
  Drawable *nearest = 0, *d1;
  double mindist = 2000.0, d1dist = 999999.0;
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) {
    d1 = tmp_draw->FindNearestObject(target, d1dist);
    if (d1dist < mindist) { mindist = d1dist; nearest = d1; }
  }
  dist = mindist;
  return nearest;
}

void ChemData::Erase(Drawable *d) {
  QList<Drawable> removelist;
  QList<Molecule> split_list;

  /*
  if (d->Type() == TYPE_TEXT) {
    Text *tmp_text = (Text *)d;
    if (tmp_text->getDataType() == TEXT_DATA_MW) {
      tmp_text->getMolecule()->MWLabelDeleted();
    }
    if (tmp_text->getDataType() == TEXT_DATA_FORMULA) {
      tmp_text->getMolecule()->FormulaLabelDeleted();
    }
  }
  */

  if (drawlist.remove(d) == false) {
    for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
	 tmp_draw = drawlist.next()) {
      tmp_draw->Erase(d);
      // collect empty Molecules for removal
      if (tmp_draw->Members() == 0) removelist.append(tmp_draw);
    }
  } else { // drawlist.remove(d) == true
    delete d;
  }
  // remove empty Molecules
  for (tmp_draw = removelist.first(); tmp_draw != NULL; 
       tmp_draw = removelist.next() ) {
    drawlist.remove(tmp_draw);
    delete tmp_draw;
  }
  // Split Molecules as needed
  DetectSplit();
}

void ChemData::EraseSelected() {
  Molecule *m;
  QList<Drawable> removelist;

  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) {
    if (tmp_draw->Type() == TYPE_MOLECULE) {
      m = (Molecule *)tmp_draw;
      m->EraseSelected();
      // collect empty Molecules for removal
      if (tmp_draw->Members() == 0) removelist.append(tmp_draw);
    } else {
      if (tmp_draw->Highlighted() == true) {
	removelist.append( tmp_draw );
      }
    }
  }
  for (tmp_draw = removelist.first(); tmp_draw != NULL; 
       tmp_draw = removelist.next() ) {
    /*
    if (tmp_draw->Type() == TYPE_TEXT) {
      Text *tmp_text = (Text *)tmp_draw;
      if (tmp_text->getDataType() == TEXT_DATA_MW) {
	tmp_text->getMolecule()->MWLabelDeleted();
      }
      if (tmp_text->getDataType() == TEXT_DATA_FORMULA) {
	tmp_text->getMolecule()->FormulaLabelDeleted();
      }
    }
    */
    drawlist.remove(tmp_draw);
    delete tmp_draw;
  }
  // Split Molecules as needed
  DetectSplit();
}

// Split Molecule's which hold multiple structures (e.g. after delete)
void ChemData::DetectSplit() {
  QList<Drawable> removelist;
  QList<Molecule> split_list;
  Molecule *tmp_mol;
  Drawable *td2;

  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) {
    if (tmp_draw->Type() == TYPE_MOLECULE) {
      tmp_mol = (Molecule *)tmp_draw;
      split_list = tmp_mol->MakeSplit();
      if (split_list.count() > 1) {
	cout << "Split needed" << endl;
	removelist.append(tmp_draw);
	for (td2 = split_list.first(); td2 != NULL; td2 = split_list.next()) {
	  drawlist.append(td2);
	}
	split_list.clear();
      }
    }
  }
  // remove old Molecules
  for (tmp_draw = removelist.first(); tmp_draw != NULL; 
       tmp_draw = removelist.next() ) {
    drawlist.remove(tmp_draw);
    delete tmp_draw;
  }
}

void ChemData::SelectAll(void) {
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) 
    tmp_draw->SelectAll();
}

void ChemData::DeselectAll(void) {
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) 
    tmp_draw->DeselectAll();
}

void ChemData::SetColorIfHighlighted(QColor c) {
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) 
    tmp_draw->SetColorIfHighlighted(c);
}

void ChemData::Move(double dx, double dy) {
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) 
    tmp_draw->Move(dx, dy);
}

void ChemData::Resize(DPoint *d1, double dy) {
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) 
    tmp_draw->Resize(d1, dy);
}

void ChemData::Rotate(DPoint *d1, double dy) {
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) 
    tmp_draw->Rotate(d1, dy);
}

void ChemData::Flip(DPoint *d1, int dy) {
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) 
    tmp_draw->Flip(d1, dy);
}

// Find minimum rectangle needed to enclose selection
QRect ChemData::selectionBox() {
  int top = 99999, bottom = 0, left = 99999, right = 0;
  QRect tmprect;

  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) {
    tmprect = tmp_draw->BoundingBox();
    cout << tmprect.width() << "X" << tmprect.height() << endl;
    if (tmprect.isValid()) {
      if (tmprect.left() < left) left = tmprect.left();
      if (tmprect.right() > right) right = tmprect.right();
      if (tmprect.top() < top) top = tmprect.top();
      if (tmprect.bottom() > bottom) bottom = tmprect.bottom();
    }
  }

  left -= 3; right += 5; top -= 3; bottom += 3;
  if (left < 0) left = 0;
  if (top < 0) top = 0;
  return QRect( QPoint(left,top), QPoint(right,bottom) );
}

// when doing multiple selection via MODE_SELECT_MULTIPLE, we will
// have to highlight/unhighlight regions of the drawing as the selection
// box changes.  This function is called to start checking whether objects
// fall within the select box.
void ChemData::NewSelectRect(QRect n, bool shiftdown) {
  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) {
    tmp_draw->WithinRect(n, shiftdown);
  }
}

// Get list of unique points contained in all Molecules.
QList<DPoint> ChemData::UniquePoints() {
  QList<DPoint> up, tp;

  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) {
    tp = tmp_draw->AllPoints();
    for (tmp_pt = tp.first(); tmp_pt != NULL; tmp_pt = tp.next() )
      up.append(tmp_pt);
  }

  cout << up.count() << endl;
  return up;
}

QList<Drawable> ChemData::UniqueObjects() {
  QList<Drawable> uo, to;
  Drawable *td2;

  for (tmp_draw = drawlist.first(); tmp_draw != NULL; 
       tmp_draw = drawlist.next()) {
    to = tmp_draw->AllObjects();
    for (td2 = to.first(); td2 != NULL; td2 = to.next() )
      uo.append(td2);
  }

  cout << uo.count() << endl;
  return uo;
}