QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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#include "moc_qgspanelwidgetstack.cpp"
24
25#include "qgspanelwidget.h"
26
28 : QWidget( parent )
29{
30 setupUi( this );
31 clear();
32
33 connect( mBackButton, &QAbstractButton::clicked, this, &QgsPanelWidgetStack::acceptCurrentPanel );
34
35 mMenuButton->setStyleSheet( QStringLiteral( "QToolButton::menu-indicator { image: none; }" ) );
36}
37
39{
40 // TODO Don't allow adding another main widget or else that would be strange for the user.
42 // using unique connection because addMainPanel() may be called multiple times
43 // for a panel, so showPanel() slot could be invoked more times from one signal
44 Qt::UniqueConnection );
45 mStackedWidget->insertWidget( 0, panel );
46 mStackedWidget->setCurrentIndex( 0 );
47 updateMenuButton();
48}
49
51{
52 return qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( 0 ) );
53}
54
56{
57 // clear out the current stack
59
60 QWidget *widget = mStackedWidget->widget( 0 );
61 if ( widget )
62 {
63 mStackedWidget->removeWidget( widget );
64 return qobject_cast<QgsPanelWidget *>( widget );
65 }
66 else
67 {
68 return nullptr;
69 }
70}
71
73{
74 for ( int i = mStackedWidget->count() - 1; i >= 0; i-- )
75 {
76 if ( QgsPanelWidget *panelWidget = qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( i ) ) )
77 {
78 mStackedWidget->removeWidget( panelWidget );
79 if ( panelWidget->autoDelete() )
80 {
81 panelWidget->deleteLater();
82 }
83 }
84 else if ( QWidget *widget = mStackedWidget->widget( i ) )
85 {
86 mStackedWidget->removeWidget( widget );
87 widget->deleteLater();
88 }
89 }
90 mTitles.clear();
91 mTitleText->hide();
92 mBackButton->hide();
93 mMenuButton->hide();
94 this->updateBreadcrumb();
95}
96
98{
99 return qobject_cast<QgsPanelWidget *>( mStackedWidget->currentWidget() );
100}
101
103{
104 if ( const QgsPanelWidget *widget = qobject_cast<const QgsPanelWidget *>( mStackedWidget->currentWidget() ) )
105 {
106 if ( widget->applySizeConstraintsToStack() )
107 return widget->sizeHint();
108 }
109 return QWidget::sizeHint();
110}
111
113{
114 if ( const QgsPanelWidget *widget = qobject_cast<const QgsPanelWidget *>( mStackedWidget->currentWidget() ) )
115 {
116 if ( widget->applySizeConstraintsToStack() )
117 return widget->minimumSizeHint();
118 }
119
120 return QWidget::minimumSizeHint();
121}
122
124{
125 // You can't accept the main panel.
126 if ( mStackedWidget->currentIndex() <= 0 )
127 return;
128
129 QgsPanelWidget *widget = currentPanel();
130 widget->acceptPanel();
131}
132
134{
135 //avoid messy multiple redraws
136 setUpdatesEnabled( false );
137 mStackedWidget->setUpdatesEnabled( false );
138
139 for ( int i = mStackedWidget->count() - 1; i > 0; --i )
140 {
141 if ( QgsPanelWidget *panelWidget = qobject_cast<QgsPanelWidget *>( mStackedWidget->widget( i ) ) )
142 {
143 panelWidget->acceptPanel();
144 }
145 }
146 setUpdatesEnabled( true );
147 mStackedWidget->setUpdatesEnabled( true );
148}
149
151{
152 mTitles.push( panel->panelTitle() );
153
156
157 const int index = mStackedWidget->addWidget( panel );
158 mStackedWidget->setCurrentIndex( index );
159 mBackButton->show();
160 mTitleText->show();
161
162 updateMenuButton();
163 updateBreadcrumb();
164}
165
167{
168 mTitles.pop();
169 mStackedWidget->setCurrentIndex( mStackedWidget->currentIndex() - 1 );
170 mStackedWidget->removeWidget( panel );
171 if ( panel->autoDelete() )
172 {
173 panel->deleteLater();
174 }
175
176 if ( mStackedWidget->currentIndex() == 0 )
177 {
178 mBackButton->hide();
179 mTitleText->hide();
180 mMenuButton->hide();
181 }
182 else
183 {
184 updateMenuButton();
185 }
186 this->updateBreadcrumb();
187}
188
190{
191 if ( e->button() == Qt::BackButton )
192 {
194 }
195}
196
198{
199 if ( e->key() == Qt::Key_Escape )
200 {
202 }
203}
204
205void QgsPanelWidgetStack::updateBreadcrumb()
206{
207 QString breadcrumb;
208 const auto constMTitles = mTitles;
209 for ( const QString &title : constMTitles )
210 {
211 breadcrumb += QStringLiteral( " %1 >" ).arg( title );
212 }
213 // Remove the last
214 breadcrumb.chop( 1 );
215 mTitleText->setText( breadcrumb );
216}
217
218void QgsPanelWidgetStack::updateMenuButton()
219{
220 if ( QMenu *menu = currentPanel()->menuButtonMenu() )
221 {
222 mMenuButton->setVisible( true );
223 mMenuButton->setToolTip( currentPanel()->menuButtonTooltip() );
224 mMenuButton->setMenu( menu );
225 }
226 else
227 {
228 mMenuButton->setVisible( false );
229 }
230}
QgsPanelWidget * currentPanel()
Returns the panel currently shown in the stack.
void closePanel(QgsPanelWidget *panel)
Closes the panel in the widget.
QSize sizeHint() const override
void acceptAllPanels()
Accepts all panel widgets open in the stack in turn until only the mainPanel() remains.
QgsPanelWidgetStack(QWidget *parent=nullptr)
A stack widget to manage panels in the interface.
QgsPanelWidget * mainPanel()
The main panel widget that is set in the stack.
QSize minimumSizeHint() const override
void showPanel(QgsPanelWidget *panel)
Show a panel in the stack widget.
void acceptCurrentPanel()
Accept the current active widget in the stack.
void setMainPanel(QgsPanelWidget *panel SIP_TRANSFER)
Sets the main panel widget for the stack and selects it for the user.
QgsPanelWidget * takeMainPanel() SIP_TRANSFERBACK
Removes the main panel widget from the stack and transfers ownsership to the caller.
void clear()
Clear the stack of all widgets.
void mouseReleaseEvent(QMouseEvent *e) override
void keyPressEvent(QKeyEvent *e) override
Base class for any widget that can be shown as a inline panel.
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
QString panelTitle()
The title of the panel.
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
bool autoDelete()
The the auto delete property on the widget.
void acceptPanel()
Accept the panel.