QTfrontend/ui/widget/hatprompt.cpp
author inu
Fri, 18 Jan 2013 11:30:09 +0100
changeset 8398 d63a0f9d2ccb
parent 8375 af2d2f56bc45
child 8434 4821897a0f10
permissions -rw-r--r--
pt_PT update

/*
 * Hedgewars, a free turn based strategy game
 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <QDialog>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QScrollArea>
#include <QPushButton>
#include <QToolButton>
#include <QWidgetItem>
#include <QModelIndex>
#include <QListView>
#include <QLineEdit>
#include <QLabel>
#include <QSortFilterProxyModel>
#include <QDebug>

#include "DataManager.h"
#include "lineeditcursor.h"
#include "HatModel.h"
#include "hatprompt.h"

HatPrompt::HatPrompt(int currentIndex, QWidget* parent) : QDialog(parent)
{
	setModal(true);
	setWindowFlags(Qt::Sheet);
	setWindowModality(Qt::WindowModal);
	setMinimumSize(550, 430);
	resize(550, 430);
	setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

	setStyleSheet("QPushButton { padding: 5px; margin-top: 10px; }");

	// Hat model, and a model for setting a filter
	HatModel * hatModel = DataManager::instance().hatModel();
	filterModel = new QSortFilterProxyModel();
	filterModel->setSourceModel(hatModel);
	filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);

	// Grid
	QGridLayout * dialogLayout = new QGridLayout(this);
	dialogLayout->setSpacing(0);
	dialogLayout->setColumnStretch(1, 1);

	QHBoxLayout * topLayout = new QHBoxLayout();

	// Help/prompt message at top
	QLabel * lblDesc = new QLabel(tr("Select a hat"));
	lblDesc->setObjectName("lblDesc");
    lblDesc->setStyleSheet("#lblDesc { color: #130F2A; background: #F6CB1C; border: solid 4px #F6CB1C; border-top-left-radius: 10px; padding: 4px 10px;}");
    lblDesc->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    lblDesc->setFixedHeight(24);
    lblDesc->setMinimumWidth(0);

    // Filter text box
    QWidget * filterContainer = new QWidget();
    filterContainer->setFixedHeight(24);
    filterContainer->setObjectName("filterContainer");
    filterContainer->setStyleSheet("#filterContainer { background: #F6CB1C; border-top-right-radius: 10px; padding: 3px; }");
    filterContainer->setFixedWidth(250);
    txtFilter = new LineEditCursor(filterContainer);
    txtFilter->setFixedWidth(250);
    txtFilter->setFocus();
    txtFilter->setFixedHeight(22);
    connect(txtFilter, SIGNAL(textChanged(const QString &)), this, SLOT(filterChanged(const QString &)));
    connect(txtFilter, SIGNAL(moveUp()), this, SLOT(moveUp()));
    connect(txtFilter, SIGNAL(moveDown()), this, SLOT(moveDown()));
    connect(txtFilter, SIGNAL(moveLeft()), this, SLOT(moveLeft()));
    connect(txtFilter, SIGNAL(moveRight()), this, SLOT(moveRight()));

    // Filter label
    QLabel * lblFilter = new QLabel(tr("Filter: "), txtFilter);
    lblFilter->setStyleSheet(QString("background: none; margin-left: -%1px; margin-top: 4px;").arg(lblFilter->width() / 2 - 3));
    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));

    // Corner widget
    QLabel * corner = new QLabel();
    corner->setPixmap(QPixmap(QString::fromUtf8(":/res/inverse-corner-bl.png")));
    corner->setFixedSize(10, 10);

    // Add widgets to top layout
    topLayout->addWidget(lblDesc);
    topLayout->addWidget(filterContainer);
    topLayout->addWidget(corner, 0, Qt::AlignBottom);
    topLayout->addStretch(1);

	// Cancel button (closes dialog)
	QPushButton * btnCancel = new QPushButton(tr("Cancel"));
	connect(btnCancel, SIGNAL(clicked()), this, SLOT(reject()));

	// Select button
	QPushButton * btnSelect = new QPushButton(tr("Use selected hat"));
	btnSelect->setDefault(true);
	connect(btnSelect, SIGNAL(clicked()), this, SLOT(onAccepted()));

	// Add hats
	list = new HatListView();
	list->setModel(filterModel);
	list->setViewMode(QListView::IconMode);
	list->setResizeMode(QListView::Adjust);
	list->setMovement(QListView::Static);
	list->setEditTriggers(QAbstractItemView::NoEditTriggers);
	list->setSpacing(8);
	list->setWordWrap(true);
	list->setSelectionMode(QAbstractItemView::SingleSelection);
	list->setObjectName("hatList");
	list->setCurrentIndex(filterModel->index(currentIndex, 0));
	connect(list, SIGNAL(activated(const QModelIndex &)), this, SLOT(hatChosen(const QModelIndex &)));
	connect(list, SIGNAL(clicked(const QModelIndex &)), this, SLOT(hatChosen(const QModelIndex &)));

	// Add elements to layouts
	dialogLayout->addLayout(topLayout, 0, 0, 1, 3);
	dialogLayout->addWidget(list, 1, 0, 1, 3);
	dialogLayout->addWidget(btnCancel, 2, 0, 1, 1, Qt::AlignLeft);
	dialogLayout->addWidget(btnSelect, 2, 2, 1, 1, Qt::AlignRight);
}

void HatPrompt::moveUp()
{
	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveUp, Qt::NoModifier));
}

void HatPrompt::moveDown()
{
	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier));
}

void HatPrompt::moveLeft()
{
	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveLeft, Qt::NoModifier));
}

void HatPrompt::moveRight()
{
	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveRight, Qt::NoModifier));
}

void HatPrompt::onAccepted()
{
	hatChosen(list->currentIndex());
}

// When a hat is selected
void HatPrompt::hatChosen(const QModelIndex & index)
{
	done(filterModel->mapToSource(index).row() + 1); // Since returning 0 means canceled
}

// When the text in the filter text box is changed
void HatPrompt::filterChanged(const QString & text)
{
	filterModel->setFilterFixedString(text);
	list->setCurrentIndex(filterModel->index(0, 0));
}