My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ellipse.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ellipse.cpp
3  -------------
4  begin : Sun Nov 23 2003
5  copyright : (C) 2003 by Michael Margraf
6  email : michael.margraf@alumni.tu-berlin.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "ellipse.h"
19 #include "filldialog.h"
20 
21 #include <qpushbutton.h>
22 #include <qlineedit.h>
23 #include <qcombobox.h>
24 #include <qcheckbox.h>
25 
26 
27 Ellipse::Ellipse(bool _filled)
28 {
29  Name = "Ellipse ";
30  isSelected = false;
31  Pen = QPen(QColor());
32  Brush = QBrush(QPen::lightGray);
33  filled = _filled;
34  cx = cy = 0;
35  x1 = x2 = 0;
36  y1 = y2 = 0;
37 }
38 
40 {
41 }
42 
43 // --------------------------------------------------------------------------
45 {
46  if(isSelected) {
47  p->Painter->setPen(QPen(QPen::darkGray,Pen.width()+5));
48  if(filled) p->Painter->setBrush(Brush);
49  p->drawEllipse(cx, cy, x2, y2);
50  p->Painter->setPen(QPen(QPen::white, Pen.width(), Pen.style()));
51  p->Painter->setBrush(QBrush::NoBrush);
52  p->drawEllipse(cx, cy, x2, y2);
53 
54  p->Painter->setPen(QPen(QPen::darkRed,2));
55  p->drawResizeRect(cx, cy+y2); // markers for changing the size
56  p->drawResizeRect(cx, cy);
57  p->drawResizeRect(cx+x2, cy+y2);
58  p->drawResizeRect(cx+x2, cy);
59  return;
60  }
61  p->Painter->setPen(Pen);
62  if(filled) p->Painter->setBrush(Brush);
63  p->drawEllipse(cx, cy, x2, y2);
64  p->Painter->setBrush(QBrush::NoBrush); // no filling for the next paintings
65 }
66 
67 // --------------------------------------------------------------------------
68 void Ellipse::paintScheme(QPainter *p)
69 {
70  p->drawEllipse(cx, cy, x2, y2);
71 }
72 
73 // --------------------------------------------------------------------------
74 void Ellipse::getCenter(int& x, int &y)
75 {
76  x = cx+(x2>>1);
77  y = cy+(y2>>1);
78 }
79 
80 // --------------------------------------------------------------------------
81 // Sets the center of the painting to x/y.
82 void Ellipse::setCenter(int x, int y, bool relative)
83 {
84  if(relative) { cx += x; cy += y; }
85  else { cx = x-(x2>>1); cy = y-(y2>>1); }
86 }
87 
88 // --------------------------------------------------------------------------
90 {
91  return new Ellipse();
92 }
93 
94 // --------------------------------------------------------------------------
95 Element* Ellipse::info(QString& Name, char* &BitmapFile, bool getNewOne)
96 {
97  Name = QObject::tr("Ellipse");
98  BitmapFile = (char *) "ellipse";
99 
100  if(getNewOne) return new Ellipse();
101  return 0;
102 }
103 
104 // --------------------------------------------------------------------------
105 Element* Ellipse::info_filled(QString& Name, char* &BitmapFile, bool getNewOne)
106 {
107  Name = QObject::tr("filled Ellipse");
108  BitmapFile = (char *) "filledellipse";
109 
110  if(getNewOne) return new Ellipse(true);
111  return 0;
112 }
113 
114 // --------------------------------------------------------------------------
115 bool Ellipse::load(const QString& s)
116 {
117  bool ok;
118 
119  QString n;
120  n = s.section(' ',1,1); // cx
121  cx = n.toInt(&ok);
122  if(!ok) return false;
123 
124  n = s.section(' ',2,2); // cy
125  cy = n.toInt(&ok);
126  if(!ok) return false;
127 
128  n = s.section(' ',3,3); // x2
129  x2 = n.toInt(&ok);
130  if(!ok) return false;
131 
132  n = s.section(' ',4,4); // y2
133  y2 = n.toInt(&ok);
134  if(!ok) return false;
135 
136  n = s.section(' ',5,5); // color
137  QColor co;
138  co.setNamedColor(n);
139  Pen.setColor(co);
140  if(!Pen.color().isValid()) return false;
141 
142  n = s.section(' ',6,6); // thickness
143  Pen.setWidth(n.toInt(&ok));
144  if(!ok) return false;
145 
146  n = s.section(' ',7,7); // line style
147  Pen.setStyle((Qt::PenStyle)n.toInt(&ok));
148  if(!ok) return false;
149 
150  n = s.section(' ',8,8); // fill color
151  co.setNamedColor(n);
152  Brush.setColor(co);
153  if(!Brush.color().isValid()) return false;
154 
155  n = s.section(' ',9,9); // fill style
156  Brush.setStyle((Qt::BrushStyle)n.toInt(&ok));
157  if(!ok) return false;
158 
159  n = s.section(' ',10,10); // filled
160  if(n.toInt(&ok) == 0) filled = false;
161  else filled = true;
162  if(!ok) return false;
163 
164  return true;
165 }
166 
167 // --------------------------------------------------------------------------
168 QString Ellipse::save()
169 {
170  QString s = Name +
171  QString::number(cx) + " " + QString::number(cy) + " " +
172  QString::number(x2) + " " + QString::number(y2) + " " +
173  Pen.color().name() + " " + QString::number(Pen.width()) + " " +
174  QString::number(Pen.style()) + " " +
175  Brush.color().name() + " " + QString::number(Brush.style());
176  if(filled) s += " 1";
177  else s += " 0";
178  return s;
179 }
180 
181 // --------------------------------------------------------------------------
183 {
184  QString b = filled ?
185  QString (", QBrush (QColor (\"%1\"), %2)").
186  arg(Brush.color().name()).arg(toBrushString(Brush.style())) : "";
187  QString s =
188  QString ("new Area (%1, %2, %3, %4, "
189  "QPen (QColor (\"%5\"), %6, %7)%8)").
190  arg(cx).arg(cy).arg(x2).arg(y2).
191  arg(Pen.color().name()).arg(Pen.width()).arg(toPenString(Pen.style())).
192  arg(b);
193  s = "Ellips.append (" + s + ");";
194  return s;
195 }
196 
197 // --------------------------------------------------------------------------
198 // Checks if the resize area was clicked.
199 bool Ellipse::resizeTouched(float fX, float fY, float len)
200 {
201  float fCX = float(cx), fCY = float(cy);
202  float fX2 = float(cx+x2), fY2 = float(cy+y2);
203 
204  State = -1;
205  if(fX < fCX-len) return false;
206  if(fY < fCY-len) return false;
207  if(fX > fX2+len) return false;
208  if(fY > fY2+len) return false;
209 
210  State = 0;
211  if(fX < fCX+len) State = 1;
212  else if(fX <= fX2-len) { State = -1; return false; }
213  if(fY < fCY+len) State |= 2;
214  else if(fY <= fY2-len) { State = -1; return false; }
215 
216  return true;
217 }
218 
219 // --------------------------------------------------------------------------
220 // Mouse move action during resize.
221 void Ellipse::MouseResizeMoving(int x, int y, QPainter *p)
222 {
223  paintScheme(p); // erase old painting
224  switch(State) {
225  case 0: x2 = x-cx; y2 = y-cy; // lower right corner
226  break;
227  case 1: x2 -= x-cx; cx = x; y2 = y-cy; // lower left corner
228  break;
229  case 2: x2 = x-cx; y2 -= y-cy; cy = y; // upper right corner
230  break;
231  case 3: x2 -= x-cx; cx = x; y2 -= y-cy; cy = y; // upper left corner
232  break;
233  }
234  if(x2 < 0) { State ^= 1; x2 *= -1; cx -= x2; }
235  if(y2 < 0) { State ^= 2; y2 *= -1; cy -= y2; }
236 
237  paintScheme(p); // paint new painting
238 }
239 
240 // --------------------------------------------------------------------------
241 // fx/fy are the precise coordinates, gx/gy are the coordinates set on grid.
242 // x/y are coordinates without scaling.
244  QPainter *paintScale, int, int, int gx, int gy,
245  QPainter *p, int x, int y, bool drawn)
246 {
247  if(State > 0) {
248  if(State > 1)
249  paintScale->drawEllipse(x1, y1, x2-x1, y2-y1); // erase old painting
250  State++;
251  x2 = gx;
252  y2 = gy;
253  paintScale->drawEllipse(x1, y1, x2-x1, y2-y1); // paint new painting
254  }
255  else { x2 = gx; y2 = gy; }
256 
257 
258  p->setPen(Qt::SolidLine);
259  if(drawn) {
260  p->drawEllipse(cx+13, cy, 18, 12); // erase old cursor symbol
261  if(filled) {
262  p->drawLine(cx+14, cy+7, cx+20, cy+1);
263  p->drawLine(cx+25, cy+2, cx+18, cy+9);
264  p->drawLine(cx+29, cy+4, cx+23, cy+10);
265  }
266  }
267  cx = x;
268  cy = y;
269  p->drawEllipse(cx+13, cy, 18, 12); // paint new cursor symbol
270  if(filled) {
271  p->drawLine(cx+14, cy+7, cx+20, cy+1);
272  p->drawLine(cx+25, cy+2, cx+18, cy+9);
273  p->drawLine(cx+29, cy+4, cx+23, cy+10);
274  }
275 }
276 
277 // --------------------------------------------------------------------------
279 {
280  State++;
281  if(State == 1) {
282  x1 = x2;
283  y1 = y2; // first corner is determined
284  }
285  else {
286  if(x1 < x2) { cx = x1; x2 = x2-x1; } // cx/cy to upper left corner
287  else { cx = x2; x2 = x1-x2; }
288  if(y1 < y2) { cy = y1; y2 = y2-y1; }
289  else { cy = y2; y2 = y1-y2; }
290  x1 = y1 = 0;
291  State = 0;
292  return true; // painting is ready
293  }
294  return false;
295 }
296 
297 // --------------------------------------------------------------------------
298 // Checks if the coordinates x/y point to the painting.
299 bool Ellipse::getSelected(float fX, float fY, float w)
300 {
301  float fX2 = float(x2);
302  float fY2 = float(y2);
303  fX -= float(cx) + fX2/2.0;
304  fY -= float(cy) + fY2/2.0;
305 
306  if(filled) {
307  float a = 2.0 * fX / fX2; a *= a;
308  float b = 2.0 * fY / fY2; b *= b;
309 
310  if(a+b > 1.0)
311  return false;
312  }
313  else {
314  float a1 = fX / (fX2/2.0 - w); a1 *= a1;
315  float a2 = fX / (fX2/2.0 + w); a2 *= a2;
316  float b1 = fY / (fY2/2.0 - w); b1 *= b1;
317  float b2 = fY / (fY2/2.0 + w); b2 *= b2;
318 
319  if(a1+b1 < 1.0) return false;
320  if(a2+b2 > 1.0) return false;
321  }
322 
323  return true;
324 }
325 
326 // --------------------------------------------------------------------------
327 // Rotates around the center.
329 {
330  cy += (y2-x2) >> 1;
331  cx += (x2-y2) >> 1;
332  int tmp = x2;
333  x2 = y2;
334  y2 = tmp;
335 }
336 
337 // --------------------------------------------------------------------------
338 // Mirrors about center line.
340 {
341  // nothing to do
342 }
343 
344 // --------------------------------------------------------------------------
345 // Mirrors about center line.
347 {
348  // nothing to do
349 }
350 
351 // --------------------------------------------------------------------------
352 // Calls the property dialog for the painting and changes them accordingly.
353 // If there were changes, it returns 'true'.
355 {
356  bool changed = false;
357 
358  FillDialog *d = new FillDialog(QObject::tr("Edit Ellipse Properties"));
359  d->ColorButt->setPaletteBackgroundColor(Pen.color());
360  d->LineWidth->setText(QString::number(Pen.width()));
361  d->StyleBox->setCurrentItem(Pen.style()-1);
362  d->FillColorButt->setPaletteBackgroundColor(Brush.color());
363  d->FillStyleBox->setCurrentItem(Brush.style());
364  d->CheckFilled->setChecked(filled);
366 
367  if(d->exec() == QDialog::Rejected) {
368  delete d;
369  return false;
370  }
371 
372  if(Pen.color() != d->ColorButt->paletteBackgroundColor()) {
373  Pen.setColor(d->ColorButt->paletteBackgroundColor());
374  changed = true;
375  }
376  if(Pen.width() != d->LineWidth->text().toUInt()) {
377  Pen.setWidth(d->LineWidth->text().toUInt());
378  changed = true;
379  }
380  if(Pen.style() != (Qt::PenStyle)(d->StyleBox->currentItem()+1)) {
381  Pen.setStyle((Qt::PenStyle)(d->StyleBox->currentItem()+1));
382  changed = true;
383  }
384  if(filled != d->CheckFilled->isChecked()) {
385  filled = d->CheckFilled->isChecked();
386  changed = true;
387  }
388  if(Brush.color() != d->FillColorButt->paletteBackgroundColor()) {
389  Brush.setColor(d->FillColorButt->paletteBackgroundColor());
390  changed = true;
391  }
392  if(Brush.style() != d->FillStyleBox->currentItem()) {
393  Brush.setStyle((Qt::BrushStyle)d->FillStyleBox->currentItem());
394  changed = true;
395  }
396 
397  delete d;
398  return changed;
399 }