PlayerSelectionDialog.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * PlayerSelectionDialog.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "PlayerSelectionDialog.h"
  11. #include "mainwindow.h"
  12. #include "../lib/mapping/CMap.h"
  13. PlayerSelectionDialog::PlayerSelectionDialog(QWidget * parent)
  14. : QDialog(parent), selectedPlayer(PlayerColor::NEUTRAL)
  15. {
  16. auto main = qobject_cast<MainWindow *>(parent);
  17. assert(main);
  18. setupDialogComponents();
  19. int maxPlayers = 0;
  20. if(main && main->controller.map())
  21. maxPlayers = main->controller.map()->players.size();
  22. bool defaultIsChecked = false;
  23. for(int i = 0; i < maxPlayers; ++i)
  24. {
  25. PlayerColor player(i);
  26. QAction * action = main->getActionPlayer(player);
  27. addCheckbox(action, player, main->controller.map()->players.at(i).canAnyonePlay(), & defaultIsChecked);
  28. }
  29. }
  30. void PlayerSelectionDialog::onCheckboxToggled(bool checked)
  31. {
  32. if(!checked)
  33. return;
  34. QCheckBox * senderCheckBox = qobject_cast<QCheckBox *>(sender());
  35. if(!senderCheckBox)
  36. return;
  37. for(int i = 0; i < checkboxes.size(); ++i)
  38. {
  39. QCheckBox * cb = checkboxes[i];
  40. if(cb == senderCheckBox)
  41. {
  42. selectedPlayer = PlayerColor(i);
  43. }
  44. else
  45. {
  46. cb->blockSignals(true);
  47. cb->setChecked(false);
  48. cb->blockSignals(false);
  49. }
  50. }
  51. }
  52. PlayerColor PlayerSelectionDialog::getSelectedPlayer() const
  53. {
  54. return selectedPlayer;
  55. }
  56. void PlayerSelectionDialog::setupDialogComponents()
  57. {
  58. setWindowTitle(tr("Select Player"));
  59. setFixedSize(sizeHint());
  60. setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
  61. font.setPointSize(10);
  62. setFont(font);
  63. mainLayout.setSpacing(10);
  64. mainLayout.setContentsMargins(20, 20, 80, 20);
  65. checkboxLayout.setContentsMargins(0, 10, 0, 20);
  66. mainLayout.addSpacing(4);
  67. QLabel * errorLabel = new QLabel(tr("Hero cannot be created as NEUTRAL"), this);
  68. font.setBold(true);
  69. errorLabel->setFont(font);
  70. mainLayout.addWidget(errorLabel);
  71. QLabel * instructionLabel = new QLabel(tr("Switch to one of the available players:"), this);
  72. font.setBold(false);
  73. instructionLabel->setFont(font);
  74. mainLayout.addWidget(instructionLabel);
  75. QWidget * checkboxContainer = new QWidget(this);
  76. checkboxContainer->setLayout(& checkboxLayout);
  77. mainLayout.addWidget(checkboxContainer);
  78. QDialogButtonBox * box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
  79. connect(box, &QDialogButtonBox::accepted, this, &QDialog::accept);
  80. connect(box, &QDialogButtonBox::rejected, this, &QDialog::reject);
  81. mainLayout.addWidget(box);
  82. setLayout(& mainLayout);
  83. }
  84. void PlayerSelectionDialog::addCheckbox(QAction * checkboxAction, PlayerColor player, bool isEnabled, bool * isDefault)
  85. {
  86. QHBoxLayout * rowLayout = new QHBoxLayout();
  87. auto * checkbox = new QCheckBox(checkboxAction->text(), this);
  88. QLabel * shortcutLabel = new QLabel(checkboxAction->shortcut().toString(), this);
  89. QFont shortcutFont = font;
  90. shortcutFont.setPointSize(9);
  91. shortcutFont.setItalic(true);
  92. shortcutLabel->setFont(shortcutFont);
  93. shortcutLabel->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
  94. QWidget * checkboxContainer = new QWidget(this);
  95. QHBoxLayout * cbLayout = new QHBoxLayout(checkboxContainer);
  96. cbLayout->setContentsMargins(0, 0, 0, 0);
  97. cbLayout->addWidget(checkbox, 0, Qt::AlignCenter);
  98. rowLayout->addWidget(checkboxContainer, 1);
  99. rowLayout->addWidget(shortcutLabel, 1);
  100. checkbox->setEnabled(isEnabled);
  101. if(checkbox->isEnabled() && !*isDefault)
  102. {
  103. checkbox->setChecked(true);
  104. selectedPlayer = player;
  105. *isDefault = true;
  106. }
  107. checkboxLayout.addLayout(rowLayout);
  108. connect(checkbox, &QCheckBox::clicked, this, [=]()
  109. {
  110. selectedPlayer = player;
  111. // Radio-style logic: uncheck other boxes
  112. for(auto * box : findChildren<QCheckBox *>())
  113. {
  114. if(box != checkbox)
  115. box->setChecked(false);
  116. }
  117. });
  118. // Add action to the dialog for shortcut support
  119. addAction(checkboxAction);
  120. // Connect action trigger to simulate checkbox click
  121. connect(checkboxAction, &QAction::triggered, this, [=]()
  122. {
  123. if(checkbox->isEnabled())
  124. checkbox->click();
  125. });
  126. }