File indexing completed on 2024-11-24 04:16:54

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "bergamotenginesettingswidget.h"
0007 #include <KLocalizedString>
0008 #include <QCheckBox>
0009 #include <QComboBox>
0010 #include <QFormLayout>
0011 #include <QGroupBox>
0012 #include <QVBoxLayout>
0013 #include <thread>
0014 
0015 BergamotEngineSettingsWidget::BergamotEngineSettingsWidget(QWidget *parent)
0016     : QWidget{parent}
0017     , mNumberThreads(new QComboBox(this))
0018     , mMemoryByThreads(new QComboBox(this))
0019     , mUseLocalCache(new QCheckBox(i18n("Use translation cache"), this))
0020 {
0021     auto mainLayout = new QVBoxLayout(this);
0022     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0023     mainLayout->setContentsMargins({});
0024 
0025     auto resourceBox = new QGroupBox(i18n("Resource Usage"), this);
0026     resourceBox->setObjectName(QStringLiteral("resourceBox"));
0027     mainLayout->addWidget(resourceBox);
0028 
0029     mNumberThreads->setObjectName(QStringLiteral("mNumberThreads"));
0030     mMemoryByThreads->setObjectName(QStringLiteral("mMemoryByThreads"));
0031 
0032     mUseLocalCache->setObjectName(QStringLiteral("mUseLocalCache"));
0033 
0034     auto formLayout = new QFormLayout;
0035     formLayout->setObjectName(QStringLiteral("formLayout"));
0036     resourceBox->setLayout(formLayout);
0037     formLayout->addRow(i18n("Thread:"), mNumberThreads);
0038     formLayout->addRow(i18n("Memory by Thread:"), mMemoryByThreads);
0039     formLayout->addWidget(mUseLocalCache);
0040     fillCombobox();
0041 }
0042 
0043 BergamotEngineSettingsWidget::~BergamotEngineSettingsWidget() = default;
0044 
0045 void BergamotEngineSettingsWidget::setSettingsInfo(const BergamotEngineUtils::SettingsInfo &info)
0046 {
0047     mNumberThreads->setCurrentIndex(mNumberThreads->findData(info.numberOfThread));
0048     mMemoryByThreads->setCurrentIndex(mMemoryByThreads->findData(info.memoryByThread));
0049     mUseLocalCache->setChecked(info.useLocalCache);
0050 }
0051 
0052 BergamotEngineUtils::SettingsInfo BergamotEngineSettingsWidget::settingsInfo() const
0053 {
0054     BergamotEngineUtils::SettingsInfo info;
0055     info.memoryByThread = mMemoryByThreads->currentData().toInt();
0056     info.numberOfThread = mNumberThreads->currentData().toInt();
0057     info.useLocalCache = mUseLocalCache->isChecked();
0058     return info;
0059 }
0060 
0061 void BergamotEngineSettingsWidget::fillCombobox()
0062 {
0063     const auto processorCount = std::thread::hardware_concurrency();
0064 
0065     for (int cores = processorCount; cores > 0; cores -= 2) {
0066         mNumberThreads->addItem(QString::number(cores), cores);
0067     }
0068 
0069     const QList<int> memorys = {64, 128, 256, 512, 768, 1024, 1280, 1536, 1762, 2048};
0070     for (const int memory : memorys) {
0071         mMemoryByThreads->addItem(QString::number(memory), memory);
0072     }
0073 }
0074 
0075 #include "moc_bergamotenginesettingswidget.cpp"