/
qgsuserinputwidget.cpp
122 lines (104 loc) · 3.54 KB
/
qgsuserinputwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/***************************************************************************
qgsuserinputwidget.h
--------------------------------------
Date : 04.2015
Copyright : (C) 2015 Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsuserinputwidget.h"
#include <QFrame>
QgsUserInputWidget::QgsUserInputWidget( QWidget *parent )
: QgsFloatingWidget( parent ? parent->window() : nullptr )
{
//TODO add title tr( "User Input Panel" )
QFrame *f = new QFrame();
f->setObjectName( QStringLiteral( "mUserInputContainer" ) );
QPalette pal = palette();
pal.setBrush( backgroundRole(), pal.window() );
f->setPalette( pal );
f->setAutoFillBackground( true );
f->setFrameShape( QFrame::StyledPanel );
f->setFrameShadow( QFrame::Plain );
mLayout = new QBoxLayout( QBoxLayout::TopToBottom );
mLayout->setAlignment( Qt::AlignRight | Qt::AlignTop );
f->setLayout( mLayout );
QBoxLayout *topLayout = new QBoxLayout( QBoxLayout::TopToBottom );
topLayout->setContentsMargins( 0, 0, 0, 0 );
topLayout->addWidget( f );
setLayout( topLayout );
// this allows the widget to be resized on demand
topLayout->setSizeConstraint( QLayout::SetFixedSize );
mLayout->setSizeConstraint( QLayout::SetFixedSize );
setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
hide();
}
void QgsUserInputWidget::addUserInputWidget( QWidget *widget )
{
QFrame *line = nullptr;
if ( mWidgetList.count() > 0 )
{
line = new QFrame( this );
line->setFrameShadow( QFrame::Sunken );
line->setFrameShape( mLayoutHorizontal ? QFrame::VLine : QFrame::HLine );
mLayout->addWidget( line );
}
mLayout->addWidget( widget );
connect( widget, &QObject::destroyed, this, &QgsUserInputWidget::widgetDestroyed );
mWidgetList.insert( widget, line );
show();
raise();
adjustSize();
}
void QgsUserInputWidget::widgetDestroyed( QObject *obj )
{
if ( obj->isWidgetType() )
{
QWidget *w = qobject_cast<QWidget *>( obj );
QMap<QWidget *, QFrame *>::iterator i = mWidgetList.find( w );
while ( i != mWidgetList.end() )
{
if ( auto *lValue = i.value() )
{
lValue->deleteLater();
}
i = mWidgetList.erase( i );
}
}
if ( mWidgetList.count() == 0 )
{
hide();
}
}
void QgsUserInputWidget::setLayoutDirection( QBoxLayout::Direction direction )
{
mLayout->setDirection( direction );
bool horizontal = direction == QBoxLayout::LeftToRight || direction == QBoxLayout::RightToLeft;
QMap<QWidget *, QFrame *>::const_iterator i = mWidgetList.constBegin();
while ( i != mWidgetList.constEnd() )
{
if ( auto *lValue = i.value() )
{
lValue->setFrameShape( horizontal ? QFrame::VLine : QFrame::HLine );
}
++i;
}
adjustSize();
}
void QgsUserInputWidget::paintEvent( QPaintEvent *event )
{
if ( mWidgetList.count() == 0 )
{
hide();
}
else
{
QgsFloatingWidget::paintEvent( event );
}
}