QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgspanelwidgetstack.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 
20 #include "qgslogger.h"
21 
22 #include "qgspanelwidgetstack.h"
23 
24 #include "qgspanelwidget.h"
25 
27  : QWidget( parent )
28 {
29  setupUi( this );
30  clear();
31 
32  connect( mBackButton, &QAbstractButton::pressed, this, &QgsPanelWidgetStack::acceptCurrentPanel );
33 }
34 
36 {
37  // TODO Don't allow adding another main widget or else that would be strange for the user.
39  // using unique connection because addMainPanel() may be called multiple times
40  // for a panel, so showPanel() slot could be invoked more times from one signal
41  Qt::UniqueConnection );
42  mStackedWidget->insertWidget( 0, panel );
43  mStackedWidget->setCurrentIndex( 0 );
44 }
45 
47 {
48  return qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( 0 ) );
49 }
50 
52 {
53  // clear out the current stack
55 
56  QWidget *widget = mStackedWidget->widget( 0 );
57  mStackedWidget->removeWidget( widget );
58  return qobject_cast<QgsPanelWidget *>( widget );
59 }
60 
62 {
63  for ( int i = mStackedWidget->count() - 1; i >= 0; i-- )
64  {
65  if ( QgsPanelWidget *panelWidget = qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( i ) ) )
66  {
67  mStackedWidget->removeWidget( panelWidget );
68  if ( panelWidget->autoDelete() )
69  {
70  panelWidget->deleteLater();
71  }
72  }
73  else if ( QWidget *widget = mStackedWidget->widget( i ) )
74  {
75  mStackedWidget->removeWidget( widget );
76  widget->deleteLater();
77  }
78  }
79  mTitles.clear();
80  mTitleText->hide();
81  mBackButton->hide();
82  this->updateBreadcrumb();
83 }
84 
86 {
87  return qobject_cast<QgsPanelWidget *>( mStackedWidget->currentWidget() );
88 }
89 
91 {
92  // You can't accept the main panel.
93  if ( mStackedWidget->currentIndex() <= 0 )
94  return;
95 
96  QgsPanelWidget *widget = currentPanel();
97  widget->acceptPanel();
98 }
99 
101 {
102  //avoid messy multiple redraws
103  setUpdatesEnabled( false );
104  mStackedWidget->setUpdatesEnabled( false );
105 
106  for ( int i = mStackedWidget->count() - 1; i > 0; --i )
107  {
108  if ( QgsPanelWidget *panelWidget = qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( i ) ) )
109  {
110  panelWidget->acceptPanel();
111  }
112  }
113  setUpdatesEnabled( true );
114  mStackedWidget->setUpdatesEnabled( true );
115 }
116 
118 {
119  mTitles.push( panel->panelTitle() );
120 
123 
124  int index = mStackedWidget->addWidget( panel );
125  mStackedWidget->setCurrentIndex( index );
126  mBackButton->show();
127  mTitleText->show();
128 
129  this->updateBreadcrumb();
130 }
131 
133 {
134  mTitles.pop();
135  mStackedWidget->setCurrentIndex( mStackedWidget->currentIndex() - 1 );
136  mStackedWidget->removeWidget( panel );
137  if ( panel->autoDelete() )
138  {
139  panel->deleteLater();
140  }
141 
142  if ( mStackedWidget->currentIndex() == 0 )
143  {
144  mBackButton->hide();
145  mTitleText->hide();
146  }
147  this->updateBreadcrumb();
148 }
149 
151 {
152  if ( e->button() == Qt::BackButton )
153  {
155  }
156 }
157 
159 {
160  if ( e->key() == Qt::Key_Escape )
161  {
163  }
164 }
165 
166 void QgsPanelWidgetStack::updateBreadcrumb()
167 {
168  QString breadcrumb;
169  const auto constMTitles = mTitles;
170  for ( QString title : constMTitles )
171  {
172  breadcrumb += QStringLiteral( " %1 >" ).arg( title );
173  }
174  // Remove the last
175  breadcrumb.chop( 1 );
176  mTitleText->setText( breadcrumb );
177 }
QgsPanelWidgetStack(QWidget *parent=nullptr)
A stack widget to manage panels in the interface.
void acceptCurrentPanel()
Accept the current active widget in the stack.
bool autoDelete()
The the auto delete property on the widget.
QgsPanelWidget * takeMainPanel() SIP_TRANSFERBACK
Removes the main panel widget from the stack and transfers ownsership to the caller.
void acceptAllPanels()
Accepts all panel widgets open in the stack in turn until until only the mainPanel() remains...
Base class for any widget that can be shown as a inline panel.
void mouseReleaseEvent(QMouseEvent *e) override
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
void closePanel(QgsPanelWidget *panel)
Closes the panel in the widget.
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
void setMainPanel(QgsPanelWidget *panel SIP_TRANSFER)
Sets the main panel widget for the stack and selects it for the user.
void acceptPanel()
Accept the panel.
QgsPanelWidget * currentPanel()
Returns the panel currently shown in the stack.
QString panelTitle()
The title of the panel.
void showPanel(QgsPanelWidget *panel)
Show a panel in the stack widget.
void clear()
Clear the stack of all widgets.
void keyPressEvent(QKeyEvent *e) override
QgsPanelWidget * mainPanel()
The main panel widget that is set in the stack.