Commit 2e845f6c authored by Lefebvre, Jordan's avatar Lefebvre, Jordan
Browse files

WIP: working version with dummy data.

parent 7cbe577d
......@@ -51,10 +51,8 @@ int NavigationItem::columnCount() const { return p->data.size(); }
QVariant NavigationItem::data(int column) const
{
radix_tagged_line("data(" << column << ") data size (" << p->data.size()
<< ")");
if (column >= p->data.size()) return QVariant();
p->data.value(column);
return p->data.value(column);
}
void NavigationItem::setData(int column, QVariant value)
......
......@@ -24,7 +24,7 @@ NavigationModel::NavigationModel(QObject* parent)
, p(new PImpl(), [](PImpl* impl) { delete impl; })
{
radix_tagged_line("Navigation Model");
p->root = new NavigationItem();
p->root = new NavigationItem({"Header 1", "Header 2"});
p->root->addChild(new NavigationItem("Hello", p->root));
NavigationItem* world = new NavigationItem("World", p->root);
p->root->addChild(world);
......
......@@ -24,11 +24,60 @@
namespace radix
{
class NavigationItemSortFilterProxyModel::PImpl
{
public:
QHash<QModelIndex, bool> seen;
};
NavigationItemSortFilterProxyModel::NavigationItemSortFilterProxyModel(
QObject* parent)
: QSortFilterProxyModel(parent)
, p(new PImpl(), [](PImpl* impl) { delete impl; })
{
}
bool NavigationItemSortFilterProxyModel::accepts(const QModelIndex& index) const
{
const QString& text = index.data().toString();
const QRegExp& regex = filterRegExp();
bool accept = text.contains(regex);
for (int i = 0, ie = sourceModel()->rowCount(index); i < ie; i++)
{
if (accepts(sourceModel()->index(i, 0, index)))
{
accept = true;
break;
}
}
p->seen[index] = accept;
return accept;
}
void NavigationItemSortFilterProxyModel::clearCache() { p->seen.clear(); }
bool NavigationItemSortFilterProxyModel::filterAcceptsRow(
int source_row, const QModelIndex& source_parent) const
{
const QModelIndex& index = sourceModel()->index(source_row, 0, source_parent);
if (p->seen.contains(index))
{
return p->seen.value(index);
}
return accepts(index);
}
class NavigationWidget::PImpl
{
public:
QLineEdit* filter;
NavigationModel* model;
NavigationItemSortFilterProxyModel* proxy;
QTreeView* view;
};
......@@ -53,6 +102,7 @@ void NavigationWidget::initMembers()
// allocate
p->filter = new QLineEdit();
p->model = new NavigationModel();
p->proxy = new NavigationItemSortFilterProxyModel();
p->view = new QTreeView();
// init properties
p->filter->setPlaceholderText("Filter");
......@@ -60,11 +110,13 @@ void NavigationWidget::initMembers()
p->view->installEventFilter(this);
p->view->setContextMenuPolicy(Qt::CustomContextMenu);
p->view->setEditTriggers(QAbstractItemView::NoEditTriggers);
p->view->setModel(p->model);
p->view->setModel(p->proxy);
p->view->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
p->view->header()->setStretchLastSection(false);
p->view->header()->setVisible(false);
p->proxy->setSourceModel(p->model);
// shortcuts
auto activateEnter = new QShortcut(p->view);
auto activateReturn = new QShortcut(p->view);
......@@ -78,5 +130,43 @@ void NavigationWidget::initMembers()
collapseBranch->setContext(Qt::WidgetShortcut);
collapseBranch->setKey(Qt::Key_Left);
//
// connect signals/slots
connect(p->filter, SIGNAL(textChanged(QString)), this,
SLOT(filterModel(QString)));
connect(p->view, SIGNAL(doubleClicked(QModelIndex)), this,
SLOT(itemDoubleClicked(QModelIndex)));
}
void NavigationWidget::filterModel(const QString& pattern)
{
// regex used to filter the model
QRegExp regex(pattern, Qt::CaseInsensitive, QRegExp::RegExp2);
// update the model
p->proxy->clearCache();
p->proxy->setFilterRegExp(regex);
// update the tree
if (pattern.isEmpty())
{
p->view->collapseAll();
for (int i = 0, ie = p->model->rowCount(); i < ie; i++)
{
p->view->expand(p->proxy->mapFromSource(p->model->index(i, 0)));
}
}
else
{
p->view->expandAll();
}
}
void NavigationWidget::itemDoubleClicked(QModelIndex index)
{
radix_tagged_line("itemDoubleClicked()");
const auto& mapped = p->proxy->mapToSource(index);
radix_tagged_line("\t[" << mapped.row() << "," << mapped.column() << "]");
}
} // namespace radix
......@@ -17,6 +17,25 @@ namespace radix
{
class NavigationItem;
class NavigationItemSortFilterProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
NavigationItemSortFilterProxyModel(QObject* parent = nullptr);
public slots:
void clearCache();
protected:
bool filterAcceptsRow(int row, const QModelIndex& parent) const;
private:
bool accepts(const QModelIndex& index) const;
class PImpl;
std::unique_ptr<PImpl, void (*)(PImpl*)> p;
};
class NavigationWidget : public QWidget
{
Q_OBJECT
......@@ -30,6 +49,10 @@ class NavigationWidget : public QWidget
QTreeView* navigationTree() const;
public slots:
void filterModel(const QString& pattern);
void itemDoubleClicked(QModelIndex index);
signals:
void itemActivated(NavigationItem* item);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment