author | dag10 |
Fri, 01 Feb 2013 18:07:43 -0500 | |
changeset 8467 | dc53b4b66fa0 |
parent 8453 | 06541556df53 |
child 8488 | e72f3398a28b |
permissions | -rw-r--r-- |
8374 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
|
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 |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
#include <QDialog> |
|
20 |
#include <QGridLayout> |
|
21 |
#include <QHBoxLayout> |
|
22 |
#include <QScrollArea> |
|
23 |
#include <QPushButton> |
|
24 |
#include <QToolButton> |
|
25 |
#include <QWidgetItem> |
|
26 |
#include <QModelIndex> |
|
27 |
#include <QListView> |
|
28 |
#include <QLineEdit> |
|
29 |
#include <QLabel> |
|
30 |
#include <QSortFilterProxyModel> |
|
8453
06541556df53
Reorganized layout and appearance of rooms list page.
dag10
parents:
8434
diff
changeset
|
31 |
#include <QFontMetrics> |
8374 | 32 |
#include <QDebug> |
33 |
||
8375 | 34 |
#include "DataManager.h" |
8374 | 35 |
#include "lineeditcursor.h" |
8375 | 36 |
#include "HatModel.h" |
8374 | 37 |
#include "hatprompt.h" |
38 |
||
39 |
HatPrompt::HatPrompt(int currentIndex, QWidget* parent) : QDialog(parent) |
|
40 |
{ |
|
8434 | 41 |
setModal(true); |
42 |
setWindowFlags(Qt::Sheet); |
|
43 |
setWindowModality(Qt::WindowModal); |
|
44 |
setMinimumSize(550, 430); |
|
45 |
resize(550, 430); |
|
46 |
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
|
8374 | 47 |
|
8434 | 48 |
setStyleSheet("QPushButton { padding: 5px; margin-top: 10px; }"); |
8374 | 49 |
|
8434 | 50 |
// Hat model, and a model for setting a filter |
51 |
HatModel * hatModel = DataManager::instance().hatModel(); |
|
52 |
filterModel = new QSortFilterProxyModel(); |
|
53 |
filterModel->setSourceModel(hatModel); |
|
54 |
filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive); |
|
8374 | 55 |
|
8434 | 56 |
// Grid |
57 |
QGridLayout * dialogLayout = new QGridLayout(this); |
|
58 |
dialogLayout->setSpacing(0); |
|
59 |
dialogLayout->setColumnStretch(1, 1); |
|
8374 | 60 |
|
8434 | 61 |
QHBoxLayout * topLayout = new QHBoxLayout(); |
8374 | 62 |
|
8434 | 63 |
// Help/prompt message at top |
64 |
QLabel * lblDesc = new QLabel(tr("Select a hat")); |
|
65 |
lblDesc->setObjectName("lblDesc"); |
|
8374 | 66 |
lblDesc->setStyleSheet("#lblDesc { color: #130F2A; background: #F6CB1C; border: solid 4px #F6CB1C; border-top-left-radius: 10px; padding: 4px 10px;}"); |
67 |
lblDesc->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
|
68 |
lblDesc->setFixedHeight(24); |
|
69 |
lblDesc->setMinimumWidth(0); |
|
70 |
||
71 |
// Filter text box |
|
72 |
QWidget * filterContainer = new QWidget(); |
|
73 |
filterContainer->setFixedHeight(24); |
|
74 |
filterContainer->setObjectName("filterContainer"); |
|
75 |
filterContainer->setStyleSheet("#filterContainer { background: #F6CB1C; border-top-right-radius: 10px; padding: 3px; }"); |
|
76 |
filterContainer->setFixedWidth(250); |
|
77 |
txtFilter = new LineEditCursor(filterContainer); |
|
78 |
txtFilter->setFixedWidth(250); |
|
79 |
txtFilter->setFocus(); |
|
80 |
txtFilter->setFixedHeight(22); |
|
81 |
connect(txtFilter, SIGNAL(textChanged(const QString &)), this, SLOT(filterChanged(const QString &))); |
|
82 |
connect(txtFilter, SIGNAL(moveUp()), this, SLOT(moveUp())); |
|
83 |
connect(txtFilter, SIGNAL(moveDown()), this, SLOT(moveDown())); |
|
84 |
connect(txtFilter, SIGNAL(moveLeft()), this, SLOT(moveLeft())); |
|
85 |
connect(txtFilter, SIGNAL(moveRight()), this, SLOT(moveRight())); |
|
86 |
||
87 |
// Filter label |
|
88 |
QLabel * lblFilter = new QLabel(tr("Filter: "), txtFilter); |
|
8453
06541556df53
Reorganized layout and appearance of rooms list page.
dag10
parents:
8434
diff
changeset
|
89 |
QFontMetrics lblMetrics(lblFilter->font()); |
06541556df53
Reorganized layout and appearance of rooms list page.
dag10
parents:
8434
diff
changeset
|
90 |
int lblFilterWidth = lblMetrics.width(lblFilter->text()); |
06541556df53
Reorganized layout and appearance of rooms list page.
dag10
parents:
8434
diff
changeset
|
91 |
lblFilter->setStyleSheet(QString("background: none; margin-left: -%1px; margin-top: 4px;").arg(lblFilterWidth + 5)); |
06541556df53
Reorganized layout and appearance of rooms list page.
dag10
parents:
8434
diff
changeset
|
92 |
txtFilter->setStyleSheet(QString("LineEditCursor, QLabel { border-width: 0px; border-radius: 6px; margin-top: 3px; margin-right: 3px; padding-left: %1px; padding-bottom: 2px; background-color: rgb(23, 11, 54); } LineEditCursor:hover, LineEditCursor:focus { background-color: rgb(13, 5, 68); }").arg(lblFilterWidth + 5)); |
8374 | 93 |
|
94 |
// Corner widget |
|
95 |
QLabel * corner = new QLabel(); |
|
96 |
corner->setPixmap(QPixmap(QString::fromUtf8(":/res/inverse-corner-bl.png"))); |
|
97 |
corner->setFixedSize(10, 10); |
|
98 |
||
99 |
// Add widgets to top layout |
|
100 |
topLayout->addWidget(lblDesc); |
|
101 |
topLayout->addWidget(filterContainer); |
|
102 |
topLayout->addWidget(corner, 0, Qt::AlignBottom); |
|
103 |
topLayout->addStretch(1); |
|
104 |
||
8434 | 105 |
// Cancel button (closes dialog) |
106 |
QPushButton * btnCancel = new QPushButton(tr("Cancel")); |
|
107 |
connect(btnCancel, SIGNAL(clicked()), this, SLOT(reject())); |
|
8374 | 108 |
|
8434 | 109 |
// Select button |
110 |
QPushButton * btnSelect = new QPushButton(tr("Use selected hat")); |
|
111 |
btnSelect->setDefault(true); |
|
112 |
connect(btnSelect, SIGNAL(clicked()), this, SLOT(onAccepted())); |
|
8374 | 113 |
|
8434 | 114 |
// Add hats |
115 |
list = new HatListView(); |
|
116 |
list->setModel(filterModel); |
|
117 |
list->setViewMode(QListView::IconMode); |
|
118 |
list->setResizeMode(QListView::Adjust); |
|
119 |
list->setMovement(QListView::Static); |
|
120 |
list->setEditTriggers(QAbstractItemView::NoEditTriggers); |
|
121 |
list->setSpacing(8); |
|
122 |
list->setWordWrap(true); |
|
123 |
list->setSelectionMode(QAbstractItemView::SingleSelection); |
|
124 |
list->setObjectName("hatList"); |
|
125 |
list->setCurrentIndex(filterModel->index(currentIndex, 0)); |
|
126 |
connect(list, SIGNAL(activated(const QModelIndex &)), this, SLOT(hatChosen(const QModelIndex &))); |
|
127 |
connect(list, SIGNAL(clicked(const QModelIndex &)), this, SLOT(hatChosen(const QModelIndex &))); |
|
8374 | 128 |
|
8434 | 129 |
// Add elements to layouts |
130 |
dialogLayout->addLayout(topLayout, 0, 0, 1, 3); |
|
131 |
dialogLayout->addWidget(list, 1, 0, 1, 3); |
|
132 |
dialogLayout->addWidget(btnCancel, 2, 0, 1, 1, Qt::AlignLeft); |
|
133 |
dialogLayout->addWidget(btnSelect, 2, 2, 1, 1, Qt::AlignRight); |
|
8374 | 134 |
} |
135 |
||
136 |
void HatPrompt::moveUp() |
|
137 |
{ |
|
8434 | 138 |
list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveUp, Qt::NoModifier)); |
8374 | 139 |
} |
140 |
||
141 |
void HatPrompt::moveDown() |
|
142 |
{ |
|
8434 | 143 |
list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier)); |
8374 | 144 |
} |
145 |
||
146 |
void HatPrompt::moveLeft() |
|
147 |
{ |
|
8434 | 148 |
list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveLeft, Qt::NoModifier)); |
8374 | 149 |
} |
150 |
||
151 |
void HatPrompt::moveRight() |
|
152 |
{ |
|
8434 | 153 |
list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveRight, Qt::NoModifier)); |
8374 | 154 |
} |
155 |
||
156 |
void HatPrompt::onAccepted() |
|
157 |
{ |
|
8434 | 158 |
hatChosen(list->currentIndex()); |
8374 | 159 |
} |
160 |
||
161 |
// When a hat is selected |
|
162 |
void HatPrompt::hatChosen(const QModelIndex & index) |
|
163 |
{ |
|
8434 | 164 |
done(filterModel->mapToSource(index).row() + 1); // Since returning 0 means canceled |
8374 | 165 |
} |
166 |
||
167 |
// When the text in the filter text box is changed |
|
168 |
void HatPrompt::filterChanged(const QString & text) |
|
169 |
{ |
|
8434 | 170 |
filterModel->setFilterFixedString(text); |
171 |
list->setCurrentIndex(filterModel->index(0, 0)); |
|
8375 | 172 |
} |