Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 4710e639 authored by Laurent Montel's avatar Laurent Montel 😁

Modernize lineeditwithcompleter

parent 297dbd32
......@@ -91,6 +91,7 @@ set(libpimcommon_widgets_SRCS
widgets/spellchecklineedit.cpp
widgets/kpimprintpreviewdialog.cpp
widgets/lineeditpassword.cpp
widgets/lineeditwithcompleterng.cpp
)
set(libpimcommon_customtools_SRCS
......
/*
Copyright (c) 2017 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "lineeditwithcompleterng.h"
#include <KLocalizedString>
#include <QCompleter>
#include <QContextMenuEvent>
#include <QMenu>
#include <QStringListModel>
using namespace PimCommon;
#define MAX_COMPLETION_ITEMS 20
LineEditWithCompleterNg::LineEditWithCompleterNg(QWidget *parent)
: QLineEdit(parent)
{
QCompleter *completer = new QCompleter(this);
mCompleterListModel = new QStringListModel(this);
completer->setModel(mCompleterListModel);
setCompleter(completer);
}
LineEditWithCompleterNg::~LineEditWithCompleterNg()
{
}
void LineEditWithCompleterNg::addCompletionItem(const QString &str)
{
mListCompetion.removeAll(str);
mListCompetion.prepend(str);
while (mListCompetion.size() > MAX_COMPLETION_ITEMS) {
mListCompetion.removeLast();
}
mCompleterListModel->setStringList(mListCompetion);
}
void LineEditWithCompleterNg::contextMenuEvent(QContextMenuEvent *e)
{
QMenu *popup = QLineEdit::createStandardContextMenu();
if (popup) {
popup->addSeparator();
popup->addAction(QIcon::fromTheme(QStringLiteral("edit-clear-locationbar-rtl")), i18n("Clear History"), this, &LineEditWithCompleterNg::slotClearHistory);
popup->exec(e->globalPos());
delete popup;
}
}
void LineEditWithCompleterNg::slotClearHistory()
{
mListCompetion.clear();
mCompleterListModel->setStringList(mListCompetion);
}
/*
Copyright (c) 2017 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef LINEEDITWITHCOMPLETERNG_H
#define LINEEDITWITHCOMPLETERNG_H
#include <QLineEdit>
#include "pimcommon_export.h"
class QStringListModel;
namespace PimCommon {
class PIMCOMMON_EXPORT LineEditWithCompleterNg : public QLineEdit
{
Q_OBJECT
public:
explicit LineEditWithCompleterNg(QWidget *parent = nullptr);
~LineEditWithCompleterNg();
void addCompletionItem(const QString &str);
protected:
void contextMenuEvent(QContextMenuEvent *e) override;
private Q_SLOTS:
void slotClearHistory();
private:
QStringListModel *mCompleterListModel;
QStringList mListCompetion;
};
}
#endif // LINEEDITWITHCOMPLETER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment