[<< Contents] [<< Prev] [Next >>]
Here we will implement the functionality for the 'MOVE' and 'DELETE' edit modes. After entering the 'MOVE' edit mode (using the tool bar, edit menu, or keyboard shortcut), the user will be able to move a station by selecting it with the mouse and dragging it while the mouse button is pressed to a new position on the canvas. Similarly after entering the 'DELETE' edit mode, the user will be able to delete a station by selecting it with a mouse click.
In the KSimulate constructor we need to remove the two lines that disable the actions that encapulsate the selecting move and delete edit modes.
actionMove->setEnabled(false); actionDel->setEnabled(false); |
Implement the virtual protected method that receives mouse movement events.
void contentsMouseMoveEvent( QMouseEvent* ); // mouse move |
Add a private pointer to provide convenient access to KSimulate's public variables and a private variable to keep track of the station being moved.
KSimulate* ksim; // shortcut to KSimulate QCanvasItem* moving; // pointer to station being moved |
In the constructor initialise the private pointer to KSimulate.
ksim = parent; |
In the contentsMouseMoveEvent method we check to see if a station is currently being moved, and if so update the station position to follow the mouse and update the canvas. We need to update the canvas after each change, otherwise we would not see the new position until the canvas was updated for some other reason.
/************************* contentsMouseMoveEvent *************************/
void CanvasView::contentsMouseMoveEvent( QMouseEvent *event )
{
// if a station is being moved, move to new position
if ( moving )
{
// ensure new position is still on canvas
int x = event->x();
int y = event->y();
if ( x < 10 ) x = 10;
if ( y < 10 ) y = 10;
if ( x+10 > canvas()->width() ) x = canvas()->width() - 10;
if ( y+10 > canvas()->height() ) y = canvas()->height() - 10;
// move station to new position
moving->move( x, y );
canvas()->update();
}
}
|
We also need to update the contents of the contentsMousePressEvent method to behave differently for each of the three edit modes. The code below uses three if statements, one for each edit mode, but it could just as easily have been constructed using a switch statement.
The first thing we do is to initialise the 'moving' pointer to ensure that contentsMouseMoveEvent never tries to move the wrong or an invalid station. When the edit mode is 'ADD' you will recognise most of the code from before. The code for the 'MOVE' and 'DELETE' both start with obtaining a pointer to a station that corresponds to the point where the user selected on the canvas using the canvas "collisions()" functionality. If the returned list is not empty we have identified one or more stations that could be moved or deleted. We just use the first station in this list.
// initialise station moving pointer each time mouse clicked
moving = 0;
if (ksim->editMode == 'A') // if edit mode is ADD
{
int x = event->x();
int y = event->y();
if ( x < 10 ) x = 10;
if ( y < 10 ) y = 10;
// add station to canvas
new Station( &stationSprite, canvas(), x, y );
canvas()->update();
// increase minimum canvas size if needed to accomondate new station
if ( x+10 > minCanvasW ) minCanvasW = x+10;
if ( y+10 > minCanvasH ) minCanvasH = y+10;
// update status bar to say station added
sbar->message(QString("Added station at %1,%2").arg(x).arg(y));
}
if (ksim->editMode == 'M') // if edit mode is MOVE
{
// find station to be moved
QCanvasItemList list = canvas()->collisions( event->pos() );
if (!list.isEmpty())
{
moving = *list.begin();
sbar->message(QString("Moving station from %1,%2").arg(event->x()).arg(event->y()));
}
}
if (ksim->editMode == 'D') // if edit mode is DELETE
{
// find station to be deleted
QCanvasItemList list = canvas()->collisions( event->pos() );
if (!list.isEmpty())
{
delete *list.begin();
canvas()->update();
sbar->message(QString("Deleted station at %1,%2").arg(event->x()).arg(event->y()));
}
}
|
The new code will be compiled automatically when you next attempt to re-run the application using KDevelop. Investigate how the application behaves.
[<< Contents] [<< Prev] [Next >>]