implemented settings window

This commit is contained in:
Geoffray Levasseur committed 2026-11-04 14:11:05 +01:00
1 parent 19006fac6c
commit a24876cf79
7 files changed
+582 -1

No files matched your search

+349
View File
@@ -0,0 +1,349 @@
// This file is part of c-evo-evo.
//
// This software is governed by the CeCILL license under French law and
// abiding by the rules of distribution of free software. You can use,
// modify and/or redistribute the software under the terms of the CeCILL
// license as circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy,
// modify and redistribute granted by the license, users are provided only
// with a limited warranty and the software's author, the holder of the
// economic rights, and the successive licensors have only limited
// liability.
//
// In this respect, the user's attention is drawn to the risks associated
// with loading, using, modifying and/or developing or reproducing the
// software by the user in light of its specific status of free software,
// that may mean that it is complicated to manipulate, and that also
// therefore means that it is reserved for developers and experienced
// professionals having in-depth computer knowledge. Users are therefore
// encouraged to load and test the software's suitability as regards their
// requirements in conditions enabling the security of their systems and/or
// data to be ensured and, more generally, to use and operate it in the
// same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
#include "Settings.h"
#include "ui_Settings.h"
#include "Cevo/CevoButtonA.h"
#include "Cevo/CevoButtonC.h"
#include "Cevo/CevoListBoxEx.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "Global.h"
#include "Messg.h"
#include <QKeyEvent>
#include <QLineEdit>
#include <QPainter>
#include <algorithm>
namespace Cevo {
namespace {
constexpr int MusicVolumeMin = 0;
constexpr int MusicVolumeMax = 100;
constexpr int MusicVolumeStep = 10;
constexpr int GammaMin = 50;
constexpr int GammaMax = 150;
QString settingsText(int index)
{
return phrases.lookup(QStringLiteral("SETTINGS"), index);
}
} // namespace
SettingsDlg::SettingsDlg(Languages &languages, QWidget *parent)
: DrawDlg(parent), m_languages(languages), ui(new Ui::SettingsDlg)
{
ui->setupUi(this);
// A modal window of its own, even though it has a parent (the window it is transient for).
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
ui->buttonOk->setCaption(phrases.lookup(QStringLiteral("BTN_OK")));
ui->buttonCancel->setCaption(phrases.lookup(QStringLiteral("BTN_CANCEL")));
ui->buttonReset->setCaption(phrases.lookup(QStringLiteral("BTN_RESET")));
ui->buttonFullscreen->setButtonIndex(0);
ui->buttonMusic->setButtonIndex(0);
ui->buttonGammaUp->setButtonIndex(1);
ui->buttonGammaDown->setButtonIndex(0);
ui->buttonMusicVolumeUp->setButtonIndex(1);
ui->buttonMusicVolumeDown->setButtonIndex(0);
initButtons();
ui->buttonOk->onClick = [this] {
saveData();
setModalResult(Dpi::ModalResult::Ok);
close();
};
ui->buttonCancel->onClick = [this] {
setModalResult(Dpi::ModalResult::Cancel);
close();
};
ui->buttonReset->onClick = [this] { resetClicked(); };
ui->buttonFullscreen->onClick = [this] {
ui->buttonFullscreen->setButtonIndex(ui->buttonFullscreen->buttonIndex() ^ 1);
};
ui->buttonMusic->onClick = [this] {
ui->buttonMusic->setButtonIndex(ui->buttonMusic->buttonIndex() ^ 1);
updateInterface();
};
ui->buttonMusicVolumeDown->onClick = [this] {
m_localMusicVolume = std::max(m_localMusicVolume - MusicVolumeStep, MusicVolumeMin);
update();
};
ui->buttonMusicVolumeUp->onClick = [this] {
m_localMusicVolume = std::min(m_localMusicVolume + MusicVolumeStep, MusicVolumeMax);
update();
};
ui->buttonGammaDown->onClick = [this] {
if (m_localGamma > GammaMin) {
--m_localGamma;
update();
}
};
ui->buttonGammaUp->onClick = [this] {
if (m_localGamma < GammaMax) {
++m_localGamma;
update();
}
};
ui->editShortCutPrimary->installEventFilter(this);
ui->editShortCutSecondary->installEventFilter(this);
connect(ui->listKeyBindings, &QListWidget::currentRowChanged, this, [this] { keyBindingSelectionChanged(); });
onShow = [this] {
centerToScreen();
setWindowTitle(phrases2.lookup(QStringLiteral("ACTIONHEADER_CONFIG")));
m_fullScreenText = settingsText(0);
m_gammaText = settingsText(1);
m_restartText = settingsText(2);
m_primaryText = settingsText(3);
m_secondaryText = settingsText(4);
m_languagesText = settingsText(5);
m_keyBindingsText = settingsText(6);
m_musicText = settingsText(9);
m_musicVolumeText = settingsText(10);
m_languages.detectAvailable();
ui->listLanguages->clear();
for (int i = 0; i < m_languages.count(); ++i) {
const Language &language = m_languages[i];
if (!language.available)
continue;
QString text = language.name;
if (!language.code.isEmpty())
text += QStringLiteral(" (") + language.code + QLatin1Char(')');
auto *item = new QListWidgetItem(text, ui->listLanguages);
item->setData(Qt::UserRole, language.code);
}
loadData();
for (QLineEdit *edit : {ui->editShortCutPrimary, ui->editShortCutSecondary}) {
QPalette pal = edit->palette();
pal.setColor(QPalette::Base, Qt::black);
pal.setColor(QPalette::Text, MainTexture->colorMark);
edit->setPalette(pal);
}
updateInterface();
};
onClose = [this] { ui->listKeyBindings->setCurrentRow(-1); };
}
SettingsDlg::~SettingsDlg()
{
delete ui;
}
bool SettingsDlg::localMusicEnabled() const
{
return (ui->buttonMusic->buttonIndex() & 1) == 1;
}
void SettingsDlg::updateInterface()
{
ui->buttonMusicVolumeDown->setEnabled(localMusicEnabled());
ui->buttonMusicVolumeUp->setEnabled(localMusicEnabled());
}
void SettingsDlg::loadData()
{
int languageRow = 0;
for (int i = 0; i < ui->listLanguages->count(); ++i) {
if (ui->listLanguages->item(i)->data(Qt::UserRole).toString() == localeCode)
languageRow = i;
}
ui->listLanguages->setCurrentRow(languageRow);
ui->buttonFullscreen->setButtonIndex(fullScreen ? 3 : 2);
ui->buttonMusic->setButtonIndex(musicEnabled ? 3 : 2);
m_localMusicVolume = qRound(musicVolume * 100);
m_localGamma = gamma;
m_localKeyBindings.assign(KeyBindings::keyBindings);
m_currentKeyBinding = nullptr;
reloadKeyBindingList();
keyBindingSelectionChanged();
}
void SettingsDlg::saveData()
{
const bool needRestart = gamma != m_localGamma;
if (ui->listLanguages->currentRow() >= 0)
localeCode = ui->listLanguages->currentItem()->data(Qt::UserRole).toString();
else
localeCode.clear();
fullScreen = (ui->buttonFullscreen->buttonIndex() & 1) == 1;
musicEnabled = localMusicEnabled();
musicVolume = static_cast<float>(m_localMusicVolume) / 100.0f;
gamma = m_localGamma;
if (needRestart)
simpleMessage(m_restartText);
// Take over a shortcut still being edited.
keyBindingSelectionChanged();
KeyBindings::keyBindings.assign(m_localKeyBindings);
}
void SettingsDlg::resetClicked()
{
// TODO (as in the original): the default values should live in one place.
ui->listLanguages->setCurrentRow(0);
ui->buttonFullscreen->setButtonIndex(3);
ui->buttonMusic->setButtonIndex(3);
m_localMusicVolume = 50;
m_localGamma = 100;
ui->listKeyBindings->setCurrentRow(-1);
m_currentKeyBinding = nullptr;
keyBindingSelectionChanged();
m_localKeyBindings.resetToDefault();
reloadKeyBindingList();
updateInterface();
update();
}
void SettingsDlg::reloadKeyBindingList()
{
const int row = ui->listKeyBindings->currentRow();
ui->listKeyBindings->blockSignals(true);
ui->listKeyBindings->clear();
ui->listKeyBindings->addItems(m_localKeyBindings.toDisplayStrings());
ui->listKeyBindings->setCurrentRow(row);
ui->listKeyBindings->blockSignals(false);
}
void SettingsDlg::keyBindingSelectionChanged()
{
if (m_currentKeyBinding) {
m_currentKeyBinding->shortCut = KeyBindings::textToShortCut(ui->editShortCutPrimary->text());
m_currentKeyBinding->shortCut2 = KeyBindings::textToShortCut(ui->editShortCutSecondary->text());
}
const int row = ui->listKeyBindings->currentRow();
m_currentKeyBinding = row >= 0 && row < m_localKeyBindings.count() ? &m_localKeyBindings[row] : nullptr;
if (m_currentKeyBinding) {
ui->editShortCutPrimary->setText(m_currentKeyBinding->shortCut.isEmpty()
? QString()
: KeyBindings::shortCutToText(m_currentKeyBinding->shortCut));
ui->editShortCutSecondary->setText(m_currentKeyBinding->shortCut2.isEmpty()
? QString()
: KeyBindings::shortCutToText(m_currentKeyBinding->shortCut2));
} else {
ui->editShortCutPrimary->clear();
ui->editShortCutSecondary->clear();
}
ui->editShortCutPrimary->setEnabled(m_currentKeyBinding != nullptr);
ui->editShortCutSecondary->setEnabled(m_currentKeyBinding != nullptr);
}
void SettingsDlg::updateShortCutItem()
{
if (!m_currentKeyBinding)
return;
if (!m_currentKeyBinding->shortCut.isEmpty())
m_localKeyBindings.removeShortCut(m_currentKeyBinding->shortCut);
if (!m_currentKeyBinding->shortCut2.isEmpty())
m_localKeyBindings.removeShortCut(m_currentKeyBinding->shortCut2);
m_currentKeyBinding->shortCut = KeyBindings::textToShortCut(ui->editShortCutPrimary->text());
m_currentKeyBinding->shortCut2 = KeyBindings::textToShortCut(ui->editShortCutSecondary->text());
reloadKeyBindingList();
}
bool SettingsDlg::eventFilter(QObject *watched, QEvent *event)
{
const bool primary = watched == ui->editShortCutPrimary;
if ((primary || watched == ui->editShortCutSecondary) && m_currentKeyBinding &&
(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)) {
// The edit shows the pressed combination instead of receiving text.
if (event->type() == QEvent::KeyRelease) {
auto *keyEvent = static_cast<QKeyEvent *>(event);
const int key = keyEvent->key();
if (key != Qt::Key_Shift && key != Qt::Key_Control && key != Qt::Key_Alt && key != Qt::Key_Meta &&
key != Qt::Key_AltGr) {
const QKeySequence shortcut(keyEvent->modifiers() | key);
if (primary) {
m_currentKeyBinding->shortCut = shortcut;
ui->editShortCutPrimary->setText(KeyBindings::shortCutToText(shortcut));
} else {
m_currentKeyBinding->shortCut2 = shortcut;
ui->editShortCutSecondary->setText(KeyBindings::shortCutToText(shortcut));
}
updateShortCutItem();
}
}
return true;
}
return DrawDlg::eventFilter(watched, event);
}
void SettingsDlg::paintEvent(QPaintEvent *)
{
constexpr int TextDistanceX = 20;
constexpr int UpDownTextWidth = 120;
QPainter painter(this);
Dpi::Canvas canvas;
canvas.bind(&painter);
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Normal)]);
canvas.brush().setStyle(Qt::NoBrush);
const QColor light = MainTexture->colorBevelLight;
const QColor shade = MainTexture->colorBevelShade;
paintBackground(canvas, 3, 3, width() - 6, height() - 6, width(), height());
frame(canvas, 0, 0, width() - 1, height() - 1, Qt::black, Qt::black);
frame(canvas, 1, 1, width() - 2, height() - 2, light, shade);
frame(canvas, 2, 2, width() - 3, height() - 3, light, shade);
editFrame(canvas, ui->editShortCutPrimary->geometry(), *MainTexture);
editFrame(canvas, ui->editShortCutSecondary->geometry(), *MainTexture);
editFrame(canvas, ui->listLanguages->geometry(), *MainTexture);
btnFrame(canvas, ui->buttonOk->geometry(), *MainTexture);
btnFrame(canvas, ui->buttonCancel->geometry(), *MainTexture);
const auto checkFrame = [&](const QWidget *button) {
rframe(canvas, button->x() - 1, button->y() - 1, button->x() + 12, button->y() + 12, shade, light);
};
checkFrame(ui->buttonFullscreen);
checkFrame(ui->buttonMusic);
loweredTextOut(canvas, -2, *MainTexture, ui->buttonMusic->x() + TextDistanceX, ui->buttonMusic->y() - 4,
m_musicText);
underlinedTitleValue(canvas, m_musicVolumeText, QString::number(m_localMusicVolume) + QLatin1Char('%'),
ui->buttonMusicVolumeUp->x() - UpDownTextWidth - 4, ui->buttonMusicVolumeUp->y() + 2,
UpDownTextWidth);
loweredTextOut(canvas, -2, *MainTexture, ui->listLanguages->x(), ui->listLanguages->y() - 26, m_languagesText);
loweredTextOut(canvas, -2, *MainTexture, ui->listKeyBindings->x(), ui->listKeyBindings->y() - 26,
m_keyBindingsText);
loweredTextOut(canvas, -2, *MainTexture, ui->buttonFullscreen->x() + TextDistanceX, ui->buttonFullscreen->y() - 4,
m_fullScreenText);
underlinedTitleValue(canvas, m_gammaText, QString::number(m_localGamma) + QLatin1Char('%'),
ui->buttonGammaUp->x() - UpDownTextWidth - 4, ui->buttonGammaUp->y() + 2, UpDownTextWidth);
loweredTextOut(canvas, -2, *MainTexture, ui->editShortCutPrimary->x(), ui->editShortCutPrimary->y() - 26,
m_primaryText);
loweredTextOut(canvas, -2, *MainTexture, ui->editShortCutSecondary->x(), ui->editShortCutSecondary->y() - 26,
m_secondaryText);
canvas.unbind();
}
} // namespace Cevo