Implemented proper tab handling
This commit is contained in:
@@ -34,8 +34,12 @@
|
||||
#include <QLabel>
|
||||
#include <QMouseEvent>
|
||||
#include <QStyle>
|
||||
#include <QApplication>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "DockWidget.h"
|
||||
#include "DockAreaWidget.h"
|
||||
|
||||
namespace ads
|
||||
{
|
||||
@@ -50,6 +54,8 @@ struct DockWidgetTitleBarPrivate
|
||||
QLabel* TitleLabel;
|
||||
QPoint DragStartMousePosition;
|
||||
bool IsActiveTab = false;
|
||||
bool TabMoving = false;
|
||||
CDockAreaWidget* DockArea = nullptr;
|
||||
|
||||
/**
|
||||
* Private data constructor
|
||||
@@ -60,6 +66,11 @@ struct DockWidgetTitleBarPrivate
|
||||
* Creates the complete layout including all controls
|
||||
*/
|
||||
void createLayout();
|
||||
|
||||
/**
|
||||
* Moves the tab depending on the position in the given mouse event
|
||||
*/
|
||||
void moveTab(QMouseEvent* ev);
|
||||
};
|
||||
// struct DockWidgetTitleBarPrivate
|
||||
|
||||
@@ -90,6 +101,18 @@ void DockWidgetTitleBarPrivate::createLayout()
|
||||
TitleLabel->setText(DockWidget->windowTitle());
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
void DockWidgetTitleBarPrivate::moveTab(QMouseEvent* ev)
|
||||
{
|
||||
ev->accept();
|
||||
int left, top, right, bottom;
|
||||
_this->getContentsMargins(&left, &top, &right, &bottom);
|
||||
QPoint moveToPos = _this->mapToParent(ev->pos()) - DragStartMousePosition;
|
||||
moveToPos.setY(0);
|
||||
_this->move(moveToPos);
|
||||
_this->raise();
|
||||
}
|
||||
|
||||
|
||||
//============================================================================
|
||||
CDockWidgetTitleBar::CDockWidgetTitleBar(CDockWidget* DockWidget, QWidget *parent) :
|
||||
@@ -124,19 +147,57 @@ void CDockWidgetTitleBar::mousePressEvent(QMouseEvent* ev)
|
||||
//============================================================================
|
||||
void CDockWidgetTitleBar::mouseReleaseEvent(QMouseEvent* ev)
|
||||
{
|
||||
// End of tab moving, change order now
|
||||
if (d->TabMoving && d->DockArea)
|
||||
{
|
||||
// Find tab under mouse
|
||||
QPoint pos = d->DockArea->mapFromGlobal(ev->globalPos());
|
||||
int fromIndex = d->DockArea->tabIndex(d->DockWidget);
|
||||
int toIndex = d->DockArea->indexOfContentByTitlePos(pos, this);
|
||||
if (-1 == toIndex)
|
||||
{
|
||||
toIndex = d->DockArea->count() - 1;
|
||||
}
|
||||
std::cout << "Move tab from " << fromIndex << " to " << toIndex << std::endl;
|
||||
d->DockArea->reorderDockWidget(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
if (!d->DragStartMousePosition.isNull())
|
||||
{
|
||||
emit clicked();
|
||||
}
|
||||
|
||||
d->DragStartMousePosition = QPoint();
|
||||
d->TabMoving = false;
|
||||
//mcw->m_SectionDropOverlay->hideDropOverlay();
|
||||
//mcw->hideContainerOverlay();
|
||||
QFrame::mouseReleaseEvent(ev);
|
||||
}
|
||||
|
||||
|
||||
//============================================================================
|
||||
void CDockWidgetTitleBar::mouseMoveEvent(QMouseEvent* ev)
|
||||
{
|
||||
if (!(ev->buttons() & Qt::LeftButton))
|
||||
{
|
||||
QFrame::mouseMoveEvent(ev);
|
||||
return;
|
||||
}
|
||||
|
||||
// move tab
|
||||
if (d->TabMoving)
|
||||
{
|
||||
d->moveTab(ev);
|
||||
}
|
||||
|
||||
if ((ev->pos() - d->DragStartMousePosition).manhattanLength() >= QApplication::startDragDistance() // Wait a few pixels before start moving
|
||||
&& d->DockArea->titleAreaGeometry().contains(d->DockArea->mapFromGlobal(ev->globalPos())))
|
||||
{
|
||||
d->TabMoving = true;
|
||||
return;
|
||||
}
|
||||
|
||||
QFrame::mouseMoveEvent(ev);
|
||||
}
|
||||
|
||||
|
||||
@@ -171,6 +232,13 @@ CDockWidget* CDockWidgetTitleBar::dockWidget() const
|
||||
{
|
||||
return d->DockWidget;
|
||||
}
|
||||
|
||||
|
||||
//============================================================================
|
||||
void CDockWidgetTitleBar::setDockAreaWidget(CDockAreaWidget* DockArea)
|
||||
{
|
||||
d->DockArea = DockArea;
|
||||
}
|
||||
} // namespace ads
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user