Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/wirelabel.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           wirelabel.cpp  -  description
00003                              -------------------
00004     begin                : Sun February 29 2004
00005     copyright            : (C) 2004 by Michael Margraf
00006     email                : michael.margraf@alumni.tu-berlin.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 #include "wirelabel.h"
00018 #include "viewpainter.h"
00019 #include "wire.h"
00020 #include "main.h"
00021 
00022 #include <QString>
00023 #include <QPainter>
00024 
00025 WireLabel::WireLabel(const QString& _Name, int _cx, int _cy,
00026                      int _x1, int _y1, int _Type)
00027 {
00028   cx = _cx;
00029   cy = _cy;
00030   x1 = _x1;
00031   y1 = _y1;
00032   setName(_Name);
00033   initValue = "";
00034 
00035   Type = _Type;
00036   isSelected = false;
00037   isHighlighted = false;
00038 }
00039 
00040 WireLabel::~WireLabel()
00041 {
00042 }
00043 
00044 // ----------------------------------------------------------------
00045 void WireLabel::paintScheme(QPainter *p)
00046 {
00047   p->drawRect(x1, y1, x2, y2);
00048 
00049   // which corner of rectangle should be connected to line ?
00050   if(cx < x1+(x2>>1)) {
00051     if(cy < y1+(y2>>1))
00052       p->drawLine(cx, cy, x1, y1);
00053     else
00054       p->drawLine(cx, cy, x1, y1+y2);
00055   }
00056   else {
00057     if(cy < y1+(y2>>1))
00058       p->drawLine(cx, cy, x1+x2, y1);
00059     else
00060       p->drawLine(cx, cy, x1+x2, y1+y2);
00061   }
00062 }
00063 
00064 // ----------------------------------------------------------------
00065 void WireLabel::setCenter(int x_, int y_, bool relative)
00066 {
00067   switch(Type) {
00068     case isMovingLabel:
00069       if(relative) {
00070         x1 += x_;  cx += x_;
00071         y1 += y_;  cy += y_;
00072       }
00073       else {
00074         x1 = x_;  cx = x_;
00075         y1 = y_;  cy = y_;
00076       }
00077       break;
00078     case isHMovingLabel:
00079       if(relative) { x1 += x_;  cx += x_; }
00080       else { x1 = x_;  cx = x_; }
00081       break;
00082     case isVMovingLabel:
00083       if(relative) { y1 += y_;  cy += y_; }
00084       else { y1 = y_;  cy = y_; }
00085       break;
00086     default:
00087       if(relative) {
00088         x1 += x_;  y1 += y_; // moving cx/cy is done by owner (wire, node)
00089       }
00090       else { x1 = x_; y1 = y_; }
00091   }
00092 }
00093 
00094 // ----------------------------------------------------------------
00095 bool WireLabel::getSelected(int x, int y)
00096 {
00097   if(x1 <= x)
00098     if(y1 <= y)
00099       if((x1+x2) >= x)
00100         if((y1+y2) >= y)
00101           return true;
00102 
00103   return false;
00104 }
00105 
00106 // ----------------------------------------------------------------
00107 void WireLabel::paint(ViewPainter *p)
00108 {
00109   QFont font = p->Painter->font();
00110   if (isHighlighted)
00111   {
00112 //    QColor highlightfill (Qt::blue);
00113 //    highlightfill.setAlpha(50);
00114 //    p->fillRect(x1-1, y1-1, x2, y2, highlightfill);
00115     p->Painter->setPen(QPen(Qt::darkBlue,3));
00116     font.setWeight (QFont::DemiBold);
00117   }
00118   else
00119   {
00120     font.setWeight (QFont::Normal);
00121     p->Painter->setPen(QPen(Qt::black,1));
00122   }
00123   p->Painter->setFont (font);
00124   x2 = p->drawText(Name, x1, y1, &y2);
00125 
00126   int xpaint=0, ypaint=4, phi=0;
00127   switch(Type) {
00128     case isVWireLabel:  ypaint=0; xpaint=4; phi=16*140; break;
00129     case isHWireLabel:  phi=16*50; break;
00130     case isNodeLabel:   ypaint = 0;
00131     default:            ;
00132   }
00133 
00134   int c, d;
00135   int a = int(double(x2) / p->Scale) >> 1;
00136   int b = int(double(y2) / p->Scale) >> 1;
00137   if(cx < x1+a) {    // where should frame be painted ?
00138     if(cy < y1+b) {
00139       if(phi == 16*50)  phi += 16*180;
00140       p->map(x1-3, y1-2, a, b);    // low right
00141       c = a + (x2>>1);
00142       d = b + y2;
00143       p->map(cx+xpaint, cy+ypaint, xpaint, ypaint);
00144     }
00145     else {
00146       if(phi != 0)  phi += 16*180;
00147       p->map(x1-3, y1+1, a, b);    // up right
00148       b += y2;
00149       c  = a + (x2>>1);
00150       d  = b - y2;
00151       p->map(cx+xpaint, cy-ypaint, xpaint, ypaint);
00152     }
00153   }
00154   else {
00155     if(cy < y1+b) {
00156       p->map(x1+3, y1-2, a, b);   // low left
00157       a += x2;
00158       c  = a - (x2>>1);
00159       d  = b + y2;
00160       p->map(cx-xpaint, cy+ypaint, xpaint, ypaint);
00161     }
00162     else {
00163       if(phi > 16*90)  phi += 16*180;
00164       p->map(x1+3, y1+1, a, b);    // up left
00165       a += x2;
00166       b += y2;
00167       c  = a - (x2>>1);
00168       d  = b - y2;
00169       p->map(cx-xpaint, cy-ypaint, xpaint, ypaint);
00170     }
00171   }
00172 
00173   if(initValue.isEmpty())
00174     p->Painter->setPen(QPen(Qt::darkMagenta,0));
00175   else
00176     p->Painter->setPen(QPen(Qt::red,0));
00177 
00178   if(phi)  p->drawArc(cx-4, cy-4, 8, 8, phi, 16*255);
00179   p->Painter->drawLine(a, b, c, b);
00180   p->Painter->drawLine(a, b, a, d);
00181   p->Painter->drawLine(xpaint, ypaint, a, b);
00182 
00183   x2 = int(double(x2) / p->Scale);
00184   y2 = int(double(y2) / p->Scale);
00185 
00186   if(isSelected)
00187   {
00188     p->Painter->setPen(QPen(Qt::darkGray,3));
00189     p->drawRoundRect(x1-2, y1-2, x2+6, y2+5);
00190   }
00191 }
00192 
00193 // ----------------------------------------------------------------
00194 void WireLabel::setName(const QString& Name_)
00195 {
00196   Name = Name_;
00197   
00198   // get size of text using the screen-compatible metric
00199   QFontMetrics metrics(QucsSettings.font, 0);
00200   QSize r = metrics.size(0, Name);
00201   x2 = r.width();
00202   y2 = r.height()-2;    // remember size of text
00203 }
00204 
00205 // ----------------------------------------------------------------
00206 // Converts all necessary data of the wire into a string. This can be used to
00207 // save it to an ASCII file or to transport it via the clipboard.
00208 // Wire labels use the same format like wires, but with length zero.
00209 QString WireLabel::save()
00210 {
00211   QString s("<");
00212   s += QString::number(cx)+" "+QString::number(cy)+" "
00213     +  QString::number(cx)+" "+QString::number(cy)
00214     +  " \""+Name +"\" "
00215     +  QString::number(x1)+" "+QString::number(y1)+" 0 \""
00216     +  initValue+"\">";
00217   return s;
00218 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines