QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgspanelwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspanelwidget.cpp
3  ---------------------
4  begin : June 2016
5  copyright : (C) 2016 by Nathan Woodrow
6  email :
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 #include <QDialogButtonBox>
16 #include <QPushButton>
17 #include <QDialog>
18 #include <QSettings>
19 #include <QVBoxLayout>
20 
21 #include "qgspanelwidget.h"
22 #include "qgslogger.h"
23 
25  : QWidget( parent )
26  , mAutoDelete( true )
27  , mDockMode( false )
28 {
29 }
30 
32 {
33  Q_FOREACH ( QgsPanelWidget* widget, panels )
34  {
35  connectChildPanel( widget );
36  }
37 }
38 
40 {
41  connect( panel, SIGNAL( showPanel( QgsPanelWidget* ) ), this, SLOT( openPanel( QgsPanelWidget* ) ) );
42  connect( panel, SIGNAL( widgetChanged() ), this, SIGNAL( widgetChanged() ) );
43 }
44 
46 {
47  mDockMode = dockMode;
48 }
49 
51 {
52  QWidget* p = widget;
53  while ( p )
54  {
55  if ( QgsPanelWidget* panel = qobject_cast< QgsPanelWidget* >( p ) )
56  return panel;
57 
58  if ( p->window() == p )
59  {
60  // break on encountering a window - eg a dialog opened from a panel should not inline
61  // widgets inside the parent panel
62  return nullptr;
63  }
64 
65  p = p->parentWidget();
66  }
67  return nullptr;
68 }
69 
71 {
72  //panel dock mode inherits from this panel
73  panel->setDockMode( dockMode() );
74 
75  if ( mDockMode )
76  {
77  emit showPanel( panel );
78  }
79  else
80  {
81  // Show the dialog version if no one is connected
82  QDialog* dlg = new QDialog();
83  QString key = QString( "/UI/paneldialog/%1" ).arg( panel->panelTitle() );
84  QSettings settings;
85  dlg->restoreGeometry( settings.value( key ).toByteArray() );
86  dlg->setWindowTitle( panel->panelTitle() );
87  dlg->setLayout( new QVBoxLayout() );
88  dlg->layout()->addWidget( panel );
89  QDialogButtonBox* buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok );
90  connect( buttonBox, SIGNAL( accepted() ), dlg, SLOT( accept() ) );
91  dlg->layout()->addWidget( buttonBox );
92  dlg->exec();
93  settings.setValue( key, dlg->saveGeometry() );
94  panel->acceptPanel();
95  }
96 }
97 
99 {
100  emit panelAccepted( this );
101 }
102 
104 {
105  if ( event->key() == Qt::Key_Escape )
106  {
107  acceptPanel();
108  }
109 }
110 
112  : QgsPanelWidget( parent )
113  , mWidget( widget )
114 {
115  this->setLayout( new QVBoxLayout() );
116  this->layout()->setContentsMargins( 0, 0, 0, 0 );
117  this->layout()->addWidget( widget );
118 }
QLayout * layout() const
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
QByteArray toByteArray() const
void connectChildPanel(QgsPanelWidget *panel)
Connect the given sub panel widgets showPanel signals to this current panels main showPanel event to ...
void setContentsMargins(int left, int top, int right, int bottom)
bool dockMode()
Return the dock mode state.
QWidget * window() const
int exec()
Base class for any widget that can be shown as a inline panel.
QgsPanelWidget(QWidget *parent=0)
Base class for any widget that can be shown as a inline panel.
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
virtual void setDockMode(bool dockMode)
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs...
void setValue(const QString &key, const QVariant &value)
void connectChildPanels(QList< QgsPanelWidget *> panels)
Connect the given sub panel widgets showPanel signals to this current panels main showPanel event to ...
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
void keyPressEvent(QKeyEvent *event)
Overridden key press event to handle the esc event on the widget.
void setLayout(QLayout *layout)
bool restoreGeometry(const QByteArray &geometry)
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget...
void acceptPanel()
Accept the panel.
QString panelTitle()
The title of the panel.
void addWidget(QWidget *w)
void widgetChanged()
Emitted when the widget state changes.
int key() const
QgsPanelWidgetWrapper(QWidget *widget, QWidget *parent=nullptr)
Wrapper widget for existing widgets which can&#39;t have the inheritance tree changed, e.g dialogs.
QVariant value(const QString &key, const QVariant &defaultValue) const
QByteArray saveGeometry() const
QWidget * parentWidget() const
void setWindowTitle(const QString &)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
virtual bool event(QEvent *event)