Make buttons toggleable by single clicking
This commit is contained in:
parent
5340635389
commit
7ade16be8b
@ -1,15 +1,15 @@
|
||||
#ifndef CIRCUITBUFFER_H
|
||||
#define CIRCUITBUFFER_H
|
||||
|
||||
class Part;
|
||||
class Wire;
|
||||
class Scene;
|
||||
|
||||
#include <QList>
|
||||
#include <QPointF>
|
||||
|
||||
#include "ePartType.h"
|
||||
|
||||
class Part;
|
||||
class Wire;
|
||||
class Scene;
|
||||
|
||||
class CircuitBuffer
|
||||
{
|
||||
public:
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <QGraphicsItem>
|
||||
#include <QList>
|
||||
|
||||
#include "eConnectorType.h"
|
||||
|
||||
class Part;
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <QTextStream>
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
|
||||
#include "Scene.h"
|
||||
#include "Part.h"
|
||||
#include "Wire.h"
|
||||
@ -14,8 +15,6 @@
|
||||
|
||||
#include "Parts/IntegratedCircuit.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
FileHandler::FileHandler(Logic* logic)
|
||||
:m_logic(logic)
|
||||
{
|
||||
@ -74,11 +73,12 @@ bool FileHandler::open(QString filename)
|
||||
|
||||
enum Sector
|
||||
{
|
||||
None,
|
||||
Parts,
|
||||
Wires
|
||||
};
|
||||
|
||||
Sector currentSector;
|
||||
Sector currentSector = None;
|
||||
|
||||
// A .csim file stores all parts with IDs, this map simply keeps track of which ID corresponds to which pointer
|
||||
QMap<QString, Part*> idPartPointerMap;
|
||||
@ -116,6 +116,9 @@ bool FileHandler::open(QString filename)
|
||||
if(words.length() < 4)
|
||||
return false;
|
||||
|
||||
if(currentSector == None)
|
||||
return false;
|
||||
|
||||
if(currentSector == Parts)
|
||||
{
|
||||
PartType::PartType type = (PartType::PartType)words[1].toInt();
|
||||
|
14
Label.cpp
14
Label.cpp
@ -2,12 +2,12 @@
|
||||
|
||||
#include <QKeyEvent>
|
||||
#include <QGraphicsScene>
|
||||
#include <QTextCursor>
|
||||
|
||||
Label::Label(QGraphicsItem* parent)
|
||||
:m_parentItem(parent)
|
||||
{
|
||||
// TODO: Find out why I have to call setParentItem each time in recalculateTextAlignment
|
||||
//setParentItem((QGraphicsItem*)m_parent);
|
||||
setParentItem((QGraphicsItem*)m_parentItem);
|
||||
if(parent->scene() != nullptr)
|
||||
parent->scene()->addItem(this);
|
||||
}
|
||||
@ -46,10 +46,16 @@ void Label::keyPressEvent(QKeyEvent* event)
|
||||
recalculateTextAlignment();
|
||||
}
|
||||
|
||||
void Label::focusOutEvent(QFocusEvent* event)
|
||||
{
|
||||
QTextCursor tc = textCursor();
|
||||
tc.clearSelection();
|
||||
setTextCursor(tc);
|
||||
QGraphicsTextItem::focusOutEvent(event);
|
||||
}
|
||||
|
||||
void Label::recalculateTextAlignment()
|
||||
{
|
||||
setParentItem(m_parentItem);
|
||||
|
||||
if(m_alignMode & AlignMode::Right)
|
||||
setX( - boundingRect().width());
|
||||
else if(m_alignMode & AlignMode::HCenter)
|
||||
|
6
Label.h
6
Label.h
@ -6,6 +6,10 @@
|
||||
|
||||
#include "eAlignMode.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QFocusEvent;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class Label : private QGraphicsTextItem
|
||||
{
|
||||
public:
|
||||
@ -23,7 +27,7 @@ private:
|
||||
AlignMode::AlignMode m_alignMode;
|
||||
|
||||
void keyPressEvent(QKeyEvent* event) override;
|
||||
|
||||
void focusOutEvent(QFocusEvent* event) override;
|
||||
void recalculateTextAlignment();
|
||||
};
|
||||
|
||||
|
@ -79,7 +79,6 @@ IntegratedCircuit* Logic::createIC(QString filename, QPointF pos)
|
||||
m_parentScene->addItem(ic);
|
||||
m_parts.append(ic);
|
||||
ic->setPos(pos);
|
||||
ic->m_oldPos = pos;
|
||||
ic->m_partType = PartType::IntegratedCircuit;
|
||||
return ic;
|
||||
}
|
||||
@ -117,7 +116,6 @@ Part* Logic::createPart(PartType::PartType partType, QPointF pos)
|
||||
m_parentScene->addItem(part);
|
||||
m_parts.append(part);
|
||||
part->setPos(pos);
|
||||
part->m_oldPos = pos;
|
||||
part->m_partType = partType;
|
||||
|
||||
return part;
|
||||
|
@ -9,6 +9,8 @@
|
||||
#include <QTimer>
|
||||
#include <QCloseEvent>
|
||||
#include <QDir>
|
||||
#include <QUndoView>
|
||||
#include <QPushButton>
|
||||
|
||||
#include "Connector.h"
|
||||
#include "Connector.h"
|
||||
|
@ -2,15 +2,13 @@
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QGraphicsScene>
|
||||
#include <QToolButton>
|
||||
#include <QUndoStack>
|
||||
#include <QUndoView>
|
||||
#include <QPushButton>
|
||||
#include "eConnectorType.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui { class MainWindow; }
|
||||
class QToolButton;
|
||||
class QUndoView;
|
||||
class QPushButton;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class Connector;
|
||||
|
28
Part.cpp
28
Part.cpp
@ -1,12 +1,11 @@
|
||||
#include "Part.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include <QPainter>
|
||||
#include <QStyleOptionGraphicsItem>
|
||||
#include <QGraphicsSceneDragDropEvent>
|
||||
#include <QLabel>
|
||||
#include <QGraphicsProxyWidget>
|
||||
#include <QtMath>
|
||||
|
||||
#include "eAlignMode.h"
|
||||
#include "eConnectorType.h"
|
||||
@ -16,8 +15,6 @@
|
||||
#include "Logic.h"
|
||||
#include "Label.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
// PUBLIC
|
||||
Part::Part(Logic* logic, CircuitItemBaseShape baseShape)
|
||||
:m_logic(logic), m_baseShape(baseShape)
|
||||
@ -91,7 +88,7 @@ void Part::setWidth(int factor)
|
||||
|
||||
void Part::recalculateLayout()
|
||||
{
|
||||
// Return if the part isn't in any scene
|
||||
// No need to recalculate any graphical things if this part isn't in any scene
|
||||
if(m_logic->parentScene() == nullptr)
|
||||
return;
|
||||
// Add inputs and outputs and position them correctly
|
||||
@ -215,6 +212,27 @@ QVariant Part::itemChange(GraphicsItemChange change, const QVariant & value)
|
||||
return QGraphicsItem::itemChange(change, value);
|
||||
}
|
||||
|
||||
// PROTECTED
|
||||
void Part::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
m_dragBeginPos = event->screenPos();
|
||||
QGraphicsItem::mousePressEvent(event);
|
||||
}
|
||||
|
||||
void Part::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
QPointF deltaPos = event->screenPos() - m_dragBeginPos;
|
||||
qreal dist = qSqrt(deltaPos.x()*deltaPos.x() + deltaPos.y()*deltaPos.y());
|
||||
if(dist <= 5)
|
||||
{
|
||||
partClickedEvent();
|
||||
return;
|
||||
}
|
||||
if(m_logic->parentScene() != nullptr)
|
||||
m_logic->parentScene()->partMoved(deltaPos);
|
||||
QGraphicsItem::mouseReleaseEvent(event);
|
||||
}
|
||||
|
||||
// PRIVATE
|
||||
void Part::updateState()
|
||||
{
|
||||
|
13
Part.h
13
Part.h
@ -3,7 +3,6 @@
|
||||
|
||||
#include <QGraphicsItem>
|
||||
#include <QList>
|
||||
#include <QLineEdit>
|
||||
|
||||
#include "ePartType.h"
|
||||
|
||||
@ -59,9 +58,6 @@ public:
|
||||
|
||||
void setWidth(int factor);
|
||||
|
||||
void addText(); //TODO
|
||||
void addTextEdit(); //TODO
|
||||
|
||||
void recalculateLayout();
|
||||
|
||||
virtual QRectF boundingRect() const override;
|
||||
@ -70,7 +66,7 @@ public:
|
||||
// Symbol to be drawn inside the circuitItem
|
||||
virtual QPainterPath symbolPainterPath(QRect limits) = 0;
|
||||
virtual QVariant itemChange(GraphicsItemChange change, const QVariant & value) override;
|
||||
// Take the inputs and calculate the outputs based on the part's logic
|
||||
// Takes the inputs and calculates the outputs based on the part's logic
|
||||
virtual QVector<bool> compute(QVector<bool> inputs) = 0;
|
||||
|
||||
protected:
|
||||
@ -87,12 +83,17 @@ protected:
|
||||
|
||||
CircuitItemBaseShape m_baseShape;
|
||||
|
||||
QPointF m_oldPos;
|
||||
QPointF m_dragBeginPos;
|
||||
|
||||
PartType::PartType m_partType;
|
||||
|
||||
int m_widthFactor;
|
||||
|
||||
virtual void partClickedEvent(){}
|
||||
|
||||
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
|
||||
private:
|
||||
// Updates all of the outputs using the inputs, called by Logic
|
||||
void updateState();
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "../Scene.h"
|
||||
#include "../Logic.h"
|
||||
#include "../Connector.h"
|
||||
|
||||
#include "ToggleButton.h"
|
||||
#include "LightBulb.h"
|
||||
|
||||
|
@ -27,7 +27,7 @@ QPainterPath ToggleButton::symbolPainterPath(QRect limits)
|
||||
return QPainterPath();
|
||||
}
|
||||
|
||||
void ToggleButton::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
||||
void ToggleButton::partClickedEvent()
|
||||
{
|
||||
m_toggleState = !m_toggleState;
|
||||
if(m_toggleState)
|
||||
@ -41,5 +41,4 @@ void ToggleButton::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
||||
m_brushColorSelected = Qt::GlobalColor::red;
|
||||
}
|
||||
update();
|
||||
Part::mouseDoubleClickEvent(event);
|
||||
}
|
||||
|
@ -14,9 +14,7 @@ public:
|
||||
QPainterPath symbolPainterPath(QRect limits) override;
|
||||
|
||||
private:
|
||||
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
|
||||
QPointF m_dragBeginPos;
|
||||
void partClickedEvent() override;
|
||||
|
||||
bool m_toggleState = false;
|
||||
};
|
||||
|
22
Scene.cpp
22
Scene.cpp
@ -7,6 +7,8 @@
|
||||
#include "Connector.h"
|
||||
#include "Wire.h"
|
||||
#include "Part.h"
|
||||
#include "FileHandler.h"
|
||||
#include "Logic.h"
|
||||
|
||||
#include "UndoCommands/AddPart.h"
|
||||
#include "UndoCommands/AddWire.h"
|
||||
@ -14,10 +16,6 @@
|
||||
#include "UndoCommands/RemoveParts.h"
|
||||
#include "UndoCommands/RemoveWire.h"
|
||||
|
||||
#include "FileHandler.h"
|
||||
|
||||
#include "Logic.h"
|
||||
|
||||
|
||||
Scene::Scene(QGraphicsView *parentGfxView, MainWindow *parentMainWindow)
|
||||
:m_parentMainWindow(parentMainWindow), m_parentGfxView(parentGfxView), m_logic(new Logic(this)), m_undoStack(new QUndoStack)
|
||||
@ -137,11 +135,6 @@ void Scene::moveParts(const QList<Part *>& parts, QPointF relPos)
|
||||
{
|
||||
m_undoStack->push(new MoveParts(this, parts, relPos));
|
||||
|
||||
for(auto part : parts)
|
||||
{
|
||||
part->m_oldPos = part->pos();
|
||||
}
|
||||
|
||||
m_parentMainWindow->changeMade();
|
||||
}
|
||||
|
||||
@ -263,18 +256,11 @@ void Scene::removeConnectorsConnections(Connector *connector)
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton)
|
||||
void Scene::partMoved(QPointF delta)
|
||||
{
|
||||
QList<QGraphicsItem*> movedItems = selectedItems();
|
||||
if(!movedItems.isEmpty() && !(((Part*)movedItems[0])->pos() == ((Part*)movedItems[0])->m_oldPos))
|
||||
{
|
||||
QList<Part*> movedParts;
|
||||
for(auto item : movedItems)
|
||||
movedParts.append((Part*)item);
|
||||
moveParts(movedParts, movedParts[0]->pos() - movedParts[0]->m_oldPos);
|
||||
}
|
||||
}
|
||||
QGraphicsScene::mouseReleaseEvent(event);
|
||||
moveParts(movedParts, delta);
|
||||
}
|
||||
|
5
Scene.h
5
Scene.h
@ -5,7 +5,6 @@
|
||||
// This includes things like undo, redo, etc...
|
||||
|
||||
#include <QGraphicsScene>
|
||||
#include <QUndoStack>
|
||||
|
||||
#include "ePartType.h"
|
||||
#include "CircuitBuffer.h"
|
||||
@ -91,8 +90,8 @@ private:
|
||||
void connectorClicked(Connector *connector);
|
||||
// Removal is done undoably
|
||||
void removeConnectorsConnections(Connector *connector);
|
||||
|
||||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
|
||||
// Called by Part when moved by mouse. Only called by the Part that was under the mouse pointer while moved
|
||||
void partMoved(QPointF delta);
|
||||
};
|
||||
|
||||
#endif // SCENE_H
|
||||
|
@ -1,14 +1,12 @@
|
||||
#include "AddPart.h"
|
||||
|
||||
#include "RemoveWire.h"
|
||||
|
||||
#include "../Part.h"
|
||||
#include "../Connector.h"
|
||||
#include "../Scene.h"
|
||||
#include "../Logic.h"
|
||||
#include "../Parts/IntegratedCircuit.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include "RemoveWire.h"
|
||||
|
||||
AddPart::AddPart(Scene* scene, PartType::PartType partType, QPointF pos, QString icFilename)
|
||||
:m_scene(scene), m_partType(partType), m_pos(pos), m_icFilename(icFilename)
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "../Part.h"
|
||||
#include "../Connector.h"
|
||||
#include "../Logic.h"
|
||||
|
||||
#include "RemoveWire.h"
|
||||
|
||||
RemoveParts::RemoveParts(Scene* scene, const QList<Part*>& parts)
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "../Logic.h"
|
||||
#include "../Wire.h"
|
||||
#include "../Part.h"
|
||||
#include "../Connector.h"\
|
||||
#include "../Connector.h"
|
||||
|
||||
RemoveWire::RemoveWire(Scene* scene, Wire* wire)
|
||||
:m_scene(scene), m_wire(wire)
|
||||
|
1
Wire.cpp
1
Wire.cpp
@ -2,6 +2,7 @@
|
||||
|
||||
#include <QPainter>
|
||||
#include <qdebug.h>
|
||||
|
||||
#include "Connector.h"
|
||||
#include "eConnectorType.h"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user