qskinny/src/dialogs/QskMessageWindow.cpp

159 lines
4.2 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 "QskMessageWindow.h"
2018-11-06 18:54:21 +01:00
#include "QskGraphicLabel.h"
#include "QskGraphic.h"
#include "QskLinearBox.h"
#include "QskSkin.h"
#include "QskTextLabel.h"
#include "QskTextOptions.h"
#include <qfontmetrics.h>
namespace
{
class TextLabel final : public QskTextLabel
{
public:
TextLabel( QskMessageWindow* box )
{
setObjectName( QStringLiteral( "QskMessageWindowTextLabel" ) );
initSizePolicy( QskSizePolicy::Preferred, QskSizePolicy::Preferred );
2018-11-06 19:00:42 +01:00
setAlignment( Qt::AlignLeft | Qt::AlignTop );
2018-11-06 18:54:21 +01:00
setWrapMode( QskTextOptions::WordWrap );
connect( this, &QskTextLabel::textChanged,
2018-11-06 19:00:42 +01:00
box, &QskMessageWindow::textChanged );
2018-11-06 18:54:21 +01:00
connect( this, &QskTextLabel::textOptionsChanged,
2018-11-06 19:00:42 +01:00
box, &QskMessageWindow::textOptionsChanged );
2018-11-06 18:54:21 +01:00
}
};
class SymbolLabel final : public QskGraphicLabel
{
public:
SymbolLabel( QskMessageWindow* )
{
setObjectName( QStringLiteral( "QskMessageWindowSymbolLabel" ) );
initSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed );
setAlignment( Qt::AlignTop | Qt::AlignHCenter );
updateSourceSize();
}
protected:
void changeEvent( QEvent* event ) override
{
if ( event->type() == QEvent::FontChange )
updateSourceSize();
QskGraphicLabel::changeEvent( event );
}
private:
void updateSourceSize()
{
// when there is no explicit size known,
// we always adjust the icon according to the font
if ( sourceSize().isEmpty() )
{
const QFont font = effectiveFont( QskTextLabel::Text );
setPreferredSize( -1.0, 1.5 * QFontMetricsF( font ).height() );
}
}
};
}
class QskMessageWindow::PrivateData
{
public:
QskGraphicLabel* symbolLabel;
QskTextLabel* textLabel;
2019-01-04 13:42:16 +01:00
};
2017-07-21 18:21:34 +02:00
2018-08-03 08:15:28 +02:00
QskMessageWindow::QskMessageWindow( QWindow* parent )
: Inherited( parent )
2018-11-06 18:54:21 +01:00
, m_data( new PrivateData() )
2017-07-21 18:21:34 +02:00
{
2018-11-06 18:54:21 +01:00
setFlags( Qt::Dialog | Qt::WindowTitleHint |
Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint );
m_data->textLabel = new TextLabel( this );
2019-01-04 13:42:16 +01:00
2018-11-06 18:54:21 +01:00
m_data->symbolLabel = new SymbolLabel( this );
m_data->symbolLabel->hide();
2019-01-04 13:42:16 +01:00
2018-11-06 18:54:21 +01:00
auto box = new QskLinearBox( Qt::Horizontal );
box->setDefaultAlignment( Qt::AlignTop | Qt::AlignHCenter );
2018-11-06 18:54:21 +01:00
box->setSpacing( 0 );
box->addItem( m_data->symbolLabel );
box->addItem( m_data->textLabel );
2018-11-06 18:54:21 +01:00
box->setStretchFactor( m_data->textLabel, 10 );
2019-01-04 13:42:16 +01:00
2018-11-06 18:54:21 +01:00
setDialogContentItem( box );
2017-07-21 18:21:34 +02:00
}
QskMessageWindow::~QskMessageWindow()
{
}
2018-11-06 19:00:42 +01:00
void QskMessageWindow::setText( const QString& text )
2018-11-06 18:54:21 +01:00
{
m_data->textLabel->setText( text );
}
2018-11-06 19:00:42 +01:00
QString QskMessageWindow::text() const
2018-11-06 18:54:21 +01:00
{
return m_data->textLabel->text();
}
2018-11-06 19:00:42 +01:00
void QskMessageWindow::setTextOptions( const QskTextOptions& options )
2018-11-06 18:54:21 +01:00
{
m_data->textLabel->setTextOptions( options );
}
2018-11-06 19:00:42 +01:00
QskTextOptions QskMessageWindow::textOptions() const
2018-11-06 18:54:21 +01:00
{
return m_data->textLabel->textOptions();
}
void QskMessageWindow::setSymbolSource( const QUrl& url )
{
m_data->symbolLabel->setSource( url );
m_data->symbolLabel->setVisible( !url.isEmpty() );
if ( auto box = qobject_cast< QskLinearBox* >( contentItem() ) )
box->setSpacing( m_data->symbolLabel->isVisible() ? 15 : 0 ); // metrics !!
}
void QskMessageWindow::setSymbolType( int symbolType )
{
const auto skin = m_data->symbolLabel->effectiveSkin();
setSymbol( skin->symbol( symbolType ) );
}
QUrl QskMessageWindow::symbolSource() const
{
return m_data->symbolLabel->source();
}
void QskMessageWindow::setSymbol( const QskGraphic& symbol )
{
m_data->symbolLabel->setVisible( !symbol.isNull() );
m_data->symbolLabel->setGraphic( symbol );
}
QskGraphic QskMessageWindow::symbol() const
{
return m_data->symbolLabel->graphic();
}
2017-07-21 18:21:34 +02:00
#include "moc_QskMessageWindow.cpp"