diff --git a/src/Actions/ZoomAction.cpp b/src/Actions/ZoomAction.cpp index 75fcbd9b..f544136d 100644 --- a/src/Actions/ZoomAction.cpp +++ b/src/Actions/ZoomAction.cpp @@ -1,147 +1,157 @@ /* This file is part of Rocs. Copyright 2011 Tomaz Canabrava This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "ZoomAction.h" #include #include #include #include #include #include #include #include #include qreal ZoomAction::_zoomFactor = 1; ZoomAction::ZoomAction(GraphScene* scene, QObject* parent) : AbstractAction(scene, parent) ,m_view(scene->views().at(0)) ,m_zoomRectItem(0) { setText(i18n ( "Zoom" )); setToolTip ( i18n ( "Zoom the canvas by the wheel, or by dragging." ) ); setIcon ( KIcon ( "page-zoom" ) ); _name = "zoom"; } bool ZoomAction::executePress(QPointF pos) { delete m_zoomRectItem; m_zoomRectItem = new QGraphicsRectItem(0,0,0,0); QColor color(Qt::green); color.setAlphaF(0.3); m_zoomRectItem->setBrush(QBrush(color)); m_zoomRectItem->setPen(QPen(QBrush(Qt::black), 0.1, Qt::SolidLine)); m_zoomRectItem->setZValue(9); m_beginZoom = m_view->mapFromScene(pos); _graphScene->addItem(m_zoomRectItem); return true; } bool ZoomAction::executeMove(QPointF pos) { if (!m_zoomRectItem) return false; QPoint movePos = m_view->mapFromScene(pos); qreal left = (m_beginZoom.x() < movePos.x()) ? m_beginZoom.x() : movePos.x(); qreal top = (m_beginZoom.y() < movePos.y()) ? m_beginZoom.y() : movePos.y(); qreal bottom = (m_beginZoom.y() > movePos.y()) ? m_beginZoom.y() : movePos.y(); qreal right = (m_beginZoom.x() > movePos.x()) ? m_beginZoom.x() : movePos.x(); QPointF topLeft(m_view->mapToScene(left, top)); QPointF bottomRight(m_view->mapToScene(right, bottom)); m_zoomRectItem->setRect(QRectF(topLeft, bottomRight)); return true; } bool ZoomAction::executeRelease(QPointF) { if (!m_zoomRectItem) return false; m_view->fitInView(m_zoomRectItem->rect(),Qt::KeepAspectRatioByExpanding); delete m_zoomRectItem; m_zoomRectItem = 0; return true; } bool ZoomAction::executeDoubleClick(QPointF) { zoomReset(); return true; } bool ZoomAction::executeKeyRelease(QKeyEvent* keyEvent) { switch(keyEvent->key()){ case Qt::Key_Plus : { m_view->scale(1.25, 1.25); _zoomFactor *= 1.25; break; } case Qt::Key_Minus : { m_view->scale(0.8, 0.8); _zoomFactor *= 0.8; break; } case Qt::Key_5 : m_view->resetMatrix(); break; } keyEvent->accept(); return true; } bool ZoomAction::executeWheelEvent(QGraphicsSceneWheelEvent* wEvent) { if (wEvent->delta() > 0){ // zoom out zoomOut(wEvent->scenePos()); }else{ // zoom in zoomIn(wEvent->scenePos()); } // qDebug() << "event position: "<< wEvent->scenePos(); wEvent->accept(); return true; } -void ZoomAction::zoomIn(QPointF zoomCenter) { +void ZoomAction::zoomInCenter() { m_view->scale(1.25, 1.25); _zoomFactor *= 1.25; +} + + +void ZoomAction::zoomIn(QPointF zoomCenter) { + zoomInCenter(); m_view->centerOn(zoomCenter); } -void ZoomAction::zoomOut(QPointF zoomCenter) { +void ZoomAction::zoomOutCenter() { if ( m_view->width()/_zoomFactor*1.25 < 5*DocumentManager::self()->activeDocument()->width() || m_view->height()/_zoomFactor*1.25 < 5*DocumentManager::self()->activeDocument()->height() ) { m_view->scale(0.8, 0.8); _zoomFactor *= 0.8; - m_view->centerOn(zoomCenter); } } + +void ZoomAction::zoomOut(QPointF zoomCenter) { + zoomOutCenter(); + m_view->centerOn(zoomCenter); +} + void ZoomAction::zoomReset() { m_view->resetMatrix(); } diff --git a/src/Actions/ZoomAction.h b/src/Actions/ZoomAction.h index 2e1e991f..a8edb5e6 100644 --- a/src/Actions/ZoomAction.h +++ b/src/Actions/ZoomAction.h @@ -1,60 +1,65 @@ /* This file is part of Rocs. Copyright 2011 Tomaz Canabrava This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef ZOOMACTION_H #define ZOOMACTION_H #include #include #include class QGraphicsRectItem; class QGraphicsView; class QKeyEvent; class QGraphicsSceneWheelEvent; class ZoomAction : public AbstractAction { + Q_OBJECT public: ZoomAction(GraphScene* scene, QObject* parent); bool executePress(QPointF pos); bool executeMove(QPointF pos); bool executeRelease(QPointF pos); bool executeDoubleClick(QPointF pos); bool executeKeyRelease(QKeyEvent* keyEvent); bool executeWheelEvent(QGraphicsSceneWheelEvent *wEvent); + +public slots: void zoomOut(QPointF zoomCenter); void zoomIn(QPointF zoomCenter); + void zoomInCenter(); + void zoomOutCenter(); void zoomReset(); private: QRectF m_zoomArea; qreal m_currentZoomFactor; QGraphicsView *m_view; QGraphicsRectItem *m_zoomRectItem; QPointF m_beginZoom; QPointF m_endZoom; int m_steps; static qreal _zoomFactor; }; #endif // ZOOM_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ee9f5d42..a7da4ca9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,198 +1,198 @@ include_directories( ${KDE4_INCLUDES} ${QT_INCLUDES} ${KROSS_INCLUDES} Core/ Actions/ GraphicsItem/ Interface/ Interface/VisualEditor Models/ Plugins/ Scripts/ ) # Prepare library. set( rocs_core_SRCS Core/Data.cpp Core/DataStructure.cpp Core/Document.cpp Core/Group.cpp Core/DataStructurePluginInterface.cpp Core/DataStructurePluginManager.cpp Core/DocumentManager.cpp Core/DynamicPropertiesList.cpp Core/KrossBackend.cpp Core/Pointer.cpp Core/QtScriptBackend.cpp Core/Modifiers/Topology.cpp Core/Modifiers/ValueModifier.cpp Plugins/PluginManager.cpp Plugins/ToolsPluginInterface.cpp Plugins/FilePluginInterface.cpp GraphicsItem/GraphicsLayout.cpp GraphicsItem/DataItem.cpp GraphicsItem/PointerItem.cpp ) kde4_add_library( rocslib SHARED ${rocs_core_SRCS} ) target_link_libraries( rocslib ${KDE4_KROSSCORE_LIBS} ${KDE4_KIO_LIBS} ${QT_QTSCRIPT_LIBRARY} ${QT_QTSCRIPTTOOLS_LIBRARY} ) set( rocslib_LIB_HDRS Core/DynamicPropertiesList.h Core/Data.h Core/DataStructure.h Core/Document.h Core/KrossBackend.h Core/Pointer.h Core/QtScriptBackend.h Core/rocslib_export.h Core/Rocs_Typedefs.h Core/DataStructurePluginInterface.h Core/DataStructurePluginManager.h Core/Modifiers/Topology.h Plugins/FilePluginInterface.h Plugins/PluginManager.h Plugins/ToolsPluginInterface.h GraphicsItem/DataItem.h GraphicsItem/PointerItem.h GraphicsItem/GraphicsLayout.h Scripts/IncludeManager.h ) set_target_properties( rocslib PROPERTIES VERSION ${GENERIC_LIB_VERSION} SOVERSION ${GENERIC_LIB_SOVERSION} ) install( TARGETS rocslib ${INSTALL_TARGETS_DEFAULT_ARGS} ) install( FILES ${rocslib_LIB_HDRS} DESTINATION ${INCLUDE_INSTALL_DIR}/rocs COMPONENT Devel ) ###################### Outros SRCS ############################ #set ( kwelcomewidget_SRCS # KWelcomeWidget/kwelcomewidget.cpp #) set ( rocs_actions_SRCS Actions/AbstractAction.cpp Actions/AddConnectionHandAction.cpp Actions/AddDataAction.cpp Actions/AlignAction.cpp Actions/DeleteAction.cpp Actions/ZoomAction.cpp Actions/AddDataHandAction.cpp Actions/DeleteHandAction.cpp Actions/AssignValueAction.cpp Actions/SelectMoveHandAction.cpp ) set( rocs_interface_SRCS Interface/ImporterExporterManager.cpp Interface/IncludeManagerSettings.cpp Interface/MainWindow.cpp Interface/OpenedFilesWidget.cpp Interface/Separator.cpp Interface/GraphPropertiesWidget.cpp Interface/GraphLayers.cpp Interface/TabWidget.cpp Interface/CodeEditor.cpp Interface/NodePropertiesWidget.cpp Interface/edgepropertieswidget.cpp Interface/ConfigureDefaultProperties.cpp - Interface/VisualEditor/GraphScene.cpp + Interface/VisualEditor/GraphScene.cpp Interface/VisualEditor/GraphVisualEditor.cpp ) set( rocs_models_SRCS Models/model_GraphDocument.cpp Models/model_GraphLayers.cpp Models/model_GraphProperties.cpp ) # set( rocs_undoredo_SRCS # UndoRedo/URAddNode.cpp # UndoRedo/URAddEdge.cpp # ) set ( rocs_script_SRC Interface/PossibleIncludes.cpp Scripts/IncludeManager.cpp ) set(rocs_SRCS Main.cpp ${rocs_actions_SRCS} ${rocs_interface_SRCS} ${rocs_models_SRCS} ${rocs_script_SRC} ) kde4_add_ui_files( rocs_SRCS Interface/OpenedFilesWidget.ui Interface/GraphPropertiesWidget.ui Interface/NodePropertiesWidget.ui Interface/EdgePropertiesWidget.ui Interface/ConfigureDefaultProperties.ui ) set (QT_USE_QTSCRIPT true) set(rocs_KCFGS settings.kcfgc) kde4_add_kcfg_files(rocs_Settings_SRCS ${rocs_KCFGS} ) kde4_add_executable(rocs ${rocs_SRCS} ${rocs_Settings_SRCS} ${rocs_RCC_SRCS}) target_link_libraries(rocs rocslib ${KDE4_KDEUI_LIBS} ${KDE4_PLASMA_LIBS} ${KDE4_KROSSUI_LIBS} ${KDE4_KIO_LIBS} ${KDE4_KTEXTEDITOR_LIBS} ${QT_QTSCRIPT_LIBRARY} ${QT_QTSCRIPTTOOLS_LIBRARY} ${KDE4_KNEWSTUFF3_LIBS} ) ################## INSTALLS ########################## install( TARGETS rocs ${INSTALL_TARGETS_DEFAULT_ARGS} ) install( PROGRAMS rocs.desktop DESTINATION ${XDG_APPS_INSTALL_DIR} ) install( FILES rocsui.rc DESTINATION ${DATA_INSTALL_DIR}/rocs ) install( FILES rocs.kcfg DESTINATION ${KCFG_INSTALL_DIR} ) install(FILES rocs.knsrc DESTINATION ${CONFIG_INSTALL_DIR}) install( FILES Core/RocsDataStructurePlugin.desktop Plugins/RocsToolsPlugin.desktop Plugins/RocsFilePlugin.desktop DESTINATION ${SERVICETYPES_INSTALL_DIR} ) add_subdirectory(Plugins) add_subdirectory(Scripts) #Build Tests if KDE_TEST is on if(KDE4_BUILD_TESTS) enable_testing() add_subdirectory(Tests) endif(KDE4_BUILD_TESTS) diff --git a/src/Interface/ConfigureDefaultProperties.cpp b/src/Interface/ConfigureDefaultProperties.cpp index eb2aa08a..edfadb35 100644 --- a/src/Interface/ConfigureDefaultProperties.cpp +++ b/src/Interface/ConfigureDefaultProperties.cpp @@ -1,65 +1,65 @@ /* This file is part of Rocs. Copyright (C) 2011 Andreas Cord-Landwehr This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "ConfigureDefaultProperties.h" #include "ui_ConfigureDefaultProperties.h" #include -#include "Interface/GraphVisualEditor.h" +#include "GraphVisualEditor.h" #include "GraphicsLayout.h" ConfigureDefaultProperties::ConfigureDefaultProperties( QWidget* parent ) : QWidget(parent), ui(new Ui::ConfigureDefaultProperties) { ui->setupUi(this); readConfig(); ui->comboDefaultNode->setCurrentIndex(_displayPositionNode); ui->comboDefaultEdge->setCurrentIndex(_displayPositionEdge); } ConfigureDefaultProperties::~ConfigureDefaultProperties() { delete ui; } void ConfigureDefaultProperties::readConfig() { _displayPositionNode = Settings::dataNodeDisplay(); _displayPositionEdge = Settings::dataEdgeDisplay(); } void ConfigureDefaultProperties::saveConfig() { Settings::setDataNodeDisplay(_displayPositionNode); Settings::setDataEdgeDisplay(_displayPositionEdge); GraphicsLayout::self()->setViewStyleDataNode(_displayPositionNode); GraphicsLayout::self()->setViewStyleDataEdge(_displayPositionEdge); } void ConfigureDefaultProperties::setDisplayPositionNode(int position) { if (position<0) return; _displayPositionNode = position; emit changed(true); } void ConfigureDefaultProperties::setDisplayPositionEdge(int position) { if (position<0) return; _displayPositionEdge = position; emit changed(true); } diff --git a/src/Interface/VisualEditor/GraphScene.cpp b/src/Interface/VisualEditor/GraphScene.cpp index fabc569b..cb2b5a03 100644 --- a/src/Interface/VisualEditor/GraphScene.cpp +++ b/src/Interface/VisualEditor/GraphScene.cpp @@ -1,430 +1,432 @@ /* This file is part of Rocs, Copyright 2004-2011 Tomaz Canabrava Copyright 2011 Andreas Cord-Landwehr This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "GraphScene.h" #include "Data.h" #include "Pointer.h" #include "DataStructure.h" #include "Document.h" #include #include "DocumentManager.h" #include "MainWindow.h" #include "DataItem.h" #include "PointerItem.h" #include "NodePropertiesWidget.h" #include "edgepropertieswidget.h" #include "AbstractAction.h" #include "AlignAction.h" #include "AssignValueAction.h" #include "AddDataAction.h" #include "DeleteAction.h" #include "ZoomAction.h" #include #include #include #include #include #include GraphScene::GraphScene( QObject *parent) : QGraphicsScene(parent) { _graphDocument = 0; _hideEdges = false; _datumPropertiesWidget = new DataPropertiesWidget(qobject_cast(parent)); _pointerPropertiesWidget = new PointerPropertiesWidget(qobject_cast(parent)); _action = 0; _minHeight = 0; _minWidth = 0; // first scene resize will resize to actual whiteboard size setSceneRect(0, 0, 0, 0); } void GraphScene::updateMinSize(qreal minWidth, qreal minHeight) { _minWidth = minWidth; _minHeight = minHeight; setSceneRect(-minWidth/2, -minHeight/2, minWidth/2, minHeight/2); Document *gd = DocumentManager::self()->activeDocument(); if (gd->width()<_minWidth) { gd->setLeft(gd->left()-(_minWidth-gd->width())/2); gd->setRight(gd->right()+(_minWidth-gd->width())/2); } if (gd->height()<_minHeight) { gd->setTop(gd->top()-(_minHeight-gd->height())/2); gd->setBottom(gd->bottom()+(_minHeight-gd->height())/2); } gd->changeMinimalSize(minWidth, minHeight); resize(); } bool GraphScene::hideEdges() { return _hideEdges; } void GraphScene::setHideEdges(bool h) { _hideEdges = h; if ( ! _hideEdges ) { foreach(QGraphicsItem *i, _hidedEdges) { i->update(); } } } void GraphScene::setActiveGraph(DataStructurePtr g) { kDebug() << "Active Graph Set"; _graph = g; } void GraphScene::updateAfter(QGraphicsItem *item) { if (_hidedEdges.contains(item)) return; _hidedEdges << item; } void GraphScene::hideGraph(DataStructurePtr g, bool visibility) { // FIXME in 4.9 this function is not working, but also not called // Reason: it depends on collecting item values in _hashGraphs but // without major changes it is not possible to remove deleted items reliable // from that hash // QList list = _hashGraphs.values(g); // foreach(QGraphicsItem *i, list){ // i->setVisible(visibility); // } } void GraphScene::setAction(QAction *action) { if(_action){ removeEventFilter(_action); } _action = qobject_cast( action ); action->setChecked(true); installEventFilter(action); } void GraphScene::setActiveDocument() { kDebug() << "Setting the document in the scene"; Document *gd = DocumentManager::self()->activeDocument(); if (_graphDocument == gd){ return; }else if ( gd == 0) { releaseDocument(); return; } // adapt document to scene if too small _graphDocument = gd; if (gd->width()<_minWidth) { gd->setLeft(gd->left()-(_minWidth-gd->width())/2); gd->setRight(gd->right()+(_minWidth-gd->width())/2); } if (gd->height()<_minHeight) { gd->setTop(gd->top()-(_minHeight-gd->height())/2); gd->setBottom(gd->bottom()+(_minHeight-gd->height())/2); } resize(); int size = gd->dataStructures().size(); for (int i = 0; i < size; i++) { updateGraph(gd->dataStructures().at(i)); connectGraphSignals(gd->dataStructures().at(i)); } connect( gd, SIGNAL(dataStructureCreated(DataStructurePtr)), this, SLOT(connectGraphSignals(DataStructurePtr))); connect( gd, SIGNAL(resized()), this, SLOT(resize())); createItems(); } void GraphScene::createItems(){ foreach(DataStructurePtr g, _graphDocument->dataStructures()){ foreach( DataPtr d, g->dataList()) createData( d ); foreach( PointerPtr p, g->pointers()) createEdge( p ); } } void GraphScene::connectGraphSignals(DataStructurePtr g){ connect( g.get(), SIGNAL(dataCreated(DataPtr)), this, SLOT(createData(DataPtr))); connect( g.get(), SIGNAL(pointerCreated(PointerPtr)), this, SLOT(createEdge(PointerPtr))); } void GraphScene::releaseDocument(){ _graphDocument->disconnect(this); disconnect(_graphDocument); foreach(DataStructurePtr ds, _graphDocument->dataStructures()){ ds->disconnect(this); disconnect(ds.get()); } } QGraphicsItem *GraphScene::createData(DataPtr n) { DataItem *nItem = (DataItem*)(DataStructurePluginManager::self()->dataItem(n)); addItem(nItem); addItem(nItem->name()); addItem(nItem->value()); return nItem; } QGraphicsItem *GraphScene::createEdge(PointerPtr e) { QGraphicsItem *pointerItem = 0; pointerItem = DataStructurePluginManager::self()->pointerItem(e); addItem(pointerItem); return pointerItem; } void GraphScene::wheelEvent(QGraphicsSceneWheelEvent *wheelEvent) { DataItem *nitem = qgraphicsitem_cast(itemAt(wheelEvent->scenePos())); if (!nitem) { wheelEvent->ignore(); return; } DataPtr movableData = nitem->data(); int numDegrees = wheelEvent->delta(); if (wheelEvent->orientation() == Qt::Vertical) { if (numDegrees > 0 && movableData->width() + 0.10 < 2.0){ movableData->setWidth(movableData->width()+0.1); nitem->update(); } else if (movableData->width() - 0.10 > 0.15){ movableData->setWidth(movableData->width()-0.1); nitem->update(); } } wheelEvent->accept(); } void GraphScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) { Q_UNUSED(mouseEvent); } void GraphScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) { if (mouseEvent->button() == Qt::MidButton) { if (DataItem *nItem = qgraphicsitem_cast(itemAt(mouseEvent->scenePos()))){ nItem->data()->setWidth(1); } } QGraphicsScene::mousePressEvent(mouseEvent); } void GraphScene::mouseDoubleClickEvent ( QGraphicsSceneMouseEvent *mouseEvent ) { if( mouseEvent->button() == Qt::LeftButton){ QGraphicsItem *i = itemAt(mouseEvent->scenePos()); if (DataItem *nItem = qgraphicsitem_cast(i)){ _datumPropertiesWidget->setData(nItem, mouseEvent->screenPos()); } else if (PointerItem *eItem = qgraphicsitem_cast(i)){ _pointerPropertiesWidget->setPointer(eItem->pointer(), mouseEvent->screenPos()); } } QGraphicsScene::mouseDoubleClickEvent(mouseEvent); } void GraphScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) { QGraphicsScene::mouseReleaseEvent(mouseEvent); } void GraphScene::keyPressEvent(QKeyEvent *keyEvent) { keyEvent->accept(); emit (keyPressed(keyEvent)); } void GraphScene::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) { event->accept(); - QMenu menu; // the context menu - QMenu *menuDataStructure = menu.addMenu( i18n("Data Structure") ); - QMenu *menuSelected = menu.addMenu( i18n("Selected") ); + _contextMenu = createContextMenu(event->screenPos()); + _contextMenu->exec(event->screenPos()); +// // TODO this is not a nice code +// // refactor this for SC 4.9 +// if (selectedItem == addAction) { +// addAction->executePress(event->scenePos()); +// } +// if ( selectedItem == alignDataStructureBottom || +// selectedItem == alignDataStructureTop || +// selectedItem == alignDataStructureCenter || +// selectedItem == alignDataStructureLeft || +// selectedItem == alignDataStructureRight || +// selectedItem == alignDataStructureCircle || +// selectedItem == alignDataStructureTree ) +// { +// AlignAction* action = static_cast(selectedItem); +// QGraphicsItem *i = itemAt(event->scenePos()); +// if (DataItem *nItem = qgraphicsitem_cast(i)){ +// action->setDataStructure(nItem->data()->dataStructure()); +// } +// action->align(); +// action->unsetDataStructure(); +// } +// +// if (selectedItem == propertyAction) { +// QGraphicsItem *i = itemAt(event->scenePos()); +// if (DataItem *nItem = qgraphicsitem_cast(i)){ +// _datumPropertiesWidget->setData(nItem, event->screenPos()); +// } +// else if (PointerItem *eItem = qgraphicsitem_cast(i)){ +// _pointerPropertiesWidget->setPointer(eItem->pointer(), event->screenPos()); +// } +// } +// else +// { +// // nothing was chosen +// } +} + + +void GraphScene::updateGraph(DataStructurePtr g) { + foreach(DataPtr n, g->dataList()) { + n->setName(n->name()); + } + + foreach(PointerPtr e, g->pointers()) { + e->setName(e->name()); + } +} + +void GraphScene::updateDocument() { + if (_graphDocument == 0) { + return; + } + + clear(); + int size = _graphDocument->dataStructures().size(); + + for (int i = 0; i < size; i++) { + updateGraph( _graphDocument->dataStructures().at(i) ); + } +} + +void GraphScene::resize() { + QRectF newSize(_graphDocument->left(), // x + _graphDocument->top(), // y + _graphDocument->right()-_graphDocument->left(), // width + _graphDocument->bottom()-_graphDocument->top()); // height + + setSceneRect( newSize ); + emit resized(); +} + +QMenu* GraphScene::createContextMenu(QPoint position) +{ + QMenu *menu = new QMenu; // the context menu + QMenu *menuDataStructure = menu->addMenu( i18n("Data Structure") ); + QMenu *menuSelected = menu->addMenu( i18n("Selected") ); + // prepare some context information bool contextAtItem = false; DataStructurePtr contextDataStructure; DataPtr contextData; - QGraphicsItem *i = itemAt(event->scenePos()); + QGraphicsItem *i = itemAt(position); if (DataItem *dataItem = (qgraphicsitem_cast(i))){ contextAtItem = true; contextDataStructure = dataItem->data()->dataStructure(); contextData = dataItem->data(); } // zoom menu QMenu *menuZoom = new QMenu( i18n("Zoom") ); ZoomAction *zoomAction = new ZoomAction(this, 0); QAction* zoomInAction = new QAction( i18n("In"), zoomAction); QAction *zoomOutAction = new QAction(i18n("Out"), zoomAction); QAction *zoomResetAction = new QAction(i18n("Reset"), zoomAction); menuZoom->addAction( zoomInAction ); menuZoom->addAction( zoomOutAction ); menuZoom->addAction( zoomResetAction ); - + connect( zoomInAction, SIGNAL(triggered(bool)), zoomAction, SLOT(zoomInCenter())); + connect( zoomOutAction, SIGNAL(triggered(bool)), zoomAction, SLOT(zoomOutCenter())); + connect( zoomResetAction, SIGNAL(triggered(bool)), zoomAction, SLOT(zoomReset())); + // alignment menu QMenu *menuDataStructureAlign = new QMenu( i18n("Align") ); QAction *alignDataStructureBottom = new AlignAction ( i18n ( "Bottom" ), AlignAction::Bottom, this,0, false ); QAction *alignDataStructureCenter = new AlignAction ( i18n ( "Center" ),AlignAction::HCenter,this,0, false ); QAction *alignDataStructureTop = new AlignAction ( i18n ( "Top" ), AlignAction::Top, this,0, false ); QAction *alignDataStructureLeft = new AlignAction ( i18n ( "Left" ), AlignAction::Left, this,0, false ); QAction *alignDataStructureRight = new AlignAction ( i18n ( "Right" ), AlignAction::Right, this,0, false ); QAction *alignDataStructureCircle = new AlignAction ( i18n ( "Circle" ), AlignAction::Circle, this,0, false ); QAction *alignDataStructureTree = new AlignAction ( i18n ( "Minimize Crossing Edges" ), AlignAction::MinCutTree, this,0, false ); menuDataStructureAlign->addAction ( alignDataStructureBottom ); menuDataStructureAlign->addAction ( alignDataStructureCenter ); menuDataStructureAlign->addAction ( alignDataStructureTop ); menuDataStructureAlign->addAction ( alignDataStructureLeft ); menuDataStructureAlign->addAction ( alignDataStructureRight ); menuDataStructureAlign->addAction ( alignDataStructureCircle ); menuDataStructureAlign->addAction ( alignDataStructureTree ); QMenu *menuSelectedAlign = new QMenu( i18n("Align") ); menuSelectedAlign->addAction ( new AlignAction ( i18n ( "Bottom" ), AlignAction::Bottom, this,0 ) ); menuSelectedAlign->addAction ( new AlignAction ( i18n ( "Center" ),AlignAction::HCenter,this,0 ) ); menuSelectedAlign->addAction ( new AlignAction ( i18n ( "Top" ), AlignAction::Top, this,0 ) ); menuSelectedAlign->addAction ( new AlignAction ( i18n ( "Left" ), AlignAction::Left, this,0 ) ); menuSelectedAlign->addAction ( new AlignAction ( i18n ( "Right" ), AlignAction::Right, this,0 ) ); menuSelectedAlign->addAction ( new AlignAction ( i18n ( "Circle" ), AlignAction::Circle, this,0 ) ); menuSelectedAlign->addAction ( new AlignAction ( i18n ( "Minimize Crossing Edges" ), AlignAction::MinCutTree, this,0 ) ); QMenu *menuDataStructureAssignValues = new QMenu( i18n("Values") ); menuDataStructureAssignValues->addAction ( new AssignValueAction ( i18n("Enumerate"), this, AssignValueAction::Enumerate, contextDataStructure, 0)); menuDataStructureAssignValues->addAction ( new AssignValueAction ( i18n("Random Integers"), this, AssignValueAction::RandomInteger, contextDataStructure, 0)); menuDataStructureAssignValues->addAction ( new AssignValueAction ( i18n("Random Reals"), this, AssignValueAction::RandomReal, contextDataStructure, 0)); QMenu *menuSelectedAssignValues = new QMenu( i18n("Values") ); menuSelectedAssignValues->addAction ( new AssignValueAction ( i18n("Enumerate"), this, AssignValueAction::Enumerate, 0)); menuSelectedAssignValues->addAction ( new AssignValueAction ( i18n("Random Integers"), this, AssignValueAction::RandomInteger, 0)); menuSelectedAssignValues->addAction ( new AssignValueAction ( i18n("Random Reals"), this, AssignValueAction::RandomReal, 0)); // puzzling the menu together AddDataAction *addAction = new AddDataAction(this); DeleteAction *deleteDataStructureAction = new DeleteAction( i18n("Delete"), this, contextDataStructure, 0); DeleteAction *deleteSelectedAction = new DeleteAction( i18n("Delete"), this, 0); DeleteAction *deleteItemAction = new DeleteAction( i18n("Delete"), this, contextData, 0); QAction *propertyAction = new QAction(i18n("Properties"), this); //FIXME remove hack menuSelected->addMenu(menuSelectedAlign); menuSelected->addMenu(menuSelectedAssignValues); menuSelected->addAction(deleteSelectedAction); menuDataStructure->addMenu(menuDataStructureAlign); menuDataStructure->addMenu(menuDataStructureAssignValues); menuDataStructure->addAction(deleteDataStructureAction); // activate/deactivate context depending on where the user click if (selectedItems().count()==0) { menuSelected->setDisabled(true); } if (!contextAtItem) { menuDataStructure->setDisabled(true); - menu.addAction(addAction); - menu.addMenu(menuZoom); + menu->addAction(addAction); + menu->addMenu(menuZoom); } else { - menu.addAction(propertyAction); - menu.addAction(deleteItemAction); - } - - // TODO this is not a nice code - // refactor this for SC 4.9 - QAction* selectedItem = menu.exec(event->screenPos()); - if (selectedItem == addAction) { - addAction->executePress(event->scenePos()); - } - if (selectedItem == zoomInAction) { - zoomAction->zoomIn(event->scenePos()); - } - if (selectedItem == zoomOutAction) { - zoomAction->zoomOut(event->scenePos()); - } - if (selectedItem == zoomResetAction) { - zoomAction->zoomReset(); - } - if ( selectedItem == alignDataStructureBottom || - selectedItem == alignDataStructureTop || - selectedItem == alignDataStructureCenter || - selectedItem == alignDataStructureLeft || - selectedItem == alignDataStructureRight || - selectedItem == alignDataStructureCircle || - selectedItem == alignDataStructureTree ) - { - AlignAction* action = static_cast(selectedItem); - QGraphicsItem *i = itemAt(event->scenePos()); - if (DataItem *nItem = qgraphicsitem_cast(i)){ - action->setDataStructure(nItem->data()->dataStructure()); - } - action->align(); - action->unsetDataStructure(); + menu->addAction(propertyAction); + menu->addAction(deleteItemAction); } - if (selectedItem == propertyAction) { - QGraphicsItem *i = itemAt(event->scenePos()); - if (DataItem *nItem = qgraphicsitem_cast(i)){ - _datumPropertiesWidget->setData(nItem, event->screenPos()); - } - else if (PointerItem *eItem = qgraphicsitem_cast(i)){ - _pointerPropertiesWidget->setPointer(eItem->pointer(), event->screenPos()); - } - } - else - { - // nothing was chosen - } -} - - -void GraphScene::updateGraph(DataStructurePtr g) { - foreach(DataPtr n, g->dataList()) { - n->setName(n->name()); - } - - foreach(PointerPtr e, g->pointers()) { - e->setName(e->name()); - } + return menu; } -void GraphScene::updateDocument() { - if (_graphDocument == 0) { - return; - } - - clear(); - int size = _graphDocument->dataStructures().size(); - - for (int i = 0; i < size; i++) { - updateGraph( _graphDocument->dataStructures().at(i) ); - } -} - -void GraphScene::resize() { - QRectF newSize(_graphDocument->left(), // x - _graphDocument->top(), // y - _graphDocument->right()-_graphDocument->left(), // width - _graphDocument->bottom()-_graphDocument->top()); // height - - setSceneRect( newSize ); - emit resized(); -} diff --git a/src/Interface/VisualEditor/GraphScene.h b/src/Interface/VisualEditor/GraphScene.h index 0d59757b..7e9befdb 100644 --- a/src/Interface/VisualEditor/GraphScene.h +++ b/src/Interface/VisualEditor/GraphScene.h @@ -1,100 +1,103 @@ /* This file is part of Rocs, Copyright 2004-2011 Tomaz Canabrava This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #ifndef SUI_GRAPHSCENE_H #define SUI_GRAPHSCENE_H #include #include #include "Rocs_Typedefs.h" class AbstractAction; class QAction; class Data; class Pointer; class QGraphicsItem; class Document; class DataStructure; class QGraphicsSceneMouseEvent; class QGraphicsSceneWheelEvent; class QGraphicsSceneContextMenuEvent; class QKeyEvent; +class QMenu; class DataPropertiesWidget; class PointerPropertiesWidget; class GraphScene : public QGraphicsScene { Q_OBJECT public: GraphScene( QObject *parent=0 ); enum borderSize{ kBORDER = 50 }; void updateMinSize(qreal minWidth, qreal minHeight); void setAction(QAction *action); void updateGraph(DataStructurePtr g); void updateDocument(); void setActiveGraph(DataStructurePtr g); void clearGraph(); void setHideEdges(bool h); bool hideEdges(); void updateAfter(QGraphicsItem *item); bool fade() const{ return _fade; } void fade(bool b){ _fade = b; } void hideGraph(DataStructurePtr g, bool visibility); public slots: QGraphicsItem* createData( DataPtr n); QGraphicsItem* createEdge( PointerPtr e); void connectGraphSignals(DataStructurePtr g); void createItems(); void setActiveDocument(); void resize(); signals: void resized(); void keyPressed(QKeyEvent* key); void addData(QPointF pos); void removeSelected(); void zoom(qreal amount); protected: void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent); void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent); void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent); void mouseDoubleClickEvent (QGraphicsSceneMouseEvent * mouseEvent); void contextMenuEvent(QGraphicsSceneContextMenuEvent* event); void wheelEvent(QGraphicsSceneWheelEvent *wheelEvent); void keyPressEvent(QKeyEvent *keyEvent); private: + QMenu * createContextMenu(QPoint position); + QMenu *_contextMenu; Document *_graphDocument; DataStructurePtr _graph; AbstractAction *_action; QMultiHash _hashGraphs; QList _hidedEdges; QGraphicsRectItem *_whiteboard; bool _hideEdges; DataPropertiesWidget *_datumPropertiesWidget; PointerPropertiesWidget *_pointerPropertiesWidget; bool _fade; void releaseDocument(); qreal _minWidth; qreal _minHeight; }; #endif