QGIS API Documentation  3.15.0-Master (61a1801e3d)
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::clicked, this, &QgsPanelWidgetStack::acceptCurrentPanel );
33 
34  mMenuButton->setStyleSheet( QStringLiteral( "QToolButton::menu-indicator { image: none; }" ) );
35 }
36 
38 {
39  // TODO Don't allow adding another main widget or else that would be strange for the user.
41  // using unique connection because addMainPanel() may be called multiple times
42  // for a panel, so showPanel() slot could be invoked more times from one signal
43  Qt::UniqueConnection );
44  mStackedWidget->insertWidget( 0, panel );
45  mStackedWidget->setCurrentIndex( 0 );
46 }
47 
49 {
50  return qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( 0 ) );
51 }
52 
54 {
55  // clear out the current stack
57 
58  QWidget *widget = mStackedWidget->widget( 0 );
59  mStackedWidget->removeWidget( widget );
60  return qobject_cast<QgsPanelWidget *>( widget );
61 }
62 
64 {
65  for ( int i = mStackedWidget->count() - 1; i >= 0; i-- )
66  {
67  if ( QgsPanelWidget *panelWidget = qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( i ) ) )
68  {
69  mStackedWidget->removeWidget( panelWidget );
70  if ( panelWidget->autoDelete() )
71  {
72  panelWidget->deleteLater();
73  }
74  }
75  else if ( QWidget *widget = mStackedWidget->widget( i ) )
76  {
77  mStackedWidget->removeWidget( widget );
78  widget->deleteLater();
79  }
80  }
81  mTitles.clear();
82  mTitleText->hide();
83  mBackButton->hide();
84  mMenuButton->hide();
85  this->updateBreadcrumb();
86 }
87 
89 {
90  return qobject_cast<QgsPanelWidget *>( mStackedWidget->currentWidget() );
91 }
92 
94 {
95  // You can't accept the main panel.
96  if ( mStackedWidget->currentIndex() <= 0 )
97  return;
98 
99  QgsPanelWidget *widget = currentPanel();
100  widget->acceptPanel();
101 }
102 
104 {
105  //avoid messy multiple redraws
106  setUpdatesEnabled( false );
107  mStackedWidget->setUpdatesEnabled( false );
108 
109  for ( int i = mStackedWidget->count() - 1; i > 0; --i )
110  {
111  if ( QgsPanelWidget *panelWidget = qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( i ) ) )
112  {
113  panelWidget->acceptPanel();
114  }
115  }
116  setUpdatesEnabled( true );
117  mStackedWidget->setUpdatesEnabled( true );
118 }
119 
121 {
122  mTitles.push( panel->panelTitle() );
123 
126 
127  int index = mStackedWidget->addWidget( panel );
128  mStackedWidget->setCurrentIndex( index );
129  mBackButton->show();
130  mTitleText->show();
131 
132  updateMenuButton();
133  updateBreadcrumb();
134 }
135 
137 {
138  mTitles.pop();
139  mStackedWidget->setCurrentIndex( mStackedWidget->currentIndex() - 1 );
140  mStackedWidget->removeWidget( panel );
141  if ( panel->autoDelete() )
142  {
143  panel->deleteLater();
144  }
145 
146  if ( mStackedWidget->currentIndex() == 0 )
147  {
148  mBackButton->hide();
149  mTitleText->hide();
150  mMenuButton->hide();
151  }
152  else
153  {
154  updateMenuButton();
155  }
156  this->updateBreadcrumb();
157 }
158 
160 {
161  if ( e->button() == Qt::BackButton )
162  {
164  }
165 }
166 
168 {
169  if ( e->key() == Qt::Key_Escape )
170  {
172  }
173 }
174 
175 void QgsPanelWidgetStack::updateBreadcrumb()
176 {
177  QString breadcrumb;
178  const auto constMTitles = mTitles;
179  for ( QString title : constMTitles )
180  {
181  breadcrumb += QStringLiteral( " %1 >" ).arg( title );
182  }
183  // Remove the last
184  breadcrumb.chop( 1 );
185  mTitleText->setText( breadcrumb );
186 }
187 
188 void QgsPanelWidgetStack::updateMenuButton()
189 {
190  if ( QMenu *menu = currentPanel()->menuButtonMenu() )
191  {
192  mMenuButton->setVisible( true );
193  mMenuButton->setToolTip( currentPanel()->menuButtonTooltip() );
194  mMenuButton->setMenu( menu );
195  }
196  else
197  {
198  mMenuButton->setVisible( false );
199  }
200 }
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.