QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 : denis.rouzaud@gmail.com
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" ), 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  QPalette pal = palette();
32  pal.setColor( QPalette::Background, QColor( 231, 245, 254 ) );
33  setPalette( pal );
34  setAutoFillBackground( true );
35 
36  connect( this, SIGNAL( dockLocationChanged( Qt::DockWidgetArea ) ), this, SLOT( areaChanged( Qt::DockWidgetArea ) ) );
37  connect( this, SIGNAL( topLevelChanged( bool ) ), this, SLOT( floatingChanged( bool ) ) );
38  hide();
39 }
40 
42 {
43 }
44 
46 {
47  QFrame* line = 0;
48  if ( mWidgetList.count() > 0 )
49  {
50  line = new QFrame( this );
51  line->setFrameShadow( QFrame::Sunken );
52  line->setFrameShape( mLayoutHorizontal ? QFrame::VLine : QFrame::HLine );
53  mLayout->addWidget( line );
54  }
55  mLayout->addWidget( widget );
56 
57  connect( widget, SIGNAL( destroyed( QObject* ) ), this, SLOT( widgetDestroyed( QObject* ) ) );
58 
59  mWidgetList.insert( widget, line );
60 
61  show();
62  adjustSize();
63 }
64 
65 void QgsUserInputDockWidget::widgetDestroyed( QObject *obj )
66 {
67  if ( obj->isWidgetType() )
68  {
69  QWidget* w = qobject_cast<QWidget*>( obj );
70  QMap<QWidget*, QFrame*>::iterator i = mWidgetList.find( w );
71  while ( i != mWidgetList.end() )
72  {
73  if ( i.value() )
74  {
75  i.value()->deleteLater();
76  }
77  mWidgetList.remove( i.key() );
78  ++i;
79  }
80  }
81 }
82 
83 void QgsUserInputDockWidget::areaChanged( Qt::DockWidgetArea area )
84 {
85  bool newLayoutHorizontal = area & Qt::BottomDockWidgetArea || area & Qt::TopDockWidgetArea;
86  if ( mLayoutHorizontal == newLayoutHorizontal )
87  {
88  // no change
89  adjustSize();
90  return;
91  }
92  mLayoutHorizontal = newLayoutHorizontal;
93  updateLayoutDirection();
94 }
95 
96 void QgsUserInputDockWidget::floatingChanged( bool floating )
97 {
98  if ( mLayoutHorizontal == floating )
99  {
100  adjustSize();
101  return;
102  }
103  mLayoutHorizontal = floating;
104  updateLayoutDirection();
105 }
106 
107 void QgsUserInputDockWidget::updateLayoutDirection()
108 {
109  mLayout->setDirection( mLayoutHorizontal ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom );
110 
111  QMap<QWidget*, QFrame*>::iterator i = mWidgetList.begin();
112  while ( i != mWidgetList.end() )
113  {
114  if ( i.value() )
115  {
116  i.value()->setFrameShape( mLayoutHorizontal ? QFrame::VLine : QFrame::HLine );
117  }
118  ++i;
119  }
120 
121  adjustSize();
122 }
123 
125 {
126  if ( mWidgetList.count() == 0 )
127  {
128  hide();
129  }
130  else
131  {
132  QDockWidget::paintEvent( event );
133  }
134 }
const QPalette & palette() const
void topLevelChanged(bool topLevel)
void setColor(ColorGroup group, ColorRole role, const QColor &color)
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 setAutoFillBackground(bool enabled)
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)