Qucs-GUI  0.0.18
 All Data Structures 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 #include <QtGui>
18 #include "wirelabel.h"
19 #include "viewpainter.h"
20 #include "wire.h"
21 #include "main.h"
22 
23 #include "qucs.h"
24 #include "schematic.h"
25 
26 
27 WireLabel::WireLabel(const QString& _Name, int _cx, int _cy,
28  int _x1, int _y1, int _Type)
29 {
30  cx = _cx;
31  cy = _cy;
32  x1 = _x1;
33  y1 = _y1;
34  setName(_Name);
35  initValue = "";
36 
37  Type = _Type;
38  isSelected = false;
39  isHighlighted = false;
40 }
41 
43 {
44 }
45 
46 // ----------------------------------------------------------------
47 void WireLabel::paintScheme(QPainter *p)
48 {
49  p->drawRect(x1, y1, x2, y2);
50 
51  // which corner of rectangle should be connected to line ?
52  if(cx < x1+(x2>>1)) {
53  if(cy < y1+(y2>>1))
54  p->drawLine(cx, cy, x1, y1);
55  else
56  p->drawLine(cx, cy, x1, y1+y2);
57  }
58  else {
59  if(cy < y1+(y2>>1))
60  p->drawLine(cx, cy, x1+x2, y1);
61  else
62  p->drawLine(cx, cy, x1+x2, y1+y2);
63  }
64 }
65 
66 // ----------------------------------------------------------------
67 void WireLabel::setCenter(int x_, int y_, bool relative)
68 {
69  switch(Type) {
70  case isMovingLabel:
71  if(relative) {
72  x1 += x_; cx += x_;
73  y1 += y_; cy += y_;
74  }
75  else {
76  x1 = x_; cx = x_;
77  y1 = y_; cy = y_;
78  }
79  break;
80  case isHMovingLabel:
81  if(relative) { x1 += x_; cx += x_; }
82  else { x1 = x_; cx = x_; }
83  break;
84  case isVMovingLabel:
85  if(relative) { y1 += y_; cy += y_; }
86  else { y1 = y_; cy = y_; }
87  break;
88  default:
89  if(relative) {
90  x1 += x_; y1 += y_; // moving cx/cy is done by owner (wire, node)
91  }
92  else { x1 = x_; y1 = y_; }
93  }
94 }
95 
96 // ----------------------------------------------------------------
97 bool WireLabel::getSelected(int x, int y)
98 {
99  if(x1 <= x)
100  if(y1 <= y)
101  if((x1+x2) >= x)
102  if((y1+y2) >= y)
103  return true;
104 
105  return false;
106 }
107 
108 // ----------------------------------------------------------------
110 {
111  QFont font = p->Painter->font();
112  if (isHighlighted)
113  {
114 // QColor highlightfill (Qt::blue);
115 // highlightfill.setAlpha(50);
116 // p->fillRect(x1-1, y1-1, x2, y2, highlightfill);
117  p->Painter->setPen(QPen(Qt::darkBlue,3));
118  font.setWeight (QFont::DemiBold);
119  }
120  else
121  {
122  font.setWeight (QFont::Normal);
123  p->Painter->setPen(QPen(Qt::black,1));
124  }
125  p->Painter->setFont (font);
126  x2 = p->drawText(Name, x1, y1, &y2);
127 
128  int xpaint=0, ypaint=4, phi=0;
129  switch(Type) {
130  case isVWireLabel: ypaint=0; xpaint=4; phi=16*140; break;
131  case isHWireLabel: phi=16*50; break;
132  case isNodeLabel: ypaint = 0;
133  default: ;
134  }
135 
136  int c, d;
137  int a = int(double(x2) / p->Scale) >> 1;
138  int b = int(double(y2) / p->Scale) >> 1;
139  if(cx < x1+a) { // where should frame be painted ?
140  if(cy < y1+b) {
141  if(phi == 16*50) phi += 16*180;
142  p->map(x1-3, y1-2, a, b); // low right
143  c = a + (x2>>1);
144  d = b + y2;
145  p->map(cx+xpaint, cy+ypaint, xpaint, ypaint);
146  }
147  else {
148  if(phi != 0) phi += 16*180;
149  p->map(x1-3, y1+1, a, b); // up right
150  b += y2;
151  c = a + (x2>>1);
152  d = b - y2;
153  p->map(cx+xpaint, cy-ypaint, xpaint, ypaint);
154  }
155  }
156  else {
157  if(cy < y1+b) {
158  p->map(x1+3, y1-2, a, b); // low left
159  a += x2;
160  c = a - (x2>>1);
161  d = b + y2;
162  p->map(cx-xpaint, cy+ypaint, xpaint, ypaint);
163  }
164  else {
165  if(phi > 16*90) phi += 16*180;
166  p->map(x1+3, y1+1, a, b); // up left
167  a += x2;
168  b += y2;
169  c = a - (x2>>1);
170  d = b - y2;
171  p->map(cx-xpaint, cy-ypaint, xpaint, ypaint);
172  }
173  }
174 
175  if(initValue.isEmpty())
176  p->Painter->setPen(QPen(Qt::darkMagenta,0));
177  else
178  p->Painter->setPen(QPen(Qt::red,0));
179 
180  if(phi) p->drawArc(cx-4, cy-4, 8, 8, phi, 16*255);
181  p->Painter->drawLine(a, b, c, b);
182  p->Painter->drawLine(a, b, a, d);
183  p->Painter->drawLine(xpaint, ypaint, a, b);
184 
185  x2 = int(double(x2) / p->Scale);
186  y2 = int(double(y2) / p->Scale);
187 
188  if(isSelected)
189  {
190  p->Painter->setPen(QPen(Qt::darkGray,3));
191  p->drawRoundRect(x1-2, y1-2, x2+6, y2+5);
192  }
193 }
194 
195 // ----------------------------------------------------------------
196 void WireLabel::setName(const QString& Name_)
197 {
198  Name = Name_;
199 
200  // only set fonts if GUI is running
201  if ((Schematic*)QucsMain != 0) {
202  QFontMetrics metrics(((Schematic*)QucsMain->DocumentTab->currentPage())->font()); // get size of text
203  QSize r = metrics.size(0, Name);
204  x2 = r.width();
205  y2 = r.height()-2; // remember size of text
206  }
207 
208 }
209 
210 // ----------------------------------------------------------------
211 // Converts all necessary data of the wire into a string. This can be used to
212 // save it to an ASCII file or to transport it via the clipboard.
213 // Wire labels use the same format like wires, but with length zero.
215 {
216  QString s("<");
217  s += QString::number(cx)+" "+QString::number(cy)+" "
218  + QString::number(cx)+" "+QString::number(cy)
219  + " \""+Name +"\" "
220  + QString::number(x1)+" "+QString::number(y1)+" 0 \""
221  + initValue+"\">";
222  return s;
223 }
QPainter * Painter
Definition: viewpainter.h:58
bool getSelected(int, int)
Definition: wirelabel.cpp:97
int Type
Definition: element.h:152
bool isHighlighted
Definition: wirelabel.h:55
int y1
Definition: element.h:153
#define isNodeLabel
Definition: element.h:126
float Scale
Definition: viewpainter.h:59
void map(int, int, int &, int &)
Definition: viewpainter.cpp:63
void setName(const QString &Name_)
Definition: wirelabel.cpp:196
int y2
Definition: element.h:153
int x1
Definition: element.h:153
Definitions and declarations for the main application.
void paint(ViewPainter *)
Definition: wirelabel.cpp:109
QucsApp * QucsMain
Definition: main.cpp:54
int cx
Definition: element.h:153
void paintScheme(QPainter *p)
Definition: wirelabel.cpp:47
bool isSelected
Definition: element.h:151
int drawText(const QString &, int, int, int *Height=0)
#define isVMovingLabel
Definition: element.h:129
void drawRoundRect(int, int, int, int)
void setCenter(int x, int y, bool relative=false)
Definition: wirelabel.cpp:67
void drawArc(int, int, int, int, int, int)
WireLabel(const QString &_Name=0, int _cx=0, int _cy=0, int _x1=0, int _y1=0, int _Type=isNodeLabel)
Definition: wirelabel.cpp:27
#define isHWireLabel
Definition: element.h:124
#define isVWireLabel
Definition: element.h:125
QString save()
Definition: wirelabel.cpp:214
QTabWidget * DocumentTab
Definition: qucs.h:164
QString Name
Definition: wirelabel.h:46
int cy
Definition: element.h:153
#define isMovingLabel
Definition: element.h:127
#define isHMovingLabel
Definition: element.h:128
int x2
Definition: element.h:153
QString initValue
Definition: wirelabel.h:46