Skip to content

Commit

Permalink
adding locator source class, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dogboydog committed Oct 27, 2022
1 parent 2ddf175 commit ae2c76f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
25 changes: 12 additions & 13 deletions src/tiled/locatorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,18 @@ inline void ResultsView::keyPressEvent(QKeyEvent *event)

///////////////////////////////////////////////////////////////////////////////

LocatorWidget::LocatorWidget(QWidget *parent)
LocatorWidget::LocatorWidget(LocatorSource *locatorSource, QWidget *parent)
: QFrame(parent, Qt::Popup)
, mFilterEdit(new FilterEdit(this))
, mResultsView(new ResultsView(this))
, mListModel(new MatchesModel(this))
, mDelegate(new MatchDelegate(this))
{
setAttribute(Qt::WA_DeleteOnClose);
setFrameStyle(QFrame::StyledPanel | QFrame::Plain);

mLocatorSource = locatorSource;
mResultsView->setUniformRowHeights(true);
mResultsView->setRootIsDecorated(false);
mResultsView->setItemDelegate(mDelegate);
mResultsView->setItemDelegate(locatorSource->delegate);
mResultsView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
mResultsView->setModel(mListModel);
mResultsView->setHeaderHidden(true);
Expand All @@ -295,12 +294,8 @@ LocatorWidget::LocatorWidget(QWidget *parent)
verticalLayout->addStretch(0);
setLayout(verticalLayout);

connect(mFilterEdit, &QLineEdit::textChanged, this, &LocatorWidget::setFilterText);
connect(mResultsView, &QAbstractItemView::activated, this, [this] (const QModelIndex &index) {
const QString file = mListModel->matches().at(index.row()).path;
close();
DocumentManager::instance()->openFile(file);
});
connect(mFilterEdit, &QLineEdit::textChanged, mLocatorSource, &LocatorSource::setFilterWords);
connect(mResultsView, &QAbstractItemView::activated, mLocatorSource, &LocatorSource::activate);
}

void LocatorWidget::setVisible(bool visible)
Expand All @@ -313,11 +308,15 @@ void LocatorWidget::setVisible(bool visible)
if (!mFilterEdit->text().isEmpty())
mFilterEdit->clear();
else
setFilterText(QString());
mLocatorSource->setFilterWords(QString());
}
}
ProjectFileLocatorSource::ProjectFileLocatorSource(QObject *parent)
: listModel(new MatchesModel(parent))
, delegate(new MatchDelegate(parent)){

void LocatorWidget::setFilterText(const QString &text)
}
void ProjectFileLocatorSource::setFilterWords(const QString &text)
{
const QString normalized = QDir::fromNativeSeparators(text);
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
Expand All @@ -338,7 +337,7 @@ void LocatorWidget::setFilterText(const QString &text)
return a.relativePath().compare(b.relativePath(), Qt::CaseInsensitive) < 0;
});

mDelegate->setWords(words);
locatorSource->delegate->setWords(words);
mListModel->setMatches(matches);

mResultsView->updateGeometry();
Expand Down
23 changes: 20 additions & 3 deletions src/tiled/locatorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,39 @@ class ResultsView : public QTreeView
void keyPressEvent(QKeyEvent *event) override;
};

class LocatorSource{
public:
explicit LocatorSource(QObject *parent);
virtual void setFilterWords(const QString &text);
virtual void activate(const QModelIndex &index);
MatchDelegate *delegate;
private:
QAbstractListModel *mListModel;
};
class LocatorWidget : public QFrame
{
Q_OBJECT

public:
explicit LocatorWidget(QWidget *parent = nullptr);
explicit LocatorWidget(LocatorSource *locatorSource, QWidget *parent = nullptr);

void setVisible(bool visible) override;

private:
void setFilterText(const QString &text);

FilterEdit *mFilterEdit;
ResultsView *mResultsView;
MatchesModel *mListModel;
MatchDelegate *mDelegate;
LocatorSource *mLocatorSource;
};


class ProjectFileLocatorSource: public LocatorSource {
public:
explicit ProjectFileLocatorSource(QObject *parent);
void setFilterWords(const QString &text) override;
void activate(const QModelIndex &index) override;
MatchesModel *listModel;
MatchDelegate *delegate;
};
} // namespace Tiled
4 changes: 2 additions & 2 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1152,8 +1152,8 @@ void MainWindow::openFileInProject()
const QPoint localPos((width() - size.width()) / 2,
qMin(remainingHeight / 5, Utils::dpiScaled(60)));
const QRect rect = QRect(mapToGlobal(localPos), size);

mLocatorWidget = new LocatorWidget(this);
ProjectFileLocatorSource *source = new ProjectFileLocatorSource(this);
mLocatorWidget = new LocatorWidget(&source, this);
mLocatorWidget->move(rect.topLeft());
mLocatorWidget->setMaximumSize(rect.size());
mLocatorWidget->show();
Expand Down

0 comments on commit ae2c76f

Please sign in to comment.