qskinny/src/controls/QskSubWindow.cpp

264 lines
5.8 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 "QskSubWindow.h"
#include "QskAspect.h"
#include "QskFunctions.h"
2018-10-29 20:11:48 +01:00
#include "QskGraphic.h"
#include "QskGraphicProvider.h"
#include "QskTextOptions.h"
2018-08-03 08:15:28 +02:00
#include "QskQuick.h"
2017-07-21 18:21:34 +02:00
2018-10-29 20:11:48 +01:00
#include <qurl.h>
2017-07-21 18:21:34 +02:00
QSK_SUBCONTROL( QskSubWindow, Panel )
2021-04-28 09:32:49 +02:00
QSK_SUBCONTROL( QskSubWindow, TitleBarPanel )
2018-10-29 20:11:48 +01:00
QSK_SUBCONTROL( QskSubWindow, TitleBarSymbol )
QSK_SUBCONTROL( QskSubWindow, TitleBarText )
2017-07-21 18:21:34 +02:00
2021-04-28 09:32:49 +02:00
namespace
{
inline QskAspect aspectDecoration()
{
return QskSubWindow::TitleBarPanel | QskAspect::Flag | QskAspect::Style;
}
}
2017-07-21 18:21:34 +02:00
class QskSubWindow::PrivateData
{
2018-08-03 08:15:28 +02:00
public:
2017-07-21 18:21:34 +02:00
PrivateData()
2018-10-29 20:11:48 +01:00
: isWindowIconSourceDirty( false )
2017-07-21 18:21:34 +02:00
{
2018-10-29 20:11:48 +01:00
windowTitleTextOptions.setElideMode( Qt::ElideRight );
2017-07-21 18:21:34 +02:00
}
2018-10-29 20:11:48 +01:00
QString windowTitle;
QskTextOptions windowTitleTextOptions;
QUrl windowIconSource;
QskGraphic windowIcon;
bool isWindowIconSourceDirty : 1;
2017-07-21 18:21:34 +02:00
};
2018-08-03 08:15:28 +02:00
QskSubWindow::QskSubWindow( QQuickItem* parent )
: Inherited( parent )
, m_data( new PrivateData() )
2017-07-21 18:21:34 +02:00
{
setMargins( 0 );
initSizePolicy( QskSizePolicy::MinimumExpanding, QskSizePolicy::MinimumExpanding );
2017-07-21 18:21:34 +02:00
setAutoLayoutChildren( true );
}
QskSubWindow::~QskSubWindow()
{
}
2021-04-28 09:32:49 +02:00
void QskSubWindow::setDecorations( Decorations decorations )
2017-07-21 18:21:34 +02:00
{
2021-04-28 09:32:49 +02:00
if ( setFlagHint( aspectDecoration(), decorations ) )
Q_EMIT decorationsChanged( decorations );
}
2017-07-21 18:21:34 +02:00
2021-04-28 09:32:49 +02:00
QskSubWindow::Decorations QskSubWindow::decorations() const
{
return flagHint< QskSubWindow::Decorations >( aspectDecoration() );
}
2017-07-21 18:21:34 +02:00
2021-04-28 09:32:49 +02:00
void QskSubWindow::setDecoration( Decoration decoration, bool on )
{
auto d = decorations();
2017-07-21 18:21:34 +02:00
2021-04-28 09:32:49 +02:00
if ( on )
d |= decoration;
else
d &= ~decoration;
2017-07-21 18:21:34 +02:00
2021-04-28 09:32:49 +02:00
setDecorations( d );
2017-07-21 18:21:34 +02:00
}
2021-04-28 09:32:49 +02:00
void QskSubWindow::resetDecorations()
2017-07-21 18:21:34 +02:00
{
2021-04-28 09:32:49 +02:00
if ( resetFlagHint( aspectDecoration() ) )
Q_EMIT decorationsChanged( decorations() );
}
bool QskSubWindow::hasDecoration( Decoration decoration ) const
{
return decorations() & decoration;
2018-10-29 20:11:48 +01:00
}
void QskSubWindow::setWindowTitle( const QString& title )
{
if ( m_data->windowTitle != title )
{
m_data->windowTitle = title;
2019-03-26 16:10:15 +01:00
2019-03-26 16:11:50 +01:00
update();
2018-10-29 20:11:48 +01:00
Q_EMIT windowTitleChanged();
}
}
QString QskSubWindow::windowTitle() const
{
return m_data->windowTitle;
2017-07-21 18:21:34 +02:00
}
2018-10-29 20:11:48 +01:00
void QskSubWindow::setWindowTitleTextOptions( const QskTextOptions& options )
2017-07-21 18:21:34 +02:00
{
2018-10-29 20:11:48 +01:00
if ( options != m_data->windowTitleTextOptions )
2017-07-21 18:21:34 +02:00
{
2018-10-29 20:11:48 +01:00
m_data->windowTitleTextOptions = options;
update();
Q_EMIT windowTitleTextOptionsChanged();
2017-07-21 18:21:34 +02:00
}
}
2018-10-29 20:11:48 +01:00
QskTextOptions QskSubWindow::windowTitleTextOptions() const
2017-07-21 18:21:34 +02:00
{
2018-10-29 20:11:48 +01:00
return m_data->windowTitleTextOptions;
2017-07-21 18:21:34 +02:00
}
2018-11-05 13:50:41 +01:00
void QskSubWindow::setWindowIconSource( const QString& url )
{
setWindowIconSource( QUrl( url ) );
}
2018-10-29 20:11:48 +01:00
void QskSubWindow::setWindowIconSource( const QUrl& url )
{
if ( m_data->windowIconSource == url )
return;
m_data->windowIconSource = url;
m_data->windowIcon.reset();
m_data->isWindowIconSourceDirty = true;
polish();
update();
Q_EMIT windowIconSourceChanged();
}
QUrl QskSubWindow::windowIconSource() const
{
return m_data->windowIconSource;
}
void QskSubWindow::setWindowIcon( const QskGraphic& graphic )
{
if ( graphic != m_data->windowIcon )
{
m_data->windowIcon = graphic;
if ( !m_data->windowIconSource.isEmpty() )
{
m_data->windowIconSource = QString();
m_data->isWindowIconSourceDirty = false;
Q_EMIT windowIconSourceChanged();
}
polish();
update();
Q_EMIT windowIconChanged();
}
2019-01-04 13:42:16 +01:00
}
2018-10-29 20:11:48 +01:00
QskGraphic QskSubWindow::windowIcon() const
{
return m_data->windowIcon;
}
bool QskSubWindow::hasWindowIcon() const
{
return !( windowIcon().isEmpty() && windowIconSource().isEmpty() );
}
2017-07-21 18:21:34 +02:00
QRectF QskSubWindow::titleBarRect() const
{
2021-04-28 09:32:49 +02:00
return subControlRect( TitleBarPanel );
2017-07-21 18:21:34 +02:00
}
bool QskSubWindow::event( QEvent* event )
{
if ( event->type() == QEvent::LayoutRequest )
resetImplicitSize();
return Inherited::event( event );
}
2018-10-29 20:11:48 +01:00
void QskSubWindow::updateLayout()
{
if ( m_data->isWindowIconSourceDirty )
{
if ( !m_data->windowIconSource.isEmpty() )
{
m_data->windowIcon = Qsk::loadGraphic( m_data->windowIconSource );
Q_EMIT windowIconChanged();
}
m_data->isWindowIconSourceDirty = false;
}
Inherited::updateLayout();
}
QRectF QskSubWindow::layoutRectForSize( const QSizeF& size ) const
2017-07-21 18:21:34 +02:00
{
2019-04-30 09:05:08 +02:00
QRectF rect;
rect.setSize( size );
2021-04-28 09:32:49 +02:00
rect.setTop( subControlRect( size, TitleBarPanel ).bottom() );
return innerBox( Panel, rect );
2017-07-21 18:21:34 +02:00
}
QSizeF QskSubWindow::layoutSizeHint(
Qt::SizeHint which, const QSizeF& constraint ) const
2017-07-21 18:21:34 +02:00
{
2018-10-29 20:11:48 +01:00
// the size we get from the children
auto hint = Inherited::layoutSizeHint( which, constraint );
2017-07-21 18:21:34 +02:00
if ( which == Qt::PreferredSize )
{
// should be Minimum Width/Height from the skin hints
if ( hint.width() < 0.0 )
hint.setWidth( qskDpiScaled( 100 ) );
2017-07-21 18:21:34 +02:00
if ( hint.height() < 0.0 )
hint.setHeight( qskDpiScaled( 80 ) );
}
2017-07-21 18:21:34 +02:00
return hint;
}
void QskSubWindow::itemChange( QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData& value )
{
Inherited::itemChange( change, value );
2018-08-03 08:15:28 +02:00
switch ( change )
2017-07-21 18:21:34 +02:00
{
case QQuickItem::ItemChildAddedChange:
case QQuickItem::ItemChildRemovedChange:
{
if ( !qskIsTransparentForPositioner( value.item ) )
2017-07-21 18:21:34 +02:00
{
resetImplicitSize();
polish();
}
break;
}
default:
;
}
}
#include "moc_QskSubWindow.cpp"