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