author | Wuzzy <almikes@aol.com> |
Sun, 01 May 2016 01:04:45 +0200 | |
changeset 11918 | 9da78300c5d0 |
parent 11674 | d59b6f289e30 |
child 12236 | a0ed4caa6d35 |
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); |
|
63 |
btnResetAll->setFixedHeight(40); |
|
64 |
catListContainer->setStretch(1, 0); |
|
65 |
catListContainer->setSpacing(10); |
|
66 |
connect(btnResetAll, SIGNAL(clicked()), this, SIGNAL(resetAllBinds())); |
|
67 |
} |
|
68 |
||
69 |
// Container for pages of key bindings |
|
70 |
QWidget * bindingsPagesContainer = new QWidget(); |
|
71 |
QVBoxLayout * rightLayout = new QVBoxLayout(bindingsPagesContainer); |
|
72 |
||
73 |
// Scroll area for key bindings |
|
74 |
QScrollArea * scrollArea = new QScrollArea(); |
|
75 |
scrollArea->setContentsMargins(0, 0, 0, 0); |
|
76 |
scrollArea->setWidget(bindingsPagesContainer); |
|
77 |
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); |
|
78 |
scrollArea->setWidgetResizable(true); |
|
79 |
scrollArea->setFrameShape(QFrame::NoFrame); |
|
80 |
scrollArea->setStyleSheet("background: #130F2A;"); |
|
81 |
||
82 |
// Add key binding pages to bindings tab |
|
83 |
pageKeysLayout->addWidget(scrollArea); |
|
84 |
pageKeysLayout->setStretch(1, 1); |
|
85 |
||
86 |
// Custom help text |
|
87 |
QLabel * helpLabel = new QLabel(); |
|
88 |
helpLabel->setText(helpText); |
|
89 |
helpLabel->setStyleSheet("color: #130F2A; background: #F6CB1C; border: solid 4px #F6CB1C; border-radius: 10px; padding: auto 20px;"); |
|
90 |
helpLabel->setFixedHeight(24); |
|
91 |
rightLayout->addWidget(helpLabel, 0, Qt::AlignCenter); |
|
92 |
||
93 |
// Category list and bind table row heights |
|
94 |
const int rowHeight = 20; |
|
95 |
QSize catSize, headerSize; |
|
96 |
catSize.setHeight(36); |
|
97 |
headerSize.setHeight(24); |
|
98 |
||
99 |
// Category list header |
|
100 |
QListWidgetItem * catListHeader = new QListWidgetItem(tr("Category")); |
|
101 |
catListHeader->setSizeHint(headerSize); |
|
102 |
catListHeader->setFlags(Qt::NoItemFlags); |
|
103 |
catListHeader->setForeground(QBrush(QColor("#130F2A"))); |
|
104 |
catListHeader->setBackground(QBrush(QColor("#F6CB1C"))); |
|
105 |
catListHeader->setTextAlignment(Qt::AlignCenter); |
|
106 |
catList->addItem(catListHeader); |
|
107 |
||
108 |
// Populate |
|
109 |
bindingsPages = new QHBoxLayout(); |
|
110 |
bindingsPages->setContentsMargins(0, 0, 0, 0); |
|
111 |
rightLayout->addLayout(bindingsPages); |
|
112 |
QWidget * curPage = NULL; |
|
113 |
QVBoxLayout * curLayout = NULL; |
|
114 |
QTableWidget * curTable = NULL; |
|
115 |
bool bFirstPage = true; |
|
116 |
selectedBindTable = NULL; |
|
117 |
bindComboBoxCellMappings = new QHash<QObject *, QTableWidgetItem *>(); |
|
118 |
bindCellComboBoxMappings = new QHash<QTableWidgetItem *, QComboBox *>(); |
|
119 |
for (int i = 0; i < BINDS_NUMBER; i++) |
|
120 |
{ |
|
121 |
if (cbinds[i].category != NULL) |
|
122 |
{ |
|
123 |
// Add stretch at end of previous layout |
|
124 |
if (curLayout != NULL) curLayout->insertStretch(-1, 1); |
|
8434 | 125 |
|
8346 | 126 |
// Category list item |
127 |
QListWidgetItem * catItem = new QListWidgetItem(HWApplication::translate("binds (categories)", cbinds[i].category)); |
|
128 |
catItem->setSizeHint(catSize); |
|
129 |
catList->addItem(catItem); |
|
8434 | 130 |
|
8346 | 131 |
// Create new page |
132 |
curPage = new QWidget(); |
|
133 |
curLayout = new QVBoxLayout(curPage); |
|
134 |
curLayout->setSpacing(2); |
|
135 |
bindingsPages->addWidget(curPage); |
|
136 |
if (!bFirstPage) curPage->setVisible(false); |
|
137 |
} |
|
138 |
||
139 |
// Description |
|
140 |
if (cbinds[i].description != NULL) |
|
141 |
{ |
|
142 |
QLabel * desc = new QLabel(HWApplication::translate("binds (descriptions)", cbinds[i].description)); |
|
143 |
curLayout->addWidget(desc, 0); |
|
144 |
QFrame * divider = new QFrame(); |
|
145 |
divider->setFrameShape(QFrame::HLine); |
|
146 |
divider->setFrameShadow(QFrame::Plain); |
|
147 |
curLayout->addWidget(divider, 0); |
|
148 |
} |
|
149 |
||
150 |
// New table |
|
151 |
if (cbinds[i].category != NULL || cbinds[i].description != NULL) |
|
152 |
{ |
|
153 |
curTable = new QTableWidget(0, 2); |
|
154 |
curTable->verticalHeader()->setVisible(false); |
|
155 |
curTable->horizontalHeader()->setVisible(false); |
|
156 |
curTable->horizontalHeader()->setResizeMode(QHeaderView::Stretch); |
|
157 |
curTable->verticalHeader()->setDefaultSectionSize(rowHeight); |
|
158 |
curTable->setShowGrid(false); |
|
159 |
curTable->setStyleSheet("QTableWidget { border: none; } "); |
|
160 |
curTable->setSelectionBehavior(QAbstractItemView::SelectRows); |
|
161 |
curTable->setSelectionMode(QAbstractItemView::SingleSelection); |
|
162 |
curTable->setFocusPolicy(Qt::NoFocus); |
|
163 |
connect(curTable, SIGNAL(itemSelectionChanged()), this, SLOT(bindSelectionChanged())); |
|
164 |
connect(curTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(bindCellClicked(QTableWidgetItem *))); |
|
165 |
curLayout->addWidget(curTable, 0); |
|
166 |
} |
|
167 |
||
168 |
// Hidden combo box |
|
169 |
QComboBox * comboBox = CBBind[i] = new QComboBox(curTable); |
|
170 |
comboBox->setModel((QAbstractItemModel*)DataManager::instance().bindsModel()); |
|
171 |
comboBox->setVisible(false); |
|
172 |
comboBox->setFixedWidth(200); |
|
11674
d59b6f289e30
Increase size of many drop-down lists
Wuzzy <almikes@aol.com>
parents:
11046
diff
changeset
|
173 |
comboBox->setMaxVisibleItems(50); |
8434 | 174 |
|
8346 | 175 |
// Table row |
176 |
int row = curTable->rowCount(); |
|
177 |
QTableWidgetItem * nameCell = new QTableWidgetItem(HWApplication::translate("binds", cbinds[i].name)); |
|
178 |
nameCell->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
|
179 |
curTable->insertRow(row); |
|
180 |
curTable->setItem(row, 0, nameCell); |
|
181 |
QTableWidgetItem * bindCell = new QTableWidgetItem(comboBox->currentText()); |
|
182 |
bindCell->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
|
183 |
curTable->setItem(row, 1, bindCell); |
|
184 |
curTable->resizeColumnsToContents(); |
|
185 |
curTable->setFixedHeight(curTable->verticalHeader()->length() + 10); |
|
186 |
||
187 |
// Updates the text in the table cell |
|
188 |
connect(comboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(bindChanged(const QString &))); |
|
189 |
||
190 |
// Map combo box and that row's cells to each other |
|
191 |
bindComboBoxCellMappings->insert(comboBox, bindCell); |
|
192 |
bindCellComboBoxMappings->insert(nameCell, comboBox); |
|
193 |
bindCellComboBoxMappings->insert(bindCell, comboBox); |
|
194 |
} |
|
195 |
||
196 |
// Add stretch at end of last layout |
|
197 |
if (curLayout != NULL) curLayout->insertStretch(-1, 1); |
|
198 |
||
199 |
// Go to first page |
|
200 |
catList->setCurrentItem(catList->item(1)); |
|
201 |
||
202 |
enableSignal = true; |
|
203 |
} |
|
204 |
||
205 |
KeyBinder::~KeyBinder() |
|
206 |
{ |
|
207 |
delete bindComboBoxCellMappings; |
|
208 |
delete bindCellComboBoxMappings; |
|
209 |
} |
|
210 |
||
211 |
// Switches between different pages of key binds |
|
212 |
void KeyBinder::changeBindingsPage(int page) |
|
213 |
{ |
|
214 |
page--; // Disregard first item (the list header) |
|
215 |
int pages = bindingsPages->count(); |
|
216 |
for (int i = 0; i < pages; i++) |
|
217 |
bindingsPages->itemAt(i)->widget()->setVisible(false); |
|
218 |
bindingsPages->itemAt(page)->widget()->setVisible(true); |
|
219 |
} |
|
220 |
||
221 |
// When a key bind combobox value is changed, updates the table cell text |
|
222 |
void KeyBinder::bindChanged(const QString & text) |
|
223 |
{ |
|
224 |
bindComboBoxCellMappings->value(sender())->setText(text); |
|
225 |
||
226 |
if (enableSignal) |
|
227 |
{ |
|
228 |
for (int i = 0; i < BINDS_NUMBER; i++) |
|
229 |
{ |
|
230 |
if (CBBind[i] == sender()) |
|
231 |
{ |
|
232 |
emit bindUpdate(i); |
|
233 |
break; |
|
234 |
} |
|
235 |
} |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
// When a row in a key bind table is clicked, this shows the popup |
|
240 |
void KeyBinder::bindCellClicked(QTableWidgetItem * item) |
|
241 |
{ |
|
242 |
QComboBox * box = bindCellComboBoxMappings->value(item); |
|
243 |
QTableWidget * table = item->tableWidget(); |
|
244 |
QFrame * frame = box->findChild<QFrame*>(); |
|
8434 | 245 |
|
8346 | 246 |
box->showPopup(); |
247 |
frame->move( |
|
248 |
frame->x() + table->horizontalHeader()->sectionSize(0), |
|
249 |
frame->y() + (table->verticalHeader()->defaultSectionSize() * item->row()) |
|
250 |
); |
|
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 |
} |