diff --git a/CMakeLists.txt b/CMakeLists.txt index d60e3c6..fd23b90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,6 +90,8 @@ set(PROJECT_SOURCES mainwindow.cpp mainwindow.h mainwindow.ui + Log.cpp + Log.h ${TS_FILES} ) diff --git a/Log.cpp b/Log.cpp new file mode 100644 index 0000000..996f952 --- /dev/null +++ b/Log.cpp @@ -0,0 +1,155 @@ +// This file is part of cevo-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 +#include +#include +#include +#include + +namespace Cevo { + +LogDlg::LogDlg(QWidget *parent) : Dpi::Form(parent) +{ + setWindowFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint); + setWindowTitle(tr("Log")); + resize(339, 280); + + list = new Dpi::Memo(this); + list->setReadOnly(true); + list->setLineWrapMode(Dpi::Memo::NoWrap); + list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + list->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); + list->setContextMenuPolicy(Qt::CustomContextMenu); + list->installEventFilter(this); + connect(list, &QPlainTextEdit::customContextMenuRequested, this, [this](const QPoint &pos) { + logPopup->popup(list->mapToGlobal(pos)); + }); + + auto *layout = new QVBoxLayout(this); + layout->setContentsMargins(0, 0, 0, 0); + layout->setSpacing(0); + layout->addWidget(list); + + logPopup = new Dpi::PopupMenu(this); + logPopup->setAutoPopup(false); + + mInvalid = logPopup->add(tr("Invalid Server Calls")); + mInvalid->action()->setCheckable(true); + mInvalid->onClick = [this] { mInvalid->setChecked(!mInvalid->checked()); }; + + mNegotiation = logPopup->add(tr("Negotiation")); + mNegotiation->action()->setCheckable(true); + mNegotiation->onClick = [this] { mNegotiation->setChecked(!mNegotiation->checked()); }; + + mTime = logPopup->add(tr("Client Handover")); + mTime->action()->setCheckable(true); + mTime->onClick = [this] { mTime->setChecked(!mTime->checked()); }; + + logPopup->add(QString())->action()->setSeparator(true); + + const std::array logLevels = { + logPopup->add(tr("AI Messages Off")), + logPopup->add(tr("AI Messages Level 1")), + logPopup->add(tr("AI Messages Level 1+2")), + logPopup->add(tr("All AI Messages")), + }; + static constexpr int logLevelValue[4] = { 0, 1, 2, 999 }; + for (int i = 0; i < 4; ++i) { + Dpi::MenuItem *item = logLevels[i]; + item->setRadioItem(true); + item->onClick = [this, logLevels, i] { setMaxLevel(logLevelValue[i], logLevels, i); }; + } + logLevels[0]->setChecked(true); + + logPopup->add(QString())->action()->setSeparator(true); + + Dpi::MenuItem *mSlot = logPopup->add(tr("Nation <-> Slot")); + mSlot->onClick = [this] { + // TODO: needs G.Difficulty[]/Tribe[].TPhrase("SHORTNAME") (Global.pas/ + // Tribes.pas, not ported yet) - deferred until those units exist. + }; + + Dpi::MenuItem *mClear = logPopup->add(tr("Clear")); + mClear->onClick = [this] { list->clear(); }; +} + +void LogDlg::setMaxLevel(int value, const std::array &items, int checkedIndex) +{ + maxLevel = value; + for (int i = 0; i < 4; ++i) + items[i]->setChecked(i == checkedIndex); // Dpi::MenuItem has no automatic radio grouping +} + +void LogDlg::add(int level, int turn, const QString &text) +{ + if ((maxLevel > 0 && level <= maxLevel) || (level == (1 << 16) + 1 && mInvalid->checked()) || + (level == (1 << 16) + 2 && mTime->checked()) || (level == (1 << 16) + 3 && mNegotiation->checked())) { + if (list->document()->blockCount() == MaxLines) { + QTextCursor cursor(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); + list->appendPlainText(prefix + ' ' + text); + QScrollBar *vBar = 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 == 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 diff --git a/Log.h b/Log.h new file mode 100644 index 0000000..f9f3263 --- /dev/null +++ b/Log.h @@ -0,0 +1,73 @@ +// This file is part of cevo-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. + +#pragma once + +#include "Dpi/DpiForms.h" +#include "Dpi/DpiMenus.h" +#include "Dpi/DpiStdCtrls.h" + +#include +#include + +namespace Cevo { + +// TLogDlg equivalent: a floating, always-on-top debug/diagnostic log window +// (AI messages, invalid server calls, client handover, negotiation), with a +// right-click popup menu to filter what gets shown. +class LogDlg : public Dpi::Form { + Q_OBJECT +public: + explicit LogDlg(QWidget *parent = nullptr); + + // Forwards key events so the game's global hotkeys keep working while + // the log window (KeyPreview in the original) has focus. + Dpi::Form *host = nullptr; + + void add(int level, int turn, const QString &text); + +protected: + void keyPressEvent(QKeyEvent *event) override; + void keyReleaseEvent(QKeyEvent *event) override; + bool eventFilter(QObject *watched, QEvent *event) override; + +private: + static constexpr int MaxLines = 1000; + + int maxLevel = 0; + + Dpi::Memo *list; + Dpi::PopupMenu *logPopup; + Dpi::MenuItem *mInvalid; + Dpi::MenuItem *mNegotiation; + Dpi::MenuItem *mTime; + + void setMaxLevel(int value, const std::array &items, int checkedIndex); +}; + +} // namespace Cevo diff --git a/widget/include/Dpi/DpiForms.h b/widget/include/Dpi/DpiForms.h index 2648e8d..e34b8bf 100644 --- a/widget/include/Dpi/DpiForms.h +++ b/widget/include/Dpi/DpiForms.h @@ -38,6 +38,7 @@ class QShowEvent; class QHideEvent; class QEventLoop; class QScreen; +class QKeyEvent; namespace Dpi { @@ -76,11 +77,15 @@ public: std::function onDeactivate; std::function onCloseQuery; // return false to veto the close std::function onClose; + std::function onKeyDown; + std::function onKeyUp; protected: void showEvent(QShowEvent *event) override; void hideEvent(QHideEvent *event) override; void closeEvent(QCloseEvent *event) override; + void keyPressEvent(QKeyEvent *event) override; + void keyReleaseEvent(QKeyEvent *event) override; bool event(QEvent *event) override; private: diff --git a/widget/src/DpiForms.cpp b/widget/src/DpiForms.cpp index ea01d29..2c974df 100644 --- a/widget/src/DpiForms.cpp +++ b/widget/src/DpiForms.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -86,6 +87,20 @@ void Form::closeEvent(QCloseEvent *event) QWidget::closeEvent(event); } +void Form::keyPressEvent(QKeyEvent *event) +{ + if (onKeyDown) + onKeyDown(event); + QWidget::keyPressEvent(event); +} + +void Form::keyReleaseEvent(QKeyEvent *event) +{ + if (onKeyUp) + onKeyUp(event); + QWidget::keyReleaseEvent(event); +} + bool Form::event(QEvent *event) { if (event->type() == QEvent::WindowActivate && onActivate)