Skip to content

Commit a36731d

Browse files
committed
gui: Add LazyStackedWidget and StackedWidgetProvider interface.
Introduce a generic lazy widget creation pattern for stacked widgets. StackedWidgetProvider defines createWidget/onShow/onRemove callbacks. LazyStackedWidget extends MapStackedWidget - when show(key) is called for a missing key, it delegates to the provider's createWidget to build and cache the widget on demand. Signed-off-by: andreidanila1 <andrei.danila@analog.com>
1 parent b0285ec commit a36731d

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2026 Analog Devices Inc.
3+
*
4+
* This file is part of Scopy
5+
* (see https://www.github.com/analogdevicesinc/scopy).
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
#ifndef LAZYSTACKEDWIDGET_H
23+
#define LAZYSTACKEDWIDGET_H
24+
25+
#include "scopy-gui_export.h"
26+
#include "mapstackedwidget.h"
27+
#include "stackedwidgetprovider.h"
28+
29+
namespace scopy {
30+
31+
class SCOPY_GUI_EXPORT LazyStackedWidget : public MapStackedWidget
32+
{
33+
Q_OBJECT
34+
public:
35+
explicit LazyStackedWidget(StackedWidgetProvider *provider, QWidget *parent = nullptr);
36+
~LazyStackedWidget();
37+
38+
bool show(QString key) override;
39+
bool remove(QString key) override;
40+
41+
private:
42+
StackedWidgetProvider *m_provider;
43+
};
44+
45+
} // namespace scopy
46+
47+
#endif // LAZYSTACKEDWIDGET_H
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2026 Analog Devices Inc.
3+
*
4+
* This file is part of Scopy
5+
* (see https://www.github.com/analogdevicesinc/scopy).
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
#ifndef STACKEDWIDGETPROVIDER_H
23+
#define STACKEDWIDGETPROVIDER_H
24+
25+
#include "scopy-gui_export.h"
26+
27+
#include <QString>
28+
#include <QWidget>
29+
30+
namespace scopy {
31+
32+
class SCOPY_GUI_EXPORT StackedWidgetProvider
33+
{
34+
public:
35+
virtual ~StackedWidgetProvider() = default;
36+
37+
virtual QWidget *createWidget(const QString &key) = 0;
38+
virtual void onShow(const QString &key, QWidget *widget) = 0;
39+
virtual void onRemove(const QString &key, QWidget *widget) = 0;
40+
};
41+
42+
} // namespace scopy
43+
44+
#endif // STACKEDWIDGETPROVIDER_H

gui/src/lazystackedwidget.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2026 Analog Devices Inc.
3+
*
4+
* This file is part of Scopy
5+
* (see https://www.github.com/analogdevicesinc/scopy).
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
#include "lazystackedwidget.h"
23+
24+
#include <QLoggingCategory>
25+
26+
Q_LOGGING_CATEGORY(CAT_LAZYSTACKEDWIDGET, "LazyStackedWidget")
27+
28+
using namespace scopy;
29+
30+
LazyStackedWidget::LazyStackedWidget(StackedWidgetProvider *provider, QWidget *parent)
31+
: MapStackedWidget(parent)
32+
, m_provider(provider)
33+
{}
34+
35+
LazyStackedWidget::~LazyStackedWidget() {}
36+
37+
bool LazyStackedWidget::show(QString key)
38+
{
39+
if(!contains(key) && m_provider) {
40+
QWidget *w = m_provider->createWidget(key);
41+
if(w) {
42+
add(key, w);
43+
} else {
44+
qWarning(CAT_LAZYSTACKEDWIDGET) << "Provider returned nullptr for key:" << key;
45+
return false;
46+
}
47+
}
48+
49+
bool result = MapStackedWidget::show(key);
50+
51+
if(result && m_provider) {
52+
m_provider->onShow(key, get(key));
53+
}
54+
55+
return result;
56+
}
57+
58+
bool LazyStackedWidget::remove(QString key)
59+
{
60+
if(m_provider && contains(key)) {
61+
m_provider->onRemove(key, get(key));
62+
}
63+
return MapStackedWidget::remove(key);
64+
}
65+
66+
#include "moc_lazystackedwidget.cpp"

0 commit comments

Comments
 (0)