UI/Qt: Don't allow tabs to be dragged past the new tab button

Before this change, if would a tab it will pass the add new tab button
(+ button).

closes #1124
This commit is contained in:
PiyushXCoder 2024-09-17 17:58:17 +05:30 committed by Tim Ledbetter
parent 779de840af
commit 9b79081a06
Notes: github-actions[bot] 2024-09-18 16:54:07 +00:00
2 changed files with 39 additions and 0 deletions

View File

@ -45,6 +45,39 @@ void TabBar::contextMenuEvent(QContextMenuEvent* event)
tab->context_menu()->exec(event->globalPos());
}
void TabBar::mousePressEvent(QMouseEvent* event)
{
event->ignore();
auto rect_of_current_tab = tabRect(tabAt(event->pos()));
m_x_position_in_selected_tab_while_dragging = event->pos().x() - rect_of_current_tab.x();
QTabBar::mousePressEvent(event);
}
void TabBar::mouseMoveEvent(QMouseEvent* event)
{
event->ignore();
auto rect_of_first_tab = tabRect(0);
auto rect_of_last_tab = tabRect(count() - 1);
auto boundary_limit_for_dragging_tab = QRect(rect_of_first_tab.x() + m_x_position_in_selected_tab_while_dragging, 0,
rect_of_last_tab.x() + m_x_position_in_selected_tab_while_dragging, 0);
if (event->pos().x() >= boundary_limit_for_dragging_tab.x() && event->pos().x() <= boundary_limit_for_dragging_tab.width()) {
QTabBar::mouseMoveEvent(event);
} else {
auto pos = event->pos();
if (event->pos().x() > boundary_limit_for_dragging_tab.width())
pos.setX(boundary_limit_for_dragging_tab.width());
else if (event->pos().x() < boundary_limit_for_dragging_tab.x())
pos.setX(boundary_limit_for_dragging_tab.x());
QMouseEvent ev(event->type(), pos, event->globalPosition(), event->button(), event->buttons(), event->modifiers());
QTabBar::mouseMoveEvent(&ev);
}
}
TabWidget::TabWidget(QWidget* parent)
: QTabWidget(parent)
{

View File

@ -28,6 +28,12 @@ public:
virtual QSize tabSizeHint(int index) const override;
virtual void contextMenuEvent(QContextMenuEvent* event) override;
private:
void mousePressEvent(QMouseEvent*) override;
void mouseMoveEvent(QMouseEvent*) override;
int m_x_position_in_selected_tab_while_dragging;
};
class TabWidget : public QTabWidget {