author | Wuzzy <Wuzzy2@mail.ru> |
Wed, 25 Oct 2017 23:09:41 +0200 | |
changeset 12763 | ad67a3804981 |
parent 12698 | f2690a0ccf19 |
child 12897 | fc47fc4af6bd |
permissions | -rw-r--r-- |
8346 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
11046 | 3 |
* Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com> |
8346 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program 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. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
9998
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
8346 | 17 |
*/ |
18 |
||
19 |
#include "keybinder.h" |
|
20 |
#include "HWApplication.h" |
|
21 |
#include "DataManager.h" |
|
22 |
#include <QHBoxLayout> |
|
23 |
#include <QScrollArea> |
|
24 |
#include <QTableWidget> |
|
25 |
#include <QTableWidgetItem> |
|
26 |
#include <QStandardItemModel> |
|
27 |
#include <QAbstractItemModel> |
|
28 |
#include <QListWidget> |
|
29 |
#include <QListWidgetItem> |
|
30 |
#include <QPushButton> |
|
31 |
#include <QHeaderView> |
|
32 |
#include <QComboBox> |
|
33 |
#include <QLabel> |
|
34 |
#include <QFrame> |
|
35 |
#include <QDebug> |
|
36 |
||
37 |
KeyBinder::KeyBinder(QWidget * parent, const QString & helpText, const QString & defaultText, const QString & resetButtonText) : QWidget(parent) |
|
38 |
{ |
|
39 |
this->defaultText = defaultText; |
|
40 |
enableSignal = false; |
|
8434 | 41 |
|
8346 | 42 |
// Two-column tab layout |
43 |
QHBoxLayout * pageKeysLayout = new QHBoxLayout(this); |
|
44 |
pageKeysLayout->setSpacing(0); |
|
45 |
pageKeysLayout->setContentsMargins(0, 0, 0, 0); |
|
8434 | 46 |
|
8346 | 47 |
// Table for category list |
48 |
QVBoxLayout * catListContainer = new QVBoxLayout(); |
|
49 |
catListContainer->setContentsMargins(10, 10, 10, 10); |
|
50 |
catList = new QListWidget(); |
|
51 |
catList->setFixedWidth(180); |
|
52 |
catList->setStyleSheet("QListWidget::item { font-size: 14px; } QListWidget:hover { border-color: #F6CB1C; } QListWidget::item:selected { background: #150A61; color: yellow; }"); |
|
53 |
catList->setFocusPolicy(Qt::NoFocus); |
|
54 |
connect(catList, SIGNAL(currentRowChanged(int)), this, SLOT(changeBindingsPage(int))); |
|
55 |
catListContainer->addWidget(catList); |
|
56 |
pageKeysLayout->addLayout(catListContainer); |
|
57 |
||
58 |
// Reset all binds button |
|
59 |
if (!resetButtonText.isEmpty()) |
|
60 |
{ |
|
61 |
QPushButton * btnResetAll = new QPushButton(resetButtonText); |
|
62 |
catListContainer->addWidget(btnResetAll); |
|
12236
a0ed4caa6d35
Apply minimal padding to all text buttons in frontend
Wuzzy <almikes@aol.com>
parents:
11674
diff
changeset
|
63 |
btnResetAll->setStyleSheet("padding: 5px 10px"); |
8346 | 64 |
btnResetAll->setFixedHeight(40); |
65 |
catListContainer->setStretch(1, 0); |
|
66 |
catListContainer->setSpacing(10); |
|
67 |
connect(btnResetAll, SIGNAL(clicked()), this, SIGNAL(resetAllBinds())); |
|
68 |
} |
|
69 |
||
70 |
// Container for pages of key bindings |
|
71 |
QWidget * bindingsPagesContainer = new QWidget(); |
|
72 |
QVBoxLayout * rightLayout = new QVBoxLayout(bindingsPagesContainer); |
|
73 |
||
74 |
// Scroll area for key bindings |
|
75 |
QScrollArea * scrollArea = new QScrollArea(); |
|
76 |
scrollArea->setContentsMargins(0, 0, 0, 0); |
|
77 |
scrollArea->setWidget(bindingsPagesContainer); |
|
78 |
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); |
|
79 |
scrollArea->setWidgetResizable(true); |
|
80 |
scrollArea->setFrameShape(QFrame::NoFrame); |
|
81 |
scrollArea->setStyleSheet("background: #130F2A;"); |
|
82 |
||
83 |
// Add key binding pages to bindings tab |
|
84 |
pageKeysLayout->addWidget(scrollArea); |
|
85 |
pageKeysLayout->setStretch(1, 1); |
|
86 |
||
87 |
// Custom help text |
|
88 |
QLabel * helpLabel = new QLabel(); |
|
89 |
helpLabel->setText(helpText); |
|
90 |
helpLabel->setStyleSheet("color: #130F2A; background: #F6CB1C; border: solid 4px #F6CB1C; border-radius: 10px; padding: auto 20px;"); |
|
91 |
helpLabel->setFixedHeight(24); |
|
92 |
rightLayout->addWidget(helpLabel, 0, Qt::AlignCenter); |
|
93 |
||
94 |
// Category list and bind table row heights |
|
95 |
const int rowHeight = 20; |
|
96 |
QSize catSize, headerSize; |
|
97 |
catSize.setHeight(36); |
|
98 |
headerSize.setHeight(24); |
|
99 |
||
100 |
// Category list header |
|
101 |
QListWidgetItem * catListHeader = new QListWidgetItem(tr("Category")); |
|
102 |
catListHeader->setSizeHint(headerSize); |
|
103 |
catListHeader->setFlags(Qt::NoItemFlags); |
|
104 |
catListHeader->setForeground(QBrush(QColor("#130F2A"))); |
|
105 |
catListHeader->setBackground(QBrush(QColor("#F6CB1C"))); |
|
106 |
catListHeader->setTextAlignment(Qt::AlignCenter); |
|
107 |
catList->addItem(catListHeader); |
|
108 |
||
109 |
// Populate |
|
110 |
bindingsPages = new QHBoxLayout(); |
|
111 |
bindingsPages->setContentsMargins(0, 0, 0, 0); |
|
112 |
rightLayout->addLayout(bindingsPages); |
|
113 |
QWidget * curPage = NULL; |
|
114 |
QVBoxLayout * curLayout = NULL; |
|
115 |
QTableWidget * curTable = NULL; |
|
116 |
bool bFirstPage = true; |
|
117 |
selectedBindTable = NULL; |
|
118 |
bindComboBoxCellMappings = new QHash<QObject *, QTableWidgetItem *>(); |
|
119 |
bindCellComboBoxMappings = new QHash<QTableWidgetItem *, QComboBox *>(); |
|
120 |
for (int i = 0; i < BINDS_NUMBER; i++) |
|
121 |
{ |
|
122 |
if (cbinds[i].category != NULL) |
|
123 |
{ |
|
124 |
// Add stretch at end of previous layout |
|
125 |
if (curLayout != NULL) curLayout->insertStretch(-1, 1); |
|
8434 | 126 |
|
8346 | 127 |
// Category list item |
128 |
QListWidgetItem * catItem = new QListWidgetItem(HWApplication::translate("binds (categories)", cbinds[i].category)); |
|
129 |
catItem->setSizeHint(catSize); |
|
130 |
catList->addItem(catItem); |
|
8434 | 131 |
|
8346 | 132 |
// Create new page |
133 |
curPage = new QWidget(); |
|
134 |
curLayout = new QVBoxLayout(curPage); |
|
135 |
curLayout->setSpacing(2); |
|
136 |
bindingsPages->addWidget(curPage); |
|
137 |
if (!bFirstPage) curPage->setVisible(false); |
|
138 |
} |
|
139 |
||
140 |
// Description |
|
141 |
if (cbinds[i].description != NULL) |
|
142 |
{ |
|
143 |
QLabel * desc = new QLabel(HWApplication::translate("binds (descriptions)", cbinds[i].description)); |
|
144 |
curLayout->addWidget(desc, 0); |
|
145 |
QFrame * divider = new QFrame(); |
|
146 |
divider->setFrameShape(QFrame::HLine); |
|
147 |
divider->setFrameShadow(QFrame::Plain); |
|
148 |
curLayout->addWidget(divider, 0); |
|
149 |
} |
|
150 |
||
151 |
// New table |
|
152 |
if (cbinds[i].category != NULL || cbinds[i].description != NULL) |
|
153 |
{ |
|
154 |
curTable = new QTableWidget(0, 2); |
|
155 |
curTable->verticalHeader()->setVisible(false); |
|
156 |
curTable->horizontalHeader()->setVisible(false); |
|
157 |
curTable->horizontalHeader()->setResizeMode(QHeaderView::Stretch); |
|
158 |
curTable->verticalHeader()->setDefaultSectionSize(rowHeight); |
|
159 |
curTable->setShowGrid(false); |
|
160 |
curTable->setStyleSheet("QTableWidget { border: none; } "); |
|
161 |
curTable->setSelectionBehavior(QAbstractItemView::SelectRows); |
|
162 |
curTable->setSelectionMode(QAbstractItemView::SingleSelection); |
|
163 |
curTable->setFocusPolicy(Qt::NoFocus); |
|
164 |
connect(curTable, SIGNAL(itemSelectionChanged()), this, SLOT(bindSelectionChanged())); |
|
165 |
connect(curTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(bindCellClicked(QTableWidgetItem *))); |
|
166 |
curLayout->addWidget(curTable, 0); |
|
167 |
} |
|
168 |
||
169 |
// Hidden combo box |
|
170 |
QComboBox * comboBox = CBBind[i] = new QComboBox(curTable); |
|
171 |
comboBox->setModel((QAbstractItemModel*)DataManager::instance().bindsModel()); |
|
172 |
comboBox->setVisible(false); |
|
173 |
comboBox->setFixedWidth(200); |
|
11674
d59b6f289e30
Increase size of many drop-down lists
Wuzzy <almikes@aol.com>
parents:
11046
diff
changeset
|
174 |
comboBox->setMaxVisibleItems(50); |
8434 | 175 |
|
8346 | 176 |
// Table row |
177 |
int row = curTable->rowCount(); |
|
178 |
QTableWidgetItem * nameCell = new QTableWidgetItem(HWApplication::translate("binds", cbinds[i].name)); |
|
179 |
nameCell->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
|
180 |
curTable->insertRow(row); |
|
181 |
curTable->setItem(row, 0, nameCell); |
|
182 |
QTableWidgetItem * bindCell = new QTableWidgetItem(comboBox->currentText()); |
|
183 |
bindCell->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
|
184 |
curTable->setItem(row, 1, bindCell); |
|
185 |
curTable->resizeColumnsToContents(); |
|
186 |
curTable->setFixedHeight(curTable->verticalHeader()->length() + 10); |
|
187 |
||
188 |
// Updates the text in the table cell |
|
189 |
connect(comboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(bindChanged(const QString &))); |
|
190 |
||
191 |
// Map combo box and that row's cells to each other |
|
192 |
bindComboBoxCellMappings->insert(comboBox, bindCell); |
|
193 |
bindCellComboBoxMappings->insert(nameCell, comboBox); |
|
194 |
bindCellComboBoxMappings->insert(bindCell, comboBox); |
|
195 |
} |
|
196 |
||
197 |
// Add stretch at end of last layout |
|
198 |
if (curLayout != NULL) curLayout->insertStretch(-1, 1); |
|
199 |
||
200 |
// Go to first page |
|
201 |
catList->setCurrentItem(catList->item(1)); |
|
202 |
||
203 |
enableSignal = true; |
|
204 |
} |
|
205 |
||
206 |
KeyBinder::~KeyBinder() |
|
207 |
{ |
|
208 |
delete bindComboBoxCellMappings; |
|
209 |
delete bindCellComboBoxMappings; |
|
210 |
} |
|
211 |
||
212 |
// Switches between different pages of key binds |
|
213 |
void KeyBinder::changeBindingsPage(int page) |
|
214 |
{ |
|
215 |
page--; // Disregard first item (the list header) |
|
216 |
int pages = bindingsPages->count(); |
|
217 |
for (int i = 0; i < pages; i++) |
|
218 |
bindingsPages->itemAt(i)->widget()->setVisible(false); |
|
219 |
bindingsPages->itemAt(page)->widget()->setVisible(true); |
|
220 |
} |
|
221 |
||
222 |
// When a key bind combobox value is changed, updates the table cell text |
|
223 |
void KeyBinder::bindChanged(const QString & text) |
|
224 |
{ |
|
225 |
bindComboBoxCellMappings->value(sender())->setText(text); |
|
226 |
||
227 |
if (enableSignal) |
|
228 |
{ |
|
229 |
for (int i = 0; i < BINDS_NUMBER; i++) |
|
230 |
{ |
|
231 |
if (CBBind[i] == sender()) |
|
232 |
{ |
|
233 |
emit bindUpdate(i); |
|
234 |
break; |
|
235 |
} |
|
236 |
} |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
// When a row in a key bind table is clicked, this shows the popup |
|
241 |
void KeyBinder::bindCellClicked(QTableWidgetItem * item) |
|
242 |
{ |
|
243 |
QComboBox * box = bindCellComboBoxMappings->value(item); |
|
244 |
QTableWidget * table = item->tableWidget(); |
|
8434 | 245 |
|
12698
f2690a0ccf19
QTfrontend: Fix flickering and bad offset of control config popup
Wuzzy <almikes@aol.com>
parents:
12236
diff
changeset
|
246 |
box->move( |
f2690a0ccf19
QTfrontend: Fix flickering and bad offset of control config popup
Wuzzy <almikes@aol.com>
parents:
12236
diff
changeset
|
247 |
table->horizontalHeader()->sectionSize(0), |
f2690a0ccf19
QTfrontend: Fix flickering and bad offset of control config popup
Wuzzy <almikes@aol.com>
parents:
12236
diff
changeset
|
248 |
(table->verticalHeader()->defaultSectionSize() * (item->row() + 1)) - (box->height()) + 1 |
f2690a0ccf19
QTfrontend: Fix flickering and bad offset of control config popup
Wuzzy <almikes@aol.com>
parents:
12236
diff
changeset
|
249 |
); |
8346 | 250 |
box->showPopup(); |
251 |
} |
|
252 |
||
253 |
// When a new row in a bind table is *selected*, this clears selection in any other table |
|
254 |
void KeyBinder::bindSelectionChanged() |
|
255 |
{ |
|
256 |
QTableWidget * theSender = (QTableWidget*)sender(); |
|
257 |
if (theSender != selectedBindTable) |
|
258 |
{ |
|
259 |
if (selectedBindTable != NULL) |
|
260 |
selectedBindTable->clearSelection(); |
|
261 |
selectedBindTable = theSender; |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
// Set a combobox's index |
|
266 |
void KeyBinder::setBindIndex(int keyIndex, int bindIndex) |
|
267 |
{ |
|
268 |
enableSignal = false; |
|
269 |
CBBind[keyIndex]->setCurrentIndex(bindIndex); |
|
270 |
enableSignal = true; |
|
271 |
} |
|
272 |
||
273 |
// Return a combobox's selected index |
|
274 |
int KeyBinder::bindIndex(int keyIndex) |
|
275 |
{ |
|
276 |
return CBBind[keyIndex]->currentIndex(); |
|
277 |
} |
|
278 |
||
279 |
// Clears selection and goes to first category |
|
280 |
void KeyBinder::resetInterface() |
|
281 |
{ |
|
282 |
enableSignal = false; |
|
8434 | 283 |
|
8346 | 284 |
catList->setCurrentItem(catList->item(1)); |
285 |
changeBindingsPage(1); |
|
286 |
if (selectedBindTable != NULL) |
|
287 |
{ |
|
288 |
selectedBindTable->clearSelection(); |
|
289 |
selectedBindTable = NULL; |
|
290 |
} |
|
8434 | 291 |
|
8346 | 292 |
// Default bind text |
293 |
DataManager::instance().bindsModel()->item(0)->setData(defaultText, Qt::DisplayRole); |
|
294 |
for (int i = 0; i < BINDS_NUMBER; i++) |
|
295 |
{ |
|
296 |
CBBind[i]->setModel(DataManager::instance().bindsModel()); |
|
297 |
CBBind[i]->setCurrentIndex(0); |
|
298 |
bindComboBoxCellMappings->value(CBBind[i])->setText(defaultText); |
|
299 |
} |
|
300 |
||
301 |
enableSignal = true; |
|
302 |
} |