My Project  0.0.16
QUCS Mapping
 All Classes Namespaces 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 
18 #include "main.h"
19 #include "portsymbol.h"
20 
21 
22 PortSymbol::PortSymbol(int cx_, int cy_, const QString& numberStr_,
23  const QString& nameStr_)
24 {
25  Name = ".PortSym ";
26  isSelected = false;
27  cx = cx_;
28  cy = cy_;
29 
30  Angel = 0;
31  nameStr = nameStr_;
32  numberStr = numberStr_;
33  QFontMetrics metrics(QucsSettings.font);
34  QSize r = metrics.size(0, nameStr);
35  x1 = -r.width() - 8;
36  y1 = -((r.height() + 8) >> 1);
37  x2 = 8 - x1;
38  y2 = r.height() + 8;
39 }
40 
42 {
43 }
44 
45 // --------------------------------------------------------------------------
47 {
48  p->Painter->setPen(QPen(QPen::red,1)); // like open node
49  p->drawEllipse(cx-4, cy-4, 8, 8);
50 
51 
52  QSize r = p->Painter->fontMetrics().size(0, nameStr);
53  int Unit = int(8.0 * p->Scale);
54  x1 = -r.width() - Unit;
55  y1 = -((r.height() + Unit) >> 1);
56  x2 = Unit - x1;
57  y2 = r.height() + Unit;
58 
59  QWMatrix wm = p->Painter->worldMatrix();
60  QWMatrix Mat(1.0, 0.0, 0.0, 1.0, p->DX + float(cx) * p->Scale,
61  p->DY + float(cy) * p->Scale);
62  p->Painter->setWorldMatrix(Mat);
63 
64  int tmp, tx, ty;
65  tx = x1 + (Unit >> 1);
66  ty = y1 + (Unit >> 1);
67  switch(Angel) {
68  case 90:
69  x1 = y1;
70  y1 = -Unit;
71  tmp = x2; x2 = y2; y2 = tmp;
72  p->Painter->rotate(-90.0); // automatically enables transformation
73  break;
74  case 180:
75  x1 = -Unit;
76  tx = Unit >> 1;
77  break;
78  case 270:
79  tx = Unit >> 1;
80  tmp = x1; x1 = y1; y1 = tmp;
81  tmp = x2; x2 = y2; y2 = tmp;
82  p->Painter->rotate(-90.0); // automatically enables transformation
83  break;
84  }
85 
86  p->Painter->setPen(Qt::black);
87  p->Painter->drawText(tx, ty, 0, 0, Qt::DontClip, nameStr);
88 
89 
90  p->Painter->setWorldMatrix(wm);
91  p->Painter->setWorldXForm(false);
92  x1 = int(float(x1) / p->Scale);
93  x2 = int(float(x2) / p->Scale);
94  y1 = int(float(y1) / p->Scale);
95  y2 = int(float(y2) / p->Scale);
96 
97  p->Painter->setPen(Qt::lightGray);
98  p->drawRect(cx+x1, cy+y1, x2, y2);
99 
100  if(isSelected) {
101  p->Painter->setPen(QPen(QPen::darkGray,3));
102  p->drawRoundRect(cx+x1-4, cy+y1-4, x2+8, y2+8);
103  }
104 }
105 
106 // --------------------------------------------------------------------------
107 void PortSymbol::paintScheme(QPainter *p)
108 {
109  p->drawEllipse(cx-4, cy-4, 8, 8);
110  p->drawRect(cx+x1, cy+y1, x2, y2);
111 }
112 
113 // --------------------------------------------------------------------------
114 void PortSymbol::getCenter(int& x, int &y)
115 {
116  x = cx;
117  y = cy;
118 }
119 
120 // --------------------------------------------------------------------------
121 // Sets the center of the painting to x/y.
122 void PortSymbol::setCenter(int x, int y, bool relative)
123 {
124  if(relative) { cx += x; cy += y; }
125  else { cx = x; cy = y; }
126 }
127 
128 // --------------------------------------------------------------------------
129 bool PortSymbol::load(const QString& s)
130 {
131  bool ok;
132 
133  QString n;
134  n = s.section(' ',1,1); // cx
135  cx = n.toInt(&ok);
136  if(!ok) return false;
137 
138  n = s.section(' ',2,2); // cy
139  cy = n.toInt(&ok);
140  if(!ok) return false;
141 
142  numberStr = s.section(' ',3,3); // number
143  if(numberStr.isEmpty()) return false;
144 
145  n = s.section(' ',4,4); // Angel
146  if(n.isEmpty()) return true; // be backward-compatible
147  Angel = n.toInt(&ok);
148  if(!ok) return false;
149 
150  return true;
151 }
152 
153 // --------------------------------------------------------------------------
155 {
156  QString s = Name+QString::number(cx)+" "+QString::number(cy)+" ";
157  s += numberStr+" "+QString::number(Angel);
158  return s;
159 }
160 
161 // --------------------------------------------------------------------------
163 {
164  QString s =
165  QString ("new Port (%1, %2)").
166  arg(cx).arg(cy);
167  s = "Ports.append (" + s + "); /* " + nameStr + " */";
168  return s;
169 }
170 
171 // --------------------------------------------------------------------------
172 // Checks if the coordinates x/y point to the painting.
173 bool PortSymbol::getSelected(float fX, float fY, float)
174 {
175  if(int(fX) < cx+x1) return false;
176  if(int(fY) < cy+y1) return false;
177  if(int(fX) > cx+x1+x2) return false;
178  if(int(fY) > cy+y1+y2) return false;
179 
180  return true;
181 }
182 
183 // --------------------------------------------------------------------------
184 void PortSymbol::Bounding(int& _x1, int& _y1, int& _x2, int& _y2)
185 {
186  _x1 = cx+x1; _y1 = cy+y1;
187  _x2 = cx+x1+x2; _y2 = cy+y1+y2;
188 }
189 
190 // --------------------------------------------------------------------------
191 // Rotates around the center.
193 {
194  if(Angel < 270) Angel += 90;
195  else Angel = 0;
196 }
197 
198 // --------------------------------------------------------------------------
199 // Mirrors about connection node (not center line !).
201 {
202  if(Angel == 90) Angel = 270;
203  else if(Angel == 270) Angel = 90;
204 }
205 
206 // --------------------------------------------------------------------------
207 // Mirrors about connection node (not center line !).
209 {
210  if(Angel == 0) Angel = 180;
211  else if(Angel == 180) Angel = 0;
212 }