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