Qucs-GUI  0.0.18
 All Data Structures 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 #include <QtGui>
18 #include "ellipse.h"
19 #include "filldialog.h"
20 #include "schematic.h"
21 #include <QPushButton>
22 #include <QLineEdit>
23 #include <QComboBox>
24 #include <QCheckBox>
25 
26 
27 Ellipse::Ellipse(bool _filled)
28 {
29  Name = "Ellipse ";
30  isSelected = false;
31  Pen = QPen(QColor());
32  Brush = QBrush(Qt::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(Qt::darkGray,Pen.width()+5));
48  if(filled) p->Painter->setBrush(Brush);
49  p->drawEllipse(cx, cy, x2, y2);
50  p->Painter->setPen(QPen(Qt::white, Pen.width(), Pen.style()));
51  p->Painter->setBrush(Qt::NoBrush);
52  p->drawEllipse(cx, cy, x2, y2);
53 
54  p->Painter->setPen(QPen(Qt::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(Qt::NoBrush); // no filling for the next paintings
65 }
66 
67 // --------------------------------------------------------------------------
69 {
70  p->PostPaintEvent(_Ellipse, 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 
198 {
199  QString b = filled ?
200  QString ("\"colorfill\" : \"%1\", \"stylefill\" : \"%2\"").
201  arg(Brush.color().name()).arg(toBrushString(Brush.style())) : "";
202  QString s =
203  QString("{\"type\" : \"ellipse\", "
204  "\"x\" : %1, \"y\" : %2, \"w\" : %3, \"h\" : %4,"
205  "\"color\" : \"%5\", \"thick\" : %6, \"style\" : \"%7\", %8},").
206  arg(cx).arg(cy).arg(x2).arg(y2).
207  arg(Pen.color().name()).arg(Pen.width()).arg(toPenString(Pen.style())).
208  arg(b);
209  return s;
210 }
211 
212 // --------------------------------------------------------------------------
213 // Checks if the resize area was clicked.
214 bool Ellipse::resizeTouched(float fX, float fY, float len)
215 {
216  float fCX = float(cx), fCY = float(cy);
217  float fX2 = float(cx+x2), fY2 = float(cy+y2);
218 
219  State = -1;
220  if(fX < fCX-len) return false;
221  if(fY < fCY-len) return false;
222  if(fX > fX2+len) return false;
223  if(fY > fY2+len) return false;
224 
225  State = 0;
226  if(fX < fCX+len) State = 1;
227  else if(fX <= fX2-len) { State = -1; return false; }
228  if(fY < fCY+len) State |= 2;
229  else if(fY <= fY2-len) { State = -1; return false; }
230 
231  return true;
232 }
233 
234 // --------------------------------------------------------------------------
235 // Mouse move action during resize.
236 void Ellipse::MouseResizeMoving(int x, int y, Schematic *p)
237 {
238  paintScheme(p); // erase old painting
239  switch(State) {
240  case 0: x2 = x-cx; y2 = y-cy; // lower right corner
241  break;
242  case 1: x2 -= x-cx; cx = x; y2 = y-cy; // lower left corner
243  break;
244  case 2: x2 = x-cx; y2 -= y-cy; cy = y; // upper right corner
245  break;
246  case 3: x2 -= x-cx; cx = x; y2 -= y-cy; cy = y; // upper left corner
247  break;
248  }
249  if(x2 < 0) { State ^= 1; x2 *= -1; cx -= x2; }
250  if(y2 < 0) { State ^= 2; y2 *= -1; cy -= y2; }
251 
252  paintScheme(p); // paint new painting
253 }
254 
255 // --------------------------------------------------------------------------
256 // fx/fy are the precise coordinates, gx/gy are the coordinates set on grid.
257 // x/y are coordinates without scaling.
259  Schematic *paintScale, int, int, int gx, int gy,
260  Schematic *p, int x, int y, bool drawn)
261 {
262  if(State > 0) {
263  if(State > 1)
264  // _Ellipse hang/crash application, using _Arc solved, see bug 141 (closed)
265  paintScale->PostPaintEvent(_Arc, x1, y1, x2-x1, y2-y1, 0, 16*360); // erase old painting
266  State++;
267  x2 = gx;
268  y2 = gy;
269  paintScale->PostPaintEvent(_Arc, x1, y1, x2-x1, y2-y1, 0, 16*360);
270  }
271  else { x2 = gx; y2 = gy; }
272 
273  if(drawn) {
274  p->PostPaintEvent(_Ellipse, cx+13, cy, 18, 12,0,0,true); // erase old cursor symbol
275  if(filled) {
276  p->PostPaintEvent(_Line, cx+14, cy+7, cx+20, cy+1,0,0,true);
277  p->PostPaintEvent(_Line, cx+25, cy+2, cx+18, cy+9,0,0,true);
278  p->PostPaintEvent(_Line, cx+29, cy+4, cx+23, cy+10,0,0,true);
279  }
280  }
281  cx = x;
282  cy = y;
283  p->PostPaintEvent(_Ellipse, cx+13, cy, 18, 12,0,0,true); // paint new cursor symbol
284  if(filled) {
285  p->PostPaintEvent(_Line, cx+14, cy+7, cx+20, cy+1,0,0,true);
286  p->PostPaintEvent(_Line, cx+25, cy+2, cx+18, cy+9,0,0,true);
287  p->PostPaintEvent(_Line, cx+29, cy+4, cx+23, cy+10,0,0,true);
288  }
289 }
290 
291 // --------------------------------------------------------------------------
293 {
294  State++;
295  if(State == 1) {
296  x1 = x2;
297  y1 = y2; // first corner is determined
298  }
299  else {
300  if(x1 < x2) { cx = x1; x2 = x2-x1; } // cx/cy to upper left corner
301  else { cx = x2; x2 = x1-x2; }
302  if(y1 < y2) { cy = y1; y2 = y2-y1; }
303  else { cy = y2; y2 = y1-y2; }
304  x1 = y1 = 0;
305  State = 0;
306  return true; // painting is ready
307  }
308  return false;
309 }
310 
311 // --------------------------------------------------------------------------
312 // Checks if the coordinates x/y point to the painting.
313 bool Ellipse::getSelected(float fX, float fY, float w)
314 {
315  float fX2 = float(x2);
316  float fY2 = float(y2);
317  fX -= float(cx) + fX2/2.0;
318  fY -= float(cy) + fY2/2.0;
319 
320  if(filled) {
321  float a = 2.0 * fX / fX2; a *= a;
322  float b = 2.0 * fY / fY2; b *= b;
323 
324  if(a+b > 1.0)
325  return false;
326  }
327  else {
328  float a1 = fX / (fX2/2.0 - w); a1 *= a1;
329  float a2 = fX / (fX2/2.0 + w); a2 *= a2;
330  float b1 = fY / (fY2/2.0 - w); b1 *= b1;
331  float b2 = fY / (fY2/2.0 + w); b2 *= b2;
332 
333  if(a1+b1 < 1.0) return false;
334  if(a2+b2 > 1.0) return false;
335  }
336 
337  return true;
338 }
339 
340 // --------------------------------------------------------------------------
341 // Rotates around the center.
343 {
344  cy += (y2-x2) >> 1;
345  cx += (x2-y2) >> 1;
346  int tmp = x2;
347  x2 = y2;
348  y2 = tmp;
349 }
350 
351 // --------------------------------------------------------------------------
352 // Mirrors about center line.
354 {
355  // nothing to do
356 }
357 
358 // --------------------------------------------------------------------------
359 // Mirrors about center line.
361 {
362  // nothing to do
363 }
364 
365 // --------------------------------------------------------------------------
366 // Calls the property dialog for the painting and changes them accordingly.
367 // If there were changes, it returns 'true'.
369 {
370  bool changed = false;
371 
372  FillDialog *d = new FillDialog(QObject::tr("Edit Ellipse Properties"));
373  d->ColorButt->setPaletteBackgroundColor(Pen.color());
374  d->LineWidth->setText(QString::number(Pen.width()));
375  d->StyleBox->setCurrentItem(Pen.style()-1);
376  d->FillColorButt->setPaletteBackgroundColor(Brush.color());
377  d->FillStyleBox->setCurrentItem(Brush.style());
378  d->CheckFilled->setChecked(filled);
380 
381  if(d->exec() == QDialog::Rejected) {
382  delete d;
383  return false;
384  }
385 
386  if(Pen.color() != d->ColorButt->paletteBackgroundColor()) {
387  Pen.setColor(d->ColorButt->paletteBackgroundColor());
388  changed = true;
389  }
390  if(Pen.width() != d->LineWidth->text().toUInt()) {
391  Pen.setWidth(d->LineWidth->text().toUInt());
392  changed = true;
393  }
394  if(Pen.style() != (Qt::PenStyle)(d->StyleBox->currentItem()+1)) {
395  Pen.setStyle((Qt::PenStyle)(d->StyleBox->currentItem()+1));
396  changed = true;
397  }
398  if(filled != d->CheckFilled->isChecked()) {
399  filled = d->CheckFilled->isChecked();
400  changed = true;
401  }
402  if(Brush.color() != d->FillColorButt->paletteBackgroundColor()) {
403  Brush.setColor(d->FillColorButt->paletteBackgroundColor());
404  changed = true;
405  }
406  if(Brush.style() != d->FillStyleBox->currentItem()) {
407  Brush.setStyle((Qt::BrushStyle)d->FillStyleBox->currentItem());
408  changed = true;
409  }
410 
411  delete d;
412  return changed;
413 }
QString save()
Definition: ellipse.cpp:168
bool resizeTouched(float, float, float)
Definition: ellipse.cpp:214
void drawResizeRect(int, int)
QPainter * Painter
Definition: viewpainter.h:58
void MouseResizeMoving(int, int, Schematic *)
Definition: ellipse.cpp:236
void mirrorX()
Definition: ellipse.cpp:353
QString toPenString(int)
Definition: painting.cpp:54
int State
Definition: painting.h:56
QCheckBox * CheckFilled
Definition: filldialog.h:52
int y1
Definition: element.h:153
void PostPaintEvent(PE pe, int x1=0, int y1=0, int x2=0, int y2=0, int a=0, int b=0, bool PaintOnViewport=false)
Definition: schematic.cpp:533
static Element * info_filled(QString &, char *&, bool getNewOne=false)
Definition: ellipse.cpp:105
bool Dialog()
Definition: ellipse.cpp:368
QString saveJSON()
Definition: ellipse.cpp:197
int y2
Definition: element.h:153
QLineEdit * LineWidth
Definition: filldialog.h:53
int x1
Definition: element.h:153
void setCenter(int, int, bool relative=false)
Definition: ellipse.cpp:82
void getCenter(int &, int &)
Definition: ellipse.cpp:74
Definition: schematic.h:55
void MouseMoving(Schematic *, int, int, int, int, Schematic *, int, int, bool)
Definition: ellipse.cpp:258
QPen Pen
Definition: ellipse.h:54
Painting * newOne()
Definition: ellipse.cpp:89
int cx
Definition: element.h:153
void paintScheme(Schematic *)
Definition: ellipse.cpp:68
void slotCheckFilled(bool on)
Definition: filldialog.cpp:156
QString saveCpp()
Definition: ellipse.cpp:182
bool isSelected
Definition: element.h:151
void mirrorY()
Definition: ellipse.cpp:360
bool filled
Definition: ellipse.h:56
~Ellipse()
Definition: ellipse.cpp:39
QString Name
Definition: painting.h:55
Superclass of all schematic drawing elements.
Definition: element.h:142
void rotate()
Definition: ellipse.cpp:342
QPushButton * FillColorButt
Definition: filldialog.h:54
static Element * info(QString &, char *&, bool getNewOne=false)
Definition: ellipse.cpp:95
void paint(ViewPainter *)
Definition: ellipse.cpp:44
int cy
Definition: element.h:153
QPushButton * ColorButt
Definition: filldialog.h:54
bool getSelected(float, float, float)
Definition: ellipse.cpp:313
QComboBox * StyleBox
Definition: filldialog.h:55
bool load(const QString &)
Definition: ellipse.cpp:115
bool MousePressing()
Definition: ellipse.cpp:292
Ellipse(bool _filled=false)
Definition: ellipse.cpp:27
QString toBrushString(int)
Definition: painting.cpp:67
int x2
Definition: element.h:153
void drawEllipse(int, int, int, int)
QComboBox * FillStyleBox
Definition: filldialog.h:55
QBrush Brush
Definition: ellipse.h:55