QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
44 void QgsPanelWidget::setDockMode( bool dockMode )
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  return QString();
72 }
73 
75 {
76  return nullptr;
77 }
78 
80 {
81  //panel dock mode inherits from this panel
82  panel->setDockMode( dockMode() );
83 
84  if ( mDockMode )
85  {
86  emit showPanel( panel );
87  }
88  else
89  {
90  // Show the dialog version if no one is connected
91  QDialog *dlg = new QDialog();
92  QString key = QStringLiteral( "/UI/paneldialog/%1" ).arg( panel->panelTitle() );
93  QgsSettings settings;
94  dlg->restoreGeometry( settings.value( key ).toByteArray() );
95  dlg->setWindowTitle( panel->panelTitle() );
96  dlg->setLayout( new QVBoxLayout() );
97  dlg->layout()->addWidget( panel );
98  QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok );
99  connect( buttonBox, &QDialogButtonBox::accepted, dlg, &QDialog::accept );
100  dlg->layout()->addWidget( buttonBox );
101  dlg->exec();
102  settings.setValue( key, dlg->saveGeometry() );
103  panel->acceptPanel();
104  }
105 }
106 
108 {
109  emit panelAccepted( this );
110 }
111 
112 void QgsPanelWidget::keyPressEvent( QKeyEvent *event )
113 {
114  if ( event->key() == Qt::Key_Escape )
115  {
116  acceptPanel();
117  }
118  else
119  {
120  QWidget::keyPressEvent( event );
121  }
122 }
123 
124 QgsPanelWidgetWrapper::QgsPanelWidgetWrapper( QWidget *widget, QWidget *parent )
125  : QgsPanelWidget( parent )
126  , mWidget( widget )
127 {
128  this->setLayout( new QVBoxLayout() );
129  this->layout()->setContentsMargins( 0, 0, 0, 0 );
130  this->layout()->addWidget( widget );
131 }
QWidget * widget()
Returns the internal widget that is wrapped in this panel.
QgsPanelWidgetWrapper(QWidget *widget, QWidget *parent=nullptr)
Wrapper widget for existing widgets which can't have the inheritance tree changed,...
Base class for any widget that can be shown as a inline panel.
void connectChildPanels(const 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 openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
virtual QMenu * menuButtonMenu()
Returns the menu to use for the menu button for this panel, or nullptr if no menu button is required.
QString panelTitle()
The title of the panel.
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
virtual QString menuButtonTooltip() const
Returns the (translated) tooltip text to use for the menu button for this panel.
void connectChildPanel(QgsPanelWidget *panel)
Connect the given sub panel widgets showPanel signals to this current panels main showPanel event to ...
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as a inline panel.
void widgetChanged()
Emitted when the widget state changes.
void acceptPanel()
Accept the panel.
void keyPressEvent(QKeyEvent *event) override
Overridden key press event to handle the esc event on the widget.
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
virtual void setDockMode(bool dockMode)
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
bool dockMode()
Returns the dock mode state.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.