QGIS API Documentation  2.12.0-Lyon
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  : QDockWidget( tr( "User Input Panel" ), parent )
23  , mLayoutHorizontal( true )
24 {
25  QWidget* w = new QWidget( 0 );
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 = 0;
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  mWidgetList.remove( i.key() );
73  ++i;
74  }
75  }
76 }
77 
78 void QgsUserInputDockWidget::areaChanged( Qt::DockWidgetArea area )
79 {
80  bool newLayoutHorizontal = area & Qt::BottomDockWidgetArea || area & Qt::TopDockWidgetArea;
81  if ( mLayoutHorizontal == newLayoutHorizontal )
82  {
83  // no change
84  adjustSize();
85  return;
86  }
87  mLayoutHorizontal = newLayoutHorizontal;
88  updateLayoutDirection();
89 }
90 
91 void QgsUserInputDockWidget::floatingChanged( bool floating )
92 {
93  if ( mLayoutHorizontal == floating )
94  {
95  adjustSize();
96  return;
97  }
98  mLayoutHorizontal = floating;
99  updateLayoutDirection();
100 }
101 
102 void QgsUserInputDockWidget::updateLayoutDirection()
103 {
104  mLayout->setDirection( mLayoutHorizontal ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom );
105 
106  QMap<QWidget*, QFrame*>::iterator i = mWidgetList.begin();
107  while ( i != mWidgetList.end() )
108  {
109  if ( i.value() )
110  {
111  i.value()->setFrameShape( mLayoutHorizontal ? QFrame::VLine : QFrame::HLine );
112  }
113  ++i;
114  }
115 
116  adjustSize();
117 }
118 
120 {
121  if ( mWidgetList.count() == 0 )
122  {
123  hide();
124  }
125  else
126  {
127  QDockWidget::paintEvent( event );
128  }
129 }
void topLevelChanged(bool topLevel)
void setFrameShape(Shape)
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)
void hide()
iterator end()
iterator begin()
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)
QgsUserInputDockWidget(QWidget *parent=0)
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
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)
int count(const Key &key) const
iterator find(const Key &key)
void destroyed(QObject *obj)
const T value(const Key &key) const
int remove(const Key &key)
#define tr(sourceText)