diff --git a/CMakeLists.txt b/CMakeLists.txt index fd23b90..10ec079 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,11 +87,12 @@ if(CEVO_WITH_APP) set(PROJECT_SOURCES main.cpp - mainwindow.cpp - mainwindow.h - mainwindow.ui - Log.cpp - Log.h + game/mainwindow.cpp + game/mainwindow.h + game/mainwindow.ui + game/Log.cpp + game/Log.h + game/Log.ui ${TS_FILES} ) diff --git a/Log.cpp b/game/Log.cpp similarity index 53% rename from Log.cpp rename to game/Log.cpp index 996f952..4d5485c 100644 --- a/Log.cpp +++ b/game/Log.cpp @@ -27,101 +27,76 @@ // knowledge of the CeCILL license and that you accept its terms. #include "Log.h" +#include "ui_Log.h" +#include #include +#include #include #include -#include -#include namespace Cevo { -LogDlg::LogDlg(QWidget *parent) : Dpi::Form(parent) +LogDlg::LogDlg(QWidget *parent) : Dpi::Form(parent), ui(new Ui::LogDlg) { + ui->setupUi(this); 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)); + ui->list->installEventFilter(this); + connect(ui->list, &QPlainTextEdit::customContextMenuRequested, this, [this](const QPoint &pos) { + logPopup->popup(ui->list->mapToGlobal(pos)); }); - auto *layout = new QVBoxLayout(this); - layout->setContentsMargins(0, 0, 0, 0); - layout->setSpacing(0); - layout->addWidget(list); + logPopup = new QMenu(this); + logPopup->addAction(ui->actionInvalid); + logPopup->addAction(ui->actionNegotiation); + logPopup->addAction(ui->actionTime); + logPopup->addSeparator(); - 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")), - }; + 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) { - Dpi::MenuItem *item = logLevels[i]; - item->setRadioItem(true); - item->onClick = [this, logLevels, i] { setMaxLevel(logLevelValue[i], logLevels, i); }; + logLevelGroup->addAction(logLevelActions[i]); + logPopup->addAction(logLevelActions[i]); + connect(logLevelActions[i], &QAction::triggered, this, [this, i] { setMaxLevel(logLevelValue[i], i); }); } - logLevels[0]->setChecked(true); + logPopup->addSeparator(); + logPopup->addAction(ui->actionSlot); + logPopup->addAction(ui->actionClear); - logPopup->add(QString())->action()->setSeparator(true); - - Dpi::MenuItem *mSlot = logPopup->add(tr("Nation <-> Slot")); - mSlot->onClick = [this] { + connect(ui->actionSlot, &QAction::triggered, 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(); }; + }); + connect(ui->actionClear, &QAction::triggered, ui->list, &QPlainTextEdit::clear); } -void LogDlg::setMaxLevel(int value, const std::array &items, int checkedIndex) +LogDlg::~LogDlg() +{ + delete ui; +} + +void LogDlg::setMaxLevel(int value, int checkedIndex) { maxLevel = value; - for (int i = 0; i < 4; ++i) - items[i]->setChecked(i == checkedIndex); // Dpi::MenuItem has no automatic radio grouping + 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 && mInvalid->checked()) || - (level == (1 << 16) + 2 && mTime->checked()) || (level == (1 << 16) + 3 && mNegotiation->checked())) { - if (list->document()->blockCount() == MaxLines) { - QTextCursor cursor(list->document()); + 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); - list->appendPlainText(prefix + ' ' + text); - QScrollBar *vBar = list->verticalScrollBar(); + ui->list->appendPlainText(prefix + ' ' + text); + QScrollBar *vBar = ui->list->verticalScrollBar(); vBar->setValue(vBar->maximum()); repaint(); } @@ -143,7 +118,7 @@ void LogDlg::keyReleaseEvent(QKeyEvent *event) bool LogDlg::eventFilter(QObject *watched, QEvent *event) { - if (watched == list && host) { + 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) diff --git a/Log.h b/game/Log.h similarity index 86% rename from Log.h rename to game/Log.h index f9f3263..4e1c869 100644 --- a/Log.h +++ b/game/Log.h @@ -29,21 +29,28 @@ #pragma once #include "Dpi/DpiForms.h" -#include "Dpi/DpiMenus.h" -#include "Dpi/DpiStdCtrls.h" #include -#include + +class QMenu; + +QT_BEGIN_NAMESPACE +namespace Ui { +class LogDlg; +} +QT_END_NAMESPACE 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. +// right-click popup menu to filter what gets shown. UI built from Log.ui +// (adapted from the original's Log.lfm). class LogDlg : public Dpi::Form { Q_OBJECT public: explicit LogDlg(QWidget *parent = nullptr); + ~LogDlg() override; // Forwards key events so the game's global hotkeys keep working while // the log window (KeyPreview in the original) has focus. @@ -61,13 +68,10 @@ private: int maxLevel = 0; - Dpi::Memo *list; - Dpi::PopupMenu *logPopup; - Dpi::MenuItem *mInvalid; - Dpi::MenuItem *mNegotiation; - Dpi::MenuItem *mTime; + Ui::LogDlg *ui; + QMenu *logPopup; - void setMaxLevel(int value, const std::array &items, int checkedIndex); + void setMaxLevel(int value, int checkedIndex); }; } // namespace Cevo diff --git a/game/Log.ui b/game/Log.ui new file mode 100644 index 0000000..59cc20c --- /dev/null +++ b/game/Log.ui @@ -0,0 +1,146 @@ + + + + LogDlg + + + + 0 + 0 + 339 + 280 + + + + Log + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::CustomContextMenu + + + QPlainTextEdit::NoWrap + + + true + + + + + + + true + + + Invalid Server Calls + + + + + true + + + Negotiation + + + + + true + + + Client Handover + + + + + true + + + true + + + AI Messages Off + + + + + true + + + AI Messages Level 1 + + + + + true + + + AI Messages Level 1+2 + + + + + true + + + All AI Messages + + + + + Nation <-> Slot + + + + + Clear + + + + + diff --git a/mainwindow.cpp b/game/mainwindow.cpp similarity index 100% rename from mainwindow.cpp rename to game/mainwindow.cpp diff --git a/mainwindow.h b/game/mainwindow.h similarity index 100% rename from mainwindow.h rename to game/mainwindow.h diff --git a/mainwindow.ui b/game/mainwindow.ui similarity index 100% rename from mainwindow.ui rename to game/mainwindow.ui diff --git a/main.cpp b/main.cpp index f1b9c6c..f10a4f8 100644 --- a/main.cpp +++ b/main.cpp @@ -26,7 +26,7 @@ // 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 "mainwindow.h" +#include "game/mainwindow.h" #include #include