QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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  const auto it = mWidgetList.find( w );
79  if ( it != mWidgetList.end() )
80  {
81  if ( QFrame *frame = it.value() )
82  {
83  frame->deleteLater();
84  }
85  mWidgetList.erase( it );
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  const 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 }
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.