QTfrontend/ui/widget/hatprompt.cpp
author dag10 <gottlieb.drew@gmail.com>
Mon, 14 Jan 2013 16:04:05 -0500
changeset 8389 6d65ed1f832c
parent 8375 af2d2f56bc45
child 8434 4821897a0f10
permissions -rw-r--r--
Fixed feedback submission by correcting Content-Type.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8374
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
     1
/*
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
     3
 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
     4
 *
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
     8
 *
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    12
 * GNU General Public License for more details.
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    13
 *
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    17
 */
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    18
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    19
#include <QDialog>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    20
#include <QGridLayout>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    21
#include <QHBoxLayout>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    22
#include <QScrollArea>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    23
#include <QPushButton>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    24
#include <QToolButton>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    25
#include <QWidgetItem>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    26
#include <QModelIndex>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    27
#include <QListView>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    28
#include <QLineEdit>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    29
#include <QLabel>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    30
#include <QSortFilterProxyModel>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    31
#include <QDebug>
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    32
8375
af2d2f56bc45 Fix build
unc0rr
parents: 8374
diff changeset
    33
#include "DataManager.h"
8374
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    34
#include "lineeditcursor.h"
8375
af2d2f56bc45 Fix build
unc0rr
parents: 8374
diff changeset
    35
#include "HatModel.h"
8374
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    36
#include "hatprompt.h"
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    37
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    38
HatPrompt::HatPrompt(int currentIndex, QWidget* parent) : QDialog(parent)
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    39
{
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    40
	setModal(true);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    41
	setWindowFlags(Qt::Sheet);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    42
	setWindowModality(Qt::WindowModal);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    43
	setMinimumSize(550, 430);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    44
	resize(550, 430);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    45
	setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    46
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    47
	setStyleSheet("QPushButton { padding: 5px; margin-top: 10px; }");
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    48
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    49
	// Hat model, and a model for setting a filter
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    50
	HatModel * hatModel = DataManager::instance().hatModel();
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    51
	filterModel = new QSortFilterProxyModel();
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    52
	filterModel->setSourceModel(hatModel);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    53
	filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    54
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    55
	// Grid
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    56
	QGridLayout * dialogLayout = new QGridLayout(this);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    57
	dialogLayout->setSpacing(0);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    58
	dialogLayout->setColumnStretch(1, 1);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    59
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    60
	QHBoxLayout * topLayout = new QHBoxLayout();
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    61
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    62
	// Help/prompt message at top
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    63
	QLabel * lblDesc = new QLabel(tr("Select a hat"));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    64
	lblDesc->setObjectName("lblDesc");
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    65
    lblDesc->setStyleSheet("#lblDesc { color: #130F2A; background: #F6CB1C; border: solid 4px #F6CB1C; border-top-left-radius: 10px; padding: 4px 10px;}");
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    66
    lblDesc->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    67
    lblDesc->setFixedHeight(24);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    68
    lblDesc->setMinimumWidth(0);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    69
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    70
    // Filter text box
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    71
    QWidget * filterContainer = new QWidget();
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    72
    filterContainer->setFixedHeight(24);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    73
    filterContainer->setObjectName("filterContainer");
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    74
    filterContainer->setStyleSheet("#filterContainer { background: #F6CB1C; border-top-right-radius: 10px; padding: 3px; }");
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    75
    filterContainer->setFixedWidth(250);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    76
    txtFilter = new LineEditCursor(filterContainer);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    77
    txtFilter->setFixedWidth(250);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    78
    txtFilter->setFocus();
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    79
    txtFilter->setFixedHeight(22);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    80
    connect(txtFilter, SIGNAL(textChanged(const QString &)), this, SLOT(filterChanged(const QString &)));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    81
    connect(txtFilter, SIGNAL(moveUp()), this, SLOT(moveUp()));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    82
    connect(txtFilter, SIGNAL(moveDown()), this, SLOT(moveDown()));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    83
    connect(txtFilter, SIGNAL(moveLeft()), this, SLOT(moveLeft()));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    84
    connect(txtFilter, SIGNAL(moveRight()), this, SLOT(moveRight()));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    85
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    86
    // Filter label
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    87
    QLabel * lblFilter = new QLabel(tr("Filter: "), txtFilter);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    88
    lblFilter->setStyleSheet(QString("background: none; margin-left: -%1px; margin-top: 4px;").arg(lblFilter->width() / 2 - 3));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    89
    txtFilter->setStyleSheet(QString("border-width: 0px; background-color: rgb(13, 5, 68); border-radius: 6px; margin-top: 3px; margin-right: 3px; padding-left: %1px; padding-bottom: 2px;").arg(lblFilter->width() / 2));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    90
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    91
    // Corner widget
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    92
    QLabel * corner = new QLabel();
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    93
    corner->setPixmap(QPixmap(QString::fromUtf8(":/res/inverse-corner-bl.png")));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    94
    corner->setFixedSize(10, 10);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    95
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    96
    // Add widgets to top layout
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    97
    topLayout->addWidget(lblDesc);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    98
    topLayout->addWidget(filterContainer);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
    99
    topLayout->addWidget(corner, 0, Qt::AlignBottom);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   100
    topLayout->addStretch(1);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   101
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   102
	// Cancel button (closes dialog)
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   103
	QPushButton * btnCancel = new QPushButton(tr("Cancel"));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   104
	connect(btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   105
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   106
	// Select button
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   107
	QPushButton * btnSelect = new QPushButton(tr("Use selected hat"));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   108
	btnSelect->setDefault(true);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   109
	connect(btnSelect, SIGNAL(clicked()), this, SLOT(onAccepted()));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   110
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   111
	// Add hats
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   112
	list = new HatListView();
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   113
	list->setModel(filterModel);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   114
	list->setViewMode(QListView::IconMode);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   115
	list->setResizeMode(QListView::Adjust);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   116
	list->setMovement(QListView::Static);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   117
	list->setEditTriggers(QAbstractItemView::NoEditTriggers);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   118
	list->setSpacing(8);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   119
	list->setWordWrap(true);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   120
	list->setSelectionMode(QAbstractItemView::SingleSelection);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   121
	list->setObjectName("hatList");
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   122
	list->setCurrentIndex(filterModel->index(currentIndex, 0));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   123
	connect(list, SIGNAL(activated(const QModelIndex &)), this, SLOT(hatChosen(const QModelIndex &)));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   124
	connect(list, SIGNAL(clicked(const QModelIndex &)), this, SLOT(hatChosen(const QModelIndex &)));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   125
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   126
	// Add elements to layouts
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   127
	dialogLayout->addLayout(topLayout, 0, 0, 1, 3);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   128
	dialogLayout->addWidget(list, 1, 0, 1, 3);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   129
	dialogLayout->addWidget(btnCancel, 2, 0, 1, 1, Qt::AlignLeft);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   130
	dialogLayout->addWidget(btnSelect, 2, 2, 1, 1, Qt::AlignRight);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   131
}
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   132
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   133
void HatPrompt::moveUp()
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   134
{
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   135
	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveUp, Qt::NoModifier));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   136
}
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   137
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   138
void HatPrompt::moveDown()
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   139
{
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   140
	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   141
}
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   142
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   143
void HatPrompt::moveLeft()
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   144
{
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   145
	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveLeft, Qt::NoModifier));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   146
}
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   147
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   148
void HatPrompt::moveRight()
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   149
{
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   150
	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveRight, Qt::NoModifier));
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   151
}
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   152
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   153
void HatPrompt::onAccepted()
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   154
{
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   155
	hatChosen(list->currentIndex());
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   156
}
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   157
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   158
// When a hat is selected
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   159
void HatPrompt::hatChosen(const QModelIndex & index)
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   160
{
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   161
	done(filterModel->mapToSource(index).row() + 1); // Since returning 0 means canceled
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   162
}
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   163
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   164
// When the text in the filter text box is changed
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   165
void HatPrompt::filterChanged(const QString & text)
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   166
{
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   167
	filterModel->setFilterFixedString(text);
3a1708759c4f Hat selection reworked
dag10
parents:
diff changeset
   168
	list->setCurrentIndex(filterModel->index(0, 0));
8375
af2d2f56bc45 Fix build
unc0rr
parents: 8374
diff changeset
   169
}