QGIS API Documentation  3.6.0-Noosa (5873452)
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 <QVBoxLayout>
19 
20 #include "qgssettings.h"
21 #include "qgspanelwidget.h"
22 #include "qgslogger.h"
23 
25  : QWidget( parent )
26 {
27 }
28 
29 void QgsPanelWidget::connectChildPanels( const QList<QgsPanelWidget *> &panels )
30 {
31  Q_FOREACH ( QgsPanelWidget *widget, panels )
32  {
33  connectChildPanel( widget );
34  }
35 }
36 
38 {
39  connect( panel, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
41 }
42 
44 {
45  mDockMode = dockMode;
46 }
47 
49 {
50  QWidget *p = widget;
51  while ( p )
52  {
53  if ( QgsPanelWidget *panel = qobject_cast< QgsPanelWidget * >( p ) )
54  return panel;
55 
56  if ( p->window() == p )
57  {
58  // break on encountering a window - e.g., a dialog opened from a panel should not inline
59  // widgets inside the parent panel
60  return nullptr;
61  }
62 
63  p = p->parentWidget();
64  }
65  return nullptr;
66 }
67 
69 {
70  //panel dock mode inherits from this panel
71  panel->setDockMode( dockMode() );
72 
73  if ( mDockMode )
74  {
75  emit showPanel( panel );
76  }
77  else
78  {
79  // Show the dialog version if no one is connected
80  QDialog *dlg = new QDialog();
81  QString key = QStringLiteral( "/UI/paneldialog/%1" ).arg( panel->panelTitle() );
82  QgsSettings settings;
83  dlg->restoreGeometry( settings.value( key ).toByteArray() );
84  dlg->setWindowTitle( panel->panelTitle() );
85  dlg->setLayout( new QVBoxLayout() );
86  dlg->layout()->addWidget( panel );
87  QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok );
88  connect( buttonBox, &QDialogButtonBox::accepted, dlg, &QDialog::accept );
89  dlg->layout()->addWidget( buttonBox );
90  dlg->exec();
91  settings.setValue( key, dlg->saveGeometry() );
92  panel->acceptPanel();
93  }
94 }
95 
97 {
98  emit panelAccepted( this );
99 }
100 
101 void QgsPanelWidget::keyPressEvent( QKeyEvent *event )
102 {
103  if ( event->key() == Qt::Key_Escape )
104  {
105  acceptPanel();
106  }
107 }
108 
109 QgsPanelWidgetWrapper::QgsPanelWidgetWrapper( QWidget *widget, QWidget *parent )
110  : QgsPanelWidget( parent )
111  , mWidget( widget )
112 {
113  this->setLayout( new QVBoxLayout() );
114  this->layout()->setContentsMargins( 0, 0, 0, 0 );
115  this->layout()->addWidget( widget );
116 }
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
void connectChildPanel(QgsPanelWidget *panel)
Connect the given sub panel widgets showPanel signals to this current panels main showPanel event to ...
bool dockMode()
Returns the dock mode state.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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 showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
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 widgetChanged()
Emitted when the widget state changes.
void connectChildPanels(const QList< QgsPanelWidget *> &panels)
Connect the given sub panel widgets showPanel signals to this current panels main showPanel event to ...
void keyPressEvent(QKeyEvent *event) override
Overridden key press event to handle the esc event on the widget.
QgsPanelWidgetWrapper(QWidget *widget, QWidget *parent=nullptr)
Wrapper widget for existing widgets which can&#39;t have the inheritance tree changed, e.g dialogs.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as a inline panel.