Use shared SVG renderers.
[sloppygui.git] / src / promotiondlg.cpp
blob02ae9d6e2c3b4fbe8732fef970a18e9b4fd6e1a6
1 /*
2 This file is part of SloppyGUI.
4 SloppyGUI is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 SloppyGUI is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with SloppyGUI. If not, see <http://www.gnu.org/licenses/>.
18 #include <QtGui>
20 #include "promotiondlg.h"
21 #include "symbols.h"
23 PromotionDialog::PromotionDialog(QWidget* parent, Qt::WindowFlags f)
24 : QDialog(parent, f)
26 setWindowTitle(tr("Promote"));
28 // Radio buttons that will control the type of the promotion
29 m_queenRadio = new QRadioButton(tr("Queen"));
30 m_knightRadio = new QRadioButton(tr("Knight"));
31 m_rookRadio = new QRadioButton(tr("Rook"));
32 m_bishopRadio = new QRadioButton(tr("Bishop"));
34 // Assign Queen as the default promotion type
35 m_queenRadio->setChecked(true);
36 m_promotionType = Chessboard::Queen;
38 QLabel* promoteToLabel = new QLabel(tr("Promote to:"));
40 // Labels that will show the Unicode chess symbol graphics
41 QLabel* queenLabel = new QLabel(Symbols::BlackQueen);
42 QLabel* knightLabel = new QLabel(Symbols::BlackKnight);
43 QLabel* rookLabel = new QLabel(Symbols::BlackRook);
44 QLabel* bishopLabel = new QLabel(Symbols::BlackBishop);
46 // Double the original point size of the symbol labels
47 // so that they're more visible
48 QFont labelFont = queenLabel->font();
49 labelFont.setPointSize(2 * labelFont.pointSize());
50 queenLabel->setFont(labelFont);
51 knightLabel->setFont(labelFont);
52 rookLabel->setFont(labelFont);
53 bishopLabel->setFont(labelFont);
55 // Promote and Cancel buttons at the bottom
56 QPushButton* promoteButton = new QPushButton(tr("Promote"));
57 QPushButton* cancelButton = new QPushButton(tr("Cancel"));
58 QDialogButtonBox* dlgButtonBox = new QDialogButtonBox(Qt::Horizontal);
59 dlgButtonBox->addButton(promoteButton, QDialogButtonBox::AcceptRole);
60 dlgButtonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
63 * The layout consist of two layouts put together; a vertical
64 * box layout and a grid layout inside of it. The grid layout
65 * is the more complex one so we discuss it here.
67 * The grid layout hold the chess symbol labels and the radio
68 * buttons. It also holds an extra padding so that the look
69 * of the dialog is more professional.
71 * The base layout of the grid is:
73 * +---------------------------------------+
74 * | <extra space> | Symbol | Radio button |
75 * +---------------------------------------+
77 * The extra space is done with setColumnMinimumWidth() and proper
78 * resizing with setColumnStretch() so that the radio button
79 * cell will expand to fill the empty space.
81 QVBoxLayout* layout = new QVBoxLayout();
82 QGridLayout* innerLayout = new QGridLayout();
83 this->setLayout(layout);
85 layout->addWidget(promoteToLabel);
87 layout->addLayout(innerLayout);
89 innerLayout->addWidget(queenLabel, 0, 1);
90 innerLayout->addWidget(m_queenRadio, 0, 2);
92 innerLayout->addWidget(knightLabel, 1, 1);
93 innerLayout->addWidget(m_knightRadio, 1, 2);
95 innerLayout->addWidget(rookLabel, 2, 1);
96 innerLayout->addWidget(m_rookRadio, 2, 2);
98 innerLayout->addWidget(bishopLabel, 3, 1);
99 innerLayout->addWidget(m_bishopRadio, 3, 2);
101 innerLayout->setColumnMinimumWidth(0, 20);
102 innerLayout->setColumnStretch(2, 1);
104 layout->addStretch(1);
105 layout->addWidget(dlgButtonBox);
107 // Connect the signals
108 connect(promoteButton, SIGNAL(clicked(bool)), this, SLOT(accept()));
109 connect(cancelButton, SIGNAL(clicked(bool)), this, SLOT(reject()));
111 // Use signal mapper to map all radio button signals to one
112 // one slot: selectPromotionType()
113 m_signalMapper = new QSignalMapper(this);
115 connect(m_queenRadio, SIGNAL(clicked(bool)), m_signalMapper, SLOT(map()));
116 m_signalMapper->setMapping(m_queenRadio, Chessboard::Queen);
118 connect(m_knightRadio, SIGNAL(clicked(bool)), m_signalMapper, SLOT(map()));
119 m_signalMapper->setMapping(m_knightRadio, Chessboard::Knight);
121 connect(m_rookRadio, SIGNAL(clicked(bool)), m_signalMapper, SLOT(map()));
122 m_signalMapper->setMapping(m_rookRadio, Chessboard::Rook);
124 connect(m_bishopRadio, SIGNAL(clicked(bool)), m_signalMapper, SLOT(map()));
125 m_signalMapper->setMapping(m_bishopRadio, Chessboard::Bishop);
127 connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(selectPromotionType(int)));
130 void PromotionDialog::selectPromotionType(int type)
132 m_promotionType = Chessboard::ChessPiece(type);
135 Chessboard::ChessPiece PromotionDialog::promotionType() const
137 return m_promotionType;