Skip to content

Commit 0292f0c

Browse files
committed
iioexplorer: Use LazyStackedWidget for runtime creation.
Signed-off-by: andreidanila1 <andrei.danila@analog.com>
1 parent a36731d commit 0292f0c

File tree

2 files changed

+61
-38
lines changed

2 files changed

+61
-38
lines changed

packages/generic-plugins/plugins/debugger/include/debugger/iioexplorerinstrument.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
#include "watchlistview.h"
3131
#include "savecontextsetup.h"
3232
#include "iiodebuglogger.h"
33-
#include <gui/mapstackedwidget.h>
33+
#include <gui/lazystackedwidget.h>
34+
#include <gui/stackedwidgetprovider.h>
3435

3536
#include <iio.h>
3637
#include <QWidget>
@@ -42,7 +43,7 @@ namespace scopy::debugger {
4243

4344
class IIOExplorerInstrument_API;
4445

45-
class SCOPY_DEBUGGER_EXPORT IIOExplorerInstrument : public QWidget
46+
class SCOPY_DEBUGGER_EXPORT IIOExplorerInstrument : public QWidget, public StackedWidgetProvider
4647
{
4748
Q_OBJECT
4849
friend class IIOExplorerInstrument_API;
@@ -64,7 +65,11 @@ private Q_SLOTS:
6465
private:
6566
void setupUi();
6667
void connectSignalsAndSlots();
67-
void showOrBuildPage(IIOStandardItem *item);
68+
69+
// StackedWidgetProvider interface
70+
QWidget *createWidget(const QString &key) override;
71+
void onShow(const QString &key, QWidget *widget) override;
72+
void onRemove(const QString &key, QWidget *widget) override;
6873

6974
// Recursive function to find an item in the source model
7075
IIOStandardItem *findItemRecursive(QStandardItem *currentItem, QStandardItem *targetItem);
@@ -96,7 +101,7 @@ private Q_SLOTS:
96101
QString m_uri;
97102
QTreeView *m_treeView;
98103
IIOModel *m_iioModel;
99-
MapStackedWidget *m_mapStack;
104+
LazyStackedWidget *m_mapStack;
100105
DetailsPage *m_currentDetailsPage;
101106
SearchBar *m_searchBar;
102107
IIOSortFilterProxyModel *m_proxyModel;

packages/generic-plugins/plugins/debugger/src/iioexplorer/iioexplorerinstrument.cpp

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void IIOExplorerInstrument::setupUi()
124124

125125
m_iioModel = new IIOModel(m_context, "context0", m_treeView);
126126
m_searchBar = new SearchBar(m_iioModel->getEntries(), this);
127-
m_mapStack = new MapStackedWidget(details_container);
127+
m_mapStack = new LazyStackedWidget(this, details_container);
128128
m_watchListView = new WatchListView(watch_list);
129129

130130
watch_list->layout()->addWidget(m_watchListView);
@@ -143,7 +143,7 @@ void IIOExplorerInstrument::setupUi()
143143
m_treeView->expand(m_proxyModel->index(0, 0));
144144
m_currentlySelectedItem =
145145
dynamic_cast<IIOStandardItem *>(m_iioModel->getModel()->invisibleRootItem()->child(0));
146-
showOrBuildPage(m_currentlySelectedItem);
146+
m_mapStack->show(m_currentlySelectedItem->path());
147147

148148
details_container->layout()->addWidget(m_mapStack);
149149
tree_view_container->layout()->addWidget(m_searchBar);
@@ -219,45 +219,63 @@ void IIOExplorerInstrument::connectSignalsAndSlots()
219219
});
220220
}
221221

222-
void IIOExplorerInstrument::showOrBuildPage(IIOStandardItem *item)
222+
QWidget *IIOExplorerInstrument::createWidget(const QString &key)
223223
{
224+
QStringList pathList = key.split('/', Qt::SkipEmptyParts);
225+
QStandardItem *root = m_iioModel->getModel()->invisibleRootItem()->child(0);
226+
IIOStandardItem *iioRoot = dynamic_cast<IIOStandardItem *>(root);
227+
if(!iioRoot) {
228+
qWarning(CAT_IIODEBUGGER) << "Cannot find the model root.";
229+
return nullptr;
230+
}
231+
232+
IIOStandardItem *item = findItemByPath(iioRoot, pathList);
224233
if(!item) {
225-
return;
234+
qWarning(CAT_IIODEBUGGER) << "Could not find item for key:" << key;
235+
return nullptr;
226236
}
227237

228-
if(!m_mapStack->contains(item->path())) {
229-
// Ensure the item's IIOWidgets and attribute children exist before building the page
230-
m_iioModel->populateChildren(item);
238+
m_iioModel->populateChildren(item);
231239

232-
auto *page = new DetailsPage(item, m_uri, m_mapStack);
233-
m_mapStack->add(item->path(), page);
240+
auto *page = new DetailsPage(item, m_uri, m_mapStack);
234241

235-
connect(page, &DetailsPage::pathSelected, this, [this](QString path) {
236-
QStringList pathList = path.split('/', Qt::SkipEmptyParts);
237-
QStandardItem *root = m_iioModel->getModel()->invisibleRootItem()->child(0);
238-
IIOStandardItem *iioRoot = dynamic_cast<IIOStandardItem *>(root);
239-
if(!iioRoot) {
240-
qWarning(CAT_IIODEBUGGER) << "Cannot find the model root.";
241-
return;
242-
}
243-
IIOStandardItem *foundItem = findItemByPath(iioRoot, pathList);
244-
if(!foundItem) {
245-
qWarning(CAT_IIODEBUGGER) << "Could not find the item with path:" << path;
246-
return;
247-
}
248-
selectItem(foundItem);
249-
});
242+
connect(page, &DetailsPage::pathSelected, this, [this](QString path) {
243+
QStringList pathList = path.split('/', Qt::SkipEmptyParts);
244+
QStandardItem *root = m_iioModel->getModel()->invisibleRootItem()->child(0);
245+
IIOStandardItem *iioRoot = dynamic_cast<IIOStandardItem *>(root);
246+
if(!iioRoot) {
247+
qWarning(CAT_IIODEBUGGER) << "Cannot find the model root.";
248+
return;
249+
}
250+
IIOStandardItem *foundItem = findItemByPath(iioRoot, pathList);
251+
if(!foundItem) {
252+
qWarning(CAT_IIODEBUGGER) << "Could not find the item with path:" << path;
253+
return;
254+
}
255+
selectItem(foundItem);
256+
});
250257

251-
connect(page->readBtn(), &QPushButton::clicked, this, &IIOExplorerInstrument::onReadAllClicked);
252-
connect(page->addToWatchlistBtn(), &QPushButton::clicked, this,
253-
&IIOExplorerInstrument::onWatchlistToggleClicked);
254-
}
258+
connect(page->readBtn(), &QPushButton::clicked, this, &IIOExplorerInstrument::onReadAllClicked);
259+
connect(page->addToWatchlistBtn(), &QPushButton::clicked, this,
260+
&IIOExplorerInstrument::onWatchlistToggleClicked);
255261

256-
m_mapStack->show(item->path());
257-
m_currentDetailsPage = qobject_cast<DetailsPage *>(m_mapStack->get(item->path()));
262+
return page;
263+
}
258264

259-
if(m_currentDetailsPage) {
260-
m_currentDetailsPage->setAddToWatchlistState(!item->isWatched());
265+
void IIOExplorerInstrument::onShow(const QString &key, QWidget *widget)
266+
{
267+
Q_UNUSED(key)
268+
m_currentDetailsPage = qobject_cast<DetailsPage *>(widget);
269+
if(m_currentDetailsPage && m_currentlySelectedItem) {
270+
m_currentDetailsPage->setAddToWatchlistState(!m_currentlySelectedItem->isWatched());
271+
}
272+
}
273+
274+
void IIOExplorerInstrument::onRemove(const QString &key, QWidget *widget)
275+
{
276+
Q_UNUSED(key)
277+
if(m_currentDetailsPage == widget) {
278+
m_currentDetailsPage = nullptr;
261279
}
262280
}
263281

@@ -461,7 +479,7 @@ void IIOExplorerInstrument::applySelection(const QItemSelection &selected, const
461479
m_currentlySelectedItem = iioItem;
462480

463481
if(iioItem) {
464-
showOrBuildPage(iioItem);
482+
m_mapStack->show(iioItem->path());
465483
m_watchListView->currentTreeSelectionChanged(iioItem);
466484
}
467485
}
@@ -491,7 +509,7 @@ void IIOExplorerInstrument::selectItem(IIOStandardItem *item)
491509
m_currentlySelectedItem = item;
492510
auto sourceModel = qobject_cast<QStandardItemModel *>(m_proxyModel->sourceModel());
493511
recursiveExpandItem(sourceModel->invisibleRootItem(), item);
494-
showOrBuildPage(item);
512+
m_mapStack->show(item->path());
495513
}
496514

497515
void IIOExplorerInstrument::setupCodeGeneratorWindow()

0 commit comments

Comments
 (0)