File indexing completed on 2024-12-22 04:17:44

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "scene.h"
0014 #include "view.h"
0015 #include "viewitem.h"
0016 #include "plotitem.h"
0017 #include "plotrenderitem.h"
0018 
0019 
0020 #include <QDebug>
0021 #include <QGraphicsItem>
0022 #include <QGraphicsSceneContextMenuEvent>
0023 
0024 namespace Kst {
0025 
0026 Scene::Scene(View *view)
0027   : QGraphicsScene(view) {
0028 }
0029 
0030 
0031 Scene::~Scene() {
0032 }
0033 
0034 /*
0035  * This is reimplemented because of a bug in Qt 4.3 QGraphicScene in the
0036  * way it handles context menu events.  The docs say that if an item ignores
0037  * this event that it'll be passed down to the next item underneath.  However,
0038  * this does not happen in the default implementation.
0039  */
0040 void Scene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
0041   QList<QGraphicsItem*> list = items(event->scenePos());
0042   if (list.isEmpty()) {
0043     if (View *view = qobject_cast<View*>(parent())) {
0044       view->viewContextMenuEvent();
0045     }
0046   } else {
0047     foreach (QGraphicsItem *item, list) {
0048       ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
0049       if (!viewItem)
0050         continue;
0051 
0052       if (viewItem->acceptsContextMenuEvents()) {
0053           event->setPos(viewItem->mapFromScene(event->scenePos()));
0054           if (viewItem->doSceneEvent(event))
0055             return;
0056       }
0057     }
0058   }
0059   QGraphicsScene::contextMenuEvent(event);
0060 }
0061 
0062 
0063 void Scene::dragEnterEvent(QGraphicsSceneDragDropEvent* event)
0064 {
0065   if (MimeDataViewItem::downcast(event->mimeData())) {
0066       event->acceptProposedAction();
0067   } else if (event->mimeData()->hasUrls()) {
0068     event->ignore();
0069   }
0070 }
0071 
0072 void Scene::dragMoveEvent(QGraphicsSceneDragDropEvent* event)
0073 {
0074   if (MimeDataViewItem::downcast(event->mimeData())) {
0075       event->acceptProposedAction();
0076   }
0077 }
0078 
0079 void Scene::dropEvent(QGraphicsSceneDragDropEvent* event)
0080 {
0081   const MimeDataViewItem* m = MimeDataViewItem::downcast(event->mimeData());
0082   if (m && m->item) {
0083     View* view = qobject_cast<View*>(parent());
0084     if (view->viewMode() != View::Layout) {
0085       view->setViewMode(View::Layout);
0086     }
0087     if (view != m->item->view()) {
0088       m->item->setView(view);
0089 
0090       m->item->setParentViewItem(0);
0091       addItem(m->item);
0092 
0093     }
0094     m->item->show();
0095     QPointF viewpos = view->mapFromScene(event->scenePos());
0096     //qDebug() << "viewpos: " << viewpos << " scenepos: " << event->scenePos();
0097     //qDebug() << "m hotspot: " << m->hotSpot << " rect: " << m->item->rect() <<
0098     //            " drop hot spot" << m->item->dropHotSpot << " topleft:" << m->item->rect().topLeft();
0099     m->item->moveTo(viewpos - m->item->rect().center() - m->item->dropHotSpot.toPoint());
0100     event->acceptProposedAction();
0101   } else {
0102     event->ignore();
0103   }
0104 }
0105 
0106 
0107 }
0108 
0109 // vim: ts=2 sw=2 et