QTfrontend/ui/widget/hatprompt.cpp
changeset 8374 3a1708759c4f
child 8375 af2d2f56bc45
equal deleted inserted replaced
8372:3c193ec03e09 8374:3a1708759c4f
       
     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>
       
    31 #include <QDebug>
       
    32 
       
    33 #include "datamanager.h"
       
    34 #include "lineeditcursor.h"
       
    35 #include "hatmodel.h"
       
    36 #include "hatprompt.h"
       
    37 
       
    38 HatPrompt::HatPrompt(int currentIndex, QWidget* parent) : QDialog(parent)
       
    39 {
       
    40 	setModal(true);
       
    41 	setWindowFlags(Qt::Sheet);
       
    42 	setWindowModality(Qt::WindowModal);
       
    43 	setMinimumSize(550, 430);
       
    44 	resize(550, 430);
       
    45 	setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
       
    46 
       
    47 	setStyleSheet("QPushButton { padding: 5px; margin-top: 10px; }");
       
    48 
       
    49 	// Hat model, and a model for setting a filter
       
    50 	HatModel * hatModel = DataManager::instance().hatModel();
       
    51 	filterModel = new QSortFilterProxyModel();
       
    52 	filterModel->setSourceModel(hatModel);
       
    53 	filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
       
    54 
       
    55 	// Grid
       
    56 	QGridLayout * dialogLayout = new QGridLayout(this);
       
    57 	dialogLayout->setSpacing(0);
       
    58 	dialogLayout->setColumnStretch(1, 1);
       
    59 
       
    60 	QHBoxLayout * topLayout = new QHBoxLayout();
       
    61 
       
    62 	// Help/prompt message at top
       
    63 	QLabel * lblDesc = new QLabel(tr("Select a hat"));
       
    64 	lblDesc->setObjectName("lblDesc");
       
    65     lblDesc->setStyleSheet("#lblDesc { color: #130F2A; background: #F6CB1C; border: solid 4px #F6CB1C; border-top-left-radius: 10px; padding: 4px 10px;}");
       
    66     lblDesc->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
       
    67     lblDesc->setFixedHeight(24);
       
    68     lblDesc->setMinimumWidth(0);
       
    69 
       
    70     // Filter text box
       
    71     QWidget * filterContainer = new QWidget();
       
    72     filterContainer->setFixedHeight(24);
       
    73     filterContainer->setObjectName("filterContainer");
       
    74     filterContainer->setStyleSheet("#filterContainer { background: #F6CB1C; border-top-right-radius: 10px; padding: 3px; }");
       
    75     filterContainer->setFixedWidth(250);
       
    76     txtFilter = new LineEditCursor(filterContainer);
       
    77     txtFilter->setFixedWidth(250);
       
    78     txtFilter->setFocus();
       
    79     txtFilter->setFixedHeight(22);
       
    80     connect(txtFilter, SIGNAL(textChanged(const QString &)), this, SLOT(filterChanged(const QString &)));
       
    81     connect(txtFilter, SIGNAL(moveUp()), this, SLOT(moveUp()));
       
    82     connect(txtFilter, SIGNAL(moveDown()), this, SLOT(moveDown()));
       
    83     connect(txtFilter, SIGNAL(moveLeft()), this, SLOT(moveLeft()));
       
    84     connect(txtFilter, SIGNAL(moveRight()), this, SLOT(moveRight()));
       
    85 
       
    86     // Filter label
       
    87     QLabel * lblFilter = new QLabel(tr("Filter: "), txtFilter);
       
    88     lblFilter->setStyleSheet(QString("background: none; margin-left: -%1px; margin-top: 4px;").arg(lblFilter->width() / 2 - 3));
       
    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));
       
    90 
       
    91     // Corner widget
       
    92     QLabel * corner = new QLabel();
       
    93     corner->setPixmap(QPixmap(QString::fromUtf8(":/res/inverse-corner-bl.png")));
       
    94     corner->setFixedSize(10, 10);
       
    95 
       
    96     // Add widgets to top layout
       
    97     topLayout->addWidget(lblDesc);
       
    98     topLayout->addWidget(filterContainer);
       
    99     topLayout->addWidget(corner, 0, Qt::AlignBottom);
       
   100     topLayout->addStretch(1);
       
   101 
       
   102 	// Cancel button (closes dialog)
       
   103 	QPushButton * btnCancel = new QPushButton(tr("Cancel"));
       
   104 	connect(btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
       
   105 
       
   106 	// Select button
       
   107 	QPushButton * btnSelect = new QPushButton(tr("Use selected hat"));
       
   108 	btnSelect->setDefault(true);
       
   109 	connect(btnSelect, SIGNAL(clicked()), this, SLOT(onAccepted()));
       
   110 
       
   111 	// Add hats
       
   112 	list = new HatListView();
       
   113 	list->setModel(filterModel);
       
   114 	list->setViewMode(QListView::IconMode);
       
   115 	list->setResizeMode(QListView::Adjust);
       
   116 	list->setMovement(QListView::Static);
       
   117 	list->setEditTriggers(QAbstractItemView::NoEditTriggers);
       
   118 	list->setSpacing(8);
       
   119 	list->setWordWrap(true);
       
   120 	list->setSelectionMode(QAbstractItemView::SingleSelection);
       
   121 	list->setObjectName("hatList");
       
   122 	list->setCurrentIndex(filterModel->index(currentIndex, 0));
       
   123 	connect(list, SIGNAL(activated(const QModelIndex &)), this, SLOT(hatChosen(const QModelIndex &)));
       
   124 	connect(list, SIGNAL(clicked(const QModelIndex &)), this, SLOT(hatChosen(const QModelIndex &)));
       
   125 
       
   126 	// Add elements to layouts
       
   127 	dialogLayout->addLayout(topLayout, 0, 0, 1, 3);
       
   128 	dialogLayout->addWidget(list, 1, 0, 1, 3);
       
   129 	dialogLayout->addWidget(btnCancel, 2, 0, 1, 1, Qt::AlignLeft);
       
   130 	dialogLayout->addWidget(btnSelect, 2, 2, 1, 1, Qt::AlignRight);
       
   131 }
       
   132 
       
   133 void HatPrompt::moveUp()
       
   134 {
       
   135 	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveUp, Qt::NoModifier));
       
   136 }
       
   137 
       
   138 void HatPrompt::moveDown()
       
   139 {
       
   140 	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier));
       
   141 }
       
   142 
       
   143 void HatPrompt::moveLeft()
       
   144 {
       
   145 	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveLeft, Qt::NoModifier));
       
   146 }
       
   147 
       
   148 void HatPrompt::moveRight()
       
   149 {
       
   150 	list->setCurrentIndex(list->moveCursor(QAbstractItemView::MoveRight, Qt::NoModifier));
       
   151 }
       
   152 
       
   153 void HatPrompt::onAccepted()
       
   154 {
       
   155 	hatChosen(list->currentIndex());
       
   156 }
       
   157 
       
   158 // When a hat is selected
       
   159 void HatPrompt::hatChosen(const QModelIndex & index)
       
   160 {
       
   161 	done(filterModel->mapToSource(index).row() + 1); // Since returning 0 means canceled
       
   162 }
       
   163 
       
   164 // When the text in the filter text box is changed
       
   165 void HatPrompt::filterChanged(const QString & text)
       
   166 {
       
   167 	filterModel->setFilterFixedString(text);
       
   168 	list->setCurrentIndex(filterModel->index(0, 0));
       
   169 }