Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/wire.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           wire.cpp  -  description
00003                              -------------------
00004     begin                : Wed Sep 3 2003
00005     copyright            : (C) 2003 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 "wire.h"
00018 
00019 #include <QPainter>
00020 
00021 Wire::Wire(int _x1, int _y1, int _x2, int _y2, Node *n1, Node *n2)
00022 {
00023   cx = 0;
00024   cy = 0;
00025   x1 = _x1;
00026   y1 = _y1;
00027   x2 = _x2;
00028   y2 = _y2;
00029   Port1 = n1;
00030   Port2 = n2;
00031   Label  = 0;
00032 
00033   Type = isWire;
00034   isSelected = false;
00035 }
00036 
00037 Wire::~Wire()
00038 {
00039 }
00040 
00041 // ----------------------------------------------------------------
00042 void Wire::rotate()
00043 {
00044   int xm, ym, tmp;
00045 
00046   xm = (x1+x2) >> 1;
00047   ym = (y1+y2) >> 1;
00048 
00049   tmp = x1;
00050   x1  = xm + y1  - ym;
00051   y1  = ym - tmp + xm;
00052 
00053   tmp = x2;
00054   x2  = xm + y2  - ym;
00055   y2  = ym - tmp + xm;
00056 
00057   if(Label) {
00058     tmp = Label->cx;
00059     Label->cx = xm + Label->cy - ym;
00060     Label->cy = ym - tmp + xm;
00061     if(Label->Type == isHWireLabel) Label->Type = isVWireLabel;
00062     else Label->Type = isHWireLabel;
00063   }
00064 }
00065 
00066 // ----------------------------------------------------------------
00067 void Wire::setCenter(int x, int y, bool relative)
00068 {
00069   if(relative) {
00070     x1 += x;  x2 += x;
00071     y1 += y;  y2 += y;
00072 //    if(Label) Label->setCenter(x, y, true);
00073   }
00074   else {
00075     x1 = x;  x2 = x;
00076     y1 = y;  y2 = y;
00077   }
00078 }
00079 
00080 // ----------------------------------------------------------------
00081 void Wire::getCenter(int& x, int& y)
00082 {
00083   x = (x1+x2) >> 1;
00084   y = (y1+y2) >> 1;
00085 }
00086 
00087 // ----------------------------------------------------------------
00088 // Lie x/y on wire ? 5 is the precision the coordinates have to fit.
00089 bool Wire::getSelected(int x_, int y_)
00090 {
00091   if(x1-5 <= x_) if(x2+5 >= x_) if(y1-5 <= y_) if(y2+5 >= y_)
00092     return true;
00093 
00094   return false;
00095 }
00096 
00097 // ----------------------------------------------------------------
00098 void Wire::paintScheme(QPainter *p)
00099 {
00100   p->drawLine(x1, y1, x2, y2);
00101 //  if(Label)
00102 //    if((Label->Type == isHWireLabel) || (Label->Type == isHWireLabel))
00103 //    if(Label->Type == isHWireLabel)
00104 //      Label->paintScheme(p);
00105 }
00106 
00107 // ----------------------------------------------------------------
00108 void Wire::paint(ViewPainter *p)
00109 {
00110   if(isSelected) {
00111     p->Painter->setPen(QPen(Qt::darkGray,6));
00112     p->drawLine(x1, y1, x2, y2);
00113     p->Painter->setPen(QPen(Qt::lightGray,2));
00114     p->drawLine(x1, y1, x2, y2);
00115   }
00116   else {
00117     p->Painter->setPen(QPen(Qt::darkBlue,2));
00118     p->drawLine(x1, y1, x2, y2);
00119   }
00120 }
00121 
00122 // ----------------------------------------------------------------
00123 bool Wire::isHorizontal()
00124 {
00125   return (y1 == y2);
00126 }
00127 
00128 // ----------------------------------------------------------------
00129 void Wire::setName(const QString& Name_, const QString& Value_, int delta_, int x_, int y_)
00130 {
00131   if(Name_.isEmpty() && Value_.isEmpty()) {
00132     if(Label) delete Label;
00133     Label = 0;
00134     return;
00135   }
00136 
00137   if(!Label) {
00138     if(isHorizontal())
00139       Label = new WireLabel(Name_, x1+delta_, y1, x_, y_, isHWireLabel);
00140     else
00141       Label = new WireLabel(Name_, x1, y1+delta_, x_, y_, isVWireLabel);
00142     Label->pOwner = this;
00143     Label->initValue = Value_;
00144   }
00145   else Label->setName(Name_);
00146 }
00147 
00148 // ----------------------------------------------------------------
00149 // Converts all necessary data of the wire into a string. This can be used to
00150 // save it to an ASCII file or to transport it via the clipboard.
00151 QString Wire::save()
00152 {
00153   QString s  = "<"+QString::number(x1)+" "+QString::number(y1);
00154           s += " "+QString::number(x2)+" "+QString::number(y2);
00155   if(Label) {
00156           s += " \""+Label->Name+"\" ";
00157           s += QString::number(Label->x1)+" "+QString::number(Label->y1)+" ";
00158           s += QString::number(Label->cx-x1 + Label->cy-y1);
00159           s += " \""+Label->initValue+"\">";
00160   }
00161   else { s += " \"\" 0 0 0 \"\">"; }
00162   return s;
00163 }
00164 
00165 // ----------------------------------------------------------------
00166 // This is the counterpart to Wire::save.
00167 bool Wire::load(const QString& _s)
00168 {
00169   bool ok;
00170   QString s = _s;
00171 
00172   if(s.at(0) != '<') return false;
00173   if(s.at(s.length()-1) != '>') return false;
00174   s = s.mid(1, s.length()-2);   // cut off start and end character
00175 
00176   QString n;
00177   n  = s.section(' ',0,0);    // x1
00178   x1 = n.toInt(&ok);
00179   if(!ok) return false;
00180 
00181   n  = s.section(' ',1,1);    // y1
00182   y1 = n.toInt(&ok);
00183   if(!ok) return false;
00184 
00185   n  = s.section(' ',2,2);    // x2
00186   x2 = n.toInt(&ok);
00187   if(!ok) return false;
00188 
00189   n  = s.section(' ',3,3);    // y2
00190   y2 = n.toInt(&ok);
00191   if(!ok) return false;
00192 
00193   n = s.section('"',1,1);
00194   if(!n.isEmpty()) {     // is wire labeled ?
00195     int nx = s.section(' ',5,5).toInt(&ok);   // x coordinate
00196     if(!ok) return false;
00197 
00198     int ny = s.section(' ',6,6).toInt(&ok);   // y coordinate
00199     if(!ok) return false;
00200 
00201     int delta = s.section(' ',7,7).toInt(&ok);// delta for x/y root coordinate
00202     if(!ok) return false;
00203 
00204     setName(n, s.section('"',3,3), delta, nx, ny);  // Wire Label
00205   }
00206 
00207   return true;
00208 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines