QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  updateMenuButton();
47 }
48 
50 {
51  return qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( 0 ) );
52 }
53 
55 {
56  // clear out the current stack
58 
59  QWidget *widget = mStackedWidget->widget( 0 );
60  if ( widget )
61  {
62  mStackedWidget->removeWidget( widget );
63  return qobject_cast<QgsPanelWidget *>( widget );
64  }
65  else
66  {
67  return nullptr;
68  }
69 }
70 
72 {
73  for ( int i = mStackedWidget->count() - 1; i >= 0; i-- )
74  {
75  if ( QgsPanelWidget *panelWidget = qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( i ) ) )
76  {
77  mStackedWidget->removeWidget( panelWidget );
78  if ( panelWidget->autoDelete() )
79  {
80  panelWidget->deleteLater();
81  }
82  }
83  else if ( QWidget *widget = mStackedWidget->widget( i ) )
84  {
85  mStackedWidget->removeWidget( widget );
86  widget->deleteLater();
87  }
88  }
89  mTitles.clear();
90  mTitleText->hide();
91  mBackButton->hide();
92  mMenuButton->hide();
93  this->updateBreadcrumb();
94 }
95 
97 {
98  return qobject_cast<QgsPanelWidget *>( mStackedWidget->currentWidget() );
99 }
100 
102 {
103  if ( const QgsPanelWidget *widget = qobject_cast<const QgsPanelWidget *>( mStackedWidget->currentWidget() ) )
104  {
105  if ( widget->applySizeConstraintsToStack() )
106  return widget->sizeHint();
107  }
108  return QWidget::sizeHint();
109 }
110 
112 {
113  if ( const QgsPanelWidget *widget = qobject_cast<const QgsPanelWidget *>( mStackedWidget->currentWidget() ) )
114  {
115  if ( widget->applySizeConstraintsToStack() )
116  return widget->minimumSizeHint();
117  }
118 
119  return QWidget::minimumSizeHint();
120 }
121 
123 {
124  // You can't accept the main panel.
125  if ( mStackedWidget->currentIndex() <= 0 )
126  return;
127 
128  QgsPanelWidget *widget = currentPanel();
129  widget->acceptPanel();
130 }
131 
133 {
134  //avoid messy multiple redraws
135  setUpdatesEnabled( false );
136  mStackedWidget->setUpdatesEnabled( false );
137 
138  for ( int i = mStackedWidget->count() - 1; i > 0; --i )
139  {
140  if ( QgsPanelWidget *panelWidget = qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( i ) ) )
141  {
142  panelWidget->acceptPanel();
143  }
144  }
145  setUpdatesEnabled( true );
146  mStackedWidget->setUpdatesEnabled( true );
147 }
148 
150 {
151  mTitles.push( panel->panelTitle() );
152 
155 
156  const int index = mStackedWidget->addWidget( panel );
157  mStackedWidget->setCurrentIndex( index );
158  mBackButton->show();
159  mTitleText->show();
160 
161  updateMenuButton();
162  updateBreadcrumb();
163 }
164 
166 {
167  mTitles.pop();
168  mStackedWidget->setCurrentIndex( mStackedWidget->currentIndex() - 1 );
169  mStackedWidget->removeWidget( panel );
170  if ( panel->autoDelete() )
171  {
172  panel->deleteLater();
173  }
174 
175  if ( mStackedWidget->currentIndex() == 0 )
176  {
177  mBackButton->hide();
178  mTitleText->hide();
179  mMenuButton->hide();
180  }
181  else
182  {
183  updateMenuButton();
184  }
185  this->updateBreadcrumb();
186 }
187 
189 {
190  if ( e->button() == Qt::BackButton )
191  {
193  }
194 }
195 
197 {
198  if ( e->key() == Qt::Key_Escape )
199  {
201  }
202 }
203 
204 void QgsPanelWidgetStack::updateBreadcrumb()
205 {
206  QString breadcrumb;
207  const auto constMTitles = mTitles;
208  for ( const QString &title : constMTitles )
209  {
210  breadcrumb += QStringLiteral( " %1 >" ).arg( title );
211  }
212  // Remove the last
213  breadcrumb.chop( 1 );
214  mTitleText->setText( breadcrumb );
215 }
216 
217 void QgsPanelWidgetStack::updateMenuButton()
218 {
219  if ( QMenu *menu = currentPanel()->menuButtonMenu() )
220  {
221  mMenuButton->setVisible( true );
222  mMenuButton->setToolTip( currentPanel()->menuButtonTooltip() );
223  mMenuButton->setMenu( menu );
224  }
225  else
226  {
227  mMenuButton->setVisible( false );
228  }
229 }
QgsPanelWidgetStack::sizeHint
QSize sizeHint() const override
Definition: qgspanelwidgetstack.cpp:101
QgsPanelWidget::autoDelete
bool autoDelete()
The the auto delete property on the widget.
Definition: qgspanelwidget.h:109
QgsPanelWidgetStack::acceptAllPanels
void acceptAllPanels()
Accepts all panel widgets open in the stack in turn until only the mainPanel() remains.
Definition: qgspanelwidgetstack.cpp:132
QgsPanelWidgetStack::currentPanel
QgsPanelWidget * currentPanel()
Returns the panel currently shown in the stack.
Definition: qgspanelwidgetstack.cpp:96
QgsPanelWidget::showPanel
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
QgsPanelWidgetStack::clear
void clear()
Clear the stack of all widgets.
Definition: qgspanelwidgetstack.cpp:71
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:29
QgsPanelWidgetStack::setMainPanel
void setMainPanel(QgsPanelWidget *panel SIP_TRANSFER)
Sets the main panel widget for the stack and selects it for the user.
Definition: qgspanelwidgetstack.cpp:37
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:112
QgsPanelWidget::panelTitle
QString panelTitle()
The title of the panel.
Definition: qgspanelwidget.h:50
QgsPanelWidgetStack::closePanel
void closePanel(QgsPanelWidget *panel)
Closes the panel in the widget.
Definition: qgspanelwidgetstack.cpp:165
qgspanelwidgetstack.h
QgsPanelWidgetStack::takeMainPanel
QgsPanelWidget * takeMainPanel() SIP_TRANSFERBACK
Removes the main panel widget from the stack and transfers ownsership to the caller.
Definition: qgspanelwidgetstack.cpp:54
QgsPanelWidgetStack::QgsPanelWidgetStack
QgsPanelWidgetStack(QWidget *parent=nullptr)
A stack widget to manage panels in the interface.
Definition: qgspanelwidgetstack.cpp:26
QgsPanelWidgetStack::minimumSizeHint
QSize minimumSizeHint() const override
Definition: qgspanelwidgetstack.cpp:111
QgsPanelWidgetStack::keyPressEvent
void keyPressEvent(QKeyEvent *e) override
Definition: qgspanelwidgetstack.cpp:196
QgsPanelWidgetStack::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *e) override
Definition: qgspanelwidgetstack.cpp:188
QgsPanelWidgetStack::showPanel
void showPanel(QgsPanelWidget *panel)
Show a panel in the stack widget.
Definition: qgspanelwidgetstack.cpp:149
QgsPanelWidgetStack::mainPanel
QgsPanelWidget * mainPanel()
The main panel widget that is set in the stack.
Definition: qgspanelwidgetstack.cpp:49
qgslogger.h
QgsPanelWidgetStack::acceptCurrentPanel
void acceptCurrentPanel()
Accept the current active widget in the stack.
Definition: qgspanelwidgetstack.cpp:122
qgspanelwidget.h