From dbe46051100dbd55dcd40fb999b5fc9d58cf65c0 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Fri, 25 Apr 2008 20:20:17 +0000 Subject: [PATCH] Draw drop indicator icon disabled if the source and destination locations are the same. Fix rect arguments to border painter calls. svn path=/trunk/KDE/kdebase/apps/konsole/; revision=801164 --- src/ViewContainer.cpp | 76 +++++++++++++++++++++++++------------------ src/ViewContainer.h | 6 +++- src/ViewProperties.h | 13 ++++++++ 3 files changed, 63 insertions(+), 32 deletions(-) diff --git a/src/ViewContainer.cpp b/src/ViewContainer.cpp index fb2ffde64..d17f62ae2 100644 --- a/src/ViewContainer.cpp +++ b/src/ViewContainer.cpp @@ -442,9 +442,10 @@ ViewContainerTabBar::ViewContainerTabBar(QWidget* parent,TabbedViewContainerV2* , _container(container) , _dropIndicator(0) , _dropIndicatorIndex(-1) + , _drawIndicatorDisabled(false) { } -void ViewContainerTabBar::setDropIndicator(int index) +void ViewContainerTabBar::setDropIndicator(int index, bool drawDisabled) { if (!parentWidget() || _dropIndicatorIndex == index) return; @@ -453,13 +454,18 @@ void ViewContainerTabBar::setDropIndicator(int index) const int ARROW_SIZE = 22; bool north = shape() == QTabBar::RoundedNorth || shape() == QTabBar::TriangularNorth; - if (!_dropIndicator) + if (!_dropIndicator || _drawIndicatorDisabled != drawDisabled) { - _dropIndicator = new QLabel(parentWidget()); + if (!_dropIndicator) + { + _dropIndicator = new QLabel(parentWidget()); + _dropIndicator->resize(ARROW_SIZE,ARROW_SIZE); + } + QIcon::Mode drawMode = drawDisabled ? QIcon::Disabled : QIcon::Normal; const QString iconName = north ? "arrow-up" : "arrow-down"; - _dropIndicator->setPixmap(KIcon(iconName).pixmap(ARROW_SIZE,ARROW_SIZE)); - _dropIndicator->resize(ARROW_SIZE,ARROW_SIZE); + _dropIndicator->setPixmap(KIcon(iconName).pixmap(ARROW_SIZE,ARROW_SIZE,drawMode)); + _drawIndicatorDisabled = drawDisabled; } if (index < 0) @@ -506,7 +512,7 @@ void ViewContainerTabBar::dragMoveEvent(QDragMoveEvent* event) if (index == -1) index = count(); - setDropIndicator(index); + setDropIndicator(index,proposedDropIsSameTab(event)); event->acceptProposedAction(); } @@ -526,7 +532,31 @@ int ViewContainerTabBar::dropIndex(const QPoint& pos) const return -1; return tab; -} +} +bool ViewContainerTabBar::proposedDropIsSameTab(const QDropEvent* event) const +{ + int index = dropIndex(event->pos()); + int droppedId = ViewProperties::decodeMimeData(event->mimeData()); + bool sameTabBar = event->source() == this; + + if (!sameTabBar) + return false; + + const QList viewList = _container->views(); + int sourceIndex = -1; + for (int i=0;iviewProperties(viewList[i])->identifier(); + if (idAtIndex == droppedId) + sourceIndex = i; + } + + bool sourceAndDropAreLast = sourceIndex == count()-1 && index == -1; + if (sourceIndex == index || sourceIndex == index-1 || sourceAndDropAreLast) + return true; + else + return false; +} void ViewContainerTabBar::dropEvent(QDropEvent* event) { setDropIndicator(-1); @@ -534,20 +564,11 @@ void ViewContainerTabBar::dropEvent(QDropEvent* event) if (!event->mimeData()->hasFormat(ViewProperties::mimeType())) event->ignore(); - int index = dropIndex(event->pos()); - int droppedId = *(int*)(event->mimeData()->data(ViewProperties::mimeType()).constData()); - bool sameTabBar = event->source() == this; - - int currentId = -1; - if (sameTabBar && index >= 0 && index < count()) - currentId = _container->viewProperties(_container->views()[index])->identifier(); - - if (currentId == droppedId) - { - event->ignore(); - return; - } + if (proposedDropIsSameTab(event)) + event->ignore(); + int index = dropIndex(event->pos()); + int droppedId = ViewProperties::decodeMimeData(event->mimeData()); bool result = false; emit _container->moveViewRequest(index,droppedId,result); @@ -574,12 +595,12 @@ QPixmap ViewContainerTabBar::dragDropPixmap(int tab) QPixmap tabPixmap(rect.width()+borderWidth, rect.height()+borderWidth); QPainter painter(&tabPixmap); - painter.drawPixmap(rect,QPixmap::grabWidget(this,rect)); + painter.drawPixmap(0,0,QPixmap::grabWidget(this,rect)); QPen borderPen; borderPen.setBrush(palette().dark()); borderPen.setWidth(borderWidth); painter.setPen(borderPen); - painter.drawRect(rect); + painter.drawRect(0,0,rect.width(),rect.height()); painter.end(); return tabPixmap; @@ -702,21 +723,14 @@ TabbedViewContainerV2::~TabbedViewContainerV2() void TabbedViewContainerV2::startTabDrag(int tab) { QDrag* drag = new QDrag(_tabBar); - - QMimeData* mimeData = new QMimeData; - const QRect tabRect = _tabBar->tabRect(tab); QPixmap tabPixmap = _tabBar->dragDropPixmap(tab); drag->setPixmap(tabPixmap); int id = viewProperties(views()[tab])->identifier(); - QByteArray data((char*)&id,sizeof(int)); - mimeData->setData(ViewProperties::mimeType(),data); - - QWidget* view = views()[tab]; - - drag->setMimeData(mimeData); + QWidget* view = views()[tab]; + drag->setMimeData(ViewProperties::createMimeData(id)); // start drag, if drag-and-drop is successful the view at 'tab' will be // deleted diff --git a/src/ViewContainer.h b/src/ViewContainer.h index c8d905f83..27c972246 100644 --- a/src/ViewContainer.h +++ b/src/ViewContainer.h @@ -383,14 +383,18 @@ protected: private: // show the indicator arrow which shows where a dropped tab will // be inserted at 'index' - void setDropIndicator(int index); + void setDropIndicator(int index, bool drawDisabled = false); // returns the index at which a tab will be inserted if the mouse // in a drag-drop operation is released at 'pos' int dropIndex(const QPoint& pos) const; + // returns true if the tab to be dropped in a drag-drop operation + // is the same as the tab at the drop location + bool proposedDropIsSameTab(const QDropEvent* event) const; TabbedViewContainerV2* _container; QLabel* _dropIndicator; int _dropIndicatorIndex; + bool _drawIndicatorDisabled; }; // internal diff --git a/src/ViewProperties.h b/src/ViewProperties.h index ead6c49ce..7a3184ec4 100644 --- a/src/ViewProperties.h +++ b/src/ViewProperties.h @@ -24,6 +24,7 @@ #include #include #include +#include // KDE #include @@ -77,6 +78,18 @@ public: /** Name of mime format to use in drag-and-drop operations. */ static QString mimeType() { return _mimeType; } + + static QMimeData* createMimeData(int id) + { + QMimeData* mimeData = new QMimeData; + QByteArray data((char*)&id,sizeof(int)); + mimeData->setData(mimeType(),data); + return mimeData; + } + static int decodeMimeData(const QMimeData* mimeData) + { + return *(int*)(mimeData->data(ViewProperties::mimeType()).constData()); + } signals: /** Emitted when the icon for a view changes */