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