Skip to content

Commit

Permalink
feat(ui): load search results on selection
Browse files Browse the repository at this point in the history
Based on PR #749 by Stephen Holdaway (@stecman).

Closes #749.
  • Loading branch information
trollixx committed Dec 29, 2019
1 parent 86f5350 commit 3118649
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
63 changes: 48 additions & 15 deletions src/libs/ui/searchsidebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ SearchSidebar::SearchSidebar(const SearchSidebar *other, QWidget *parent)
if (other) {
if (other->m_searchEdit->text().isEmpty()) {
m_searchModel = new Registry::SearchModel(this);

m_treeView->setRootIsDecorated(true);
m_treeView->setModel(Core::Application::instance()->docsetRegistry()->model());
setTreeViewModel(Core::Application::instance()->docsetRegistry()->model(), true);

for (const QModelIndex &index : other->m_expandedIndexList) {
m_treeView->expand(index);
Expand All @@ -134,8 +132,7 @@ SearchSidebar::SearchSidebar(const SearchSidebar *other, QWidget *parent)
m_searchEdit->setText(other->m_searchEdit->text());

m_searchModel = other->m_searchModel->clone(this);
m_treeView->setRootIsDecorated(false);
m_treeView->setModel(m_searchModel);
setTreeViewModel(m_searchModel, false);
}

for (const QModelIndex &index : other->m_treeView->selectionModel()->selectedIndexes()) {
Expand All @@ -146,26 +143,35 @@ SearchSidebar::SearchSidebar(const SearchSidebar *other, QWidget *parent)
m_pendingVerticalPosition = other->m_treeView->verticalScrollBar()->value();
} else {
m_searchModel = new Registry::SearchModel(this);

m_treeView->setRootIsDecorated(true);
m_treeView->setModel(Core::Application::instance()->docsetRegistry()->model());
setTreeViewModel(Core::Application::instance()->docsetRegistry()->model(), true);
}

connect(m_searchEdit, &QLineEdit::textChanged, this, [this](const QString &text) {
QItemSelectionModel *oldSelectionModel = m_treeView->selectionModel();

if (text.isEmpty()) {
m_treeView->setModel(Core::Application::instance()->docsetRegistry()->model());
m_treeView->setRootIsDecorated(true);
setTreeViewModel(Core::Application::instance()->docsetRegistry()->model(), true);
} else {
m_treeView->setModel(m_searchModel);
m_treeView->setRootIsDecorated(false);
setTreeViewModel(m_searchModel, false);
}

// TODO: Remove once QTBUG-49966 is addressed.
QItemSelectionModel *newSelectionModel = m_treeView->selectionModel();
if (oldSelectionModel && newSelectionModel != oldSelectionModel) {
oldSelectionModel->deleteLater();
if (newSelectionModel != oldSelectionModel) {
// TODO: Remove once QTBUG-49966 is addressed.
if (oldSelectionModel) {
oldSelectionModel->deleteLater();
}

// Connect to the new selection model.
connect(m_treeView->selectionModel(), &QItemSelectionModel::selectionChanged,
this, [this](const QItemSelection &selected) {
if (selected.isEmpty()) {
return;
}

m_delayedNavigationTimer->setProperty("index", selected.indexes().first());
m_delayedNavigationTimer->start();
});
}

m_treeView->reset();
Expand Down Expand Up @@ -252,6 +258,33 @@ SearchSidebar::SearchSidebar(const SearchSidebar *other, QWidget *parent)
});
}

void SearchSidebar::setTreeViewModel(QAbstractItemModel *model, bool isRootDecorated)
{
QItemSelectionModel *oldSelectionModel = m_treeView->selectionModel();

m_treeView->setModel(model);
m_treeView->setRootIsDecorated(isRootDecorated);

QItemSelectionModel *newSelectionModel = m_treeView->selectionModel();
if (newSelectionModel != oldSelectionModel) {
// TODO: Remove once QTBUG-49966 is addressed.
if (oldSelectionModel) {
oldSelectionModel->deleteLater();
}

// Connect to the new selection model.
connect(m_treeView->selectionModel(), &QItemSelectionModel::selectionChanged,
this, [this](const QItemSelection &selected) {
if (selected.isEmpty()) {
return;
}

m_delayedNavigationTimer->setProperty("index", selected.indexes().first());
m_delayedNavigationTimer->start();
});
}
}

SearchSidebar *SearchSidebar::clone(QWidget *parent) const
{
return new SearchSidebar(this, parent);
Expand Down
1 change: 1 addition & 0 deletions src/libs/ui/searchsidebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private slots:
private:
Q_DISABLE_COPY(SearchSidebar)
explicit SearchSidebar(const SearchSidebar *other, QWidget *parent = nullptr);
void setTreeViewModel(QAbstractItemModel *model, bool isRootDecorated);

SearchEdit *m_searchEdit = nullptr;
bool m_pendingSearchEditFocus = false;
Expand Down

0 comments on commit 3118649

Please sign in to comment.