My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
wirelabel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  wirelabel.cpp - description
3  -------------------
4  begin : Sun February 29 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 "wirelabel.h"
19 #include "viewpainter.h"
20 #include "wire.h"
21 #include "main.h"
22 
23 
24 WireLabel::WireLabel(const QString& _Name, int _cx, int _cy,
25  int _x1, int _y1, int _Type)
26 {
27  cx = _cx;
28  cy = _cy;
29  x1 = _x1;
30  y1 = _y1;
31  setName(_Name);
32  initValue = "";
33 
34  Type = _Type;
35  isSelected = false;
36 }
37 
39 {
40 }
41 
42 // ----------------------------------------------------------------
43 void WireLabel::paintScheme(QPainter *p)
44 {
45  p->drawRect(x1, y1, x2, y2);
46 
47  // which corner of rectangle should be connected to line ?
48  if(cx < x1+(x2>>1)) {
49  if(cy < y1+(y2>>1))
50  p->drawLine(cx, cy, x1, y1);
51  else
52  p->drawLine(cx, cy, x1, y1+y2);
53  }
54  else {
55  if(cy < y1+(y2>>1))
56  p->drawLine(cx, cy, x1+x2, y1);
57  else
58  p->drawLine(cx, cy, x1+x2, y1+y2);
59  }
60 }
61 
62 // ----------------------------------------------------------------
63 void WireLabel::setCenter(int x_, int y_, bool relative)
64 {
65  switch(Type) {
66  case isMovingLabel:
67  if(relative) {
68  x1 += x_; cx += x_;
69  y1 += y_; cy += y_;
70  }
71  else {
72  x1 = x_; cx = x_;
73  y1 = y_; cy = y_;
74  }
75  break;
76  case isHMovingLabel:
77  if(relative) { x1 += x_; cx += x_; }
78  else { x1 = x_; cx = x_; }
79  break;
80  case isVMovingLabel:
81  if(relative) { y1 += y_; cy += y_; }
82  else { y1 = y_; cy = y_; }
83  break;
84  default:
85  if(relative) {
86  x1 += x_; y1 += y_; // moving cx/cy is done by owner (wire, node)
87  }
88  else { x1 = x_; y1 = y_; }
89  }
90 }
91 
92 // ----------------------------------------------------------------
93 bool WireLabel::getSelected(int x, int y)
94 {
95  if(x1 <= x)
96  if(y1 <= y)
97  if((x1+x2) >= x)
98  if((y1+y2) >= y)
99  return true;
100 
101  return false;
102 }
103 
104 // ----------------------------------------------------------------
106 {
107  p->Painter->setPen(QPen(QPen::black,1));
108  x2 = p->drawText(Name, x1, y1, &y2);
109 
110  int xpaint=0, ypaint=4, phi=0;
111  switch(Type) {
112  case isVWireLabel: ypaint=0; xpaint=4; phi=16*140; break;
113  case isHWireLabel: phi=16*50; break;
114  case isNodeLabel: ypaint = 0;
115  default: ;
116  }
117 
118  int c, d;
119  int a = int(double(x2) / p->Scale) >> 1;
120  int b = int(double(y2) / p->Scale) >> 1;
121  if(cx < x1+a) { // where should frame be painted ?
122  if(cy < y1+b) {
123  if(phi == 16*50) phi += 16*180;
124  p->map(x1-3, y1-2, a, b); // low right
125  c = a + (x2>>1);
126  d = b + y2;
127  p->map(cx+xpaint, cy+ypaint, xpaint, ypaint);
128  }
129  else {
130  if(phi != 0) phi += 16*180;
131  p->map(x1-3, y1+1, a, b); // up right
132  b += y2;
133  c = a + (x2>>1);
134  d = b - y2;
135  p->map(cx+xpaint, cy-ypaint, xpaint, ypaint);
136  }
137  }
138  else {
139  if(cy < y1+b) {
140  p->map(x1+3, y1-2, a, b); // low left
141  a += x2;
142  c = a - (x2>>1);
143  d = b + y2;
144  p->map(cx-xpaint, cy+ypaint, xpaint, ypaint);
145  }
146  else {
147  if(phi > 16*90) phi += 16*180;
148  p->map(x1+3, y1+1, a, b); // up left
149  a += x2;
150  b += y2;
151  c = a - (x2>>1);
152  d = b - y2;
153  p->map(cx-xpaint, cy-ypaint, xpaint, ypaint);
154  }
155  }
156 
157  if(initValue.isEmpty())
158  p->Painter->setPen(QPen(QPen::darkMagenta,0));
159  else
160  p->Painter->setPen(QPen(QPen::red,0));
161 
162  if(phi) p->drawArc(cx-4, cy-4, 8, 8, phi, 16*255);
163  p->Painter->drawLine(a, b, c, b);
164  p->Painter->drawLine(a, b, a, d);
165  p->Painter->drawLine(xpaint, ypaint, a, b);
166 
167  x2 = int(double(x2) / p->Scale);
168  y2 = int(double(y2) / p->Scale);
169  if(isSelected) {
170  p->Painter->setPen(QPen(QPen::darkGray,3));
171  p->drawRoundRect(x1-2, y1-2, x2+6, y2+5);
172  }
173 }
174 
175 // ----------------------------------------------------------------
176 void WireLabel::setName(const QString& Name_)
177 {
178  Name = Name_;
179 
180  QFontMetrics metrics(QucsSettings.font); // get size of text
181  QSize r = metrics.size(0, Name);
182  x2 = r.width();
183  y2 = r.height()-2; // remember size of text
184 }
185 
186 // ----------------------------------------------------------------
187 // Converts all necessary data of the wire into a string. This can be used to
188 // save it to an ASCII file or to transport it via the clipboard.
189 // Wire labels use the same format like wires, but with length zero.
191 {
192  QString s("<");
193  s += QString::number(cx)+" "+QString::number(cy)+" "
194  + QString::number(cx)+" "+QString::number(cy)
195  + " \""+Name +"\" "
196  + QString::number(x1)+" "+QString::number(y1)+" 0 \""
197  + initValue+"\">";
198  return s;
199 }