Pass the DropArea to setDropIndicatorAllowedFunc() too

So the lambda can have more advanced usage and inspect the
target layout before allowing or disallowing the drop.

For our use case, we want to limit max 3 dock widgets side by side,
for example.
This commit is contained in:
Sergio Martins
2022-06-02 15:19:01 +01:00
parent b2b75cd5bb
commit 982904e2ba
4 changed files with 13 additions and 5 deletions

View File

@@ -267,7 +267,8 @@ int main(int argc, char **argv)
// Dock widget 8 will only be allowed to dock to the outer areasa
auto func = [](KDDockWidgets::DropLocation location,
const KDDockWidgets::DockWidgetBase::List &source,
const KDDockWidgets::DockWidgetBase::List &target) {
const KDDockWidgets::DockWidgetBase::List &target,
KDDockWidgets::DropArea *) {
Q_UNUSED(target); // When dragging into a tab, 'target' would have the list of already tabbed dock widgets
const bool isDraggingDW8 = std::find_if(source.cbegin(), source.cend(), [] (KDDockWidgets::DockWidgetBase *dw) {

View File

@@ -34,6 +34,7 @@ namespace KDDockWidgets {
class DockWidgetBase;
class MainWindowBase;
class FrameworkWidgetFactory;
class DropArea;
typedef KDDockWidgets::DockWidgetBase *(*DockWidgetFactoryFunc)(const QString &name);
typedef KDDockWidgets::MainWindowBase *(*MainWindowFactoryFunc)(const QString &name);
@@ -47,11 +48,13 @@ typedef KDDockWidgets::MainWindowBase *(*MainWindowFactoryFunc)(const QString &n
/// @param location The drop indicator location to allow or disallow
/// @param source The dock widgets being dragged
/// @param target The dock widgets within an existing docked tab group
/// @param dropArea The target drop area. Can belong to a MainWindow or a FloatingWindow.
/// @return true if the docking is allowed.
/// @sa setDropIndicatorAllowedFunc
typedef bool (*DropIndicatorAllowedFunc)(DropLocation location,
const QVector<DockWidgetBase *> &source,
const QVector<DockWidgetBase *> &target);
const QVector<DockWidgetBase *> &target,
DropArea *dropArea);
/// @deprecated Use DropIndicatorAllowedFunc instead.
/// @brief Function to allow the user more granularity to disallow dock widgets to tab together
@@ -262,7 +265,8 @@ public:
*
* auto func = [] (KDDockWidgets::DropLocation loc,
* const KDDockWidgets::DockWidgetBase::List &source,
* const KDDockWidgets::DockWidgetBase::List &target)
* const KDDockWidgets::DockWidgetBase::List &target,
* KDDockWidgets::DropArea *)
* {
* // disallows dockFoo to be docked to outer areas
* return !((loc & KDDockWidgets::DropLocation_Outter) && source.contains(dockFoo));

View File

@@ -96,6 +96,9 @@ public:
FloatingWindow *floatingWindowBeingDragged() const;
/// @brief Returns the current drop area under the mouse
DropArea *dropAreaUnderCursor() const;
///@brief Returns the window being dragged
WindowBeingDragged *windowBeingDragged() const;
@@ -126,7 +129,6 @@ private:
DragController(QObject * = nullptr);
StateBase *activeState() const;
WidgetType *qtTopLevelUnderCursor() const;
DropArea *dropAreaUnderCursor() const;
Draggable *draggableForQObject(QObject *o) const;
QPoint m_pressPos;
QPoint m_offset;

View File

@@ -161,7 +161,8 @@ bool DropIndicatorOverlayInterface::dropIndicatorVisible(DropLocation dropLoc) c
}
if (auto dropIndicatorAllowedFunc = Config::self().dropIndicatorAllowedFunc()) {
if (!dropIndicatorAllowedFunc(dropLoc, source, target))
DropArea *dropArea = DragController::instance()->dropAreaUnderCursor();
if (!dropIndicatorAllowedFunc(dropLoc, source, target, dropArea))
return false;
}