Skip to content

Commit

Permalink
refactor: Make the searchable view an adapter
Browse files Browse the repository at this point in the history
This way the class is view agnostic, and could be used for other things
than just table (but could be used for tree).
  • Loading branch information
narnaud authored and LeonMatthesKDAB committed Dec 13, 2024
1 parent 55aaa15 commit b1aca95
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ set(PROJECT_SOURCES
documentpalette.cpp
fileselector.h
fileselector.cpp
findadapter.h
findadapter.cpp
findinterface.h
findwidget.h
findinfilespanel.cpp
Expand Down Expand Up @@ -93,8 +95,6 @@ set(PROJECT_SOURCES
scriptpanel.cpp
scriptlistpanel.cpp
scriptlistpanel.h
searchabletableview.h
searchabletableview.cpp
shortcutmanager.h
shortcutmanager.cpp
shortcutsettings.h
Expand Down
53 changes: 31 additions & 22 deletions src/gui/searchabletableview.cpp → src/gui/findadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,43 @@
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "searchabletableview.h"
#include "FindAdapter.h"
#include "core/textdocument.h"
#include "highlightdelegate.h"

namespace Gui {

SearchableTableView::SearchableTableView(QWidget *parent)
: QTableView(parent)
FindAdapter::FindAdapter(QAbstractItemView *view)
: m_view(view)
{
setItemDelegate(new HighlightDelegate(this));
Q_ASSERT(view);
}

SearchableTableView::~SearchableTableView() = default;

void SearchableTableView::find(const QString &text, int options)
void FindAdapter::find(const QString &text, int options)
{
if (text.isEmpty()) {
// Clear the search results
static_cast<HighlightDelegate *>(itemDelegate())->setHighlightedText({}, Core::TextDocument::NoFindFlags);
if (auto delegate = dynamic_cast<HighlightDelegate *>(m_view->itemDelegate())) {
delegate->setHighlightedText({}, Core::TextDocument::NoFindFlags);
m_view->viewport()->update();
}

m_searchResults.clear();
m_currentResultIndex = 0;
m_highlightedText.clear();
m_options = 0;
viewport()->update();
return;
}

// Search only in case the search was not already processed
if (text != m_highlightedText || options != m_options) {
// Update delegate with the search text
static_cast<HighlightDelegate *>(itemDelegate())->setHighlightedText(text, options);
viewport()->update();
if (auto delegate = dynamic_cast<HighlightDelegate *>(m_view->itemDelegate())) {
delegate->setHighlightedText(text, options);
m_view->viewport()->update();
}

m_searchResults = searchModel(text, options);
m_searchResults = text.isEmpty() ? QModelIndexList {} : searchModel(text, options);
m_currentResultIndex = 0;

// Update the current search text
Expand All @@ -59,30 +62,36 @@ void SearchableTableView::find(const QString &text, int options)
}

if (!m_searchResults.isEmpty()) {
selectionModel()->setCurrentIndex(m_searchResults.at(m_currentResultIndex), QItemSelectionModel::SelectCurrent);
m_view->selectionModel()->setCurrentIndex(m_searchResults.at(m_currentResultIndex),
QItemSelectionModel::SelectCurrent);
}
}

QModelIndexList SearchableTableView::searchModel(const QString &text, int options) const
void FindAdapter::updateItemDelegate()
{
if (text.isEmpty())
return {};
m_view->setItemDelegate(new HighlightDelegate(m_view));
}

QModelIndexList FindAdapter::searchModel(const QString &text, int options, const QModelIndex &parent) const
{
QModelIndexList searchResults;
for (int row = 0; row < model()->rowCount(); ++row) {
for (int column = 0; column < model()->columnCount(); ++column) {
const QModelIndex index = model()->index(row, column);
const QString data = model()->data(index).toString();
auto model = m_view->model();
for (int row = 0; row < model->rowCount(parent); ++row) {
const QModelIndex index = model->index(row, 0, parent);
for (int column = 0; column < model->columnCount(parent); ++column) {
auto columnIndex = index.siblingAtColumn(column);
const QString data = columnIndex.data().toString();
if (options & Core::TextDocument::FindRegexp) {
QRegularExpression re(text);
if (data.contains(re))
searchResults.append(index);
searchResults.append(columnIndex);
} else {
const bool caseSensitive = options & Core::TextDocument::FindCaseSensitively;
if (data.contains(text, caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive))
searchResults.append(index);
searchResults.append(columnIndex);
}
}
searchResults.append(searchModel(text, options, index));
}
return searchResults;
}
Expand Down
13 changes: 6 additions & 7 deletions src/gui/searchabletableview.h → src/gui/findadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@

#pragma once

#include <QTableWidget>
#include <QAbstractItemView>

namespace Gui {

class SearchableTableView : public QTableView
class FindAdapter
{
Q_OBJECT

public:
explicit SearchableTableView(QWidget *parent = nullptr);
~SearchableTableView() override;
explicit FindAdapter(QAbstractItemView *view);

void find(const QString &text, int options);
void updateItemDelegate();

private:
QModelIndexList searchModel(const QString &text, int options) const;
QModelIndexList searchModel(const QString &text, int options, const QModelIndex &parent = {}) const;

QAbstractItemView *const m_view;
QString m_highlightedText;
int m_options = 0;
QModelIndexList m_searchResults;
Expand Down
11 changes: 7 additions & 4 deletions src/gui/qttsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

#include "qttsview.h"
#include "core/textdocument.h"
#include "highlightdelegate.h"
#include "searchabletableview.h"
#include "findadapter.h"

#include <QAbstractTableModel>
#include <QHeaderView>
Expand Down Expand Up @@ -39,6 +38,8 @@ class QtTsModelView : public QAbstractTableModel
int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
Q_UNUSED(parent)
if (parent.isValid())
return 0;
return m_document->messages().count();
}
int columnCount(const QModelIndex &parent = QModelIndex()) const override
Expand Down Expand Up @@ -153,13 +154,15 @@ class QtTsProxy : public QSortFilterProxyModel
QtTsView::QtTsView(QWidget *parent)
: QWidget(parent)
, FindInterface(FindInterface::CanSearch)
, m_tableView(new SearchableTableView(this))
, m_tableView(new QTableView(this))
, m_findAdapter(new FindAdapter(m_tableView))
, m_searchLineEdit(new QLineEdit(this))
, m_contentProxyModel(new QtTsProxy(this))
{
auto mainWidgetLayout = new QVBoxLayout(this);
m_tableView->verticalHeader()->hide();
m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_findAdapter->updateItemDelegate();
mainWidgetLayout->addWidget(m_tableView);
mainWidgetLayout->addWidget(m_searchLineEdit);
m_searchLineEdit->setPlaceholderText(tr("Filter..."));
Expand Down Expand Up @@ -210,7 +213,7 @@ void QtTsView::updateView()

void QtTsView::find(const QString &searchText, int options)
{
m_tableView->find(searchText, options);
m_findAdapter->find(searchText, options);
}

} // namespace Gui
5 changes: 3 additions & 2 deletions src/gui/qttsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class QLineEdit;
namespace Gui {

class QtTsProxy;
class SearchableTableView;
class FindAdapter;

class QtTsView : public QWidget, public FindInterface
{
Expand All @@ -39,7 +39,8 @@ class QtTsView : public QWidget, public FindInterface
private:
void updateView();

SearchableTableView *const m_tableView;
QTableView *const m_tableView;
FindAdapter *const m_findAdapter;
QLineEdit *const m_searchLineEdit;
Core::QtTsDocument *m_document = nullptr;
QtTsProxy *const m_contentProxyModel;
Expand Down
11 changes: 7 additions & 4 deletions src/gui/qtuiview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
#include "qtuiview.h"
#include "core/qtuidocument.h"
#include "core/textdocument.h"
#include "highlightdelegate.h"
#include "searchabletableview.h"
#include "findadapter.h"

#include <QAbstractTableModel>
#include <QFile>
Expand Down Expand Up @@ -44,6 +43,8 @@ class QtUiModelView : public QAbstractTableModel
int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
Q_UNUSED(parent)
if (parent.isValid())
return 0;
return m_document->widgets().count();
}
int columnCount(const QModelIndex &parent = QModelIndex()) const override
Expand Down Expand Up @@ -104,21 +105,23 @@ class QtUiModelView : public QAbstractTableModel
QtUiView::QtUiView(QWidget *parent)
: QSplitter(parent)
, FindInterface(FindInterface::CanSearch)
, m_tableView(new SearchableTableView(this))
, m_tableView(new QTableView(this))
, m_findAdapter(new FindAdapter(m_tableView))
, m_previewArea(new QMdiArea(this))
{
addWidget(m_previewArea);
addWidget(m_tableView);

m_tableView->verticalHeader()->hide();
m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_findAdapter->updateItemDelegate();
m_previewArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_previewArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
}

void QtUiView::find(const QString &searchText, int options)
{
m_tableView->find(searchText, options);
m_findAdapter->find(searchText, options);
}

void QtUiView::setUiDocument(Core::QtUiDocument *document)
Expand Down
5 changes: 3 additions & 2 deletions src/gui/qtuiview.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class QMdiSubWindow;

namespace Gui {

class SearchableTableView;
class FindAdapter;

class QtUiView : public QSplitter, public FindInterface
{
Expand All @@ -39,7 +39,8 @@ class QtUiView : public QSplitter, public FindInterface
private:
void updateView();

SearchableTableView *const m_tableView;
QTableView *const m_tableView;
FindAdapter *const m_findAdapter;
QMdiArea *const m_previewArea;
Core::QtUiDocument *m_document = nullptr;
QMdiSubWindow *m_previewWindow = nullptr;
Expand Down

0 comments on commit b1aca95

Please sign in to comment.