// 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 "Log.h" #include "ui_Log.h" #include "localplayer/ClientTools.h" #include "localplayer/Tribes.h" #include #include #include #include #include namespace Cevo { LogDlg::LogDlg(QWidget *parent) : Dpi::Form(parent), ui(new Ui::LogDlg) { ui->setupUi(this); setWindowFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint); ui->list->installEventFilter(this); connect(ui->list, &QPlainTextEdit::customContextMenuRequested, this, [this](const QPoint &pos) { logPopup->popup(ui->list->mapToGlobal(pos)); }); logPopup = new QMenu(this); logPopup->addAction(ui->actionInvalid); logPopup->addAction(ui->actionNegotiation); logPopup->addAction(ui->actionTime); logPopup->addSeparator(); auto *logLevelGroup = new QActionGroup(this); QAction *logLevelActions[4] = { ui->actionLog0, ui->actionLog1, ui->actionLog2, ui->actionLog3 }; static constexpr int logLevelValue[4] = { 0, 1, 2, 999 }; for (int i = 0; i < 4; ++i) { logLevelGroup->addAction(logLevelActions[i]); logPopup->addAction(logLevelActions[i]); connect(logLevelActions[i], &QAction::triggered, this, [this, i] { setMaxLevel(logLevelValue[i], i); }); } logPopup->addSeparator(); logPopup->addAction(ui->actionSlot); logPopup->addAction(ui->actionClear); connect(ui->actionSlot, &QAction::triggered, this, [this] { listSlots(); }); connect(ui->actionClear, &QAction::triggered, ui->list, &QPlainTextEdit::clear); } LogDlg::~LogDlg() { delete ui; } void LogDlg::clear() { ui->list->clear(); } void LogDlg::setSlotItemVisible(bool visible) { ui->actionSlot->setVisible(visible); } void LogDlg::listSlots() { // Player slot numbers as laid out around the start screen's 3x3 grid. static constexpr int SlotNo[3][3] = { { 8, 1, 2 }, { 7, 0, 3 }, { 6, 5, 4 } }; const auto shortName = [](int p) { const auto &tribe = Tribes::tribe[static_cast(p)]; return tribe ? tribe->tPhrase(QStringLiteral("SHORTNAME")) : QString(); }; for (int y = 0; y < 3; ++y) { QString s = QStringLiteral("| "); for (int x = 0; x < 3; ++x) { const int slot = SlotNo[y][x]; const int difficulty = ClientTools::g.Difficulty[slot]; if (difficulty == 0) { s += QStringLiteral("SUP |"); } else if (difficulty < 0) { s += QStringLiteral("--- |"); } else { if (slot >= 6 && slot <= 8) { // check multi control if (ClientTools::g.Difficulty[slot + 3] >= 0) s += shortName(slot + 3) + QLatin1Char('+'); if (ClientTools::g.Difficulty[slot + 6] >= 0) s += shortName(slot + 6) + QLatin1Char('+'); } s += shortName(slot) + QStringLiteral(" | "); } } ui->list->appendPlainText(s); } QScrollBar *vBar = ui->list->verticalScrollBar(); vBar->setValue(vBar->maximum()); } void LogDlg::setMaxLevel(int value, int checkedIndex) { maxLevel = value; Q_UNUSED(checkedIndex); // the QActionGroup already enforces mutual exclusivity } void LogDlg::add(int level, int turn, const QString &text) { if ((maxLevel > 0 && level <= maxLevel) || (level == (1 << 16) + 1 && ui->actionInvalid->isChecked()) || (level == (1 << 16) + 2 && ui->actionTime->isChecked()) || (level == (1 << 16) + 3 && ui->actionNegotiation->isChecked())) { if (ui->list->document()->blockCount() == MaxLines) { QTextCursor cursor(ui->list->document()); cursor.movePosition(QTextCursor::Start); cursor.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor); cursor.removeSelectedText(); } const QString prefix = QString("%1%2%3").arg((turn / 100) % 10).arg((turn / 10) % 10).arg(turn % 10); ui->list->appendPlainText(prefix + ' ' + text); QScrollBar *vBar = ui->list->verticalScrollBar(); vBar->setValue(vBar->maximum()); repaint(); } } void LogDlg::keyPressEvent(QKeyEvent *event) { if (host && host->onKeyDown) host->onKeyDown(event); Dpi::Form::keyPressEvent(event); } void LogDlg::keyReleaseEvent(QKeyEvent *event) { if (host && host->onKeyUp) host->onKeyUp(event); Dpi::Form::keyReleaseEvent(event); } bool LogDlg::eventFilter(QObject *watched, QEvent *event) { if (watched == ui->list && host) { if (event->type() == QEvent::KeyPress && host->onKeyDown) host->onKeyDown(static_cast(event)); else if (event->type() == QEvent::KeyRelease && host->onKeyUp) host->onKeyUp(static_cast(event)); } return Dpi::Form::eventFilter(watched, event); } } // namespace Cevo