![]() |
TinkerCell Core 1.0
TinkerCell's Core library providing all basic functionalities
|
00001 /**************************************************************************** 00002 00003 Copyright (c) 2008 Deepak Chandran 00004 Contact: Deepak Chandran (dchandran1@gmail.com) 00005 See COPYRIGHT.TXT 00006 00007 The GraphicsView class provides a view for a GraphicsScene. It 00008 00009 ****************************************************************************/ 00010 00011 #include <QMimeData> 00012 #include "NetworkWindow.h" 00013 #include "MainWindow.h" 00014 #include "NodeGraphicsItem.h" 00015 #include "fileIO/NodeGraphicsReader.h" 00016 #include "ConnectionGraphicsItem.h" 00017 #include "TextGraphicsItem.h" 00018 #include "ItemHandle.h" 00019 #include "UndoCommands.h" 00020 #include "ConsoleWindow.h" 00021 #include "CloneItems.h" 00022 #include "NetworkHandle.h" 00023 #include "GraphicsScene.h" 00024 #include "GraphicsView.h" 00025 00026 namespace Tinkercell 00027 { 00028 qreal GraphicsView::DEFAULT_ZOOM = 0.5; 00029 00030 void GraphicsView::drawBackground( QPainter * painter, const QRectF & rect ) 00031 { 00032 if (!background.isNull() && painter) 00033 painter->drawPixmap(rect,background,QRectF(background.rect())); 00034 } 00035 00036 void GraphicsView::drawForeground( QPainter * painter, const QRectF & rect ) 00037 { 00038 if (!foreground.isNull() && painter) 00039 painter->drawPixmap(rect,foreground,QRectF(foreground.rect())); 00040 } 00041 00042 void GraphicsView::wheelEvent(QWheelEvent * wheelEvent) 00043 { 00044 if (scene && wheelEvent->modifiers() & Qt::ControlModifier) 00045 { 00046 double factor = 1.0 + 0.2 * qAbs(wheelEvent->delta()/120.0); 00047 if (wheelEvent->delta() > 0) 00048 scale(factor,factor); 00049 else 00050 scale(1.0/factor,1.0/factor); 00051 00052 scene->scaleGraphicalTools(); 00053 } 00054 else 00055 { 00056 QGraphicsView::wheelEvent(wheelEvent); 00057 } 00058 } 00059 00060 void GraphicsView::scrollContentsBy ( int dx, int dy ) 00061 { 00062 QGraphicsView::scrollContentsBy(dx,dy); 00063 if (scene) 00064 scene->update(); 00065 } 00066 00067 QSize GraphicsView::sizeHint() const 00068 { 00069 return QSize(500,500); 00070 } 00071 00073 GraphicsView::GraphicsView(NetworkWindow * network) 00074 : QGraphicsView (network->scene, network), scene(network->scene) 00075 { 00076 setCacheMode(QGraphicsView::CacheBackground); 00077 setViewportUpdateMode (QGraphicsView::BoundingRectViewportUpdate); 00078 00079 //setViewportUpdateMode (QGraphicsView::FullViewportUpdate); 00080 //setViewportUpdateMode (QGraphicsView::SmartViewportUpdate); 00081 //setViewport(new QGLWidget); 00082 //setDragMode(QGraphicsView::RubberBandDrag); 00083 //setDragMode(QGraphicsView::ScrollHandDrag); 00084 //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontSavePainterState); 00085 00086 setMouseTracking (true); 00087 setPalette(QPalette(GraphicsScene::BackgroundColor)); 00088 setAutoFillBackground(true); 00089 setAcceptDrops(true); 00090 00091 setRenderHint(QPainter::Antialiasing); 00092 setCacheMode(QGraphicsView::CacheBackground); 00093 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); 00094 QPointF center(scene->sceneRect().width()/2, scene->sceneRect().height()/2); 00095 scene->lastPoint() = center; 00096 centerOn( center); 00097 scale(DEFAULT_ZOOM, DEFAULT_ZOOM); 00098 setFocusPolicy(Qt::StrongFocus); 00099 } 00100 00101 void GraphicsView::mousePressEvent ( QMouseEvent * event ) 00102 { 00103 if (dragMode() != QGraphicsView::NoDrag && event->modifiers() != Qt::ControlModifier) 00104 setDragMode(QGraphicsView::NoDrag); 00105 00106 if (scene && scene->networkWindow) 00107 { 00108 scene->networkWindow->setAsCurrentWindow(); 00109 } 00110 00111 QGraphicsView::mousePressEvent(event); 00112 } 00113 00114 void GraphicsView::mouseMoveEvent ( QMouseEvent * event ) 00115 { 00116 if ( dragMode() == QGraphicsView::NoDrag && 00117 scene->selectedItems.isEmpty() && 00118 (scene->clickedButton == Qt::RightButton || 00119 (scene->clickedButton == Qt::LeftButton && event->modifiers() == Qt::ControlModifier) )) 00120 setDragMode(QGraphicsView::ScrollHandDrag); 00121 00122 QGraphicsView::mouseMoveEvent(event); 00123 } 00124 00125 void GraphicsView::keyPressEvent ( QKeyEvent * event ) 00126 { 00127 if (scene && scene->networkWindow) 00128 { 00129 scene->networkWindow->setAsCurrentWindow(); 00130 } 00131 QGraphicsView::keyPressEvent(event); 00132 } 00133 00134 void GraphicsView::dragEnterEvent(QDragEnterEvent *event) 00135 { 00136 event->accept(); 00137 } 00138 00139 void GraphicsView::dragMoveEvent(QDragMoveEvent *event) 00140 { 00141 } 00142 00143 void GraphicsView::dropEvent(QDropEvent * event) 00144 { 00145 QList<QUrl> urlList; 00146 QList<QFileInfo> files; 00147 QString fName; 00148 QFileInfo info; 00149 00150 const QMimeData * mimeData = event->mimeData(); 00151 00152 if (mimeData->hasUrls()) 00153 { 00154 urlList = event->mimeData()->urls(); // returns list of QUrls 00155 // if just text was dropped, urlList is empty (size == 0) 00156 if ( urlList.size() > 0) // if at least one QUrl is present in list 00157 { 00158 fName = urlList[0].toLocalFile(); // convert first QUrl to local path 00159 info.setFile( fName ); // information about file 00160 if ( info.isFile() ) 00161 files += info; 00162 } 00163 } 00164 00165 if (!files.isEmpty() && scene && scene->network && scene->network->mainWindow) 00166 { 00167 event->accept(); 00168 scene->network->mainWindow->loadFiles(files); 00169 } 00170 else 00171 { 00172 QString text = mimeData->text(); 00173 if (!text.isNull() && !text.isEmpty()) 00174 { 00175 scene->lastPoint() = mapToScene(event->pos()); 00176 emit itemsDropped(scene, text, scene->lastPoint()); 00177 } 00178 } 00179 } 00180 00181 }