2
0

PlayerSelectionDialog.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "../lib/mapping/CMap.h"
  12. #include "mainwindow.h"
  13. #include <QRadioButton>
  14. #include <QButtonGroup>
  15. #include <QDialogButtonBox>
  16. #include <QAction>
  17. #include <QLabel>
  18. PlayerSelectionDialog::PlayerSelectionDialog(MainWindow * mainWindow)
  19. : QDialog(mainWindow), selectedPlayer(PlayerColor::NEUTRAL)
  20. {
  21. setupDialogComponents();
  22. int maxPlayers = 0;
  23. if(mainWindow && mainWindow->controller.map())
  24. maxPlayers = mainWindow->controller.map()->players.size();
  25. for(int i = 0; i < maxPlayers; ++i)
  26. {
  27. PlayerColor player(i);
  28. addRadioButton(mainWindow->getActionPlayer(player), player);
  29. }
  30. }
  31. PlayerColor PlayerSelectionDialog::getSelectedPlayer() const
  32. {
  33. return selectedPlayer;
  34. }
  35. void PlayerSelectionDialog::setupDialogComponents()
  36. {
  37. setWindowTitle(tr("Select Player"));
  38. setFixedWidth(dialogWidth);
  39. setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
  40. font.setPointSize(10);
  41. setFont(font);
  42. buttonGroup = new QButtonGroup(this);
  43. buttonGroup->setExclusive(true);
  44. QLabel * errorLabel = new QLabel(tr("Hero cannot be created as NEUTRAL"), this);
  45. font.setBold(true);
  46. errorLabel->setFont(font);
  47. errorLabel->setWordWrap(true);
  48. mainLayout.addWidget(errorLabel);
  49. QLabel * instructionLabel = new QLabel(tr("Switch to one of the available players:"), this);
  50. font.setBold(false);
  51. instructionLabel->setFont(font);
  52. instructionLabel->setWordWrap(true);
  53. mainLayout.addWidget(instructionLabel);
  54. QWidget * radioContainer = new QWidget(this);
  55. radioContainer->setLayout(& radioButtonsLayout);
  56. mainLayout.addWidget(radioContainer);
  57. QDialogButtonBox * box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
  58. connect(box, &QDialogButtonBox::accepted, this, &QDialog::accept);
  59. connect(box, &QDialogButtonBox::rejected, this, &QDialog::reject);
  60. mainLayout.addWidget(box);
  61. setLayout(& mainLayout);
  62. }
  63. void PlayerSelectionDialog::addRadioButton(QAction * action, PlayerColor player)
  64. {
  65. auto * radioButton = new QRadioButton(action->text(), this);
  66. radioButton->setEnabled(action->isEnabled());
  67. // Select first available player by default
  68. if(buttonGroup->buttons().isEmpty() && radioButton->isEnabled())
  69. {
  70. radioButton->setChecked(true);
  71. selectedPlayer = player;
  72. }
  73. radioButton->setToolTip(tr("Shortcut: %1").arg(action->shortcut().toString()));
  74. buttonGroup->addButton(radioButton, player.getNum());
  75. radioButtonsLayout.addWidget(radioButton);
  76. connect(radioButton, &QRadioButton::clicked, this, [this, player]()
  77. {
  78. selectedPlayer = player;
  79. });
  80. addAction(action);
  81. connect(action, &QAction::triggered, this, [radioButton]()
  82. {
  83. if(radioButton->isEnabled())
  84. radioButton->click();
  85. });
  86. }