qskinny/src/controls/QskTabButton.cpp

161 lines
3.3 KiB
C++
Raw Normal View History

2017-07-21 18:21:34 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
#include "QskTabButton.h"
#include "QskTabBar.h"
#include "QskTextOptions.h"
2017-11-17 08:03:38 +01:00
#include "QskSkinlet.h"
2017-07-21 18:21:34 +02:00
2018-07-19 14:10:48 +02:00
#include <qfontmetrics.h>
#include <qpointer.h>
2017-07-21 18:21:34 +02:00
QSK_SUBCONTROL( QskTabButton, Panel )
QSK_SUBCONTROL( QskTabButton, Text )
class QskTabButton::PrivateData
{
public:
PrivateData( const QString& txt ):
text( txt )
{
}
QString text;
QskTextOptions textOptions;
QPointer< QskTabBar > tabBar;
};
QskTabButton::QskTabButton( QQuickItem* parent ):
QskTabButton( QString(), parent )
{
}
QskTabButton::QskTabButton( const QString& text, QQuickItem* parent ):
Inherited( parent ),
m_data( new PrivateData( text ) )
{
resolveTabBar();
initSizePolicy( QskSizePolicy::Minimum, QskSizePolicy::Fixed );
2017-07-21 18:21:34 +02:00
setCheckable( true );
setExclusive( true );
}
QskTabButton::~QskTabButton()
{
}
void QskTabButton::setText( const QString& text )
{
if ( m_data->text == text )
return;
m_data->text = text;
Q_EMIT textChanged( text );
resetImplicitSize();
update();
}
QString QskTabButton::text() const
{
return m_data->text;
}
void QskTabButton::setTextOptions( const QskTextOptions& options )
{
if ( options != m_data->textOptions )
{
m_data->textOptions = options;
Q_EMIT textOptionsChanged();
}
}
QskTextOptions QskTabButton::textOptions() const
{
return m_data->textOptions;
}
QSizeF QskTabButton::contentsSizeHint() const
{
QSizeF size( metric( Panel | QskAspect::MinimumWidth ),
metric( Panel | QskAspect::MinimumHeight ) );
if ( !m_data->text.isEmpty() )
{
const QFontMetricsF fm( effectiveFont( Text ) );
const auto textSize = fm.size( Qt::TextShowMnemonic, m_data->text );
size += textSize;
}
return size;
}
2017-11-17 08:03:38 +01:00
QRectF QskTabButton::layoutRect() const
{
return innerBox( Panel, effectiveSkinlet()->subControlRect( this, Panel ) );
}
QskAspect::Placement QskTabButton::effectivePlacement() const
{
using namespace QskAspect;
if ( m_data->tabBar )
{
const auto o = m_data->tabBar->orientation();
return ( o == Qt::Horizontal ) ? Preserved : Transposed;
}
return Preserved;
}
2017-07-21 18:21:34 +02:00
QskTabBar* QskTabButton::tabBar() const
{
return m_data->tabBar;
}
void QskTabButton::changeEvent( QEvent* event )
{
switch( event->type() )
{
case QEvent::LocaleChange:
{
if ( !m_data->text.isEmpty() )
{
// maybe QLocale::textDirection() has changed
update();
}
break;
}
case QEvent::ParentChange:
{
resolveTabBar();
break;
}
default:
break;
}
Inherited::changeEvent( event );
}
void QskTabButton::resolveTabBar()
{
auto p = parent();
while ( p )
{
if ( const auto tabBar = qobject_cast< QskTabBar* >( p ) )
{
m_data->tabBar = tabBar;
break;
}
p = p->parent();
}
}
#include "moc_QskTabButton.cpp"