Documentation of all program constructs

class Ohmcha::Branch : public Ohmcha::Component
#include <component.h>

An electrical branch.

Public Functions

Branch()

A default branch, equivalent to a short circuit.

Branch(const Branch &branch1, const Branch &branch2)

Create a branch by merging two branches that contain a common node.

Exceptions
  • TODO..if: they don’t have a common node

Branch(RowVector3f A, float B)
Branch(Node &node1, Node &node2, RowVector3f A, float B)
Branch(Component *component, Node *node1 = nullptr, Node *node2 = nullptr)
Component *copy() const override
void addComponent(Component *component, bool inverted = false)
void setA(const RowVector3f &matrix)
void setB(float x)
void setNode1(Node &n)
void setNode2(Node &n)
void setNode1(Node *n)
void setNode2(Node *n)
RowVector3f getA() const
float getB() const
Node *getNode1() const
Node *getNode2() const
int getTerminalCount() const override
bool hasNode(Node *node) const
Node *getOtherNode(Node *node) const

Public Members

std::vector<Component*> attached
std::vector<bool> inversions

Protected Attributes

Node *node1 = {}

Current flows through this object from node 1 to node 2. Consequently, the reference voltage is positive if node 1 is at a higher potential than node 2.

Node *node2 = {}
RowVector3f A

A branch has three quantities: V1 (potential at node1), V2 (potential at node2), I (current from node1 to node2). to represent a linear branch, we require an equation A * transpose([ V1 V2 I ]) = B.

float B
class Ohmcha::CircuitView : public QGraphicsView

Public Types

enum Mode

Values:

enumerator Idle
enumerator InsertDragDrop
enumerator InsertPoints

Public Functions

CircuitView(QWidget *parent = nullptr)
CircuitView(QWidget *parent, Schematic *schematic)
~CircuitView()
void initialize()
void initiateInsertComponent(GraphicComponent *component, Mode insertMode)
void solve()
void showValues(bool show = true)
void load(Schematic *schematic)
void zoomIn(float scale = 1.2)
void resetZoom()
CircuitViewScene *scene()
void setGridVisibility(bool visibility)

Show/hide the grid.

void setSnap(bool state)

Enable/disable snap to grid.

void setComponentPreview(ComponentPreview *componentPreview)
void setZoomLevel(float zoom)
QPointF getCursorPosition()

Return the cursor position with any snap-to-grid applied.

ComponentPreview *getComponentPreview()
Schematic *getSchematic()

Return the Schematic representation of this circuit. The result is guaranteed to be non-null.

float getZoomLevel() const

Signals

void componentSelected(GraphicComponent*)

Emitted when a component is selected individually.

void itemDeleted(GraphicComponent*)

Private Functions

void wheelEvent(QWheelEvent *event) override
void mousePressEvent(QMouseEvent *event) override
void mouseReleaseEvent(QMouseEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
void keyPressEvent(QKeyEvent *event) override
void resizeEvent(QResizeEvent *event) override
void updateCursorGuides()

Update cursor guides’ position based on the current cursor position.

void snapToGrid()

Change the internally tracked cursor position to make it snap to grid.

Private Members

float zoomLevel = 1
QGraphicsLineItem *hGuide = {}
QGraphicsLineItem *vGuide = {}
GraphicComponent *pendingInsert = {}
ComponentPreview *componentPreview = {}
Schematic *schematic = {}
QList<QGraphicsItem*> results
Mode mode = Idle
QPointF rawCursorPos
QPointF cursorPos
float gridSpacingX = 20
float gridSpacingY = 20
bool snapOn = false
bool showGrid = true
bool _selectionModeDetermined = false
QPointF _dragPos
bool _dragging = false

Friends

friend class CircuitViewScene
class Ohmcha::CircuitViewScene : public QGraphicsScene

Public Functions

CircuitViewScene(CircuitView *circuitView)
void clear()
bool isInsertingComponent() const
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
void keyPressEvent(QKeyEvent *event) override
void terminalClickEvent(GraphicComponent *source, QPointF terminal)

Private Members

GraphicBranch *_insertedBranch = {}
CircuitView *circuitView

Private Slots

void on_selectionChanged()
class Ohmcha::Component

Subclassed by Ohmcha::Branch, Ohmcha::CurrentSource, Ohmcha::Emf, Ohmcha::Node, Ohmcha::Resistor

Public Functions

Component *copy() const = 0
void setName(const std::string &name)
void setPosition(Pos *position)

Set the position of this component in the schematic. If position is null, the position is considered arbitrary.

void setAngle(float angle)
void setTextPos(const Pos &pos)
void setTextAngle(float angle)
void setTextOrientationIndependent(bool flag)
void setTextAnchor(int anchor)
std::string getName() const

Return

Name given by the user.

Pos *getPosition() const

See setPosition

float getAngle() const
Pos getTextPos() const
float getTextAngle() const
bool isTextOrientationIndependent() const
int getTextAnchor() const
int getTerminalCount() const = 0

Public Static Functions

Component *newByName(std::string name)

Dynamically allocate a Component based on the specified class name.

Return

Pointer to the dynamically allocated object.

Protected Attributes

MetaInfo *metaInfo = nullptr
class Ohmcha::ComponentPreview : public QWidget

Public Functions

ComponentPreview(QWidget *parent = nullptr)
~ComponentPreview()
void initialize()

Connect this preview to the active circuit view. Call this when first showing this widget or when the active circuit view changes.

void initializeNewComponent(const QString &type)

Create a new GraphicComponent, with the same properties as the previous component.

void synchronize()

Synchronize the contents of the text boxes and buttons with the actual properties of component.

void setCircuitView(CircuitView *cv)
void setComponentType(const QString &type)

Public Slots

void setEditExisting(GraphicComponent *component)

Private Functions

void updatePreview()

Call this when component is modified.

Private Members

Ui::ComponentPreview *ui
GraphicComponent *component = nullptr

component is guaranteed to be non-null whenever this widget is visible.

QString lastComponentType
QString selectedComponentType
CircuitView *circuitView = nullptr

The circuit view that is currently active in the window

QRadioButton anchors[3][3]

A 3x3 grid of buttons used to choose the text anchor

QButtonGroup *anchorGroup = {}

Private Slots

void on_selectionChanged()

Called when the selection in the circuit view changes

void on_itemDeleted(GraphicComponent *component)
void on_textAnchorPicked(int id)
void on_textIndependencePicked(int id)
void on_btnAdd_clicked()
void on_editAngle_textEdited(const QString &s)
void on_editText_textEdited(const QString &s)
void on_editTextAngle_textEdited(const QString &s)
void on_editTextX_textEdited(const QString &s)
void on_editTextY_textEdited(const QString &s)
void on_editValue_textEdited(const QString &s)
class Ohmcha::CurrentSource : public Ohmcha::Component

Public Functions

CurrentSource()
CurrentSource(float current)
Component *copy() const override
void setCurrent(float current)
int getTerminalCount() const override
float getCurrent() const
Pos getTextPos() const override

Private Members

float current = 1
class Ohmcha::Emf : public Ohmcha::Component

Public Functions

Emf()
Emf(float emf)
Component *copy() const override
void setEmf(float emf)
int getTerminalCount() const override
float getEmf() const
Pos getTextPos() const override

Private Members

float emf = 1
class Ohmcha::GraphicBranch : public Ohmcha::GraphicComponent

Public Functions

GraphicBranch()
GraphicBranch(Branch *branch)
GraphicBranch(const GraphicBranch &original)
~GraphicBranch()
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override
QRectF boundingRect() const override
QPainterPath shape() const override
void swapTerminals()
GraphicComponent *copy() const override
void split(QPointF point)

Create a node at point and split the branch into two branches connected to that node. Point is specified in local coordinates.

void setFirstAnchor(GraphicComponent *item, const QPointF &point)

Point is specified in item’s coordinate system. If item is null, then the point is specified in scene coordinates. This is useful in the interim while connecting two components together.

void setSecondAnchor(GraphicComponent *item, const QPointF &point)

See setFirstAnchor

GraphicComponent *getFirstAnchor() const
QPointF getFirstAnchorPoint() const
QPointF getSecondAnchorPoint() const
GraphicComponent *getSecondAnchor() const
bool isConnectedTo(GraphicComponent *item) const
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override

Private Members

GraphicComponent *first = {}

The components to which this branch is attached

GraphicComponent *second = {}
QPointF pFirst

The points on first/second to which this branch is attached, in their respective coordinate systems.

QPointF pSecond
QPen pen
class Ohmcha::GraphicComponent : public QGraphicsItem

Subclassed by Ohmcha::GraphicBranch, Ohmcha::GraphicCurrentSource, Ohmcha::GraphicEmf, Ohmcha::GraphicNode, Ohmcha::GraphicResistor

Public Types

enum Anchor

Values:

enumerator NW
enumerator N
enumerator NE
enumerator W
enumerator C
enumerator E
enumerator SW
enumerator S
enumerator SE

Public Functions

GraphicComponent()
GraphicComponent(const GraphicComponent &original)
const QPointF *findNearestTerminal(QPointF point) const

Return the terminal that is within a certain distance to the specified point. If there is no such terminal, return null. The point is specified in this item’s coordinate system.

GraphicComponent *copy() const = 0
void synchronize()
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
void setName(QString name)
void setCenter(QPointF center)
void setRotation(float angle)
void setTextAnchor(Anchor anchor)
void setTextPosition(QPointF pos)
void setTextRotation(float angle)
void setTextRotationIndependent(bool independent)
void setComponent(Component *component)
QSizeF getSize() const
QString getName() const
QPointF getCenter() const
Anchor getTextAnchor() const
QPointF getTextPosition() const
float getTextRotation() const
bool isTextRotationIndependent() const
Component *getComponent() const
CircuitViewScene *getScene() const
int getTerminalId(const QPointF &terminal) const
QList<QPointF> getTerminals() const
void onTerminalClicked(QPointF position)
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override
void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override

Public Static Functions

GraphicComponent *newFromComponent(Component *component)

Dynamically allocate a GraphicComponent wrapper around the specified Component. The type of the resulting object is determined based on the type of Component. TODO: implement.

Protected Functions

void applyColors(QPainter *painter, const QStyleOptionGraphicsItem *option)
void drawText(QPainter *painter)

Protected Attributes

QList<QPointF> terminals
bool branch1 = false
bool branch2 = false
QSizeF size
Component *component = nullptr
Anchor textAnchor = W
QPointF textPos
float textAngle = 0
bool textRotationIndependent = false
const QPointF *_selectedTerminal = nullptr

Friends

friend class GraphicBranch
friend class CircuitView
class Ohmcha::GraphicCurrentSource : public Ohmcha::GraphicComponent

Public Functions

GraphicCurrentSource()
GraphicCurrentSource(CurrentSource *source)
GraphicCurrentSource(const GraphicCurrentSource &original)
QRectF boundingRect() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override
GraphicComponent *copy() const override

Private Members

QPen pen
class Ohmcha::GraphicEmf : public Ohmcha::GraphicComponent

Public Functions

GraphicEmf()
GraphicEmf(Emf *emf)
GraphicEmf(const GraphicEmf &original)
QRectF boundingRect() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override
GraphicComponent *copy() const override

Private Members

QPen pen
class Ohmcha::GraphicNode : public Ohmcha::GraphicComponent

Public Functions

GraphicNode()
GraphicNode(Node *node)
GraphicNode(const GraphicNode &original)
~GraphicNode()
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override
QRectF boundingRect() const override
GraphicComponent *copy() const override
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
class Ohmcha::GraphicResistor : public Ohmcha::GraphicComponent

Public Functions

GraphicResistor()
GraphicResistor(Resistor *resistor)
GraphicResistor(QPointF node1, QPointF node2)
GraphicResistor(const GraphicResistor &original)
QRectF boundingRect() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override
GraphicComponent *copy() const override
void setCenter(QPointF center) override
Resistor *getComponent()

Private Members

float terminalSize = 10
QPen pen
class Ohmcha::MainWindow : public QMainWindow

Public Functions

MainWindow(QWidget *parent = nullptr)
~MainWindow()
CircuitView *getCircuitView()

Return

The currently displayed Circuit View.

Private Functions

void initializeComponentList()

Refers to the component list in the docked widget.

Private Members

Ui::MainWindow *ui
QString fileName

Private Slots

void on_listComponents_itemDoubleClicked(QListWidgetItem *item)
void on_actionOpen_triggered()
void on_actionNew_triggered()
void on_actionSave_triggered()
void on_actionSaveAs_triggered()
void on_actionSolve_triggered()
void on_actionHideSolution_triggered()
struct Ohmcha::Component::MetaInfo
#include <component.h>

Additional information that is not relevant for algorithms.

Public Members

std::string name
Pos *pos = {}
Pos textPos
int textAnchor = 3
float angle = {}
float textAngle = {}
bool textOrientationIndependent = false
class Ohmcha::Node : public Ohmcha::Component
#include <component.h>

An electrical node, i.e. a point at which two or more branches meet.

Public Functions

Node()
Component *copy() const override
int getTerminalCount() const override
struct Ohmcha::Component::Pos

Public Members

float x = 0
float y = 0
class Ohmcha::Resistor : public Ohmcha::Component

Public Functions

Resistor()
Resistor(float resistance)
Component *copy() const override
void setResistance(float r)
int getTerminalCount() const override
float getResistance() const
Pos getTextPos() const override

Private Members

float resistance = 1
class Ohmcha::Schematic

Public Functions

Schematic()
void add(Component *component)
void clearBranches()
void clearNodes()
void print()
std::vector<Component*> &getComponents()
std::vector<Branch*> &getBranches()
std::vector<Node*> &getNodes()
std::vector<Component*> getComponents() const
std::vector<Branch*> getBranches() const
std::vector<Node*> getNodes() const
int getComponentId(Component *component) const
int getNodeId(Node *node) const

Private Members

std::vector<Component*> components
std::vector<Branch*> branches
std::vector<Node*> nodes
namespace Ohmcha

All entities of Ohmcha are part of this namespace. This makes it possible to use the project as a library for other projects.

Typedefs

typedef std::vector<std::vector<float>> Matrix

Functions

float parseValue(const std::string &text)
Component *xmlParseComponent(Schematic *schematic, const QDomElement &element)
Component *xmlParseComponentRecursively(const QDomElement &element, Schematic *schematic)
Component *xmlParseResistor(const QDomElement &element)
Component *xmlParseEmf(const QDomElement &element)
Component *xmlParseCurrentSource(const QDomElement &element)
Component *xmlParseNode(const QDomElement &element)
Component *xmlParseBranch(const QDomElement &element, Schematic *schematic)
Schematic *xmlParseSchematic(std::string filename)
QString xmlConvertSchematic(Schematic *schematic)
QString xmlConvertComponent(Component *component)
QString xmlConvertResistor(Resistor *resistor)
QString xmlConvertEmf(Emf *emf)
QString xmlConvertCurrentSource(CurrentSource *source)
QString xmlConvertBranch(Schematic *schematic, Branch *branch)
QString xmlConvertNode(Node *node)
std::vector<Branch*> generate_MST(Node *start)

Generate a minimum spanning tree of a connected sub-graph starting at the specified node.

TODO

Return

Vector of pointers of branches that constitute the generated MST

Parameters
  • start: - The start node

VectorXf khoffSolve(const std::vector<Branch> &branches, const std::vector<Node> &nodes)
VectorXf khoffSolve(Schematic *schematic)
VectorXf loopCurrentSolve(Schematic *schematic, std::vector<int> &tree)
namespace Ui
file circuitview.h
#include “graphic_component.h”#include “component_preview.h”#include “graphic_branch.h”#include “src/model/schematic.h”#include <QWidget>#include <QGraphicsView>#include <QGraphicsItem>
file component.h
#include <vector>#include <Eigen/Dense>
file component_preview.h
#include <QRadioButton>#include <QWidget>#include “circuitview.h”#include “graphic_component.h”
file graph.h
#include “component.h”
file graphic_branch.h
#include “graphic_component.h”
file graphic_component.h
#include <QObject>#include <QGraphicsItem>#include <QPen>#include “src/model/component.h”
file graphic_currentsource.h
#include “graphic_component.h”
file graphic_emf.h
#include “graphic_component.h”
file graphic_node.h
#include “graphic_component.h”
file graphic_resistor.h
#include “graphic_component.h”
file kirchhoff.h
#include <Eigen/Dense>#include “component.h”#include “schematic.h”
file main.cpp
#include <iostream>#include <vector>#include <Eigen/Dense>#include <map>#include “program/program.h”#include “test/test.h”#include “ui/mainwindow.h”#include <QApplication>#include “model/component.h”#include “model/kirchhoff.h”#include “fileio/xml.h”

Functions

int execute_command(const arglist_iter &it_arg, const arglist_iter &end)

Execute a command, or print a message if it does not exist.

Return

Status code of the executed command.

Parameters
  • it_arg: Iterator to the first argument in the list.

  • end: - End of the argument list

int start_gui(int argc, char *argv[])
int main(int argc, char *argv[])

Variables

std::map<std::string, int (*)(const arglist_iter&, const arglist_iter&)> commands = {}
file mainwindow.h
#include “circuitview.h”#include <QMainWindow>#include <QListWidgetItem>
file program.h
#include <vector>#include <string>#include <iostream>#include <sstream>

Defines

is_opt(s, o1, o2)
eval(function)

Execute ‘function’. If it returns a non-zero value, an error has occurred, exit the calling function with the same error code.

This is to be used when a command calls a sub-command.

Typedefs

typedef std::vector<std::string> arglist
typedef std::vector<std::string>::iterator arglist_iter

Functions

arglist read_line()

Read a line from std::cin and parse it into a vector of strings Delimiter used for parsing is any type of whitespace.

Text enclosed in double quotes is considered a single word even if it contains whitespace, and the quotes are not included in the result.

void unrecognized_opt_cmd(const std::string &opt)
template<class Int>
bool extract_n(const std::string &s, Int &n)

Extract variable count from string.

Return

Whether the operation was successful.

Template Parameters
  • Int: - any integral type

file schematic.h
#include “component.h”
file test.h

Functions

int test()
file xml.h
#include <QtXml>#include <QFile>#include <string>#include “src/model/component.h”#include “src/model/schematic.h”
dir fileio
dir model
dir program
dir src
dir test
dir ui