3535#include < QDebug>
3636#include < QBoxLayout>
3737#include < QApplication>
38+ #include < QtGlobal>
3839
3940#include " FloatingDockContainer.h"
4041#include " DockAreaWidget.h"
@@ -69,6 +70,16 @@ struct DockAreaTabBarPrivate
6970 * The function reassigns the stylesheet to update the tabs
7071 */
7172 void updateTabs ();
73+
74+ /* *
75+ * Convenience function to access first tab
76+ */
77+ CDockWidgetTab* firstTab () const {return _this->tab (0 );}
78+
79+ /* *
80+ * Convenience function to access last tab
81+ */
82+ CDockWidgetTab* lastTab () const {return _this->tab (_this->count () - 1 );}
7283};
7384// struct DockAreaTabBarPrivate
7485
@@ -366,6 +377,8 @@ void CDockAreaTabBar::onTabWidgetMoved(const QPoint& GlobalPos)
366377
367378 int fromIndex = d->TabsLayout ->indexOf (MovingTab);
368379 auto MousePos = mapFromGlobal (GlobalPos);
380+ MousePos.rx () = qMax (d->firstTab ()->geometry ().left (), MousePos.x ());
381+ MousePos.rx () = qMin (d->lastTab ()->geometry ().right (), MousePos.x ());
369382 int toIndex = -1 ;
370383 // Find tab under mouse
371384 for (int i = 0 ; i < count (); ++i)
@@ -381,38 +394,23 @@ void CDockAreaTabBar::onTabWidgetMoved(const QPoint& GlobalPos)
381394 if (toIndex == fromIndex)
382395 {
383396 toIndex = -1 ;
384- continue ;
385- }
386-
387- if (toIndex < 0 )
388- {
389- toIndex = 0 ;
390397 }
391398 break ;
392399 }
393400
394- // Now check if the mouse is behind the last tab
395- if (toIndex < 0 )
396- {
397- if (MousePos.x () > tab (count () - 1 )->geometry ().right ())
398- {
399- ADS_PRINT (" after all tabs" );
400- toIndex = count () - 1 ;
401- }
402- else
403- {
404- toIndex = fromIndex;
405- }
406- }
407-
408- d->TabsLayout ->removeWidget (MovingTab);
409- d->TabsLayout ->insertWidget (toIndex, MovingTab);
410- if (toIndex >= 0 )
401+ if (toIndex > -1 )
411402 {
403+ d->TabsLayout ->removeWidget (MovingTab);
404+ d->TabsLayout ->insertWidget (toIndex, MovingTab);
412405 ADS_PRINT (" tabMoved from " << fromIndex << " to " << toIndex);
413406 emit tabMoved (fromIndex, toIndex);
414407 setCurrentIndex (toIndex);
415408 }
409+ else
410+ {
411+ // Ensure that the moved tab is reset to its start position
412+ d->TabsLayout ->update ();
413+ }
416414}
417415
418416// ===========================================================================
0 commit comments