author | alfadur |
Wed, 28 Nov 2018 21:35:54 +0300 | |
changeset 14340 | 97be4f6bf264 |
parent 14223 | 925e2c9877a8 |
child 14851 | 75b515a64202 |
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); |
|
12241
a0ed4caa6d35
Apply minimal padding to all text buttons in frontend
Wuzzy <almikes@aol.com>
parents:
11679
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); |
|
12902
fc47fc4af6bd
Finish porting. Seems to work, but no thorough testing has been performed
unc0rr
parents:
12703
diff
changeset
|
157 |
curTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
8346 | 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); |
|
14223
925e2c9877a8
Increase width for key binding drop-down list
Wuzzy <Wuzzy2@mail.ru>
parents:
13314
diff
changeset
|
173 |
comboBox->setMinimumWidth(400); |
11679
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()); |
|
13314
2bdae461ae5f
Show simple drop-down icon next to key binder widget
Wuzzy <Wuzzy2@mail.ru>
parents:
12902
diff
changeset
|
183 |
QIcon dropDownIcon = QIcon(); |
2bdae461ae5f
Show simple drop-down icon next to key binder widget
Wuzzy <Wuzzy2@mail.ru>
parents:
12902
diff
changeset
|
184 |
QPixmap dd1 = QPixmap(":/res/dropdown.png"); |
2bdae461ae5f
Show simple drop-down icon next to key binder widget
Wuzzy <Wuzzy2@mail.ru>
parents:
12902
diff
changeset
|
185 |
QPixmap dd2 = QPixmap(":/res/dropdown_selected.png"); |
2bdae461ae5f
Show simple drop-down icon next to key binder widget
Wuzzy <Wuzzy2@mail.ru>
parents:
12902
diff
changeset
|
186 |
dropDownIcon.addPixmap(dd1, QIcon::Normal); |
2bdae461ae5f
Show simple drop-down icon next to key binder widget
Wuzzy <Wuzzy2@mail.ru>
parents:
12902
diff
changeset
|
187 |
dropDownIcon.addPixmap(dd2, QIcon::Selected); |
2bdae461ae5f
Show simple drop-down icon next to key binder widget
Wuzzy <Wuzzy2@mail.ru>
parents:
12902
diff
changeset
|
188 |
bindCell->setIcon(dropDownIcon); |
8346 | 189 |
bindCell->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
190 |
curTable->setItem(row, 1, bindCell); |
|
191 |
curTable->resizeColumnsToContents(); |
|
192 |
curTable->setFixedHeight(curTable->verticalHeader()->length() + 10); |
|
193 |
||
194 |
// Updates the text in the table cell |
|
195 |
connect(comboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(bindChanged(const QString &))); |
|
196 |
||
197 |
// Map combo box and that row's cells to each other |
|
198 |
bindComboBoxCellMappings->insert(comboBox, bindCell); |
|
199 |
bindCellComboBoxMappings->insert(nameCell, comboBox); |
|
200 |
bindCellComboBoxMappings->insert(bindCell, comboBox); |
|
201 |
} |
|
202 |
||
203 |
// Add stretch at end of last layout |
|
204 |
if (curLayout != NULL) curLayout->insertStretch(-1, 1); |
|
205 |
||
206 |
// Go to first page |
|
207 |
catList->setCurrentItem(catList->item(1)); |
|
208 |
||
209 |
enableSignal = true; |
|
210 |
} |
|
211 |
||
212 |
KeyBinder::~KeyBinder() |
|
213 |
{ |
|
214 |
delete bindComboBoxCellMappings; |
|
215 |
delete bindCellComboBoxMappings; |
|
216 |
} |
|
217 |
||
218 |
// Switches between different pages of key binds |
|
219 |
void KeyBinder::changeBindingsPage(int page) |
|
220 |
{ |
|
221 |
page--; // Disregard first item (the list header) |
|
222 |
int pages = bindingsPages->count(); |
|
223 |
for (int i = 0; i < pages; i++) |
|
224 |
bindingsPages->itemAt(i)->widget()->setVisible(false); |
|
225 |
bindingsPages->itemAt(page)->widget()->setVisible(true); |
|
226 |
} |
|
227 |
||
228 |
// When a key bind combobox value is changed, updates the table cell text |
|
229 |
void KeyBinder::bindChanged(const QString & text) |
|
230 |
{ |
|
231 |
bindComboBoxCellMappings->value(sender())->setText(text); |
|
232 |
||
233 |
if (enableSignal) |
|
234 |
{ |
|
235 |
for (int i = 0; i < BINDS_NUMBER; i++) |
|
236 |
{ |
|
237 |
if (CBBind[i] == sender()) |
|
238 |
{ |
|
239 |
emit bindUpdate(i); |
|
240 |
break; |
|
241 |
} |
|
242 |
} |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
// When a row in a key bind table is clicked, this shows the popup |
|
247 |
void KeyBinder::bindCellClicked(QTableWidgetItem * item) |
|
248 |
{ |
|
249 |
QComboBox * box = bindCellComboBoxMappings->value(item); |
|
250 |
QTableWidget * table = item->tableWidget(); |
|
8434 | 251 |
|
12703
f2690a0ccf19
QTfrontend: Fix flickering and bad offset of control config popup
Wuzzy <almikes@aol.com>
parents:
12241
diff
changeset
|
252 |
box->move( |
f2690a0ccf19
QTfrontend: Fix flickering and bad offset of control config popup
Wuzzy <almikes@aol.com>
parents:
12241
diff
changeset
|
253 |
table->horizontalHeader()->sectionSize(0), |
f2690a0ccf19
QTfrontend: Fix flickering and bad offset of control config popup
Wuzzy <almikes@aol.com>
parents:
12241
diff
changeset
|
254 |
(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:
12241
diff
changeset
|
255 |
); |
8346 | 256 |
box->showPopup(); |
257 |
} |
|
258 |
||
259 |
// When a new row in a bind table is *selected*, this clears selection in any other table |
|
260 |
void KeyBinder::bindSelectionChanged() |
|
261 |
{ |
|
262 |
QTableWidget * theSender = (QTableWidget*)sender(); |
|
263 |
if (theSender != selectedBindTable) |
|
264 |
{ |
|
265 |
if (selectedBindTable != NULL) |
|
266 |
selectedBindTable->clearSelection(); |
|
267 |
selectedBindTable = theSender; |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
// Set a combobox's index |
|
272 |
void KeyBinder::setBindIndex(int keyIndex, int bindIndex) |
|
273 |
{ |
|
274 |
enableSignal = false; |
|
275 |
CBBind[keyIndex]->setCurrentIndex(bindIndex); |
|
276 |
enableSignal = true; |
|
277 |
} |
|
278 |
||
279 |
// Return a combobox's selected index |
|
280 |
int KeyBinder::bindIndex(int keyIndex) |
|
281 |
{ |
|
282 |
return CBBind[keyIndex]->currentIndex(); |
|
283 |
} |
|
284 |
||
285 |
// Clears selection and goes to first category |
|
286 |
void KeyBinder::resetInterface() |
|
287 |
{ |
|
288 |
enableSignal = false; |
|
8434 | 289 |
|
8346 | 290 |
catList->setCurrentItem(catList->item(1)); |
291 |
changeBindingsPage(1); |
|
292 |
if (selectedBindTable != NULL) |
|
293 |
{ |
|
294 |
selectedBindTable->clearSelection(); |
|
295 |
selectedBindTable = NULL; |
|
296 |
} |
|
8434 | 297 |
|
8346 | 298 |
// Default bind text |
299 |
DataManager::instance().bindsModel()->item(0)->setData(defaultText, Qt::DisplayRole); |
|
300 |
for (int i = 0; i < BINDS_NUMBER; i++) |
|
301 |
{ |
|
302 |
CBBind[i]->setModel(DataManager::instance().bindsModel()); |
|
303 |
CBBind[i]->setCurrentIndex(0); |
|
304 |
bindComboBoxCellMappings->value(CBBind[i])->setText(defaultText); |
|
305 |
} |
|
306 |
||
307 |
enableSignal = true; |
|
308 |
} |