QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsuserinputdockwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsuserinputdockwidget.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 "qgsuserinputdockwidget.h"
17 
18 #include <QFrame>
19 #include <QBoxLayout>
20 
22  : QgsDockWidget( tr( "User Input Panel" ), parent )
23  , mLayoutHorizontal( true )
24 {
25  QWidget* w = new QWidget( nullptr );
26  mLayout = new QBoxLayout( QBoxLayout::LeftToRight );
27  mLayout->setAlignment( Qt::AlignLeft | Qt::AlignTop );
28  w->setLayout( mLayout );
29  setWidget( w );
30 
31  connect( this, SIGNAL( dockLocationChanged( Qt::DockWidgetArea ) ), this, SLOT( areaChanged( Qt::DockWidgetArea ) ) );
32  connect( this, SIGNAL( topLevelChanged( bool ) ), this, SLOT( floatingChanged( bool ) ) );
33  hide();
34 }
35 
37 {
38 }
39 
41 {
42  QFrame* line = nullptr;
43  if ( mWidgetList.count() > 0 )
44  {
45  line = new QFrame( this );
46  line->setFrameShadow( QFrame::Sunken );
47  line->setFrameShape( mLayoutHorizontal ? QFrame::VLine : QFrame::HLine );
48  mLayout->addWidget( line );
49  }
50  mLayout->addWidget( widget );
51 
52  connect( widget, SIGNAL( destroyed( QObject* ) ), this, SLOT( widgetDestroyed( QObject* ) ) );
53 
54  mWidgetList.insert( widget, line );
55 
56  show();
57  adjustSize();
58 }
59 
60 void QgsUserInputDockWidget::widgetDestroyed( QObject *obj )
61 {
62  if ( obj->isWidgetType() )
63  {
64  QWidget* w = qobject_cast<QWidget*>( obj );
65  QMap<QWidget*, QFrame*>::iterator i = mWidgetList.find( w );
66  while ( i != mWidgetList.end() )
67  {
68  if ( i.value() )
69  {
70  i.value()->deleteLater();
71  }
72  i = mWidgetList.erase( i );
73  }
74  }
75 }
76 
77 void QgsUserInputDockWidget::areaChanged( Qt::DockWidgetArea area )
78 {
79  bool newLayoutHorizontal = area & Qt::BottomDockWidgetArea || area & Qt::TopDockWidgetArea;
80  if ( mLayoutHorizontal == newLayoutHorizontal )
81  {
82  // no change
83  adjustSize();
84  return;
85  }
86  mLayoutHorizontal = newLayoutHorizontal;
87  updateLayoutDirection();
88 }
89 
90 void QgsUserInputDockWidget::floatingChanged( bool floating )
91 {
92  if ( mLayoutHorizontal == floating )
93  {
94  adjustSize();
95  return;
96  }
97  mLayoutHorizontal = floating;
98  updateLayoutDirection();
99 }
100 
101 void QgsUserInputDockWidget::updateLayoutDirection()
102 {
103  mLayout->setDirection( mLayoutHorizontal ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom );
104 
106  while ( i != mWidgetList.constEnd() )
107  {
108  if ( i.value() )
109  {
110  i.value()->setFrameShape( mLayoutHorizontal ? QFrame::VLine : QFrame::HLine );
111  }
112  ++i;
113  }
114 
115  adjustSize();
116 }
117 
119 {
120  if ( mWidgetList.count() == 0 )
121  {
122  hide();
123  }
124  else
125  {
126  QgsDockWidget::paintEvent( event );
127  }
128 }
iterator erase(iterator pos)
void topLevelChanged(bool topLevel)
void setFrameShape(Shape)
const_iterator constBegin() const
void addUserInputWidget(QWidget *widget)
Add a widget to be displayed in the dock.
void adjustSize()
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
void setLayout(QLayout *layout)
QgsDockWidget subclass with more fine-grained control over how the widget is closed or opened...
Definition: qgsdockwidget.h:28
const_iterator constEnd() const
QgsUserInputDockWidget(QWidget *parent=nullptr)
const T & value() const
void hide()
iterator end()
virtual void paintEvent(QPaintEvent *event)
void setFrameShadow(Shadow)
void setWidget(QWidget *widget)
bool setAlignment(QWidget *w, QFlags< Qt::AlignmentFlag > alignment)
void dockLocationChanged(Qt::DockWidgetArea area)
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
virtual bool event(QEvent *event)
void setDirection(Direction direction)
iterator insert(const Key &key, const T &value)
void show()
void paintEvent(QPaintEvent *event) override
will not display the dock if it contains no widget
bool isWidgetType() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QWidget * widget() const
int count(const Key &key) const
iterator find(const Key &key)
void destroyed(QObject *obj)