Baloo

queryresultsmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "queryresultsmodel.h"
8#include "query.h"
9
10#include <QMimeDatabase>
11#include <QUrl>
12
13Query::Query(QObject *parent)
14 : QObject(parent)
15 , m_limit(0)
16{
17}
18
19Query::~Query()
20{
21}
22
23void Query::setSearchString(const QString &searchString)
24{
25 if (m_searchString == searchString) {
26 return;
27 }
28
29 m_searchString = searchString;
30 Q_EMIT searchStringChanged();
31}
32
33QString Query::searchString() const
34{
35 return m_searchString;
36}
37
38void Query::setLimit(const int &limit)
39{
40 if (m_limit == limit) {
41 return;
42 }
43
44 m_limit = limit;
45 Q_EMIT limitChanged();
46}
47
48int Query::limit() const
49{
50 return m_limit;
51}
52
53QueryResultsModel::QueryResultsModel(QObject *parent)
54 : QAbstractListModel(parent),
55 m_query(new Query(this))
56{
57 connect(m_query, &Query::searchStringChanged, this, &QueryResultsModel::populateModel);
58 connect(m_query, &Query::limitChanged, this, &QueryResultsModel::populateModel);
59}
60
61QueryResultsModel::~QueryResultsModel()
62{
63}
64
65QHash<int, QByteArray> QueryResultsModel::roleNames() const
66{
68 roleNames[UrlRole] = "url";
69
70 return roleNames;
71}
72
73QVariant QueryResultsModel::data(const QModelIndex &index, int role) const
74{
75 if (!index.isValid()) {
76 return QVariant();
77 }
78
79 switch (role) {
80 case Qt::DisplayRole: {
81 const QUrl url = QUrl::fromLocalFile(m_balooEntryList.at(index.row()));
82 return url.fileName();
83 }
84 case Qt::DecorationRole: {
85 QString localUrl = m_balooEntryList.at(index.row());
86 return QMimeDatabase().mimeTypeForFile(localUrl).iconName();
87 }
88 case UrlRole:
89 return m_balooEntryList.at(index.row());
90 default:
91 return QVariant();
92 }
93}
94
95int QueryResultsModel::rowCount(const QModelIndex &parent) const
96{
97 if (parent.isValid()) {
98 return 0;
99 }
100
101 return m_balooEntryList.count();
102}
103
104void QueryResultsModel::setQuery(Query *query)
105{
106 if (m_query == query) {
107 return;
108 }
109
110 delete m_query;
111 m_query = query;
112 m_query->setParent(this);
113 Q_EMIT queryChanged();
114}
115
116Query* QueryResultsModel::query() const
117{
118 return m_query;
119}
120
121void QueryResultsModel::populateModel()
122{
124 query.setSearchString(m_query->searchString());
125 query.setLimit(m_query->limit());
126 Baloo::ResultIterator it = query.exec();
127
129 m_balooEntryList.clear();
130 while (it.next()) {
131 m_balooEntryList << it.filePath();
132 }
134}
135
136#include "moc_queryresultsmodel.cpp"
The Query class is the central class to query to search for files from the Index.
Definition query.h:54
std::optional< QSqlQuery > query(const QString &queryStatement)
virtual QHash< int, QByteArray > roleNames() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
const_reference at(qsizetype i) const const
void clear()
qsizetype count() const const
QMimeType mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode) const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QObject * parent() const const
void setParent(QObject *parent)
DisplayRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString fileName(ComponentFormattingOptions options) const const
QUrl fromLocalFile(const QString &localFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.