[Kst] branches/work/kst/portto4/kst/src/libkstapp
Peter Kümmel
syntheticpp at gmx.net
Sat Sep 4 20:21:03 CEST 2010
SVN commit 1171629 by kuemmel:
Add complete drag and drop between tabs.
CCBUG:248885
M +46 -0 scene.cpp
M +4 -0 scene.h
M +10 -22 tabwidget.cpp
M +2 -0 view.h
M +30 -20 viewitem.cpp
M +5 -1 viewitem.h
--- branches/work/kst/portto4/kst/src/libkstapp/scene.cpp #1171628:1171629
@@ -12,7 +12,10 @@
#include "scene.h"
#include "view.h"
#include "viewitem.h"
+#include "plotitem.h"
+#include "plotrenderitem.h"
+
#include <QDebug>
#include <QGraphicsItem>
#include <QGraphicsSceneContextMenuEvent>
@@ -55,6 +58,49 @@
QGraphicsScene::contextMenuEvent(event);
}
+
+void Scene::dragEnterEvent(QGraphicsSceneDragDropEvent* event)
+{
+ if (MimeDataViewItem::downcast(event->mimeData())) {
+ event->acceptProposedAction();
}
+}
+void Scene::dragMoveEvent(QGraphicsSceneDragDropEvent* event)
+{
+ if (MimeDataViewItem::downcast(event->mimeData())) {
+ event->acceptProposedAction();
+ }
+}
+
+void Scene::dropEvent(QGraphicsSceneDragDropEvent* event)
+{
+ const MimeDataViewItem* m = MimeDataViewItem::downcast(event->mimeData());
+ if (m && m->item) {
+ View* view = qobject_cast<View*>(parent());
+ if (view->viewMode() != View::Layout) {
+ view->setViewMode(View::Layout);
+ }
+ if (view != m->item->parentView()) {
+ m->item->setParentView(view);
+ PlotItem* plotItem = qobject_cast<PlotItem*>(m->item);
+ if (plotItem) {
+ QList<PlotRenderItem*> renderItems = plotItem->renderItems();
+ foreach (PlotRenderItem* renderItem, renderItems) {
+ renderItem->setParentView(view);
+ }
+ }
+ m->item->setParentViewItem(0);
+ addItem(m->item);
+ }
+ m->item->show();
+ QPointF viewpos = view->mapFromScene(event->scenePos());
+ m->item->moveTo(viewpos - m->hotSpot);
+ event->acceptProposedAction();
+ }
+}
+
+
+}
+
// vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/scene.h #1171628:1171629
@@ -29,6 +29,10 @@
protected:
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
+
+ void dragEnterEvent(QGraphicsSceneDragDropEvent*);
+ void dragMoveEvent(QGraphicsSceneDragDropEvent*);
+ void dropEvent(QGraphicsSceneDragDropEvent*);
};
}
--- branches/work/kst/portto4/kst/src/libkstapp/tabwidget.cpp #1171628:1171629
@@ -28,16 +28,14 @@
class TabBar : public QTabBar
{
public:
- TabBar(TabWidget* tabw);
-
+ TabBar();
void dragEnterEvent(QDragEnterEvent* event);
void dragMoveEvent(QDragMoveEvent* event);
void dropEvent(QDropEvent* event);
-
- TabWidget* tabWidget;
};
-TabBar::TabBar(TabWidget* tabw) : QTabBar(), tabWidget(tabw)
+
+TabBar::TabBar()
{
setAcceptDrops(true);
}
@@ -45,6 +43,7 @@
void TabBar::dragEnterEvent(QDragEnterEvent* event)
{
if (MimeDataViewItem::downcast(event->mimeData())) {
+ setCurrentIndex(tabAt(event->pos()));
event->acceptProposedAction();
}
}
@@ -59,33 +58,21 @@
void TabBar::dropEvent(QDropEvent* event)
{
- const MimeDataViewItem* m = MimeDataViewItem::downcast(event->mimeData());
- if (m && m->item) {
- View* view = tabWidget->currentView();
- m->item->setParentView(view);
- PlotItem* plotItem = qobject_cast<PlotItem*>(m->item);
- if (plotItem) {
- QList<PlotRenderItem*> renderItems = plotItem->renderItems();
- foreach (PlotRenderItem* renderItem, renderItems) {
- renderItem->setParentView(view);
+ event->setDropAction(Qt::IgnoreAction);
+ event->accept();
}
- }
- m->item->setParentViewItem(0);
- view->appendToLayout(CurvePlacement::Auto, m->item);
- event->acceptProposedAction();
- }
-}
TabWidget::TabWidget(QWidget *parent)
: QTabWidget(parent) {
- setTabBar(new TabBar(this));
+ setTabBar(new TabBar);
tabBar()->setContextMenuPolicy(Qt::CustomContextMenu);
connect(tabBar(), SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(contextMenu(const QPoint&)));
_cnt = 0;
#if QT_VERSION >= 0x040500
tabBar()->setMovable(true);
+ tabBar()->setExpanding(true);
#endif
}
@@ -108,13 +95,14 @@
parent->undoGroup()->setActiveStack(view->undoStack());
}
+ connect(view, SIGNAL(viewModeChanged(View::ViewMode)), this, SIGNAL(currentViewModeChanged()));
+
QString label = view->objectName().isEmpty() ?
tr("View &%1").arg(++_cnt) :
view->objectName();
addTab(view, label);
setCurrentWidget(view);
- tabBar()->setAcceptDrops(true);
}
--- branches/work/kst/portto4/kst/src/libkstapp/view.h #1171628:1171629
@@ -112,6 +112,7 @@
void setChildMaximized(bool isMax) { _childMaximized = isMax;}
bool childMaximized() const {return _childMaximized;}
+
Q_SIGNALS:
void viewModeChanged(View::ViewMode oldMode);
void mouseModeChanged(View::MouseMode oldMode);
@@ -124,6 +125,7 @@
void viewChanged();
void forceChildResize(QRectF oldRect, QRectF newRect);
void processResize(QSize size);
+
protected:
bool event(QEvent *event);
bool eventFilter(QObject *obj, QEvent *event);
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #1171628:1171629
@@ -49,8 +49,8 @@
// TODO check for memory leaks when enabled.
//#define KST_DISBALE_QOBJECT_PARENT
-// disbale drag & drop
-#define KST_DISABLE_DD
+// enable drag & drop
+#define KST_ENABLE_DD
namespace Kst {
@@ -948,26 +948,38 @@
Q_UNUSED(menu);
}
-void ViewItem::startDragging(QWidget *widget) {
+void ViewItem::startDragging(QWidget *widget, const QPointF& hotspot) {
// UNDO tied zoom settings done in PlotItem::mousePressEvent
setTiedZoom(false, false);
QDrag *drag = new QDrag(widget);
MimeDataViewItem* mimeData = new MimeDataViewItem;
mimeData->item = this;
+ mimeData->hotSpot = hotspot;
drag->setMimeData(mimeData);
QPixmap pixmap(sceneBoundingRect().size().toSize());
pixmap.fill(Qt::white);
QPainter painter(&pixmap);
paint(&painter); // TODO also paint curves
+ QList<QGraphicsItem*> children = childItems();
+ foreach(QGraphicsItem* child, children) {
+ ViewItem* item = qgraphicsitem_cast<ViewItem*>(child);
+ if (item) {
+ item->paint(&painter);
+ }
+ }
+
painter.end();
pixmap.setMask(pixmap.createHeuristicMask());
- drag->setPixmap(pixmap.scaled(pixmap.size()/1.5));
+ drag->setPixmap(pixmap.scaled(pixmap.size()));
+ drag->setHotSpot(hotspot.toPoint());
+ hide();
Qt::DropActions dact = Qt::MoveAction;
Qt::DropAction dropAction = drag->exec(dact);
- if (dropAction == Qt::MoveAction) {
+ if (dropAction != Qt::MoveAction) {
+ show();
}
}
@@ -977,12 +989,10 @@
return;
}
-#ifndef KST_DISABLE_DD
+#ifdef KST_ENABLE_DD
if (!dragStartPosition.isNull() && event->buttons() & Qt::LeftButton) {
- if ((event->pos() - dragStartPosition).toPoint().manhattanLength() > QApplication::startDragDistance()) {
- startDragging(event->widget());
- } else {
- // we are starting drag&drop
+ if (parentView()->mouseMode() == View::Move) {
+ startDragging(event->widget(), dragStartPosition.toPoint());
return;
}
}
@@ -1655,12 +1665,7 @@
const QPointF p = event->pos();
- dragStartPosition = QPointF(0, 0);
- if (checkBox().contains(p)) {
- if (event->buttons() & Qt::LeftButton) {
dragStartPosition = p;
- }
- }
if (isAllowed(TopLeftGrip) && topLeftGrip().contains(p)) {
setActiveGrip(TopLeftGrip);
@@ -1813,6 +1818,13 @@
return QGraphicsItem::itemChange(change, value);
}
+void ViewItem::moveTo(const QPointF& pos)
+{
+ setPos(parentView()->snapPoint(pos));
+ new MoveCommand(this, _originalPosition, pos);
+ maybeReparent();
+ updateRelativeSize();
+}
void ViewItem::viewMouseModeChanged(View::MouseMode oldMode) {
if (parentView()->mouseMode() == View::Move) {
@@ -1823,11 +1835,9 @@
_originalRect = rect();
_originalTransform = transform();
} else if (oldMode == View::Move && _originalPosition != pos()) {
- setPos(parentView()->snapPoint(pos()));
- new MoveCommand(this, _originalPosition, pos());
-
- maybeReparent();
- updateRelativeSize();
+#ifndef KST_ENABLE_DD
+ moveTo(pos());
+#endif
} else if (oldMode == View::Resize && _originalRect != rect()) {
new ResizeCommand(this, _originalRect, rect());
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #1171628:1171629
@@ -86,6 +86,8 @@
virtual void updateRelativeSize();
+ void moveTo(const QPointF& pos);
+
qreal relativeHeight() const { return _parentRelativeHeight; }
void setRelativeHeight(const qreal height) { _parentRelativeHeight = height; }
qreal relativeWidth() const { return _parentRelativeWidth; }
@@ -325,8 +327,9 @@
QObject* parent() const;
void setParent(QObject*);
- void startDragging(QWidget *widget);
+ void startDragging(QWidget *widget, const QPointF& hotspot);
+
friend class View;
friend class Scene;
};
@@ -546,6 +549,7 @@
MimeDataViewItem();
ViewItem* item;
+ QPointF hotSpot;
static const MimeDataViewItem* downcast(const QMimeData*);
};
More information about the Kst
mailing list