QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  const auto constPanels = panels;
32  for ( QgsPanelWidget *widget : constPanels )
33  {
34  connectChildPanel( widget );
35  }
36 }
37 
39 {
40  connect( panel, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
42 }
43 
45 {
46  mDockMode = dockMode;
47 }
48 
50 {
51  QWidget *p = widget;
52  while ( p )
53  {
54  if ( QgsPanelWidget *panel = qobject_cast< QgsPanelWidget * >( p ) )
55  return panel;
56 
57  if ( p->window() == p )
58  {
59  // break on encountering a window - e.g., a dialog opened from a panel should not inline
60  // widgets inside the parent panel
61  return nullptr;
62  }
63 
64  p = p->parentWidget();
65  }
66  return nullptr;
67 }
68 
70 {
71  //panel dock mode inherits from this panel
72  panel->setDockMode( dockMode() );
73 
74  if ( mDockMode )
75  {
76  emit showPanel( panel );
77  }
78  else
79  {
80  // Show the dialog version if no one is connected
81  QDialog *dlg = new QDialog();
82  QString key = QStringLiteral( "/UI/paneldialog/%1" ).arg( panel->panelTitle() );
83  QgsSettings settings;
84  dlg->restoreGeometry( settings.value( key ).toByteArray() );
85  dlg->setWindowTitle( panel->panelTitle() );
86  dlg->setLayout( new QVBoxLayout() );
87  dlg->layout()->addWidget( panel );
88  QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok );
89  connect( buttonBox, &QDialogButtonBox::accepted, dlg, &QDialog::accept );
90  dlg->layout()->addWidget( buttonBox );
91  dlg->exec();
92  settings.setValue( key, dlg->saveGeometry() );
93  panel->acceptPanel();
94  }
95 }
96 
98 {
99  emit panelAccepted( this );
100 }
101 
102 void QgsPanelWidget::keyPressEvent( QKeyEvent *event )
103 {
104  if ( event->key() == Qt::Key_Escape )
105  {
106  acceptPanel();
107  }
108 }
109 
110 QgsPanelWidgetWrapper::QgsPanelWidgetWrapper( QWidget *widget, QWidget *parent )
111  : QgsPanelWidget( parent )
112  , mWidget( widget )
113 {
114  this->setLayout( new QVBoxLayout() );
115  this->layout()->setContentsMargins( 0, 0, 0, 0 );
116  this->layout()->addWidget( widget );
117 }
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.