WQtDockTitleWidget.cpp 8.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#include "../WQt4Gui.h"
#include "../WMainWindow.h"

#include "WQtDockTitleWidget.h"
#include "WQtDockTitleWidget.moc"

WQtDockTitleWidget::WQtDockTitleWidget( QDockWidget* parent ):
    QWidget( parent ),
    dockParent( parent )
{
    construct();
}

void WQtDockTitleWidget::setupButton( QToolButton* btn )
{
    btn->setToolButtonStyle( Qt::ToolButtonIconOnly );
    btn->setAutoRaise( true );
42 43 44 45 46 47 48 49
}

void WQtDockTitleWidget::setupSizeConstraints( QWidget* widget )
{
    widget->setContentsMargins( 0, 0, 0, 0 );
    widget->setFixedHeight( 24 );
    widget->setMinimumSize( 24, 24 );
    widget->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) );
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
}

void WQtDockTitleWidget::construct()
{
    // create titlebar widget and its layout
    QHBoxLayout* titleWidgetLayout = new QHBoxLayout( this );
    titleWidgetLayout->setMargin( 0 );
    titleWidgetLayout->setSpacing( 0 );
    setSizePolicy( QSizePolicy( QSizePolicy::Ignored, QSizePolicy::Fixed ) );

    // title
    m_title = new WScaleLabel( " " + dockParent->windowTitle(), 3, this );
    m_title->setTextInteractionFlags( Qt::NoTextInteraction );

    // close Btn
    m_closeBtn = new QToolButton( this );
    QAction* closeAction = new QAction( WQt4Gui::getMainWindow()->getIconManager()->getIcon( "popup_close" ), "Close", this );
    connect( closeAction, SIGNAL( triggered( bool ) ), dockParent, SLOT( close() ) );
    m_closeBtn->setDefaultAction( closeAction );
    setupButton( m_closeBtn );
70
    setupSizeConstraints( m_closeBtn );
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    m_closeBtn->setMinimumSize( 24, 24 );
    m_closeBtn->setMaximumSize( 24, 24 );

    // create the container for the actions shown in the titlebar directly
    m_tools = new QWidget( this );
    m_toolsLayout = new QHBoxLayout( m_tools );
    m_tools->setLayout( m_toolsLayout );
    m_tools->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
    m_toolsLayout->setMargin( 0 );
    m_toolsLayout->setSpacing( 0 );
    m_tools->setContentsMargins( 0, 0, 0, 0 );
    m_tools->setMinimumSize( 1, 24 );

    // create the container for the actions dropped out the titlebar to save some space
    m_toolsMenu = new QWidget( this );
    m_toolsMenuLayout = new QHBoxLayout( m_toolsMenu );
    m_toolsMenu->setLayout( m_toolsMenuLayout );
    m_toolsMenuLayout->setMargin( 0 );
    m_toolsMenuLayout->setSpacing( 0 );
    m_toolsMenu->setContentsMargins( 0, 0, 0, 0 );

    // create a button that shows the m_toolsMenu container widget
    m_moreBtn = new QToolButton( this );
    setupButton( m_moreBtn );
95
    setupSizeConstraints( m_moreBtn );
96
    m_moreBtn->setFixedWidth( 32 );
97 98 99 100 101 102 103 104
    m_moreBtn->setPopupMode( QToolButton::InstantPopup );
    m_moreBtn->setIcon(  WQt4Gui::getMainWindow()->getIconManager()->getIcon( "popup_more" ) );
    QMenu* moreMenu = new QMenu();
    QWidgetAction* moreAction = new QWidgetAction( m_toolsMenu );
    moreAction->setDefaultWidget( m_toolsMenu );
    moreMenu->addAction( moreAction );
    m_moreBtn->setMenu( moreMenu );

105 106 107 108 109 110 111 112 113 114
    // help button
    m_helpBtn = new QToolButton( this );
    QAction* helpAction = new QAction( WQt4Gui::getMainWindow()->getIconManager()->getIcon( "questionmark" ), "Help", this );
    connect( helpAction, SIGNAL( triggered( bool ) ), dockParent, SLOT( showHelp() ) );
    m_helpBtn->setDefaultAction( helpAction );
    setupButton( m_helpBtn );
    setupSizeConstraints( m_helpBtn );
    m_helpBtn->setMinimumSize( 24, 24 );
    m_helpBtn->setMaximumSize( 24, 24 );

115 116 117 118 119
    // fill layout
    titleWidgetLayout->addWidget( m_title );
    titleWidgetLayout->addStretch( 100000 );
    titleWidgetLayout->addWidget( m_tools );
    titleWidgetLayout->addWidget( m_moreBtn );
120
    titleWidgetLayout->addWidget( m_helpBtn );
121 122 123 124 125 126 127 128 129 130 131 132 133
    titleWidgetLayout->addWidget( m_closeBtn );
}

void WQtDockTitleWidget::resizeEvent( QResizeEvent* event )
{
    updateLayouts( event->size().width() );
}

void WQtDockTitleWidget::addTitleAction( QAction* action, bool instantPopup )
{
    QToolButton* actionBtn = new QToolButton( this );
    actionBtn->setDefaultAction( action );
    setupButton( actionBtn );
134
    setupSizeConstraints( actionBtn );
135 136 137 138

    if( instantPopup )
    {
        actionBtn->setPopupMode( QToolButton::InstantPopup );
139
        actionBtn->setFixedWidth( 32 );
140
    }
141

142
    addTitleWidget( actionBtn );
143 144
}

145 146 147
void WQtDockTitleWidget::addTitleButton( QToolButton* button )
{
    setupButton( button );
148 149 150 151 152 153 154 155
    setupSizeConstraints( button );

    addTitleWidget( button );
}

void WQtDockTitleWidget::addTitleWidget( QWidget* widget )
{
    setupSizeConstraints( widget );
156 157

    // we keep track of the widgets:
158
    m_titleActionWidgets.push_back( widget );
159 160 161 162 163

    // update the layouts
    updateLayouts( width() );
}

164 165 166 167 168 169 170 171 172 173 174 175
void WQtDockTitleWidget::removeTitleWidget( QWidget* widget )
{
    if( widget )
    {
        m_toolsLayout->removeWidget( widget );
        m_toolsMenuLayout->removeWidget( widget );
        m_titleActionWidgets.removeAll( widget );
        // update the layouts
        updateLayouts( width() );
    }
}

176 177 178 179 180 181 182 183 184 185 186 187 188
void WQtDockTitleWidget::removeTitleAction( QAction* action )
{
    // find the widget for this action
    QToolButton* btn = NULL;
    for( QList< QWidget* >::iterator i = m_titleActionWidgets.begin(); i != m_titleActionWidgets.end(); ++i )
    {
        QToolButton* btnCandidate = dynamic_cast< QToolButton* >( *i );
        if( btnCandidate && ( btnCandidate->defaultAction() == action ) )
        {
            btn = btnCandidate;
        }
    }

189
    removeTitleWidget( btn );
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
}

void WQtDockTitleWidget::addTitleSeperator()
{
    // use a qframe
    QFrame* line = new QFrame();
    line->setFrameShape( QFrame::VLine );
    line->setFrameShadow( QFrame::Sunken );

    // add it
    m_titleActionWidgets.push_back( line );
    // update the layouts
    updateLayouts( width() );
}

void WQtDockTitleWidget::updateLayouts( int width )
{
    // calc the size of widgets and the title and the mandatory close button
208 209 210 211
    int minRequired = m_title->calculateSize( m_title->text().length() ) +
                      m_moreBtn->sizeHint().width() +
                      m_helpBtn->isVisible() * m_helpBtn->sizeHint().width() +
                      m_closeBtn->sizeHint().width();
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

    // check and move items
    int curWidth = minRequired;
    QList< QWidget* > visible;
    QList< QWidget* > hidden;
    QList< QWidget* >* currentList = &visible;
    for( QList< QWidget* >::iterator i = m_titleActionWidgets.begin(); i != m_titleActionWidgets.end(); ++i )
    {
        if( curWidth > width )
        {
            // we reached the size limit.
            currentList = &hidden;
        }
        currentList->push_back( *i );
        curWidth += ( *i )->sizeHint().width();
    }

    // move all visible items to the m_toolsLayout
    for( QList< QWidget* >::iterator i = visible.begin(); i != visible.end(); ++i )
    {
        m_toolsMenuLayout->removeWidget( *i );
        m_toolsLayout->addWidget( *i );
    }

    // move all visible items to the m_toolsMenuLayout
    for( QList< QWidget* >::iterator i = hidden.begin(); i != hidden.end(); ++i )
    {
        m_toolsLayout->removeWidget( *i );
        m_toolsMenuLayout->addWidget( *i );
    }

    // hide more button if nothing needs to be hiden
    m_moreBtn->setHidden( !( hidden.size() ) );
}