QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
6 Email : denis.rouzaud@gmail.com
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#include "moc_qgsuserinputwidget.cpp"
18
19#include <QFrame>
20
22 : QgsFloatingWidget( parent ? parent->window() : nullptr )
23{
24 //TODO add title tr( "User Input Panel" )
25
26 QFrame *f = new QFrame();
27 f->setObjectName( QStringLiteral( "mUserInputContainer" ) );
28
29 QPalette pal = palette();
30 pal.setBrush( backgroundRole(), pal.window() );
31 f->setPalette( pal );
32 f->setAutoFillBackground( true );
33 f->setFrameShape( QFrame::StyledPanel );
34 f->setFrameShadow( QFrame::Plain );
35
36 mLayout = new QBoxLayout( QBoxLayout::TopToBottom );
37 mLayout->setAlignment( Qt::AlignRight | Qt::AlignTop );
38 f->setLayout( mLayout );
39
40 QBoxLayout *topLayout = new QBoxLayout( QBoxLayout::TopToBottom );
41 topLayout->setContentsMargins( 0, 0, 0, 0 );
42 topLayout->addWidget( f );
43 setLayout( topLayout );
44
45 // this allows the widget to be resized on demand
46 topLayout->setSizeConstraint( QLayout::SetFixedSize );
47 mLayout->setSizeConstraint( QLayout::SetFixedSize );
48
49 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
50 hide();
51}
52
54{
55 QFrame *line = nullptr;
56 if ( mWidgetList.count() > 0 )
57 {
58 line = new QFrame( this );
59 line->setFrameShadow( QFrame::Sunken );
60 line->setFrameShape( mLayoutHorizontal ? QFrame::VLine : QFrame::HLine );
61 mLayout->addWidget( line );
62 }
63 mLayout->addWidget( widget );
64
65 connect( widget, &QObject::destroyed, this, &QgsUserInputWidget::widgetDestroyed );
66
67 mWidgetList.insert( widget, line );
68
69 show();
70 raise();
71 adjustSize();
72}
73
74void QgsUserInputWidget::widgetDestroyed( QObject *obj )
75{
76 if ( obj->isWidgetType() )
77 {
78 QWidget *w = qobject_cast<QWidget *>( obj );
79 const auto it = mWidgetList.find( w );
80 if ( it != mWidgetList.end() )
81 {
82 if ( QFrame *frame = it.value() )
83 {
84 frame->deleteLater();
85 }
86 mWidgetList.erase( it );
87 }
88 }
89 if ( mWidgetList.count() == 0 )
90 {
91 hide();
92 }
93}
94
95void QgsUserInputWidget::setLayoutDirection( QBoxLayout::Direction direction )
96{
97 mLayout->setDirection( direction );
98
99 const bool horizontal = direction == QBoxLayout::LeftToRight || direction == QBoxLayout::RightToLeft;
100 QMap<QWidget *, QFrame *>::const_iterator i = mWidgetList.constBegin();
101 while ( i != mWidgetList.constEnd() )
102 {
103 if ( auto *lValue = i.value() )
104 {
105 lValue->setFrameShape( horizontal ? QFrame::VLine : QFrame::HLine );
106 }
107 ++i;
108 }
109
110 adjustSize();
111}
112
113void QgsUserInputWidget::paintEvent( QPaintEvent *event )
114{
115 if ( mWidgetList.count() == 0 )
116 {
117 hide();
118 }
119 else
120 {
122 }
123}
A QWidget subclass for creating widgets which float outside of the normal Qt layout system.
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.