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