Skip to content

Commit

Permalink
Support re-ordering tabs with mouse
Browse files Browse the repository at this point in the history
Just set KDDockWidgets::Config::Flag_AllowReorderTabs before creating
the dock widgets.

Fixes #20
  • Loading branch information
iamsergio committed Dec 26, 2019
1 parent a29a33a commit 927510d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* v1.0 (November 4th, 2019)
- Initial Release

* v1.1 (, 2020)
- Fix dock widgets not filling their complete available space
- Allow tab re-ordering with mouse, via KDDockWidgets::Config::Flag_AllowReorderTabs.
11 changes: 10 additions & 1 deletion examples/dockwidgets/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ int main(int argc, char **argv)
QCommandLineOption customStyle("p", QCoreApplication::translate("main", "Shows how to style framework internals via FrameworkWidgetFactory"));
parser.addOption(customStyle);

QCommandLineOption reorderTabsOption("r", QCoreApplication::translate("main", "Support re-ordering tabs with mouse"));
parser.addOption(reorderTabsOption);

QCommandLineOption noTitleBars("t", QCoreApplication::translate("main", "Never show titlebars"));
parser.addOption(noTitleBars);

Expand All @@ -72,8 +75,14 @@ int main(int argc, char **argv)
: MainWindowOption_HasCentralFrame;
#endif

auto flags = KDDockWidgets::Config::self().flags();
if (parser.isSet(noTitleBars))
KDDockWidgets::Config::self().setFlags(KDDockWidgets::Config::Flags() | KDDockWidgets::Config::Flag_HideTitleBarWhenTabsVisible | KDDockWidgets::Config::Flag_AlwaysShowTabs);
flags |= KDDockWidgets::Config::Flag_HideTitleBarWhenTabsVisible | KDDockWidgets::Config::Flag_AlwaysShowTabs;

if (parser.isSet(reorderTabsOption))
flags |= KDDockWidgets::Config::Flag_AllowReorderTabs;

KDDockWidgets::Config::self().setFlags(flags);

MyMainWindow mainWindow(options);
mainWindow.resize(1200, 1200);
Expand Down
3 changes: 2 additions & 1 deletion src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class DOCKS_EXPORT Config
Flag_NativeTitleBar = 1, ///> Enables the Native OS title bar on OSes that support it (Windows 10, macOS), ignored otherwise. This is mutually exclusive with Flag_AeroSnap
Flag_AeroSnapWithClientDecos = 2, ///> Enables AeroSnap even if we're not using the native title bar. Only supported on Windows 10.
Flag_HideTitleBarWhenTabsVisible = 8, ///> Hides the title bar if there's tabs visible. The empty space in the tab bar becomes draggable.
Flag_AlwaysShowTabs = 16, ///> Always show tabs, even if there's only one
Flag_AlwaysShowTabs = 16, ///> Always show tabs, even if there's only one,
Flag_AllowReorderTabs = 32, /// Allows user to re-order tabs by dragging them
Flag_Default = Flag_AeroSnapWithClientDecos ///> The defaults
};
Q_DECLARE_FLAGS(Flags, Flag)
Expand Down
6 changes: 3 additions & 3 deletions src/private/DragController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,17 @@ bool StatePreDrag::handleMouseMove(QPoint globalPos)
{
if (q->m_draggable->dragCanStart(q->m_pressPos, globalPos)) {
Q_EMIT q->manhattanLengthMove();
return true;
}
return true;
return false;
}

bool StatePreDrag::handleMouseButtonRelease(QPoint)
{
Q_EMIT q->dragCanceled();
return true;
return false;
}


StateDragging::StateDragging(DragController *parent)
: StateBase(parent)
{
Expand Down
36 changes: 36 additions & 0 deletions src/private/widgets/TabBarWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@
*/

#include "TabBarWidget_p.h"
#include "Config.h"

#include <QMouseEvent>
#include <QApplication>

using namespace KDDockWidgets;

TabBarWidget::TabBarWidget(TabWidget *parent)
: QTabBar(parent->asWidget())
, TabBar(this, parent)
{
setMovable(Config::self().flags() & Config::Flag_AllowReorderTabs);
}

int TabBarWidget::numDockWidgets() const
Expand All @@ -53,3 +56,36 @@ void TabBarWidget::mousePressEvent(QMouseEvent *e)
onMousePress(e->pos());
QTabBar::mousePressEvent(e);
}

bool TabBarWidget::dragCanStart(QPoint pressPos, QPoint pos) const
{
// Here we allow the user to re-order tabs instead of dragging them off.
// To do that we just return false here, and QTabBar will handle the mouse event, assuming QTabBar::isMovable.

const bool defaultResult = Draggable::dragCanStart(pressPos, pos);

if (!defaultResult || !isMovable()) {
// Nothing more to do. If the drag wouldn't start anyway, return false.
// And if the tabs aren't movable, just return the default result, which just considers
// QApplication::startDragDistances
return defaultResult;
}

const int index = tabAt(mapFromGlobal(pos));
if (index == -1)
return defaultResult;

const int deltaX = qAbs(pos.x() - pressPos.x());
const int deltaY = qAbs(pos.y() - pressPos.y());

if (deltaY > 5 * QApplication::startDragDistance()) {
// Moving up or down too much results in a detach. No tab re-ordering allowed.
return true;
} else if (deltaY > QApplication::startDragDistance() && deltaX < QApplication::startDragDistance()) {
// Moved a bit up or down, but not left/right, then detach too.
// Only if it's going considerably left/right we allow to re-order tabs.
return true;
}

return false;
}
1 change: 1 addition & 0 deletions src/private/widgets/TabBarWidget_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class DOCKS_EXPORT TabBarWidget : public QTabBar, public TabBar
int numDockWidgets() const override;
int tabAt(QPoint localPos) const override;
protected:
bool dragCanStart(QPoint pressPos, QPoint pos) const override;
void mousePressEvent(QMouseEvent *) override;

};
Expand Down

0 comments on commit 927510d

Please sign in to comment.