swift
sharedstringlistcompleter.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QDateTime>
7 
8 namespace swift::gui
9 {
10  bool CSharedStringListCompleter::updateData(const QStringList &data, int cacheTimeMs)
11  {
12  QStringListModel *model = this->getCompleterModel();
13  Q_ASSERT_X(model, Q_FUNC_INFO, "Model missing");
14  bool updated = false;
15  const qint64 now = QDateTime::currentMSecsSinceEpoch();
16  if (model->rowCount() != data.size() || (now - m_lastUpdated) > cacheTimeMs)
17  {
18  // quick check, different size means data changed -> update
19  // otherwise rely on time
20  m_lastUpdated = now;
21  model->setStringList(data);
22  updated = true;
23  }
24  return updated;
25  }
26 
28 
30  {
31  return (QDateTime::currentMSecsSinceEpoch() - m_lastUpdated) <= checkTimeMs;
32  }
33 
35  {
36  return this->stringList().contains(value, cs);
37  }
38 
40  {
41  const QStringListModel *model = this->getCompleterModel();
42  if (!model) { return QStringList(); }
43  return model->stringList();
44  }
45 
46  QStringListModel *CSharedStringListCompleter::getCompleterModel() const
47  {
48  return qobject_cast<QStringListModel *>(m_completer->model());
49  }
50 
52  {
53  Q_ASSERT_X(completer, Q_FUNC_INFO, "Need completer");
55  completer->setWrapAround(true);
58  }
59 } // namespace swift::gui
bool wasUpdatedWithinTime(int checkTimeMs) const
Was updated within check time.
bool contains(const QString &value, Qt::CaseSensitivity cs=Qt::CaseInsensitive) const
Contains the string completer the given value.
bool updateData(const QStringList &data, int cacheTimeMs)
Update data.
static void setCompleterParameters(QCompleter *completer)
How completer behaves.
GUI related classes.
CaseInsensitivelySortedModel
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
void setCompletionMode(QCompleter::CompletionMode mode)
void setModelSorting(QCompleter::ModelSorting sorting)
void setWrapAround(bool wrap)
qint64 currentMSecsSinceEpoch()
qsizetype size() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
virtual int rowCount(const QModelIndex &parent) const const override
void setStringList(const QStringList &strings)
QStringList stringList() const const
CaseSensitivity