2020-04-28 13:12:48 +03:00
/*
2021-05-27 15:01:00 +00:00
SPDX - FileCopyrightText : 2020 Michail Vourlakos < mvourlakos @ gmail . com >
SPDX - License - Identifier : GPL - 2.0 - or - later
*/
2020-04-28 13:12:48 +03:00
# include "dialog.h"
2021-04-27 16:58:50 +03:00
// Qt
# include <QScreen>
# include <QWindow>
2020-04-28 13:12:48 +03:00
namespace Latte {
namespace Quick {
Dialog : : Dialog ( QQuickItem * parent )
: PlasmaQuick : : Dialog ( parent )
{
2021-12-29 13:01:21 +02:00
connect ( this , & PlasmaQuick : : Dialog : : visualParentChanged , this , & Dialog : : onVisualParentChanged ) ;
2020-04-28 13:12:48 +03:00
}
bool Dialog : : containsMouse ( ) const
{
return m_containsMouse ;
}
void Dialog : : setContainsMouse ( bool contains )
{
if ( m_containsMouse = = contains ) {
return ;
}
m_containsMouse = contains ;
emit containsMouseChanged ( ) ;
}
2021-04-27 16:58:50 +03:00
Plasma : : Types : : Location Dialog : : edge ( ) const
{
return m_edge ;
}
void Dialog : : setEdge ( const Plasma : : Types : : Location & edge )
{
if ( m_edge = = edge ) {
return ;
}
m_edge = edge ;
emit edgeChanged ( ) ;
}
2021-04-28 15:52:17 +03:00
bool Dialog : : isRespectingAppletsLayoutGeometry ( ) const
{
//! As it appears plasma applets popups are defining their popups to Normal window.
2021-05-09 19:27:42 +03:00
return ( type ( ) = = Dialog : : Normal | | type ( ) = = Dialog : : PopupMenu | | type ( ) = = Dialog : : Tooltip ) ;
2021-04-28 15:52:17 +03:00
}
QRect Dialog : : appletsLayoutGeometryFromContainment ( ) const
{
QVariant geom = visualParent ( ) & & visualParent ( ) - > window ( ) ? visualParent ( ) - > window ( ) - > property ( " _applets_layout_geometry " ) : QVariant ( ) ;
return geom . isValid ( ) ? geom . toRect ( ) : QRect ( ) ;
}
2021-04-27 16:58:50 +03:00
2021-05-03 03:08:36 +03:00
int Dialog : : appletsPopUpMargin ( ) const
2021-04-27 16:58:50 +03:00
{
2021-05-03 03:08:36 +03:00
QVariant margin = visualParent ( ) & & visualParent ( ) - > window ( ) ? visualParent ( ) - > window ( ) - > property ( " _applets_popup_margin " ) : QVariant ( ) ;
return margin . isValid ( ) ? margin . toInt ( ) : - 1 ;
}
2021-12-29 13:01:21 +02:00
void Dialog : : onVisualParentChanged ( )
{
// clear mode
for ( auto & c : m_visualParentConnections ) {
disconnect ( c ) ;
}
if ( ! visualParent ( ) | | ! flags ( ) . testFlag ( Qt : : ToolTip ) | | ! visualParent ( ) - > metaObject ( ) ) {
return ;
}
bool hassignal = ( visualParent ( ) - > metaObject ( ) - > indexOfSignal ( QMetaObject : : normalizedSignature ( " anchoredTooltipPositionChanged() " ) ) ! = - 1 ) ;
if ( hassignal ) {
m_visualParentConnections [ 0 ] = connect ( visualParent ( ) , SIGNAL ( anchoredTooltipPositionChanged ( ) ) , this , SLOT ( updateGeometry ( ) ) ) ;
}
}
void Dialog : : updateGeometry ( )
{
if ( visualParent ( ) ) {
setPosition ( popupPosition ( visualParent ( ) , size ( ) ) ) ;
}
}
2021-05-03 03:08:36 +03:00
void Dialog : : updatePopUpEnabledBorders ( )
{
QRect appletslayoutgeometry = appletsLayoutGeometryFromContainment ( ) ;
int appletspopupmargin = appletsPopUpMargin ( ) ;
//! Plasma Scenario
bool hideEdgeBorder = isRespectingAppletsLayoutGeometry ( ) & & ! appletslayoutgeometry . isEmpty ( ) & & appletspopupmargin = = - 1 ;
if ( hideEdgeBorder ) {
setLocation ( m_edge ) ;
} else {
setLocation ( Plasma : : Types : : Floating ) ;
2021-04-27 16:58:50 +03:00
}
2021-05-03 03:08:36 +03:00
}
2021-04-27 16:58:50 +03:00
2021-05-09 20:47:17 +03:00
QPoint Dialog : : popupPosition ( QQuickItem * item , const QSize & size )
{
auto visualparent = item ;
if ( visualparent & & visualparent - > window ( ) & & visualparent - > window ( ) - > screen ( ) ) {
updatePopUpEnabledBorders ( ) ;
QPointF parenttopleftf = visualparent - > mapToGlobal ( QPointF ( 0 , 0 ) ) ;
QPoint parenttopleft = parenttopleftf . toPoint ( ) ;
QScreen * screen = visualparent - > window ( ) - > screen ( ) ;
QRect screengeometry = screen - > geometry ( ) ;
int x = 0 ;
int y = 0 ;
2021-05-09 23:01:34 +03:00
int popupmargin = qMax ( 0 , appletsPopUpMargin ( ) ) ;
2021-05-09 20:47:17 +03:00
if ( m_edge = = Plasma : : Types : : LeftEdge | | m_edge = = Plasma : : Types : : RightEdge ) {
2021-05-09 23:01:34 +03:00
//! vertical scenario
screengeometry - = QMargins ( 0 , popupmargin , 0 , popupmargin ) ;
2021-05-09 20:47:17 +03:00
y = parenttopleft . y ( ) + ( visualparent - > height ( ) / 2 ) - ( size . height ( ) / 2 ) ;
} else {
2021-05-09 23:01:34 +03:00
//! horizontal scenario
screengeometry - = QMargins ( popupmargin , 0 , popupmargin , 0 ) ;
2021-05-09 20:47:17 +03:00
x = parenttopleft . x ( ) + ( visualparent - > width ( ) / 2 ) - ( size . width ( ) / 2 ) ;
}
if ( m_edge = = Plasma : : Types : : LeftEdge ) {
x = parenttopleft . x ( ) + visualparent - > width ( ) + popupmargin ;
} else if ( m_edge = = Plasma : : Types : : RightEdge ) {
x = parenttopleft . x ( ) - size . width ( ) - popupmargin ;
} else if ( m_edge = = Plasma : : Types : : TopEdge ) {
y = parenttopleft . y ( ) + visualparent - > height ( ) + popupmargin ;
} else { // bottom case
y = parenttopleft . y ( ) - size . height ( ) - popupmargin ;
}
2021-05-09 23:01:34 +03:00
x = qBound ( screengeometry . x ( ) , x , screengeometry . right ( ) - size . width ( ) + 1 ) ;
y = qBound ( screengeometry . y ( ) , y , screengeometry . bottom ( ) - size . height ( ) + 1 ) ;
2021-05-09 20:47:17 +03:00
QRect appletslayoutgeometry = appletsLayoutGeometryFromContainment ( ) ;
2021-05-09 23:01:34 +03:00
2021-05-09 20:47:17 +03:00
if ( isRespectingAppletsLayoutGeometry ( ) & & ! appletslayoutgeometry . isEmpty ( ) ) {
QPoint appletsglobaltopleft = visualparent - > window ( ) - > mapToGlobal ( appletslayoutgeometry . topLeft ( ) ) ;
QRect appletsglobalgeometry ( appletsglobaltopleft . x ( ) , appletsglobaltopleft . y ( ) , appletslayoutgeometry . width ( ) , appletslayoutgeometry . height ( ) ) ;
if ( m_edge = = Plasma : : Types : : LeftEdge | | m_edge = = Plasma : : Types : : RightEdge ) {
int bottomy = appletsglobalgeometry . bottom ( ) - size . height ( ) ;
if ( appletsglobalgeometry . height ( ) > = size . height ( ) ) {
y = qBound ( appletsglobalgeometry . y ( ) , y , bottomy + 1 ) ;
}
} else {
int rightx = appletsglobalgeometry . right ( ) - size . width ( ) ;
if ( appletsglobalgeometry . width ( ) > = size . width ( ) ) {
x = qBound ( appletsglobalgeometry . x ( ) , x , rightx + 1 ) ;
}
}
}
return QPoint ( x , y ) ;
}
return PlasmaQuick : : Dialog : : popupPosition ( item , size ) ;
}
/*
2021-05-03 03:08:36 +03:00
void Dialog : : adjustGeometry ( const QRect & geom )
{
2021-04-27 16:58:50 +03:00
auto visualparent = visualParent ( ) ;
if ( visualparent & & visualparent - > window ( ) & & visualparent - > window ( ) - > screen ( ) ) {
2021-05-03 03:08:36 +03:00
updatePopUpEnabledBorders ( ) ;
2021-04-27 16:58:50 +03:00
QPointF parenttopleftf = visualparent - > mapToGlobal ( QPointF ( 0 , 0 ) ) ;
QPoint parenttopleft = parenttopleftf . toPoint ( ) ;
QScreen * screen = visualparent - > window ( ) - > screen ( ) ;
QRect screengeometry = screen - > geometry ( ) ;
int x = 0 ;
int y = 0 ;
if ( m_edge = = Plasma : : Types : : LeftEdge | | m_edge = = Plasma : : Types : : RightEdge ) {
y = parenttopleft . y ( ) + ( visualparent - > height ( ) / 2 ) - ( geom . height ( ) / 2 ) ;
} else {
x = parenttopleft . x ( ) + ( visualparent - > width ( ) / 2 ) - ( geom . width ( ) / 2 ) ;
}
2021-05-03 03:08:36 +03:00
int popupmargin = qMax ( 0 , appletsPopUpMargin ( ) ) ;
2021-04-27 16:58:50 +03:00
if ( m_edge = = Plasma : : Types : : LeftEdge ) {
2021-05-03 04:14:56 +03:00
x = parenttopleft . x ( ) + visualparent - > width ( ) + popupmargin ;
2021-04-27 16:58:50 +03:00
} else if ( m_edge = = Plasma : : Types : : RightEdge ) {
2021-05-03 04:14:56 +03:00
x = parenttopleft . x ( ) - geom . width ( ) - popupmargin ;
2021-04-27 16:58:50 +03:00
} else if ( m_edge = = Plasma : : Types : : TopEdge ) {
2021-05-03 04:14:56 +03:00
y = parenttopleft . y ( ) + visualparent - > height ( ) + popupmargin ;
2021-04-27 16:58:50 +03:00
} else { // bottom case
2021-05-03 04:14:56 +03:00
y = parenttopleft . y ( ) - geom . height ( ) - popupmargin ;
2021-04-27 16:58:50 +03:00
}
x = qBound ( screengeometry . x ( ) , x , screengeometry . right ( ) - 1 ) ;
y = qBound ( screengeometry . y ( ) , y , screengeometry . bottom ( ) - 1 ) ;
2021-04-28 15:52:17 +03:00
QRect appletslayoutgeometry = appletsLayoutGeometryFromContainment ( ) ;
if ( isRespectingAppletsLayoutGeometry ( ) & & ! appletslayoutgeometry . isEmpty ( ) ) {
QPoint appletsglobaltopleft = visualparent - > window ( ) - > mapToGlobal ( appletslayoutgeometry . topLeft ( ) ) ;
QRect appletsglobalgeometry ( appletsglobaltopleft . x ( ) , appletsglobaltopleft . y ( ) , appletslayoutgeometry . width ( ) , appletslayoutgeometry . height ( ) ) ;
if ( m_edge = = Plasma : : Types : : LeftEdge | | m_edge = = Plasma : : Types : : RightEdge ) {
int bottomy = appletsglobalgeometry . bottom ( ) - geom . height ( ) ;
if ( appletsglobalgeometry . height ( ) > = geom . height ( ) ) {
y = qBound ( appletsglobalgeometry . y ( ) , y , bottomy + 1 ) ;
}
} else {
int rightx = appletsglobalgeometry . right ( ) - geom . width ( ) ;
if ( appletsglobalgeometry . width ( ) > = geom . width ( ) ) {
x = qBound ( appletsglobalgeometry . x ( ) , x , rightx + 1 ) ;
}
}
}
2021-04-27 16:58:50 +03:00
QRect repositionedrect ( x , y , geom . width ( ) , geom . height ( ) ) ;
setGeometry ( repositionedrect ) ;
return ;
}
PlasmaQuick : : Dialog : : adjustGeometry ( geom ) ;
}
2021-05-09 20:47:17 +03:00
*/
2021-04-27 16:58:50 +03:00
2020-04-28 13:12:48 +03:00
bool Dialog : : event ( QEvent * e )
{
if ( e - > type ( ) = = QEvent : : Enter ) {
setContainsMouse ( true ) ;
2020-10-25 11:02:52 +02:00
} else if ( e - > type ( ) = = QEvent : : Leave
| | e - > type ( ) = = QEvent : : Hide ) {
2020-04-28 13:12:48 +03:00
setContainsMouse ( false ) ;
}
return PlasmaQuick : : Dialog : : event ( e ) ;
}
}
}