KTextAddons

translatorconfigurelanguagelistwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "translatorconfigurelanguagelistwidget.h"
8#include <KLineEditEventHandler>
9#include <KLocalizedString>
10#include <QLabel>
11#include <QLineEdit>
12#include <QListView>
13#include <QSortFilterProxyModel>
14#include <QStandardItemModel>
15#include <QVBoxLayout>
16using namespace TextTranslator;
17
18TranslatorConfigureLanguageListWidget::TranslatorConfigureLanguageListWidget(const QString &labelText, QWidget *parent)
19 : QWidget{parent}
20 , mLanguageListWidget(new QListView(this))
21 , mListSearchLine(new QLineEdit(this))
22 , mLabel(new QLabel(labelText, this))
23 , mModel(new QStandardItemModel(this))
24{
25 auto mainLayout = new QVBoxLayout(this);
26 mainLayout->setContentsMargins({});
27 mainLayout->setObjectName(QStringLiteral("mainLayout"));
28
29 mLabel->setObjectName(QStringLiteral("mLabel"));
30 mainLayout->addWidget(mLabel);
31
32 mModel->setObjectName(QStringLiteral("mModel"));
33
34 mListSearchLine->setObjectName(QStringLiteral("mListSearchLine"));
35 mainLayout->addWidget(mListSearchLine);
36 mListSearchLine->setPlaceholderText(i18nc("@info:placeholder", "Search…"));
37 auto filterProxyModel = new QSortFilterProxyModel(this);
38 filterProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
39 filterProxyModel->setSourceModel(mModel);
40 connect(mListSearchLine, &QLineEdit::textChanged, this, [filterProxyModel](const QString &str) {
41 filterProxyModel->setFilterFixedString(str);
42 });
43
44 mLanguageListWidget->setObjectName(QStringLiteral("mLanguageListWidget"));
45 mainLayout->addWidget(mLanguageListWidget);
46 mLanguageListWidget->setModel(filterProxyModel);
48}
49
50TranslatorConfigureLanguageListWidget::~TranslatorConfigureLanguageListWidget() = default;
51
52void TranslatorConfigureLanguageListWidget::clear()
53{
54 mModel->clear();
55}
56
57void TranslatorConfigureLanguageListWidget::addItem(const QString &translatedStr, const QString &languageCode)
58{
59 QStandardItem *item = new QStandardItem();
60 item->setText(translatedStr);
61 item->setData(languageCode, LanguageCode);
62 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
63 item->setEditable(false);
65 mModel->appendRow(item);
66}
67
68QStringList TranslatorConfigureLanguageListWidget::selectedLanguages() const
69{
70 QStringList langs;
71 for (int i = 0; i < mModel->rowCount(); ++i) {
72 const auto item = mModel->item(i);
73 if (item->checkState() == Qt::Checked) {
74 langs.append(item->data(LanguageCode).toString());
75 }
76 }
77 return langs;
78}
79
80void TranslatorConfigureLanguageListWidget::setSelectedLanguages(const QStringList &list)
81{
82 for (int i = 0, total = mModel->rowCount(); i < total; ++i) {
83 const auto item = mModel->item(i);
84 item->setCheckState(list.contains(item->data(LanguageCode).toString()) ? Qt::Checked : Qt::Unchecked);
85 }
86}
87
88#include "moc_translatorconfigurelanguagelistwidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void textChanged(const QString &text)
void append(QList< T > &&value)
Qt::CheckState checkState() const const
virtual QVariant data(int role) const const
Qt::ItemFlags flags() const const
void setCheckState(Qt::CheckState state)
virtual void setData(const QVariant &value, int role)
void setEditable(bool editable)
void setFlags(Qt::ItemFlags flags)
void setText(const QString &text)
void appendRow(QStandardItem *item)
QStandardItem * item(int row, int column) const const
virtual int rowCount(const QModelIndex &parent) const const override
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
CaseInsensitive
Unchecked
ItemIsUserCheckable
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:48:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.