From 73d7da6d872dd1a345a3b1c89b687c7b27a7d82a Mon Sep 17 00:00:00 2001 From: Jekyll Wu Date: Tue, 22 May 2012 17:28:38 +0800 Subject: [PATCH] Make ViewContainerTabBar and TabbedViewContainer less coupled In general, those two classes are coupled too closely. Ideally, TabbedViewContainer should be transformed into a generic enhanced tabbar which knows nothing about TabbedViewContainer or its user. --- src/ViewContainer.cpp | 31 +++++++++++++++++++++---------- src/ViewContainer.h | 4 ++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/ViewContainer.cpp b/src/ViewContainer.cpp index 0f03a7f3..afecf786 100644 --- a/src/ViewContainer.cpp +++ b/src/ViewContainer.cpp @@ -357,22 +357,15 @@ int ViewContainerTabBar::dropIndex(const QPoint& pos) const } bool ViewContainerTabBar::proposedDropIsSameTab(const QDropEvent* event) const { - const int index = dropIndex(event->pos()); - const int droppedId = ViewProperties::decodeMimeData(event->mimeData()); const bool sameTabBar = event->source() == this; - if (!sameTabBar) return false; - const QList viewList = _container->views(); + const int index = dropIndex(event->pos()); int sourceIndex = -1; - for (int i = 0; i < count(); i++) { - int idAtIndex = _container->viewProperties(viewList[i])->identifier(); - if (idAtIndex == droppedId) - sourceIndex = i; - } + emit querySourceIndex(event, sourceIndex); - bool sourceAndDropAreLast = sourceIndex == count() - 1 && index == -1; + const bool sourceAndDropAreLast = sourceIndex == count() - 1 && index == -1; if (sourceIndex == index || sourceIndex == index - 1 || sourceAndDropAreLast) return true; else @@ -442,6 +435,8 @@ TabbedViewContainer::TabbedViewContainer(NavigationPosition position , QObject* connect(_tabBar, SIGNAL(newTabRequest()), this, SIGNAL(newViewRequest())); connect(_tabBar, SIGNAL(wheelDelta(int)), this, SLOT(wheelScrolled(int))); connect(_tabBar, SIGNAL(initiateDrag(int)), this, SLOT(startTabDrag(int))); + connect(_tabBar, SIGNAL(querySourceIndex(const QDropEvent*,int&)), + this, SLOT(querySourceIndex(const QDropEvent*,int&))); connect(_tabBar, SIGNAL(contextMenu(int,QPoint)), this, SLOT(openTabContextMenu(int,QPoint))); @@ -673,6 +668,22 @@ void TabbedViewContainer::startTabDrag(int tab) } } +void TabbedViewContainer::querySourceIndex(const QDropEvent* event ,int& sourceIndex) +{ + const int droppedId = ViewProperties::decodeMimeData(event->mimeData()); + + const QList viewList = views(); + const int count = viewList.count(); + int index = -1 ; + for (index = 0; index < count; index++) { + const int id = viewProperties(viewList[index])->identifier(); + if (id == droppedId) + break; + } + + sourceIndex = index; +} + void TabbedViewContainer::tabDoubleClicked(int index) { renameTab(index); diff --git a/src/ViewContainer.h b/src/ViewContainer.h index e7f29449..e1a14cd6 100644 --- a/src/ViewContainer.h +++ b/src/ViewContainer.h @@ -362,6 +362,9 @@ public: // returns a pixmap image of a tab for use with QDrag QPixmap dragDropPixmap(int tab); +signals: + void querySourceIndex(const QDropEvent* event, int& sourceIndex) const; + protected: virtual void dragEnterEvent(QDragEnterEvent* event); virtual void dragLeaveEvent(QDragLeaveEvent* event); @@ -434,6 +437,7 @@ private slots: void tabContextMenuRenameTab(); void tabContextMenuDetachTab(); void startTabDrag(int index); + void querySourceIndex(const QDropEvent* event, int& sourceIndex); signals: void detachTab(ViewContainer * self, QWidget * activeView);