Skip to content

Commit

Permalink
component = common base class; added resistor and capacitor class
Browse files Browse the repository at this point in the history
  • Loading branch information
ClemensFMN committed Nov 2, 2013
1 parent 91d137b commit 9c01561
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 20 deletions.
19 changes: 19 additions & 0 deletions capacitor.cpp
@@ -0,0 +1,19 @@
#include "capacitor.h"

Capacitor::Capacitor(QString name) : Component(name) {

Capacitor::NUM_NODES = 2;
_wires = QVector<QVector<Wire*> > (Capacitor::NUM_NODES);

QPointF p1(-20, 0);
_nodes.append(p1);
QPointF p2(20, 0);
_nodes.append(p2);

}

QRectF Capacitor::boundingRect() const {
return QRectF(-30, -20, 60, 40);
}


19 changes: 19 additions & 0 deletions capacitor.h
@@ -0,0 +1,19 @@
#ifndef CAPACITOR_H
#define CAPACITOR_H

#include <QtGui>
#include <QtWidgets>
#include <QDebug>
#include <QVector>

#include "component.h"


class Capacitor : public Component {

public:
Capacitor(QString);
QRectF boundingRect() const;
};

#endif // CAPACITOR_H
10 changes: 1 addition & 9 deletions component.cpp
Expand Up @@ -3,19 +3,11 @@

Component::Component(QString name) {
_name = name;

QPointF p1(-20, 0);
_nodes.append(p1);
QPointF p2(20, 0);
_nodes.append(p2);

_wires = QVector<QVector<Wire*> > (Component::NUM_NODES);

setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemSendsGeometryChanges);
}

QRectF Component::boundingRect() const {
return QRectF(-30, -20, 60, 40);
return QRectF(0, 0, 0, 0);
}

void Component::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
Expand Down
6 changes: 2 additions & 4 deletions component.h
Expand Up @@ -23,21 +23,19 @@ class Component : public QGraphicsItem {
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void addWire(int, Wire *e);
QString getName();
//QVector<Wire*> getWires1();
//QVector<Wire*> getWires2();
QVector<Wire*> getWires(int);
QVector<QPointF> getNodes();


protected:
QVariant itemChange(GraphicsItemChange change, const QVariant & value);

private:
protected:
QString _name;
//the set of node positions
QVector<QPointF> _nodes;
QVector<QVector<Wire*> > _wires;
const int NUM_NODES = 2;
int NUM_NODES;
};

#endif // COMPONENT_H
13 changes: 9 additions & 4 deletions mainwindow.cpp
Expand Up @@ -9,8 +9,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
itemList->setFixedWidth(150);


QListWidgetItem *item1 = new QListWidgetItem("Item 1");
QListWidgetItem *item2 = new QListWidgetItem("Item 2");
QListWidgetItem *item1 = new QListWidgetItem("Resistor");
QListWidgetItem *item2 = new QListWidgetItem("Capacitor");

itemList->addItem(item1);
itemList->addItem(item2);
Expand Down Expand Up @@ -73,8 +73,13 @@ void MainWindow::toolActionsSlot(QAction *a) {
if(a->text() == "Place Item") {
scene->setMode(2);
//initialization for "Place Item"
itemList->setCurrentRow(0);
scene->setDrawingItem("Item 1");
int selected = itemList->currentRow();
if(selected == 0) {
scene->setDrawingItem("Resistor");
}
if(selected == 1) {
scene->setDrawingItem("Capacitor");
}
}

}
Expand Down
14 changes: 11 additions & 3 deletions myscene.cpp
Expand Up @@ -85,10 +85,18 @@ void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) {
// place item
if(mode == 2) {
Component *n;
if(itemtype == "Item 1") {
if(itemtype == "Resistor") {
numComponents++;
QString name = QString("Node %0").arg(numComponents);
n = new Component(name);
QString name = QString("Resistor %0").arg(numComponents);
n = new Resistor(name);
components.append(n);
addItem(n);
n->setPos(mouseEvent->scenePos());
}
if(itemtype == "Capacitor") {
numComponents++;
QString name = QString("Capacitor %0").arg(numComponents);
n = new Capacitor(name);
components.append(n);
addItem(n);
n->setPos(mouseEvent->scenePos());
Expand Down
2 changes: 2 additions & 0 deletions myscene.h
Expand Up @@ -6,6 +6,8 @@
#include <QDebug>

#include "component.h"
#include "resistor.h"
#include "capacitor.h"
#include "mainwindow.h"


Expand Down
19 changes: 19 additions & 0 deletions resistor.cpp
@@ -0,0 +1,19 @@
#include "resistor.h"

Resistor::Resistor(QString name) : Component(name) {

Resistor::NUM_NODES = 2;
_wires = QVector<QVector<Wire*> > (Resistor::NUM_NODES);

QPointF p1(-20, 0);
_nodes.append(p1);
QPointF p2(20, 0);
_nodes.append(p2);

}


QRectF Resistor::boundingRect() const {
return QRectF(-30, -20, 60, 40);
}

18 changes: 18 additions & 0 deletions resistor.h
@@ -0,0 +1,18 @@
#ifndef RESISTOR_H
#define RESISTOR_H

#include <QtGui>
#include <QtWidgets>
#include <QDebug>
#include <QVector>

#include "component.h"

class Resistor : public Component {

public:
Resistor(QString);
QRectF boundingRect() const;
};

#endif // RESISTOR_H

0 comments on commit 9c01561

Please sign in to comment.