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