Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
portsymbol.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  portsymbol.cpp - description
3  -------------------
4  begin : Sun Sep 5 2004
5  copyright : (C) 2004 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 "main.h"
19 #include "portsymbol.h"
20 #include "schematic.h"
21 #include "qucs.h"
22 
23 PortSymbol::PortSymbol(int cx_, int cy_, const QString& numberStr_,
24  const QString& nameStr_)
25 {
26  Name = ".PortSym ";
27  isSelected = false;
28  cx = cx_;
29  cy = cy_;
30 
31  Angel = 0;
32  nameStr = nameStr_;
33  numberStr = numberStr_;
34  QFontMetrics metrics(((Schematic*)QucsMain->DocumentTab->currentPage())->font()); // get size of text
35  QSize r = metrics.size(0, nameStr);
36  x1 = -r.width() - 8;
37  y1 = -((r.height() + 8) >> 1);
38  x2 = 8 - x1;
39  y2 = r.height() + 8;
40 }
41 
43 {
44 }
45 
46 // --------------------------------------------------------------------------
48 {
49  // keep track of painter state
50  p->Painter->save();
51 
52  p->Painter->setPen(QPen(Qt::red,1)); // like open node
53  p->drawEllipse(cx-4, cy-4, 8, 8);
54 
55  QSize r = p->Painter->fontMetrics().size(0, nameStr);
56  int Unit = int(8.0 * p->Scale);
57  x1 = -r.width() - Unit;
58  y1 = -((r.height() + Unit) >> 1);
59  x2 = Unit - x1;
60  y2 = r.height() + Unit;
61 
62  QMatrix wm = p->Painter->worldMatrix();
63  QMatrix Mat(1.0, 0.0, 0.0, 1.0, p->DX + float(cx) * p->Scale,
64  p->DY + float(cy) * p->Scale);
65  p->Painter->setWorldMatrix(Mat);
66 
67  int tmp, tx, ty;
68  tx = x1 + (Unit >> 1);
69  ty = y1 + (Unit >> 1);
70  switch(Angel) {
71  case 90:
72  x1 = y1;
73  y1 = -Unit;
74  tmp = x2; x2 = y2; y2 = tmp;
75  p->Painter->rotate(-90.0); // automatically enables transformation
76  break;
77  case 180:
78  x1 = -Unit;
79  tx = Unit >> 1;
80  break;
81  case 270:
82  tx = Unit >> 1;
83  tmp = x1; x1 = y1; y1 = tmp;
84  tmp = x2; x2 = y2; y2 = tmp;
85  p->Painter->rotate(-90.0); // automatically enables transformation
86  break;
87  }
88 
89  p->Painter->setPen(Qt::black);
90  p->Painter->drawText(tx, ty, 0, 0, Qt::TextDontClip, nameStr);
91 
92 
93  p->Painter->setWorldMatrix(wm);
94  p->Painter->setWorldXForm(false);
95 
96  // restore painter state
97  p->Painter->restore();
98 
99  x1 = int(float(x1) / p->Scale);
100  x2 = int(float(x2) / p->Scale);
101  y1 = int(float(y1) / p->Scale);
102  y2 = int(float(y2) / p->Scale);
103 
104  p->Painter->setPen(Qt::lightGray);
105  p->drawRect(cx+x1, cy+y1, x2, y2);
106 
107  if(isSelected) {
108  p->Painter->setPen(QPen(Qt::darkGray,3));
109  p->drawRoundRect(cx+x1-4, cy+y1-4, x2+8, y2+8);
110  }
111 }
112 
113 // --------------------------------------------------------------------------
115 {
116  p->PostPaintEvent(_Ellipse, cx-4, cy-4, 8, 8);
117  p->PostPaintEvent(_Rect, cx+x1, cy+y1, x2, y2);
118 }
119 
120 // --------------------------------------------------------------------------
121 void PortSymbol::getCenter(int& x, int &y)
122 {
123  x = cx;
124  y = cy;
125 }
126 
127 // --------------------------------------------------------------------------
128 // Sets the center of the painting to x/y.
129 void PortSymbol::setCenter(int x, int y, bool relative)
130 {
131  if(relative) { cx += x; cy += y; }
132  else { cx = x; cy = y; }
133 }
134 
135 // --------------------------------------------------------------------------
136 bool PortSymbol::load(const QString& s)
137 {
138  bool ok;
139 
140  QString n;
141  n = s.section(' ',1,1); // cx
142  cx = n.toInt(&ok);
143  if(!ok) return false;
144 
145  n = s.section(' ',2,2); // cy
146  cy = n.toInt(&ok);
147  if(!ok) return false;
148 
149  numberStr = s.section(' ',3,3); // number
150  if(numberStr.isEmpty()) return false;
151 
152  n = s.section(' ',4,4); // Angel
153  if(n.isEmpty()) return true; // be backward-compatible
154  Angel = n.toInt(&ok);
155  if(!ok) return false;
156 
157  return true;
158 }
159 
160 // --------------------------------------------------------------------------
162 {
163  QString s = Name+QString::number(cx)+" "+QString::number(cy)+" ";
164  s += numberStr+" "+QString::number(Angel);
165  return s;
166 }
167 
168 // --------------------------------------------------------------------------
170 {
171  QString s =
172  QString ("new Port (%1, %2)").
173  arg(cx).arg(cy);
174  s = "Ports.append (" + s + "); /* " + nameStr + " */";
175  return s;
176 }
177 
179 {
180  QString s = QString ("{\"type\" : \"portsymbol\", "
181  "\"x\" : %1, \"y\" : %2},").arg(cx).arg(cy);
182  return s;
183 }
184 
185 // --------------------------------------------------------------------------
186 // Checks if the coordinates x/y point to the painting.
187 bool PortSymbol::getSelected(float fX, float fY, float)
188 {
189  if(int(fX) < cx+x1) return false;
190  if(int(fY) < cy+y1) return false;
191  if(int(fX) > cx+x1+x2) return false;
192  if(int(fY) > cy+y1+y2) return false;
193 
194  return true;
195 }
196 
197 // --------------------------------------------------------------------------
198 void PortSymbol::Bounding(int& _x1, int& _y1, int& _x2, int& _y2)
199 {
200  _x1 = cx+x1; _y1 = cy+y1;
201  _x2 = cx+x1+x2; _y2 = cy+y1+y2;
202 }
203 
204 // --------------------------------------------------------------------------
205 // Rotates around the center.
207 {
208  if(Angel < 270) Angel += 90;
209  else Angel = 0;
210 }
211 
212 // --------------------------------------------------------------------------
213 // Mirrors about connection node (not center line !).
215 {
216  if(Angel == 90) Angel = 270;
217  else if(Angel == 270) Angel = 90;
218 }
219 
220 // --------------------------------------------------------------------------
221 // Mirrors about connection node (not center line !).
223 {
224  if(Angel == 0) Angel = 180;
225  else if(Angel == 180) Angel = 0;
226 }
QPainter * Painter
Definition: viewpainter.h:58
void setCenter(int, int, bool relative=false)
Definition: portsymbol.cpp:129
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
float Scale
Definition: viewpainter.h:59
PortSymbol(int cx_=0, int cy_=0, const QString &numberStr_="1", const QString &nameStr_="")
Definition: portsymbol.cpp:23
void paintScheme(Schematic *)
Definition: portsymbol.cpp:114
bool load(const QString &)
Definition: portsymbol.cpp:136
QString nameStr
Definition: portsymbol.h:47
int y2
Definition: element.h:153
int x1
Definition: element.h:153
void Bounding(int &, int &, int &, int &)
Definition: portsymbol.cpp:198
void getCenter(int &, int &)
Definition: portsymbol.cpp:121
bool getSelected(float, float, float)
Definition: portsymbol.cpp:187
void mirrorY()
Definition: portsymbol.cpp:222
Definitions and declarations for the main application.
QucsApp * QucsMain
Definition: main.cpp:54
int cx
Definition: element.h:153
QString numberStr
Definition: portsymbol.h:47
void rotate()
Definition: portsymbol.cpp:206
bool isSelected
Definition: element.h:151
void drawRoundRect(int, int, int, int)
QString Name
Definition: painting.h:55
void drawRect(int, int, int, int)
QTabWidget * DocumentTab
Definition: qucs.h:164
void paint(ViewPainter *)
Definition: portsymbol.cpp:47
int cy
Definition: element.h:153
QString save()
Definition: portsymbol.cpp:161
void mirrorX()
Definition: portsymbol.cpp:214
QString saveCpp()
Definition: portsymbol.cpp:169
int x2
Definition: element.h:153
QString saveJSON()
Definition: portsymbol.cpp:178
void drawEllipse(int, int, int, int)