Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
wire.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  wire.cpp - description
3  -------------------
4  begin : Wed Sep 3 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 "wire.h"
19 
20 
21 Wire::Wire(int _x1, int _y1, int _x2, int _y2, Node *n1, Node *n2)
22 {
23  cx = 0;
24  cy = 0;
25  x1 = _x1;
26  y1 = _y1;
27  x2 = _x2;
28  y2 = _y2;
29  Port1 = n1;
30  Port2 = n2;
31  Label = 0;
32 
33  Type = isWire;
34  isSelected = false;
35 }
36 
38 {
39 }
40 
41 // ----------------------------------------------------------------
43 {
44  int xm, ym, tmp;
45 
46  xm = (x1+x2) >> 1;
47  ym = (y1+y2) >> 1;
48 
49  tmp = x1;
50  x1 = xm + y1 - ym;
51  y1 = ym - tmp + xm;
52 
53  tmp = x2;
54  x2 = xm + y2 - ym;
55  y2 = ym - tmp + xm;
56 
57  if(Label) {
58  tmp = Label->cx;
59  Label->cx = xm + Label->cy - ym;
60  Label->cy = ym - tmp + xm;
62  else Label->Type = isHWireLabel;
63  }
64 }
65 
66 // ----------------------------------------------------------------
67 void Wire::setCenter(int x, int y, bool relative)
68 {
69  if(relative) {
70  x1 += x; x2 += x;
71  y1 += y; y2 += y;
72 // if(Label) Label->setCenter(x, y, true);
73  }
74  else {
75  x1 = x; x2 = x;
76  y1 = y; y2 = y;
77  }
78 }
79 
80 // ----------------------------------------------------------------
81 void Wire::getCenter(int& x, int& y)
82 {
83  x = (x1+x2) >> 1;
84  y = (y1+y2) >> 1;
85 }
86 
87 // ----------------------------------------------------------------
88 // Lie x/y on wire ? 5 is the precision the coordinates have to fit.
89 bool Wire::getSelected(int x_, int y_)
90 {
91  if(x1-5 <= x_) if(x2+5 >= x_) if(y1-5 <= y_) if(y2+5 >= y_)
92  return true;
93 
94  return false;
95 }
96 
97 // ----------------------------------------------------------------
98 void Wire::paintScheme(QPainter *p)
99 {
100  p->drawLine(x1, y1, x2, y2);
101 // if(Label)
102 // if((Label->Type == isHWireLabel) || (Label->Type == isHWireLabel))
103 // if(Label->Type == isHWireLabel)
104 // Label->paintScheme(p);
105 }
106 
107 // ----------------------------------------------------------------
109 {
110  if(isSelected) {
111  p->Painter->setPen(QPen(Qt::darkGray,6));
112  p->drawLine(x1, y1, x2, y2);
113  p->Painter->setPen(QPen(Qt::lightGray,2));
114  p->drawLine(x1, y1, x2, y2);
115  }
116  else {
117  p->Painter->setPen(QPen(Qt::darkBlue,2));
118  p->drawLine(x1, y1, x2, y2);
119  }
120 }
121 
122 // ----------------------------------------------------------------
124 {
125  return (y1 == y2);
126 }
127 
128 // ----------------------------------------------------------------
129 void Wire::setName(const QString& Name_, const QString& Value_, int delta_, int x_, int y_)
130 {
131  if(Name_.isEmpty() && Value_.isEmpty()) {
132  if(Label) delete Label;
133  Label = 0;
134  return;
135  }
136 
137  if(!Label) {
138  if(isHorizontal())
139  Label = new WireLabel(Name_, x1+delta_, y1, x_, y_, isHWireLabel);
140  else
141  Label = new WireLabel(Name_, x1, y1+delta_, x_, y_, isVWireLabel);
142  Label->pOwner = this;
143  Label->initValue = Value_;
144  }
145  else Label->setName(Name_);
146 }
147 
148 // ----------------------------------------------------------------
149 // Converts all necessary data of the wire into a string. This can be used to
150 // save it to an ASCII file or to transport it via the clipboard.
151 QString Wire::save()
152 {
153  QString s = "<"+QString::number(x1)+" "+QString::number(y1);
154  s += " "+QString::number(x2)+" "+QString::number(y2);
155  if(Label) {
156  s += " \""+Label->Name+"\" ";
157  s += QString::number(Label->x1)+" "+QString::number(Label->y1)+" ";
158  s += QString::number(Label->cx-x1 + Label->cy-y1);
159  s += " \""+Label->initValue+"\">";
160  }
161  else { s += " \"\" 0 0 0 \"\">"; }
162  return s;
163 }
164 
165 // ----------------------------------------------------------------
166 // This is the counterpart to Wire::save.
167 bool Wire::load(const QString& _s)
168 {
169  bool ok;
170  QString s = _s;
171 
172  if(s.at(0) != '<') return false;
173  if(s.at(s.length()-1) != '>') return false;
174  s = s.mid(1, s.length()-2); // cut off start and end character
175 
176  QString n;
177  n = s.section(' ',0,0); // x1
178  x1 = n.toInt(&ok);
179  if(!ok) return false;
180 
181  n = s.section(' ',1,1); // y1
182  y1 = n.toInt(&ok);
183  if(!ok) return false;
184 
185  n = s.section(' ',2,2); // x2
186  x2 = n.toInt(&ok);
187  if(!ok) return false;
188 
189  n = s.section(' ',3,3); // y2
190  y2 = n.toInt(&ok);
191  if(!ok) return false;
192 
193  n = s.section('"',1,1);
194  if(!n.isEmpty()) { // is wire labeled ?
195  int nx = s.section(' ',5,5).toInt(&ok); // x coordinate
196  if(!ok) return false;
197 
198  int ny = s.section(' ',6,6).toInt(&ok); // y coordinate
199  if(!ok) return false;
200 
201  int delta = s.section(' ',7,7).toInt(&ok);// delta for x/y root coordinate
202  if(!ok) return false;
203 
204  setName(n, s.section('"',3,3), delta, nx, ny); // Wire Label
205  }
206 
207  return true;
208 }
QPainter * Painter
Definition: viewpainter.h:58
int Type
Definition: element.h:152
Definition: node.h:30
int y1
Definition: element.h:153
void setName(const QString &Name_)
Definition: wirelabel.cpp:196
int y2
Definition: element.h:153
int x1
Definition: element.h:153
void drawLine(int, int, int, int)
Definition: viewpainter.cpp:85
void paint(ViewPainter *)
Definition: wire.cpp:108
void paintScheme(QPainter *)
Definition: wire.cpp:98
bool getSelected(int, int)
Definition: wire.cpp:89
Wire(int _x1=0, int _y1=0, int _x2=0, int _y2=0, Node *n1=0, Node *n2=0)
Definition: wire.cpp:21
bool isHorizontal()
Definition: wire.cpp:123
int cx
Definition: element.h:153
QString save()
Definition: wire.cpp:151
bool isSelected
Definition: element.h:151
void getCenter(int &, int &)
Definition: wire.cpp:81
void setCenter(int, int, bool relative=false)
Definition: wire.cpp:67
#define isHWireLabel
Definition: element.h:124
~Wire()
Definition: wire.cpp:37
#define isVWireLabel
Definition: element.h:125
bool load(const QString &)
Definition: wire.cpp:167
QString Name
Definition: wirelabel.h:46
Conductor * pOwner
Definition: wirelabel.h:42
int cy
Definition: element.h:153
void rotate()
Definition: wire.cpp:42
WireLabel * Label
Definition: element.h:163
Node * Port1
Definition: wire.h:43
#define isWire
Definition: element.h:118
int x2
Definition: element.h:153
Node * Port2
Definition: wire.h:43
void setName(const QString &, const QString &, int delta_=0, int x_=0, int y_=0)
Definition: wire.cpp:129
QString initValue
Definition: wirelabel.h:46