Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[r579]: trunk / src / canvastip.cpp Maximize Restore History

Download this file

canvastip.cpp    165 lines (120 with data), 3.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// C++ Implementation: canvastip
//
// Description:
//
// Author: David Saxton, Alan Grimes <agrimes@speakeasy.net>, (C) 2009
//
// Copyright: See COPYING file that comes with this distribution
//
#include <cmath>
#include <qpainter.h>
#include <qapplication.h>
#include "canvastip.h"
#include "icndocument.h"
#include "ecnode.h"
#include "electronicconnector.h"
#include "circuitdocument.h"
#include "simulator.h"
#include "cnitem.h"
//BEGIN class CanvasTip
CanvasTip::CanvasTip(ItemDocument *itemDocument, QCanvas *qcanvas)
: QCanvasRectangle(qcanvas) {
p_itemDocument = itemDocument;
setZ(ICNDocument::Z::Tip);
}
CanvasTip::~CanvasTip() {
}
void CanvasTip::displayVI(ECNode *node, const QPoint &pos) {
if (!node || !updateVI())
return;
unsigned num = node->numPins();
info.resize(num);
for (unsigned i = 0; i < num; i++) {
if (Pin *pin = &node->pin(i)) {
info[i].V = pin->voltage();
info[i].I = std::abs(pin->calculateCurrentFromWires());
info[i].id = pin->eqId();
}
}
display(pos);
}
void CanvasTip::displayVI(Connector *connector, const QPoint &pos) {
if(!updateVI()) return;
ElectronicConnector *econnector = dynamic_cast<ElectronicConnector*>(connector);
if(!econnector) return;
unsigned num = econnector->numWires();
info.resize(num);
for (unsigned i = 0; i < num; i++) {
if (Wire *wire = econnector->wire(i)) {
info[i].V = wire->voltage();
info[i].I = std::abs(wire->current());
info[i].id = -2;
}
}
display(pos);
}
bool CanvasTip::updateVI() {
CircuitDocument *circuitDocument = dynamic_cast<CircuitDocument*>(p_itemDocument);
if (!circuitDocument || !Simulator::self()->isSimulating())
return false;
circuitDocument->calculateConnectorCurrents();
return true;
}
void CanvasTip::display(const QPoint &pos) {
unsigned num = info.size();
#ifdef BULLSHIT
for (unsigned i = 0; i < num; i++) {
if (!std::isfinite(m_v[i]) || std::abs(m_v[i]) < 1e-9)
m_v[i] = 0.;
if (!std::isfinite(m_i[i]) || std::abs(m_i[i]) < 1e-9)
m_i[i] = 0.;
}
#endif
move(pos.x() + 20, pos.y() + 4);
if (num == 0) return;
if (num == 1)
setText(displayText(0));
else {
QString text;
for (unsigned i = 0; i < num; i++)
text += QString("%1: %2\n").arg(QString::number(i)).arg(displayText(i));
setText(text);
}
}
QString CanvasTip::displayText(unsigned num) const {
if(info.size() <= num)
return QString::null;
if(info[0].id != -2) {
return QString("%1%2V %3%4A #%5")
.arg(QString::number(info[num].V / CNItem::getMultiplier(info[num].V), 'g', 3))
.arg(CNItem::getNumberMag(info[num].V))
.arg(QString::number(info[num].I / CNItem::getMultiplier(info[num].I), 'g', 3))
.arg(CNItem::getNumberMag(info[num].I))
.arg(QString::number(info[num].id));
}
return QString("%1%2V %3%4A")
.arg(QString::number(info[num].V / CNItem::getMultiplier(info[num].V), 'g', 3))
.arg(CNItem::getNumberMag(info[num].V))
.arg(QString::number(info[num].I / CNItem::getMultiplier(info[num].I), 'g', 3))
.arg(CNItem::getNumberMag(info[num].I));
}
void CanvasTip::draw(QPainter &p) {
CircuitDocument *circuitDocument = dynamic_cast<CircuitDocument*>(p_itemDocument);
if (!circuitDocument || !Simulator::self()->isSimulating())
return;
p.setBrush(QColor(0xff, 0xff, 0xdc));
p.setPen(Qt::black);
p.drawRect(boundingRect());
QRect textRect = boundingRect();
textRect.setLeft(textRect.left() + 3);
textRect.setTop(textRect.top() + 1);
p.drawText(textRect, 0, m_text);
}
void CanvasTip::setText(const QString &text) {
m_text = text;
canvas()->setChanged(boundingRect());
QRect r = QFontMetrics(qApp->font()).boundingRect(0, 0, 0, 0, 0, m_text);
setSize(r.width() + 4, r.height() - 1);
}
//END class CanvasTip