CTabbedWindowTabBar
::CTabbedWindowTabBar( QWidget* Parent
) :m_DragWindow( nullptr ),
m_IsDraggingTab( false ),
m_IsDraggingWindow( false )
{
}
void CTabbedWindowTabBar
::mouseMoveEvent( QMouseEvent* event
) {
// Base class.
// Check the dragging window state.
if( m_IsDraggingWindow )
{
// Get the window.
CTabbedWindow* TabbedWindow = static_cast< CTabbedWindow* >( parent()->parent()->parent() );
// Move the window is only possible if the window is not maximized.
if( TabbedWindow->isMaximized() )
TabbedWindow->showNormal();
// Move the window using this delta.
TabbedWindow
->move
( QCursor::pos() - m_DragWindowOffsetPos
);
// Stop here.
return;
}
// Check the dragging tab state.
if( m_IsDraggingTab == false )
return;
// Check if the drag window is not valid.
if( m_DragWindow == nullptr )
{
// Test if the mouse is outside the tab bar.
if( rect().contains( event->pos() ) == false )
{
// Get the tab widget.
// Don't create a new window if the tab bar only has 1 tab.
if( TabWidget->count() == 1 )
{
m_DragWindow = static_cast< CTabbedWindow* >( TabWidget->parent()->parent() );
}
else
{
// Get the main window.
// Remove the tab.
static_cast< CTabbedWindow* >( TabWidget->parent()->parent() )->RemoveTab( TabWidget->currentIndex() );
// Add the tab on the new window.
m_DragWindow = new CTabbedWindow;
m_DragWindow->AddTab( MainWindow );
}
// Set the window properties.
m_DragWindow->setWindowOpacity( 0.5 );
m_DragWindow->setAttribute( Qt::WA_TransparentForMouseEvents );
// Show and move the window.
m_DragWindow->show();
m_DragWindow->move( event->globalPos() );
}
}
else
{
// Find the tab bar under the mouse.
CTabbedWindowTabBar
* TargetTabBar
= qobject_cast< CTabbedWindowTabBar
* >
( QApplication::widgetAt( event
->globalPos
() ) );
// Check if a tab bar is found.
if( TargetTabBar )
{
CTabbedWindow* TargetTabbedWindow = static_cast< CTabbedWindow* >( TargetTabBar->parent()->parent()->parent() );
QMainWindow* MainWindow
= m_DragWindow
->GetCurrentTabWindow
();
m_DragWindow->RemoveTab( m_DragWindow->GetCurrentTab() );
TargetTabbedWindow->AddTab( MainWindow );
m_IsDraggingTab = false;
m_DragWindow = nullptr;
}
else
{
m_DragWindow->move( event->globalPos() );
}
}
}
void CTabbedWindowTabBar
::mousePressEvent( QMouseEvent* event
) {
// Base class.
// Actions only possible with the left button.
if( event->button() == Qt::LeftButton )
{
// Get the tab index, -1 is returned if no tab found.
const int TabIndex = tabAt( event->pos() );
// Check the widget on each side of the tab.
// The dragging tab flag is enabled only if one tab is found and if one widget is in the tab.
m_IsDraggingTab = ( TabIndex != -1 ) && ( ( LeftSide != nullptr ) || ( RightSide != nullptr ) );
// The dragging window flag is enabled only if it's an empty space.
if( TabIndex == -1 )
{
m_IsDraggingWindow = true;
m_DragWindowOffsetPos
= static_cast< CTabbedWindow
* >
( parent
()->parent
()->parent
() )->mapFromGlobal
( QCursor::pos() );
}
else
{
m_IsDraggingWindow = false;
}
}
}
void CTabbedWindowTabBar
::mouseReleaseEvent( QMouseEvent* event
) {
// Base class.
QTabBar::mouseReleaseEvent( event
);
// Set the window properties.
if( m_DragWindow != nullptr )
{
m_DragWindow->setWindowOpacity( 1.0 );
m_DragWindow->setAttribute( Qt::WA_TransparentForMouseEvents, false );
}
// Reset states.
m_IsDraggingWindow = false;
m_DragWindowOffsetPos
= QPoint( 0,
0 );
m_IsDraggingTab = false;
m_DragWindow = nullptr;
}
void CTabbedWindowTabBar
::mouseDoubleClickEvent( QMouseEvent* event
) {
// Action only valid with the left button.
if( event->button() == Qt::LeftButton )
{
// Check if the double click is on an empty space.
if( tabAt( event->pos() ) == -1 )
{
// Get the window.
CTabbedWindow* TabbedWindow = static_cast< CTabbedWindow* >( parent()->parent()->parent() );
// Change the maximize state.
if( TabbedWindow->isMaximized() )
TabbedWindow->showNormal();
else
TabbedWindow->showMaximized();
// Accept the event.
event->accept();
}
}
}
Bookmarks