Qucs-GUI
0.0.19
|
00001 /*************************************************************************** 00002 portsymbol.cpp - description 00003 ------------------- 00004 begin : Sun Sep 5 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 "main.h" 00018 #include "portsymbol.h" 00019 #include "schematic.h" 00020 00021 #include <QPainter> 00022 00023 PortSymbol::PortSymbol(int cx_, int cy_, const QString& numberStr_, 00024 const QString& nameStr_) 00025 { 00026 Name = ".PortSym "; 00027 isSelected = false; 00028 cx = cx_; 00029 cy = cy_; 00030 00031 Angel = 0; 00032 nameStr = nameStr_; 00033 numberStr = numberStr_; 00034 // get size of text using the screen-compatible metric 00035 QFontMetrics metrics(QucsSettings.font, 0); 00036 QSize r = metrics.size(0, nameStr); 00037 x1 = -r.width() - 8; 00038 y1 = -((r.height() + 8) >> 1); 00039 x2 = 8 - x1; 00040 y2 = r.height() + 8; 00041 } 00042 00043 PortSymbol::~PortSymbol() 00044 { 00045 } 00046 00047 // -------------------------------------------------------------------------- 00048 void PortSymbol::paint(ViewPainter *p) 00049 { 00050 // keep track of painter state 00051 p->Painter->save(); 00052 00053 p->Painter->setPen(QPen(Qt::red,1)); // like open node 00054 p->drawEllipse(cx-4, cy-4, 8, 8); 00055 00056 QSize r = p->Painter->fontMetrics().size(0, nameStr); 00057 int Unit = int(8.0 * p->Scale); 00058 x1 = -r.width() - Unit; 00059 y1 = -((r.height() + Unit) >> 1); 00060 x2 = Unit - x1; 00061 y2 = r.height() + Unit; 00062 00063 QMatrix wm = p->Painter->worldMatrix(); 00064 QMatrix Mat(1.0, 0.0, 0.0, 1.0, p->DX + float(cx) * p->Scale, 00065 p->DY + float(cy) * p->Scale); 00066 p->Painter->setWorldMatrix(Mat); 00067 00068 int tmp, tx, ty; 00069 tx = x1 + (Unit >> 1); 00070 ty = y1 + (Unit >> 1); 00071 switch(Angel) { 00072 case 90: 00073 x1 = y1; 00074 y1 = -Unit; 00075 tmp = x2; x2 = y2; y2 = tmp; 00076 p->Painter->rotate(-90.0); // automatically enables transformation 00077 break; 00078 case 180: 00079 x1 = -Unit; 00080 tx = Unit >> 1; 00081 break; 00082 case 270: 00083 tx = Unit >> 1; 00084 tmp = x1; x1 = y1; y1 = tmp; 00085 tmp = x2; x2 = y2; y2 = tmp; 00086 p->Painter->rotate(-90.0); // automatically enables transformation 00087 break; 00088 } 00089 00090 p->Painter->setPen(Qt::black); 00091 p->Painter->drawText(tx, ty, 0, 0, Qt::TextDontClip, nameStr); 00092 00093 00094 p->Painter->setWorldMatrix(wm); 00095 p->Painter->setWorldMatrixEnabled(false); 00096 00097 // restore painter state 00098 p->Painter->restore(); 00099 00100 x1 = int(float(x1) / p->Scale); 00101 x2 = int(float(x2) / p->Scale); 00102 y1 = int(float(y1) / p->Scale); 00103 y2 = int(float(y2) / p->Scale); 00104 00105 p->Painter->setPen(Qt::lightGray); 00106 p->drawRect(cx+x1, cy+y1, x2, y2); 00107 00108 if(isSelected) { 00109 p->Painter->setPen(QPen(Qt::darkGray,3)); 00110 p->drawRoundRect(cx+x1-4, cy+y1-4, x2+8, y2+8); 00111 } 00112 } 00113 00114 // -------------------------------------------------------------------------- 00115 void PortSymbol::paintScheme(Schematic *p) 00116 { 00117 p->PostPaintEvent(_Ellipse, cx-4, cy-4, 8, 8); 00118 p->PostPaintEvent(_Rect, cx+x1, cy+y1, x2, y2); 00119 } 00120 00121 // -------------------------------------------------------------------------- 00122 void PortSymbol::getCenter(int& x, int &y) 00123 { 00124 x = cx; 00125 y = cy; 00126 } 00127 00128 // -------------------------------------------------------------------------- 00129 // Sets the center of the painting to x/y. 00130 void PortSymbol::setCenter(int x, int y, bool relative) 00131 { 00132 if(relative) { cx += x; cy += y; } 00133 else { cx = x; cy = y; } 00134 } 00135 00136 // -------------------------------------------------------------------------- 00137 bool PortSymbol::load(const QString& s) 00138 { 00139 bool ok; 00140 00141 QString n; 00142 n = s.section(' ',1,1); // cx 00143 cx = n.toInt(&ok); 00144 if(!ok) return false; 00145 00146 n = s.section(' ',2,2); // cy 00147 cy = n.toInt(&ok); 00148 if(!ok) return false; 00149 00150 numberStr = s.section(' ',3,3); // number 00151 if(numberStr.isEmpty()) return false; 00152 00153 n = s.section(' ',4,4); // Angel 00154 if(n.isEmpty()) return true; // be backward-compatible 00155 Angel = n.toInt(&ok); 00156 if(!ok) return false; 00157 00158 return true; 00159 } 00160 00161 // -------------------------------------------------------------------------- 00162 QString PortSymbol::save() 00163 { 00164 QString s = Name+QString::number(cx)+" "+QString::number(cy)+" "; 00165 s += numberStr+" "+QString::number(Angel); 00166 return s; 00167 } 00168 00169 // -------------------------------------------------------------------------- 00170 QString PortSymbol::saveCpp() 00171 { 00172 QString s = 00173 QString ("new Port (%1, %2)"). 00174 arg(cx).arg(cy); 00175 s = "Ports.append (" + s + "); /* " + nameStr + " */"; 00176 return s; 00177 } 00178 00179 QString PortSymbol::saveJSON() 00180 { 00181 QString s = QString ("{\"type\" : \"portsymbol\", " 00182 "\"x\" : %1, \"y\" : %2},").arg(cx).arg(cy); 00183 return s; 00184 } 00185 00186 // -------------------------------------------------------------------------- 00187 // Checks if the coordinates x/y point to the painting. 00188 bool PortSymbol::getSelected(float fX, float fY, float) 00189 { 00190 if(int(fX) < cx+x1) return false; 00191 if(int(fY) < cy+y1) return false; 00192 if(int(fX) > cx+x1+x2) return false; 00193 if(int(fY) > cy+y1+y2) return false; 00194 00195 return true; 00196 } 00197 00198 // -------------------------------------------------------------------------- 00199 void PortSymbol::Bounding(int& _x1, int& _y1, int& _x2, int& _y2) 00200 { 00201 _x1 = cx+x1; _y1 = cy+y1; 00202 _x2 = cx+x1+x2; _y2 = cy+y1+y2; 00203 } 00204 00205 // -------------------------------------------------------------------------- 00206 // Rotates around the center. 00207 void PortSymbol::rotate() 00208 { 00209 if(Angel < 270) Angel += 90; 00210 else Angel = 0; 00211 } 00212 00213 // -------------------------------------------------------------------------- 00214 // Mirrors about connection node (not center line !). 00215 void PortSymbol::mirrorX() 00216 { 00217 if(Angel == 90) Angel = 270; 00218 else if(Angel == 270) Angel = 90; 00219 } 00220 00221 // -------------------------------------------------------------------------- 00222 // Mirrors about connection node (not center line !). 00223 void PortSymbol::mirrorY() 00224 { 00225 if(Angel == 0) Angel = 180; 00226 else if(Angel == 180) Angel = 0; 00227 }