qskinny/src/controls/QskFocusIndicator.cpp

166 lines
4.4 KiB
C++
Raw Normal View History

2017-07-21 16:21:34 +00:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
#include "QskFocusIndicator.h"
#include "QskBoxNode.h"
2017-07-21 16:21:34 +00:00
#include "QskAspect.h"
#include "QskEvent.h"
#include <QQuickWindow>
QSK_SUBCONTROL( QskFocusIndicator, Panel )
static void qskSetupGeometryConnections(
const QQuickItem* sender, QQuickItem* receiver, const char* method )
{
QObject::connect( sender, SIGNAL( xChanged() ), receiver, method );
QObject::connect( sender, SIGNAL( yChanged() ), receiver, method );
QObject::connect( sender, SIGNAL( widthChanged() ), receiver, method );
QObject::connect( sender, SIGNAL( heightChanged() ), receiver, method );
}
QskFocusIndicator::QskFocusIndicator( QQuickItem* parent ):
Inherited( parent ) // parentItem() might change, but parent() stays
2017-07-21 16:21:34 +00:00
{
setTransparentForPositioner( true );
resetConnections();
connectWindow( window(), true );
}
QskFocusIndicator::~QskFocusIndicator()
{
}
void QskFocusIndicator::onFocusItemGeometryChanged()
{
updateFocusFrame();
}
void QskFocusIndicator::onFocusItemChanged()
{
disconnect( this, SLOT( onFocusItemGeometryChanged() ) );
if ( window() == nullptr )
return;
2017-07-21 16:21:34 +00:00
const QQuickItem* focusItem = window()->activeFocusItem();
if ( ( focusItem == nullptr ) || ( focusItem == window()->contentItem() ) )
2017-07-21 16:21:34 +00:00
{
/*
We might get here, when the previously focused item was destroyed.
Might happen in common situations, like when a subwindow
was closed. We put ourself below the root item then.
*/
2017-07-21 16:21:34 +00:00
if ( parentItem() != window()->contentItem() )
{
setParentItem( window()->contentItem() );
updateFocusFrame();
}
return;
}
qskSetupGeometryConnections( focusItem,
this, SLOT( onFocusItemGeometryChanged() ) );
2017-07-21 16:21:34 +00:00
const QQuickItem* item = focusItem;
while ( item->parentItem() )
{
auto itemParent = item->parentItem();
if ( itemParent == window()->contentItem() || itemParent->clip() )
2017-07-21 16:21:34 +00:00
{
/*
When the focus item is clipped - maybe because of being at the
border of a scrollarea - the focus indicator needs to be
clipped as well. The easiest way to have this is to put us
below the item having the clip.
*/
setParentItem( itemParent );
}
2017-07-21 16:21:34 +00:00
if ( itemParent == parentItem() )
{
// We want to be on top, but do we cover all corner cases ???
2017-07-21 16:21:34 +00:00
setZ( item->z() + 10e-6 );
break;
2017-07-21 16:21:34 +00:00
}
item = itemParent;
qskSetupGeometryConnections( item,
this, SLOT( onFocusItemGeometryChanged() ) );
2017-07-21 16:21:34 +00:00
}
updateFocusFrame();
}
void QskFocusIndicator::updateFocusFrame()
{
QRectF r = focusRect();
setVisible( !r.isEmpty() );
if ( !r.isEmpty() )
{
r = r.marginsAdded( marginsHint( Panel | QskAspect::Padding ) );
2017-07-21 16:21:34 +00:00
setGeometry( r );
}
update();
}
QRectF QskFocusIndicator::focusRect() const
{
if ( window() && parentItem() )
{
const QQuickItem* focusItem = window()->activeFocusItem();
if ( focusItem && ( focusItem != window()->contentItem() ) )
2017-07-21 16:21:34 +00:00
return parentItem()->mapRectFromItem( focusItem, focusItem->boundingRect() );
}
return QRectF();
}
void QskFocusIndicator::resetConnections()
{
disconnect( this, SLOT( updateFocusFrame() ) );
QQuickItem* item = parentItem();
if ( item )
{
qskSetupGeometryConnections( item, this, SLOT(updateFocusFrame()) );
}
}
void QskFocusIndicator::windowChangeEvent( QskWindowChangeEvent* event )
{
Inherited::windowChangeEvent( event );
connectWindow( event->oldWindow(), false );
connectWindow( event->window(), true );
}
void QskFocusIndicator::connectWindow( const QQuickWindow* window, bool on )
{
if ( window == nullptr )
return;
if ( on )
{
connect( window, &QQuickWindow::activeFocusItemChanged,
this, &QskFocusIndicator::onFocusItemChanged );
}
else
{
disconnect( window, &QQuickWindow::activeFocusItemChanged,
this, &QskFocusIndicator::onFocusItemChanged );
}
}
#include "moc_QskFocusIndicator.cpp"