diff --git a/capacitor.cpp b/capacitor.cpp new file mode 100644 index 0000000..397f8d1 --- /dev/null +++ b/capacitor.cpp @@ -0,0 +1,19 @@ +#include "capacitor.h" + +Capacitor::Capacitor(QString name) : Component(name) { + + Capacitor::NUM_NODES = 2; + _wires = QVector > (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); +} + + diff --git a/capacitor.h b/capacitor.h new file mode 100644 index 0000000..565f5d3 --- /dev/null +++ b/capacitor.h @@ -0,0 +1,19 @@ +#ifndef CAPACITOR_H +#define CAPACITOR_H + +#include +#include +#include +#include + +#include "component.h" + + +class Capacitor : public Component { + +public: + Capacitor(QString); + QRectF boundingRect() const; +}; + +#endif // CAPACITOR_H diff --git a/component.cpp b/component.cpp index 1b22db0..93151b3 100644 --- a/component.cpp +++ b/component.cpp @@ -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 > (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) { diff --git a/component.h b/component.h index 2f46cc0..d45a3eb 100644 --- a/component.h +++ b/component.h @@ -23,8 +23,6 @@ class Component : public QGraphicsItem { void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void addWire(int, Wire *e); QString getName(); - //QVector getWires1(); - //QVector getWires2(); QVector getWires(int); QVector getNodes(); @@ -32,12 +30,12 @@ class Component : public QGraphicsItem { protected: QVariant itemChange(GraphicsItemChange change, const QVariant & value); -private: +protected: QString _name; //the set of node positions QVector _nodes; QVector > _wires; - const int NUM_NODES = 2; + int NUM_NODES; }; #endif // COMPONENT_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 804aa8a..1490c25 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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); @@ -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"); + } } } diff --git a/myscene.cpp b/myscene.cpp index 76feb49..75a05af 100644 --- a/myscene.cpp +++ b/myscene.cpp @@ -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()); diff --git a/myscene.h b/myscene.h index 413d148..fbe3688 100644 --- a/myscene.h +++ b/myscene.h @@ -6,6 +6,8 @@ #include #include "component.h" +#include "resistor.h" +#include "capacitor.h" #include "mainwindow.h" diff --git a/resistor.cpp b/resistor.cpp new file mode 100644 index 0000000..5e3d119 --- /dev/null +++ b/resistor.cpp @@ -0,0 +1,19 @@ +#include "resistor.h" + +Resistor::Resistor(QString name) : Component(name) { + + Resistor::NUM_NODES = 2; + _wires = QVector > (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); +} + diff --git a/resistor.h b/resistor.h new file mode 100644 index 0000000..579708a --- /dev/null +++ b/resistor.h @@ -0,0 +1,18 @@ +#ifndef RESISTOR_H +#define RESISTOR_H + +#include +#include +#include +#include + +#include "component.h" + +class Resistor : public Component { + +public: + Resistor(QString); + QRectF boundingRect() const; +}; + +#endif // RESISTOR_H