-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchoosedialog.cpp
87 lines (77 loc) · 2.73 KB
/
choosedialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
#include "choosedialog.h"
#include "cmd.h"
#include "ui_choosedialog.h"
chooseDialog::chooseDialog(QWidget *parent)
: QDialog(parent),
ui(new Ui::chooseDialog)
{
ui->setupUi(this);
setup();
}
chooseDialog::~chooseDialog()
{
delete ui;
}
// Setup various items first time program runs
void chooseDialog::setup()
{
this->setWindowTitle(tr("MX Locale", "name of application"));
buildLocaleList();
ui->textSearch->setFocus();
connect(ui->textSearch, &QLineEdit::textChanged, this, &chooseDialog::textSearch_textChanged);
}
void chooseDialog::buildLocaleList()
{
QFile libFile("/usr/lib/mx-locale/locale.lib");
QString locales = Cmd().getOut(R"(locale --all-locales)");
QStringList availableLocales = locales.split(QRegularExpression(R"((\r\n)|(\n\r)|\r|\n)"), Qt::SkipEmptyParts)
.filter(QRegularExpression(R"(\.(utf8|UTF-8)$)"))
.replaceInStrings(".utf8", ".UTF-8", Qt::CaseInsensitive);
if (!libFile.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, tr("Error"),
tr("Could not open %1", "message that a file could not be opened, file takes place of %1")
.arg(libFile.fileName()));
return;
}
QTextStream in(&libFile);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
QStringList list = line.split('-', Qt::SkipEmptyParts);
if (list.size() == 2) {
localeLib.insert(list.at(0).trimmed(), list.at(1).trimmed());
}
}
libFile.close();
for (const auto &locale : qAsConst(availableLocales)) {
QString item = locale;
item.remove(QRegularExpression("\\..*$"));
QString line = locale.leftJustified(20, ' ');
if (localeLib.contains(item)) {
line.append('\t' + localeLib.value(item));
}
ui->listWidgetAvailableLocales->addItem(line);
}
}
QString chooseDialog::selection() const
{
if (ui->listWidgetAvailableLocales->currentRow() == -1) {
return {};
}
QString selection = ui->listWidgetAvailableLocales->currentItem()->text().section('\t', 0, 0).trimmed();
selection = selection.replace(".utf8", ".UTF-8", Qt::CaseInsensitive);
return selection;
}
void chooseDialog::textSearch_textChanged()
{
QString searchText = ui->textSearch->text();
for (int i = 0; i < ui->listWidgetAvailableLocales->count(); ++i) {
auto *item = ui->listWidgetAvailableLocales->item(i);
if (item) {
bool shouldHide = !item->text().contains(searchText, Qt::CaseInsensitive);
item->setHidden(shouldHide);
}
}
}