diff --git a/choosedialog.cpp b/choosedialog.cpp index 0968740..94505fe 100644 --- a/choosedialog.cpp +++ b/choosedialog.cpp @@ -33,7 +33,7 @@ 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))")) + .filter(QRegularExpression(R"(\.(utf8|UTF-8)$)")) .replaceInStrings(".utf8", ".UTF-8", Qt::CaseInsensitive); if (!libFile.open(QIODevice::ReadOnly)) { @@ -46,7 +46,7 @@ void chooseDialog::buildLocaleList() QTextStream in(&libFile); while (!in.atEnd()) { QString line = in.readLine().trimmed(); - QStringList list = line.split('-'); + QStringList list = line.split('-', Qt::SkipEmptyParts); if (list.size() == 2) { localeLib.insert(list.at(0).trimmed(), list.at(1).trimmed()); } @@ -56,9 +56,8 @@ void chooseDialog::buildLocaleList() for (const auto &locale : qAsConst(availableLocales)) { QString item = locale; item.remove(QRegularExpression("\\..*$")); - QString line = locale; + QString line = locale.leftJustified(20, ' '); if (localeLib.contains(item)) { - line = line.leftJustified(20, ' '); line.append('\t' + localeLib.value(item)); } ui->listWidgetAvailableLocales->addItem(line); @@ -77,10 +76,12 @@ QString chooseDialog::selection() const 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) { - item->setHidden(!item->text().contains(ui->textSearch->text(), Qt::CaseInsensitive)); + bool shouldHide = !item->text().contains(searchText, Qt::CaseInsensitive); + item->setHidden(shouldHide); } } } diff --git a/mainwindow.cpp b/mainwindow.cpp index 967b12b..7b9a93a 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -43,22 +43,26 @@ MainWindow::MainWindow(const QCommandLineParser &args, QWidget *parent) ui->setupUi(this); setWindowFlags(Qt::Window); // For the close, min and max buttons setup(); + configureTabs(args); + configureCategories(args); + adjustSize(); +} + +void MainWindow::configureTabs(const QCommandLineParser &args) +{ if (args.isSet("only-lang")) { ui->tabWidget->setTabVisible(Tab::Subvariables, false); ui->tabWidget->setTabVisible(Tab::Management, false); } - if (args.isSet("full-categories")) { - ui->label_Ctype->setHidden(false); - ui->pushButtonCType->setHidden(false); - ui->label_Ident->setHidden(false); - ui->pushButtonIdentification->setHidden(false); - } else { - ui->label_Ident->setHidden(true); - ui->pushButtonIdentification->setHidden(true); - ui->label_Ctype->setHidden(true); - ui->pushButtonCType->setHidden(true); - } - this->adjustSize(); +} + +void MainWindow::configureCategories(const QCommandLineParser &args) +{ + bool fullCategories = args.isSet("full-categories"); + ui->label_Ctype->setHidden(!fullCategories); + ui->pushButtonCType->setHidden(!fullCategories); + ui->label_Ident->setHidden(!fullCategories); + ui->pushButtonIdentification->setHidden(!fullCategories); } MainWindow::~MainWindow() @@ -279,31 +283,29 @@ void MainWindow::onFilterChanged(const QString &text) bool filterAll = text == tr("All", "all as in everything"); bool searchEmpty = searchText.isEmpty(); - // If filter is set to "All" and search text is empty, no need to filter - if (filterAll && searchEmpty) { - for (int i = 0; i < ui->listWidget->count(); ++i) { - ui->listWidget->item(i)->setHidden(false); - } - return; - } - - // Iterate through the list and update visibility based on the filter and search text - for (int i = 0; i < ui->listWidget->count(); ++i) { - auto *item = ui->listWidget->item(i); + auto shouldHideItem = [&](QListWidgetItem *item) { if (!item) { - continue; + return false; } - bool hideItem = false; if (!filterAll) { bool isDisabled = item->checkState() == Qt::Checked && text == tr("Disabled"); bool isEnabled = item->checkState() == Qt::Unchecked && text == tr("Enabled"); - hideItem = isDisabled || isEnabled; + if (isDisabled || isEnabled) { + return true; + } } - if (!hideItem && !searchEmpty) { - hideItem = !item->text().contains(searchText, Qt::CaseInsensitive); + + if (!searchEmpty && !item->text().contains(searchText, Qt::CaseInsensitive)) { + return true; } - item->setHidden(hideItem); + + return false; + }; + + for (int i = 0; i < ui->listWidget->count(); ++i) { + auto *item = ui->listWidget->item(i); + item->setHidden(shouldHideItem(item)); } } @@ -362,8 +364,7 @@ void MainWindow::displayLocalesGen() return; } - QStringList localeFiles = getLocaleFiles({"/usr/share/i18n/locales", "/usr/local/share/i18n/locales"}); - processLocaleFiles(localeFiles); + processLocaleFiles(getLocaleFiles({"/usr/share/i18n/locales", "/usr/local/share/i18n/locales"})); for (const QString &filePath : supportedFiles) { if (!QFile::exists(filePath)) { @@ -371,7 +372,7 @@ void MainWindow::displayLocalesGen() } QFile file(filePath); if (!file.open(QIODevice::ReadOnly)) { - QMessageBox::critical(nullptr, tr("Error"), tr("Could not open %1").arg(file.fileName())); + QMessageBox::critical(this, tr("Error"), tr("Could not open %1").arg(file.fileName())); return; } readLocaleFile(file, enabledLocales); @@ -380,18 +381,19 @@ void MainWindow::displayLocalesGen() updateLocaleListUI(); } -QStringList MainWindow::readEnabledLocales(const QString &filePath) const +QStringList MainWindow::readEnabledLocales(const QString &filePath) { - QFile localeFile(filePath); QStringList enabledLocales; + QFile localeFile(filePath); if (!localeFile.open(QIODevice::ReadOnly)) { - QMessageBox::critical(nullptr, tr("Error"), tr("Could not open %1").arg(localeFile.fileName())); - return {}; + QMessageBox::critical(this, tr("Error"), tr("Could not open %1").arg(localeFile.fileName())); + return enabledLocales; } QTextStream in(&localeFile); QRegularExpression localeRegex("^[a-z]{2,3}([^_]|_[A-Z]{2})"); + while (!in.atEnd()) { QString line = in.readLine().trimmed(); if (localeRegex.match(line).hasMatch()) { @@ -406,8 +408,7 @@ QStringList MainWindow::getLocaleFiles(const QStringList &directories) const { QStringList localeFiles; for (const QString &dirPath : directories) { - QDirIterator it(dirPath, QStringList() << "*", QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, - QDirIterator::Subdirectories); + QDirIterator it(dirPath, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); while (it.hasNext()) { localeFiles.append(it.next()); } @@ -420,21 +421,21 @@ void MainWindow::processLocaleFiles(const QStringList &localeFiles) QRegularExpression titleRegex(R"(^title[[:space:]]+["](?