Ejemplo n.º 1
0
void ElidingButton::elideText( const QSize &widgetSize )
{
    const int width = widgetSize.width();
    const int iconWidth = icon().isNull() ? 0 : iconSize().width();

    int left, top, right, bottom;
    getContentsMargins( &left, &top, &right, &bottom );
    int padding = left + right + 4;
    int textWidth = width - ( iconWidth + padding );

    QFontMetrics fm( font() );
    QString elidedText = fm.elidedText( m_fullText, Qt::ElideRight, textWidth );
    QPushButton::setText( elidedText );

    bool elided = ( elidedText != m_fullText );

    // If there is no tooltip set, then we set it to be the full text when elided,
    // and clear it if the button is no longer elided.
    const QString tip = toolTip();
    if( elided && tip.isEmpty() )
        setToolTip( m_fullText );
    else if( !elided && tip == m_fullText )
        setToolTip( QString() );

    if( elided )
        setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
    else
        setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );

    if( m_isElided != elided )
    {
        m_isElided = elided;
        emit( sizePolicyChanged() );
    }
}
Ejemplo n.º 2
0
BrowserBreadcrumbItem::BrowserBreadcrumbItem( BrowserCategory* category, QWidget* parent )
    : KHBox( parent )
    , m_menuButton( 0 )
{
    //figure out if we want to add a menu to this item. A menu allows you to select
    //any of the _sibling_ items. (yes, I know, this is different from how Dolphin
    //does it, but I find the Dolphin way amazingly unintuitive and I always get it
    //wrong when using it...)

    BrowserCategoryList * parentList = category->parentList();
    if( parentList )
    {
        m_menuButton = new BreadcrumbItemMenuButton( this );
        QMenu *menu = new QMenu( 0 );
        
        QMap<QString,BrowserCategory *> siblingMap = parentList->categories();

        QStringList siblingNames = siblingMap.keys();

        foreach( const QString &siblingName, siblingNames )
        {
            //no point in adding ourselves to this menu
            if ( siblingName == category->name() )
                continue;
            
            BrowserCategory *siblingCategory = siblingMap.value( siblingName );
            
            QAction *action = menu->addAction( siblingCategory->icon(), siblingCategory->prettyName() );
            connect( action, SIGNAL( triggered() ), siblingMap.value( siblingName ), SLOT( activate() ) );
        }

        m_menuButton->setMenu( menu );
    }

    m_mainButton = new BreadcrumbItemButton( category->icon(), category->prettyName(), this );

    if( category->prettyName().isEmpty() )
    {
        // root item
        m_mainButton->setToolTip( i18n( "Media Sources Home" ) );
        m_mainButton->setIcon( KIcon( "user-home" ) );
    }

    connect( m_mainButton, SIGNAL( sizePolicyChanged() ), this, SLOT( updateSizePolicy() ) );

    //if this is a list, make cliking on this item cause us
    //to navigate to its home.
    BrowserCategoryList *list = qobject_cast<BrowserCategoryList*>( category );
    if ( list )
    {
        connect( m_mainButton, SIGNAL( clicked( bool ) ), list, SLOT( home() ) );
    }
    else  
    {
        connect( m_mainButton, SIGNAL( clicked( bool ) ), category, SLOT( reActivate() ) );
    }

    adjustSize();
    m_nominalWidth = width();
    
    hide();

    updateSizePolicy();
}