Pimcommon

lineeditwithcompleterng.cpp
1/*
2 SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "lineeditwithcompleterng.h"
8#include <KLocalizedString>
9#include <QCompleter>
10#include <QContextMenuEvent>
11#include <QMenu>
12#include <QStringListModel>
13
14using namespace PimCommon;
15#define MAX_COMPLETION_ITEMS 20
16LineEditWithCompleterNg::LineEditWithCompleterNg(QWidget *parent)
17 : QLineEdit(parent)
18 , mCompleterListModel(new QStringListModel(this))
19{
20 auto completer = new QCompleter(this);
21 completer->setCaseSensitivity(Qt::CaseInsensitive);
22 completer->setModel(mCompleterListModel);
23 setCompleter(completer);
24}
25
26LineEditWithCompleterNg::~LineEditWithCompleterNg() = default;
27
28void LineEditWithCompleterNg::addCompletionItem(const QString &str)
29{
30 mListCompetion.removeAll(str);
31 mListCompetion.prepend(str);
32 while (mListCompetion.size() > MAX_COMPLETION_ITEMS) {
33 mListCompetion.removeLast();
34 }
35 mCompleterListModel->setStringList(mListCompetion);
36}
37
38void LineEditWithCompleterNg::contextMenuEvent(QContextMenuEvent *e)
39{
41 if (popup) {
42 popup->addSeparator();
43 popup->addAction(QIcon::fromTheme(QStringLiteral("edit-clear-locationbar-rtl")),
44 i18n("Clear History"),
45 this,
46 &LineEditWithCompleterNg::slotClearHistory);
47 popup->exec(e->globalPos());
48 delete popup;
49 }
50}
51
52void LineEditWithCompleterNg::slotClearHistory()
53{
54 mListCompetion.clear();
55 mCompleterListModel->setStringList(mListCompetion);
56}
57
58#include "moc_lineeditwithcompleterng.cpp"
QString i18n(const char *text, const TYPE &arg...)
folderdialogacltab.h
const QPoint & globalPos() const const
QIcon fromTheme(const QString &name)
QMenu * createStandardContextMenu()
void clear()
void prepend(parameter_type value)
qsizetype removeAll(const AT &t)
void removeLast()
qsizetype size() const const
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * addSeparator()
QAction * exec()
void setStringList(const QStringList &strings)
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:39:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.