Skip to content

Commit

Permalink
#8 - Undo move and add components
Browse files Browse the repository at this point in the history
  • Loading branch information
NourhanEssam committed Feb 19, 2016
1 parent e42435a commit fd7e3ce
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 2 deletions.
6 changes: 4 additions & 2 deletions logicsim.pro
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ SOURCES += src/main.cpp \
src/gates.cpp \
src/connectionline.cpp \
src/workspacetab.cpp \
src/pin.cpp
src/pin.cpp \
src/commands.cpp

HEADERS += src/mainwindow.h \
src/canvas.h \
Expand All @@ -35,7 +36,8 @@ HEADERS += src/mainwindow.h \
src/connectionline.h \
src/gates.h \
src/workspacetab.h \
src/pin.h
src/pin.h \
src/commands.h

FORMS += src/mainwindow.ui

Expand Down
41 changes: 41 additions & 0 deletions src/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Local includes

#include "pin.h"
#include "commands.h"
#include "logicsim_global.h"

namespace Logicsim
Expand Down Expand Up @@ -36,6 +37,12 @@ class Canvas::Private
Canvas::Canvas(QObject *parent)
: QGraphicsScene(parent), d(new Private)
{
undoStack = new QUndoStack(this);
createUndoView();

connect(this, SIGNAL(itemMoved(Component*,QPointF)),this,SLOT(itemMovedS(Component*,QPointF)));
connect(this,SIGNAL(itemAdded(Component*,QPointF)),this,SLOT(itemAddedS(Component*,QPointF)));

d->view = new QGraphicsView(this);
d->view->setSceneRect(0,0,CANVAS_WIDTH,CANVAS_HEIGHT);

Expand Down Expand Up @@ -94,6 +101,7 @@ void Canvas::dropEvent(QGraphicsSceneDragDropEvent * event)

Component* component = static_cast<Component*>(QMetaType::create(typeId));
d->mCanvasManager->addComponent(component, event->scenePos());
emit itemAdded(component,component->pos());
}
else
{
Expand Down Expand Up @@ -174,6 +182,12 @@ void Canvas::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
d->mCanvasManager->pinPressed(p);
}

Component *component = dynamic_cast<Component*>(mouseGrabberItem());
if(component)
{
oldpt = component->pos();
}
}
else
{
Expand Down Expand Up @@ -215,6 +229,7 @@ void Canvas::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
d->mCanvasManager->movingComponent(component);
d->mCanvasManager->componentMoved(component, event->scenePos());
d->view->setCursor(Qt::ArrowCursor);
emit itemMoved(component,component->pos());
}
}
QGraphicsScene::mouseReleaseEvent(event);
Expand Down Expand Up @@ -248,5 +263,31 @@ void Canvas::drawBackground(QPainter *painter, const QRectF &rect)
}
}

void Canvas::itemMovedS(Component *item, const QPointF &pos)
{
Q_UNUSED(pos);
pushInStack(new MoveCommand(item,oldpt));
update();
}

void Canvas::itemAddedS(Component *addedItem, const QPointF &position)
{
Q_UNUSED(position);
pushInStack(new AddCommand(addedItem,this));
}

void Canvas::pushInStack(QUndoCommand* command)
{
undoStack->push(command);
qDebug()<<"..............pushed";
}

void Canvas::createUndoView()
{
undoView = new QUndoView(undoStack);
undoView->show();
undoView->setAttribute(Qt::WA_QuitOnClose, false);
}

} // namespace Logicsim

14 changes: 14 additions & 0 deletions src/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Canvas : public QGraphicsScene
int tabIndex() const;
void setTabIndex(int index);
CanvasManager *canvasManager();
void createUndoView();
void pushInStack(QUndoCommand* command);

protected:
void dropEvent(QGraphicsSceneDragDropEvent * event);
Expand All @@ -52,6 +54,18 @@ public Q_SLOTS:
private:
class Private;
Private* const d;

QUndoStack *undoStack;
QUndoView *undoView;
QPointF oldpt;

signals:
void itemMoved(Component *movedItem, const QPointF &movedFromPosition);
void itemAdded(Component *addedItem, const QPointF &position);

private slots:
void itemMovedS(Component *item, const QPointF &pos);
void itemAddedS(Component *item, const QPointF &pos);
};

} // namespace Logicsim
Expand Down
63 changes: 63 additions & 0 deletions src/commands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "commands.h"

namespace Logicsim
{

MoveCommand::MoveCommand(Component *Mcomponent, const QPointF &MOldPos,QUndoCommand *parent) : QUndoCommand(parent)
{
draggedComponent = Mcomponent;
newPos = Mcomponent->pos();
oldPos = MOldPos;
}

void MoveCommand::undo()
{
draggedComponent->setPos(oldPos);
setText(QObject::tr("Move %1").arg(createCommandString(draggedComponent, newPos)));
}

void MoveCommand::redo()
{
draggedComponent->setPos(newPos);
setText(QObject::tr("Move %1").arg(createCommandString(draggedComponent, newPos)));
}

AddCommand::AddCommand(Component *Acomponent, Canvas *Acanvas,QUndoCommand *parent) : QUndoCommand(parent)
{
static int itemCount = 0;

canvas = Acanvas;
addedComponent = Acomponent;
pos = Acomponent->pos();

Acanvas->update();
++itemCount;
setText(QObject::tr("Add %1").arg(createCommandString(addedComponent, pos)));
}

AddCommand::~AddCommand()
{
if (!addedComponent->scene())
delete addedComponent;
}

void AddCommand::undo()
{
canvas->removeItem(addedComponent);
canvas->update();
}

void AddCommand::redo()
{
canvas->addItem(addedComponent);
addedComponent->setPos(pos);
canvas->clearSelection();
canvas->update();
}

QString createCommandString(Component *item, const QPointF &pos)
{
return QObject::tr("%2, %3").arg(pos.x()).arg(pos.y());
}

} //LogicSim
42 changes: 42 additions & 0 deletions src/commands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef COMMANDS_H
#define COMMANDS_H

#include <QUndoCommand>

#include "component.h"
#include "canvas.h"

namespace Logicsim
{

class MoveCommand : public QUndoCommand
{
public:
MoveCommand(Component *Mcomponent, const QPointF &MOldPos,QUndoCommand *parent = 0);
void undo() Q_DECL_OVERRIDE;
void redo() Q_DECL_OVERRIDE;

private:
Component *draggedComponent;
QPointF oldPos;
QPointF newPos;
};

class AddCommand : public QUndoCommand
{
public:
AddCommand(Component *Acomponent, Canvas *Acanvas,QUndoCommand *parent = 0);
~AddCommand();
void undo() Q_DECL_OVERRIDE;
void redo() Q_DECL_OVERRIDE;

private:
Component *addedComponent;
Canvas *canvas;
QPointF pos;
};

QString createCommandString(Component *item, const QPointF &point);

}
#endif // COMMANDS_H

0 comments on commit fd7e3ce

Please sign in to comment.