QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 }
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsPanelWidget::setDockMode
virtual void setDockMode(bool dockMode)
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
Definition: qgspanelwidget.cpp:44
QgsPanelWidget::findParentPanel
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
Definition: qgspanelwidget.cpp:49
QgsPanelWidget::openPanel
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
Definition: qgspanelwidget.cpp:79
QgsPanelWidget::connectChildPanel
void connectChildPanel(QgsPanelWidget *panel)
Connect the given sub panel widgets showPanel signals to this current panels main showPanel event to ...
Definition: qgspanelwidget.cpp:38
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QgsPanelWidget::dockMode
bool dockMode()
Returns the dock mode state.
Definition: qgspanelwidget.h:83
QgsPanelWidget::showPanel
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
QgsPanelWidget::QgsPanelWidget
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.cpp:24
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:30
QgsPanelWidgetWrapper::QgsPanelWidgetWrapper
QgsPanelWidgetWrapper(QWidget *widget, QWidget *parent=nullptr)
Wrapper widget for existing widgets which can't have the inheritance tree changed,...
Definition: qgspanelwidget.cpp:124
QgsPanelWidgetWrapper::widget
QWidget * widget()
Returns the internal widget that is wrapped in this panel.
Definition: qgspanelwidget.h:217
QgsPanelWidget::panelAccepted
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
QgsPanelWidget::acceptPanel
void acceptPanel()
Accept the panel.
Definition: qgspanelwidget.cpp:107
QgsPanelWidget::widgetChanged
void widgetChanged()
Emitted when the widget state changes.
QgsPanelWidget::menuButtonMenu
virtual QMenu * menuButtonMenu()
Returns the menu to use for the menu button for this panel, or nullptr if no menu button is required.
Definition: qgspanelwidget.cpp:74
QgsPanelWidget::panelTitle
QString panelTitle()
The title of the panel.
Definition: qgspanelwidget.h:50
QgsPanelWidget::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
Overridden key press event to handle the esc event on the widget.
Definition: qgspanelwidget.cpp:112
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:289
qgssettings.h
qgslogger.h
qgspanelwidget.h
QgsPanelWidget::menuButtonTooltip
virtual QString menuButtonTooltip() const
Returns the (translated) tooltip text to use for the menu button for this panel.
Definition: qgspanelwidget.cpp:69
QgsPanelWidget::connectChildPanels
void connectChildPanels(const QList< QgsPanelWidget * > &panels)
Connect the given sub panel widgets showPanel signals to this current panels main showPanel event to ...
Definition: qgspanelwidget.cpp:29