QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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, SIGNAL( pressed() ), this, SLOT( acceptCurrentPanel() ) );
33 }
34 
36 {
37  // TODO Don't allow adding another main widget or else that would be strange for the user.
38  connect( panel, SIGNAL( showPanel( QgsPanelWidget* ) ), this, SLOT( showPanel( QgsPanelWidget* ) ),
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  QWidget* widget = mStackedWidget->widget( 0 );
54  mStackedWidget->removeWidget( widget );
55  return qobject_cast<QgsPanelWidget*>( widget );
56 }
57 
59 {
60  for ( int i = mStackedWidget->count(); i >= 0; i-- )
61  {
62  if ( QgsPanelWidget* panelWidget = qobject_cast<QgsPanelWidget*>( mStackedWidget->widget( i ) ) )
63  {
64  mStackedWidget->removeWidget( panelWidget );
65  if ( panelWidget->autoDelete() )
66  {
67  panelWidget->deleteLater();
68  }
69  }
70  else if ( QWidget* widget = mStackedWidget->widget( i ) )
71  {
72  mStackedWidget->removeWidget( widget );
73  widget->deleteLater();
74  }
75  }
76  mTitles.clear();
77  mTitleText->hide();
78  mBackButton->hide();
79  this->updateBreadcrumb();
80 }
81 
83 {
84  // You can't accept the main panel.
85  if ( mStackedWidget->currentIndex() == 0 )
86  return;
87 
88  QgsPanelWidget* widget = qobject_cast<QgsPanelWidget*>( mStackedWidget->currentWidget() );
89  widget->acceptPanel();
90 }
91 
93 {
94  mTitles.push( panel->panelTitle() );
95 
96  connect( panel, SIGNAL( panelAccepted( QgsPanelWidget* ) ), this, SLOT( closePanel( QgsPanelWidget* ) ) );
97  connect( panel, SIGNAL( showPanel( QgsPanelWidget* ) ), this, SLOT( showPanel( QgsPanelWidget* ) ) );
98 
99  int index = mStackedWidget->addWidget( panel );
100  mStackedWidget->setCurrentIndex( index );
101  mBackButton->show();
102  mTitleText->show();
103 
104  this->updateBreadcrumb();
105 }
106 
108 {
109  mTitles.pop();
110  mStackedWidget->setCurrentIndex( mStackedWidget->currentIndex() - 1 );
111  mStackedWidget->removeWidget( panel );
112  if ( panel->autoDelete() )
113  {
114  panel->deleteLater();
115  }
116 
117  if ( mStackedWidget->currentIndex() == 0 )
118  {
119  mBackButton->hide();
120  mTitleText->hide();
121  }
122  this->updateBreadcrumb();
123 }
124 
125 void QgsPanelWidgetStack::updateBreadcrumb()
126 {
127  QString breadcrumb;
128  Q_FOREACH ( QString title, mTitles )
129  {
130  breadcrumb += QString( " %1 >" ).arg( title );
131  }
132  // Remove the last
133  breadcrumb.chop( 1 );
134  mTitleText->setText( breadcrumb );
135 }
static unsigned index
QgsPanelWidgetStack(QWidget *parent=nullptr)
A stack widget to manage panels in the interface.
void acceptCurrentPanel()
Accept the current active widget in the stack.
void setupUi(QWidget *widget)
QgsPanelWidget * mainWidget()
The main widget that is set in the stack.
void push(const T &t)
bool autoDelete()
The the auto delete property on the widget.
Base class for any widget that can be shown as a inline panel.
void chop(int n)
void closePanel(QgsPanelWidget *panel)
Closes the panel in the widget.
QgsPanelWidget * takeMainWidget()
Removes the main widget from the stack and transfers ownsership to the caller.
void acceptPanel()
Accept the panel.
QString panelTitle()
The title of the panel.
void showPanel(QgsPanelWidget *panel)
Show a panel in the stack widget.
void deleteLater()
void clear()
Clear the stack of all widgets.
void addMainPanel(QgsPanelWidget *panel)
Adds the main widget to the stack and selects it for the user The main widget can not be closed and o...
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const