Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Commits on Oct 14, 2013
Zoltan Padrah add VoltageAndCurrentItem e43bfc4
Zoltan Padrah show tooltip when the cursor covers a voltage and current item.
Note that the current implementation is buggy, because it moves the item when it is displaying the tooltip first time.
4a7dee0
View
1  src/lib/circuit/CMakeLists.txt
@@ -9,6 +9,7 @@ set(circuit_SRCS
theme.cpp
circuitmodel.cpp
simulator/genericelementfactory.cpp
+ voltageandcurrentitem.cpp
)
kde4_add_library( ktlcircuit SHARED ${circuit_SRCS} )
View
5 src/lib/circuit/pinitem.cpp
@@ -24,11 +24,14 @@
#include <qdrag.h>
#include <interfaces/component/icomponentitem.h>
+#include "voltageandcurrentitem.h"
+
using namespace KTechLab;
PinItem::PinItem(const QRectF& rect, IComponentItem* parent, IDocumentScene* scene)
: Node(parent, scene),
- m_circuit(scene)
+ m_circuit(scene),
+ m_voltageAndCurrentDisplay(new VoltageAndCurrentItem(rect, this, scene))
{
setFlag(ItemIsSelectable, false);
setFlag(ItemIsMovable, false);
View
4 src/lib/circuit/pinitem.h
@@ -23,11 +23,14 @@
#include "interfaces/component/node.h"
#include "circuitexport.h"
+#include <QSharedPointer>
+
namespace KTechLab
{
class IComponentItem;
class IDocumentScene;
+class VoltageAndCurrentItem;
/**
* A QGraphicsItem used to indicate mouse interaction. When this
@@ -44,6 +47,7 @@ class CIRCUIT_EXPORT PinItem : public Node
private:
IDocumentScene* m_circuit;
+ QSharedPointer<VoltageAndCurrentItem> m_voltageAndCurrentDisplay;
};
}
View
145 src/lib/circuit/voltageandcurrentitem.cpp
@@ -0,0 +1,145 @@
+/*
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) 2013 Zoltan Padrah <zoltan.padrah@gmail.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+
+#include "voltageandcurrentitem.h"
+
+#include "interfaces/idocumentscene.h"
+
+#include <QGraphicsSceneMouseEvent>
+#include <QGraphicsSimpleTextItem>
+#include <QDebug>
+
+#include <cmath>
+
+using namespace KTechLab;
+
+const qreal VoltageAndCurrentItem::m_centerX = 0;
+const qreal VoltageAndCurrentItem::m_centerY = -10;
+
+VoltageAndCurrentItem::VoltageAndCurrentItem(const QRectF& rect, QGraphicsItem* parent, IDocumentScene* scene) :
+ QGraphicsRectItem(parent, scene),
+ m_voltageInV(0), m_currentInA(0),
+ m_tooltip(new QGraphicsSimpleTextItem(this, scene))
+{
+ Q_UNUSED(rect);
+ m_tooltip->setPos(0, -30);
+ m_tooltip->hide();
+ setFlag(ItemIsSelectable, false);
+ setFlag(ItemIsMovable, false);
+ setFlag(ItemIgnoresParentOpacity);
+ setAcceptHoverEvents(true);
+ setOpacity(0.95);
+ updateAppearance();
+}
+
+VoltageAndCurrentItem::~VoltageAndCurrentItem()
+{
+
+}
+
+void VoltageAndCurrentItem::setVoltage(qreal voltageInV)
+{
+ m_voltageInV = voltageInV;
+ updateAppearance();
+}
+
+void VoltageAndCurrentItem::setCurrent(qreal currentInA)
+{
+ m_currentInA = currentInA;
+ updateAppearance();
+}
+
+void VoltageAndCurrentItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
+{
+ m_tooltip->show();
+ event->accept();
+}
+
+void VoltageAndCurrentItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
+{
+ m_tooltip->hide();
+ event->accept();
+}
+
+
+/// The maximum length of the voltage indiactor
+const int vLength = 8;
+
+/// The current at the middle of the current indicator
+const qreal iMidPoint = 0.03;
+
+/// The maximum thicnkess of the current indicator
+const int iLength = 8;
+
+inline qreal calcIProp(const qreal i) {
+ return 1 - iMidPoint / (iMidPoint + std::abs(i));
+}
+
+inline int calcThickness(const qreal prop) {
+ return (int)((iLength -2)*prop + 2);
+}
+
+qreal voltageLength(qreal v) {
+ qreal v_max = 1e1;
+ qreal v_min = 1e-1;
+
+ v = std::abs(v);
+
+ if(v >= v_max) return 1.0;
+ else if(v <= v_min) return 0.0;
+ else return std::log(v / v_min) / std::log(v_max / v_min);
+}
+
+inline int calcLength(qreal v) {
+ qreal prop = voltageLength(v);
+
+ if (v > 0)
+ prop *= -1.0;
+
+ return int(vLength * prop);
+}
+
+
+
+QColor voltageColor(qreal v) {
+ qreal prop = voltageLength(v);
+
+ if(v >= 0)
+ return QColor(int(255 * prop), int(166 * prop), 0);
+ else return QColor(0, int(136 * prop), int(255 * prop));
+}
+
+void VoltageAndCurrentItem::updateAppearance()
+{
+ QColor color = voltageColor(m_voltageInV);
+ qreal iProp = calcIProp(m_currentInA);
+ int thickness = calcThickness(iProp);
+ int length = calcLength(m_voltageInV);
+
+ setPen(QPen(color, thickness));
+
+ qreal startX = m_centerX - length / 2;
+ qreal y = m_centerY - thickness / 2;
+ qreal width = length;
+ qreal height = thickness;
+ setRect(startX, y, width, height);
+
+ m_tooltip->setText(QString("V = %1V; I = %2A").arg(m_voltageInV).arg(m_currentInA));
+}
View
64 src/lib/circuit/voltageandcurrentitem.h
@@ -0,0 +1,64 @@
+/*
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) 2013 Zoltan Padrah <zoltan.padrah@gmail.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+
+#ifndef VOLTAGEANDCURRENTITEM_H
+#define VOLTAGEANDCURRENTITEM_H
+
+#include <QGraphicsRectItem>
+
+class QGraphicsSceneHoverEvent;
+class QGraphicsSimpleTextItem;
+
+namespace KTechLab {
+
+class IDocumentScene;
+
+
+/**
+ * Class to display a rectangle with the height being the voltage and the width being the current.
+ * The color intensity depends on the voltage.
+ */
+class VoltageAndCurrentItem : public QGraphicsRectItem
+{
+ qreal m_voltageInV;
+ qreal m_currentInA;
+ QSharedPointer<QGraphicsSimpleTextItem> m_tooltip;
+
+public:
+ VoltageAndCurrentItem(const QRectF & rect, QGraphicsItem* parent = 0, IDocumentScene* scene = 0);
+ virtual ~VoltageAndCurrentItem();
+
+ void setVoltage(qreal voltageInV);
+ void setCurrent(qreal currentInA);
+
+protected:
+ virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event );
+ virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event );
+
+protected:
+ void updateAppearance();
+
+ static const qreal m_centerX;
+ static const qreal m_centerY;
+};
+
+}
+
+#endif // VOLTAGEANDCURRENTITEM_H

No commit comments for this range

Something went wrong with that request. Please try again.