Implement simple search
This commit is contained in:
parent
b121008372
commit
7da64ee167
@ -51,10 +51,11 @@ DesignWidget::DesignWidget(QWidget *parent) : QWidget(parent), ctx(nullptr), sel
|
||||
propertyEditor->treeWidget()->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
propertyEditor->treeWidget()->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
||||
|
||||
QLineEdit *lineEdit = new QLineEdit();
|
||||
lineEdit->setClearButtonEnabled(true);
|
||||
lineEdit->addAction(QIcon(":/icons/resources/zoom.png"), QLineEdit::LeadingPosition);
|
||||
lineEdit->setPlaceholderText("Search...");
|
||||
searchEdit = new QLineEdit();
|
||||
searchEdit->setClearButtonEnabled(true);
|
||||
searchEdit->addAction(QIcon(":/icons/resources/zoom.png"), QLineEdit::LeadingPosition);
|
||||
searchEdit->setPlaceholderText("Search...");
|
||||
connect(searchEdit, SIGNAL(returnPressed()), this, SLOT(onSearchInserted()));
|
||||
|
||||
actionFirst = new QAction("", this);
|
||||
actionFirst->setIcon(QIcon(":/icons/resources/resultset_first.png"));
|
||||
@ -123,7 +124,7 @@ DesignWidget::DesignWidget(QWidget *parent) : QWidget(parent), ctx(nullptr), sel
|
||||
topWidget->setLayout(vbox1);
|
||||
vbox1->setSpacing(5);
|
||||
vbox1->setContentsMargins(0, 0, 0, 0);
|
||||
vbox1->addWidget(lineEdit);
|
||||
vbox1->addWidget(searchEdit);
|
||||
vbox1->addWidget(treeView);
|
||||
|
||||
QWidget *toolbarWidget = new QWidget();
|
||||
@ -714,4 +715,19 @@ void DesignWidget::onItemDoubleClicked(QTreeWidgetItem *item, int column)
|
||||
}
|
||||
|
||||
void DesignWidget::onDoubleClicked(const QModelIndex &index) { Q_EMIT zoomSelected(); }
|
||||
|
||||
void DesignWidget::onSearchInserted()
|
||||
{
|
||||
if (currentSearch == searchEdit->text()) {
|
||||
currentIndex++;
|
||||
if (currentIndex >= currentSearchIndexes.size())
|
||||
currentIndex = 0;
|
||||
} else {
|
||||
currentSearch = searchEdit->text();
|
||||
currentSearchIndexes = treeModel->search(searchEdit->text());
|
||||
currentIndex = 0;
|
||||
}
|
||||
if (currentSearchIndexes.size() > 0 && currentIndex < currentSearchIndexes.size())
|
||||
selectionModel->setCurrentIndex(currentSearchIndexes.at(currentIndex), QItemSelectionModel::ClearAndSelect);
|
||||
}
|
||||
NEXTPNR_NAMESPACE_END
|
||||
|
@ -64,6 +64,7 @@ class DesignWidget : public QWidget
|
||||
void onSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
|
||||
void onItemDoubleClicked(QTreeWidgetItem *item, int column);
|
||||
void onDoubleClicked(const QModelIndex &index);
|
||||
void onSearchInserted();
|
||||
public Q_SLOTS:
|
||||
void newContext(Context *ctx);
|
||||
void updateTree();
|
||||
@ -77,6 +78,7 @@ class DesignWidget : public QWidget
|
||||
QTreeView *treeView;
|
||||
QItemSelectionModel *selectionModel;
|
||||
ContextTreeModel *treeModel;
|
||||
QLineEdit *searchEdit;
|
||||
QtVariantPropertyManager *variantManager;
|
||||
QtVariantPropertyManager *readOnlyManager;
|
||||
QtGroupPropertyManager *groupManager;
|
||||
@ -98,6 +100,10 @@ class DesignWidget : public QWidget
|
||||
|
||||
QColor highlightColors[8];
|
||||
QMap<ContextTreeItem *, int> highlightSelected;
|
||||
|
||||
QString currentSearch;
|
||||
QList<QModelIndex> currentSearchIndexes;
|
||||
int currentIndex;
|
||||
};
|
||||
|
||||
NEXTPNR_NAMESPACE_END
|
||||
|
@ -326,4 +326,19 @@ Qt::ItemFlags ContextTreeModel::flags(const QModelIndex &index) const
|
||||
ContextTreeItem *node = nodeFromIndex(index);
|
||||
return Qt::ItemIsEnabled | (node->type() != ElementType::NONE ? Qt::ItemIsSelectable : Qt::NoItemFlags);
|
||||
}
|
||||
|
||||
QList<QModelIndex> ContextTreeModel::search(QString text)
|
||||
{
|
||||
QList<QModelIndex> list;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
for (auto key : nameToItem[i].keys()) {
|
||||
if (key.contains(text, Qt::CaseInsensitive)) {
|
||||
list.append(indexFromNode(nameToItem[i].value(key)));
|
||||
if (list.count() > 500)
|
||||
break; // limit to 500 results
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
NEXTPNR_NAMESPACE_END
|
@ -72,6 +72,7 @@ class ContextTreeModel : public QAbstractItemModel
|
||||
ContextTreeItem *nodeFromIndex(const QModelIndex &idx) const;
|
||||
QModelIndex indexFromNode(ContextTreeItem *node);
|
||||
ContextTreeItem *nodeForIdType(const ElementType type, const QString name) const;
|
||||
QList<QModelIndex> search(QString text);
|
||||
// Override QAbstractItemModel methods
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
|
||||
|
Loading…
Reference in New Issue
Block a user