Make buttons toggleable by single clicking

This commit is contained in:
xypwn 2020-04-04 14:07:16 +02:00
parent 5340635389
commit 7ade16be8b
20 changed files with 80 additions and 65 deletions

View File

@ -1,15 +1,15 @@
#ifndef CIRCUITBUFFER_H #ifndef CIRCUITBUFFER_H
#define CIRCUITBUFFER_H #define CIRCUITBUFFER_H
class Part;
class Wire;
class Scene;
#include <QList> #include <QList>
#include <QPointF> #include <QPointF>
#include "ePartType.h" #include "ePartType.h"
class Part;
class Wire;
class Scene;
class CircuitBuffer class CircuitBuffer
{ {
public: public:

View File

@ -3,6 +3,7 @@
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QList> #include <QList>
#include "eConnectorType.h" #include "eConnectorType.h"
class Part; class Part;

View File

@ -4,6 +4,7 @@
#include <QTextStream> #include <QTextStream>
#include <QFile> #include <QFile>
#include <QDir> #include <QDir>
#include "Scene.h" #include "Scene.h"
#include "Part.h" #include "Part.h"
#include "Wire.h" #include "Wire.h"
@ -14,8 +15,6 @@
#include "Parts/IntegratedCircuit.h" #include "Parts/IntegratedCircuit.h"
#include <QDebug>
FileHandler::FileHandler(Logic* logic) FileHandler::FileHandler(Logic* logic)
:m_logic(logic) :m_logic(logic)
{ {
@ -74,11 +73,12 @@ bool FileHandler::open(QString filename)
enum Sector enum Sector
{ {
None,
Parts, Parts,
Wires 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 // A .csim file stores all parts with IDs, this map simply keeps track of which ID corresponds to which pointer
QMap<QString, Part*> idPartPointerMap; QMap<QString, Part*> idPartPointerMap;
@ -116,6 +116,9 @@ bool FileHandler::open(QString filename)
if(words.length() < 4) if(words.length() < 4)
return false; return false;
if(currentSector == None)
return false;
if(currentSector == Parts) if(currentSector == Parts)
{ {
PartType::PartType type = (PartType::PartType)words[1].toInt(); PartType::PartType type = (PartType::PartType)words[1].toInt();

View File

@ -2,12 +2,12 @@
#include <QKeyEvent> #include <QKeyEvent>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QTextCursor>
Label::Label(QGraphicsItem* parent) Label::Label(QGraphicsItem* parent)
:m_parentItem(parent) :m_parentItem(parent)
{ {
// TODO: Find out why I have to call setParentItem each time in recalculateTextAlignment setParentItem((QGraphicsItem*)m_parentItem);
//setParentItem((QGraphicsItem*)m_parent);
if(parent->scene() != nullptr) if(parent->scene() != nullptr)
parent->scene()->addItem(this); parent->scene()->addItem(this);
} }
@ -46,10 +46,16 @@ void Label::keyPressEvent(QKeyEvent* event)
recalculateTextAlignment(); recalculateTextAlignment();
} }
void Label::focusOutEvent(QFocusEvent* event)
{
QTextCursor tc = textCursor();
tc.clearSelection();
setTextCursor(tc);
QGraphicsTextItem::focusOutEvent(event);
}
void Label::recalculateTextAlignment() void Label::recalculateTextAlignment()
{ {
setParentItem(m_parentItem);
if(m_alignMode & AlignMode::Right) if(m_alignMode & AlignMode::Right)
setX( - boundingRect().width()); setX( - boundingRect().width());
else if(m_alignMode & AlignMode::HCenter) else if(m_alignMode & AlignMode::HCenter)

View File

@ -6,6 +6,10 @@
#include "eAlignMode.h" #include "eAlignMode.h"
QT_BEGIN_NAMESPACE
class QFocusEvent;
QT_END_NAMESPACE
class Label : private QGraphicsTextItem class Label : private QGraphicsTextItem
{ {
public: public:
@ -23,7 +27,7 @@ private:
AlignMode::AlignMode m_alignMode; AlignMode::AlignMode m_alignMode;
void keyPressEvent(QKeyEvent* event) override; void keyPressEvent(QKeyEvent* event) override;
void focusOutEvent(QFocusEvent* event) override;
void recalculateTextAlignment(); void recalculateTextAlignment();
}; };

View File

@ -79,7 +79,6 @@ IntegratedCircuit* Logic::createIC(QString filename, QPointF pos)
m_parentScene->addItem(ic); m_parentScene->addItem(ic);
m_parts.append(ic); m_parts.append(ic);
ic->setPos(pos); ic->setPos(pos);
ic->m_oldPos = pos;
ic->m_partType = PartType::IntegratedCircuit; ic->m_partType = PartType::IntegratedCircuit;
return ic; return ic;
} }
@ -117,7 +116,6 @@ Part* Logic::createPart(PartType::PartType partType, QPointF pos)
m_parentScene->addItem(part); m_parentScene->addItem(part);
m_parts.append(part); m_parts.append(part);
part->setPos(pos); part->setPos(pos);
part->m_oldPos = pos;
part->m_partType = partType; part->m_partType = partType;
return part; return part;

View File

@ -9,6 +9,8 @@
#include <QTimer> #include <QTimer>
#include <QCloseEvent> #include <QCloseEvent>
#include <QDir> #include <QDir>
#include <QUndoView>
#include <QPushButton>
#include "Connector.h" #include "Connector.h"
#include "Connector.h" #include "Connector.h"

View File

@ -2,15 +2,13 @@
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QMainWindow>
#include <QGraphicsScene>
#include <QToolButton>
#include <QUndoStack>
#include <QUndoView>
#include <QPushButton>
#include "eConnectorType.h" #include "eConnectorType.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow; }
class QToolButton;
class QUndoView;
class QPushButton;
QT_END_NAMESPACE QT_END_NAMESPACE
class Connector; class Connector;

View File

@ -1,12 +1,11 @@
#include "Part.h" #include "Part.h"
#include <QDebug>
#include <QPainter> #include <QPainter>
#include <QStyleOptionGraphicsItem> #include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneDragDropEvent> #include <QGraphicsSceneDragDropEvent>
#include <QLabel> #include <QLabel>
#include <QGraphicsProxyWidget> #include <QGraphicsProxyWidget>
#include <QtMath>
#include "eAlignMode.h" #include "eAlignMode.h"
#include "eConnectorType.h" #include "eConnectorType.h"
@ -16,8 +15,6 @@
#include "Logic.h" #include "Logic.h"
#include "Label.h" #include "Label.h"
#include <QDebug>
// PUBLIC // PUBLIC
Part::Part(Logic* logic, CircuitItemBaseShape baseShape) Part::Part(Logic* logic, CircuitItemBaseShape baseShape)
:m_logic(logic), m_baseShape(baseShape) :m_logic(logic), m_baseShape(baseShape)
@ -91,7 +88,7 @@ void Part::setWidth(int factor)
void Part::recalculateLayout() 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) if(m_logic->parentScene() == nullptr)
return; return;
// Add inputs and outputs and position them correctly // 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); 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 // PRIVATE
void Part::updateState() void Part::updateState()
{ {

13
Part.h
View File

@ -3,7 +3,6 @@
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QList> #include <QList>
#include <QLineEdit>
#include "ePartType.h" #include "ePartType.h"
@ -59,9 +58,6 @@ public:
void setWidth(int factor); void setWidth(int factor);
void addText(); //TODO
void addTextEdit(); //TODO
void recalculateLayout(); void recalculateLayout();
virtual QRectF boundingRect() const override; virtual QRectF boundingRect() const override;
@ -70,7 +66,7 @@ public:
// Symbol to be drawn inside the circuitItem // Symbol to be drawn inside the circuitItem
virtual QPainterPath symbolPainterPath(QRect limits) = 0; virtual QPainterPath symbolPainterPath(QRect limits) = 0;
virtual QVariant itemChange(GraphicsItemChange change, const QVariant & value) override; 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; virtual QVector<bool> compute(QVector<bool> inputs) = 0;
protected: protected:
@ -87,12 +83,17 @@ protected:
CircuitItemBaseShape m_baseShape; CircuitItemBaseShape m_baseShape;
QPointF m_oldPos; QPointF m_dragBeginPos;
PartType::PartType m_partType; PartType::PartType m_partType;
int m_widthFactor; int m_widthFactor;
virtual void partClickedEvent(){}
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
private: private:
// Updates all of the outputs using the inputs, called by Logic // Updates all of the outputs using the inputs, called by Logic
void updateState(); void updateState();

View File

@ -5,6 +5,7 @@
#include "../Scene.h" #include "../Scene.h"
#include "../Logic.h" #include "../Logic.h"
#include "../Connector.h" #include "../Connector.h"
#include "ToggleButton.h" #include "ToggleButton.h"
#include "LightBulb.h" #include "LightBulb.h"

View File

@ -27,7 +27,7 @@ QPainterPath ToggleButton::symbolPainterPath(QRect limits)
return QPainterPath(); return QPainterPath();
} }
void ToggleButton::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) void ToggleButton::partClickedEvent()
{ {
m_toggleState = !m_toggleState; m_toggleState = !m_toggleState;
if(m_toggleState) if(m_toggleState)
@ -41,5 +41,4 @@ void ToggleButton::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
m_brushColorSelected = Qt::GlobalColor::red; m_brushColorSelected = Qt::GlobalColor::red;
} }
update(); update();
Part::mouseDoubleClickEvent(event);
} }

View File

@ -14,9 +14,7 @@ public:
QPainterPath symbolPainterPath(QRect limits) override; QPainterPath symbolPainterPath(QRect limits) override;
private: private:
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override; void partClickedEvent() override;
QPointF m_dragBeginPos;
bool m_toggleState = false; bool m_toggleState = false;
}; };

View File

@ -7,6 +7,8 @@
#include "Connector.h" #include "Connector.h"
#include "Wire.h" #include "Wire.h"
#include "Part.h" #include "Part.h"
#include "FileHandler.h"
#include "Logic.h"
#include "UndoCommands/AddPart.h" #include "UndoCommands/AddPart.h"
#include "UndoCommands/AddWire.h" #include "UndoCommands/AddWire.h"
@ -14,10 +16,6 @@
#include "UndoCommands/RemoveParts.h" #include "UndoCommands/RemoveParts.h"
#include "UndoCommands/RemoveWire.h" #include "UndoCommands/RemoveWire.h"
#include "FileHandler.h"
#include "Logic.h"
Scene::Scene(QGraphicsView *parentGfxView, MainWindow *parentMainWindow) Scene::Scene(QGraphicsView *parentGfxView, MainWindow *parentMainWindow)
:m_parentMainWindow(parentMainWindow), m_parentGfxView(parentGfxView), m_logic(new Logic(this)), m_undoStack(new QUndoStack) :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)); m_undoStack->push(new MoveParts(this, parts, relPos));
for(auto part : parts)
{
part->m_oldPos = part->pos();
}
m_parentMainWindow->changeMade(); m_parentMainWindow->changeMade();
} }
@ -263,18 +256,11 @@ void Scene::removeConnectorsConnections(Connector *connector)
} }
} }
void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void Scene::partMoved(QPointF delta)
{ {
if (event->button() == Qt::LeftButton) QList<QGraphicsItem*> movedItems = selectedItems();
{ QList<Part*> movedParts;
QList<QGraphicsItem*> movedItems = selectedItems(); for(auto item : movedItems)
if(!movedItems.isEmpty() && !(((Part*)movedItems[0])->pos() == ((Part*)movedItems[0])->m_oldPos)) movedParts.append((Part*)item);
{ moveParts(movedParts, delta);
QList<Part*> movedParts;
for(auto item : movedItems)
movedParts.append((Part*)item);
moveParts(movedParts, movedParts[0]->pos() - movedParts[0]->m_oldPos);
}
}
QGraphicsScene::mouseReleaseEvent(event);
} }

View File

@ -5,7 +5,6 @@
// This includes things like undo, redo, etc... // This includes things like undo, redo, etc...
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QUndoStack>
#include "ePartType.h" #include "ePartType.h"
#include "CircuitBuffer.h" #include "CircuitBuffer.h"
@ -91,8 +90,8 @@ private:
void connectorClicked(Connector *connector); void connectorClicked(Connector *connector);
// Removal is done undoably // Removal is done undoably
void removeConnectorsConnections(Connector *connector); void removeConnectorsConnections(Connector *connector);
// Called by Part when moved by mouse. Only called by the Part that was under the mouse pointer while moved
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void partMoved(QPointF delta);
}; };
#endif // SCENE_H #endif // SCENE_H

View File

@ -1,14 +1,12 @@
#include "AddPart.h" #include "AddPart.h"
#include "RemoveWire.h"
#include "../Part.h" #include "../Part.h"
#include "../Connector.h" #include "../Connector.h"
#include "../Scene.h" #include "../Scene.h"
#include "../Logic.h" #include "../Logic.h"
#include "../Parts/IntegratedCircuit.h" #include "../Parts/IntegratedCircuit.h"
#include <QDebug> #include "RemoveWire.h"
AddPart::AddPart(Scene* scene, PartType::PartType partType, QPointF pos, QString icFilename) AddPart::AddPart(Scene* scene, PartType::PartType partType, QPointF pos, QString icFilename)
:m_scene(scene), m_partType(partType), m_pos(pos), m_icFilename(icFilename) :m_scene(scene), m_partType(partType), m_pos(pos), m_icFilename(icFilename)

View File

@ -4,6 +4,7 @@
#include "../Part.h" #include "../Part.h"
#include "../Connector.h" #include "../Connector.h"
#include "../Logic.h" #include "../Logic.h"
#include "RemoveWire.h" #include "RemoveWire.h"
RemoveParts::RemoveParts(Scene* scene, const QList<Part*>& parts) RemoveParts::RemoveParts(Scene* scene, const QList<Part*>& parts)

View File

@ -4,7 +4,7 @@
#include "../Logic.h" #include "../Logic.h"
#include "../Wire.h" #include "../Wire.h"
#include "../Part.h" #include "../Part.h"
#include "../Connector.h"\ #include "../Connector.h"
RemoveWire::RemoveWire(Scene* scene, Wire* wire) RemoveWire::RemoveWire(Scene* scene, Wire* wire)
:m_scene(scene), m_wire(wire) :m_scene(scene), m_wire(wire)

View File

@ -2,6 +2,7 @@
#include <QPainter> #include <QPainter>
#include <qdebug.h> #include <qdebug.h>
#include "Connector.h" #include "Connector.h"
#include "eConnectorType.h" #include "eConnectorType.h"

View File

@ -1,6 +1,7 @@
#include "MainWindow.h"
#include <QApplication> #include <QApplication>
#include "MainWindow.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);