#include "MessageDialog.h" #include #include #include #include namespace { const int kSpacingPx = 8; } MessageDialog::MessageDialog(const QString& title, const QString& text, QWidget* parent) : ModalDialog(parent) , m_buttonLayout(nullptr) , m_buttonCount(0) { QVBoxLayout* mainLayout = new QVBoxLayout(this); mainLayout->setContentsMargins(kSpacingPx, kSpacingPx, kSpacingPx, kSpacingPx); mainLayout->setSpacing(kSpacingPx); if (!text.isEmpty()) { QLabel* textLabel = new QLabel(text, this); mainLayout->addWidget(textLabel); } // The buttons sit at the right of their row, the side a dialog is confirmed from. m_buttonLayout = new QHBoxLayout(); m_buttonLayout->setSpacing(kSpacingPx); m_buttonLayout->addStretch(); mainLayout->addLayout(m_buttonLayout); // Last, so it becomes the first row (REQ-UI-MODAL-CHROME). No close button: closing // is one of the buttons here, or nothing at all. addHeader(mainLayout, title, false); } int MessageDialog::addButton(const QString& caption) { const int index = m_buttonCount++; QPushButton* button = new QPushButton(caption, this); m_buttonLayout->addWidget(button); connect(button, &QPushButton::clicked, this, [this, index]() { onButtonClicked(index); }); return index; } void MessageDialog::setEscapeButtonIndex(int index) { m_escapeButtonIndex = index; } std::optional MessageDialog::getClickedButtonIndex() const { return m_clickedButtonIndex; } void MessageDialog::reject() { // Escape stands for a button rather than for a dismissal of its own, so the caller // reads one answer whichever way the player gave it (REQ-UI-GAME-MENU). if (!m_escapeButtonIndex.has_value()) { return; } onButtonClicked(*m_escapeButtonIndex); } void MessageDialog::onButtonClicked(int index) { m_clickedButtonIndex = index; accept(); }