-
Notifications
You must be signed in to change notification settings - Fork 0
/
specificenemiesview.cpp
243 lines (227 loc) · 7.64 KB
/
specificenemiesview.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* specificenemiesview.cpp - Sof Game Psi plugin
* Copyright (C) 2011 Aleksey Andreev
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
* You can also redistribute and/or modify this program under the
* terms of the Psi License, specified in the accompanied COPYING
* file, as published by the Psi Project; either dated January 1st,
* 2005, or (at your option) any later version.
*
* 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <QHeaderView>
#include <QMenu>
#include <QAction>
#include "specificenemiesview.h"
#include "settings.h"
#include "pluginhosts.h"
SpecificEnemiesModel::SpecificEnemiesModel(QObject *parent) :
QAbstractTableModel(parent)
{
columnNames << QString::fromUtf8("Имя") << QString::fromUtf8("Не отмечать") << QString::fromUtf8("Сброс очереди");
enemyList = Settings::instance()->getSpecificEnemies();
enemyList.append(Settings::SpecificEnemy(QString(), false, false)); // Для ввода новой строки
}
void SpecificEnemiesModel::save()
{
QList<Settings::SpecificEnemy> forSave;
for (int i = 0, cnt = enemyList.size(); i < cnt; i++) {
const Settings::SpecificEnemy *se = &enemyList.at(i);
QString name = se->name;
if (!name.isEmpty()) {
forSave.append(Settings::SpecificEnemy(se->name, se->mapNotMark, se->resetQueue));
}
}
Settings::instance()->setSpecificEnemies(forSave);
}
int SpecificEnemiesModel::rowCount(const QModelIndex &/*parent*/) const
{
return enemyList.size();
}
int SpecificEnemiesModel::columnCount(const QModelIndex &/*parent*/) const
{
return columnNames.size();
}
QVariant SpecificEnemiesModel::data(const QModelIndex &index, int role) const
{
if (index.isValid()) {
const int row = index.row();
if (row >=0 && row < enemyList.size()) {
const int col = index.column();
if (col == 0) {
if (role == Qt::DisplayRole || role == Qt::EditRole) {
return enemyList.at(row).name;
}
} else {
if (role == Qt::CheckStateRole) {
if (col == 1) {
return enemyList.at(row).mapNotMark ? Qt::Checked : Qt::Unchecked;
}
if (col == 2) {
return enemyList.at(row).resetQueue ? Qt::Checked : Qt::Unchecked;
}
} else if (role == Qt::TextAlignmentRole) {
return (int)(Qt::AlignRight | Qt::AlignVCenter);
}
}
}
}
return QVariant();
}
QVariant SpecificEnemiesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section >= 0 && section < columnNames.size()) {
return columnNames.at(section);
}
}
return QVariant();
}
Qt::ItemFlags SpecificEnemiesModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
const int col = index.column();
if (col == 1 || col == 2) {
flags |= Qt::ItemIsUserCheckable;
} else {
flags |= Qt::ItemIsEditable;
}
return flags;
}
bool SpecificEnemiesModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid()) {
const int row = index.row();
const int col = index.column();
if (row >= 0 && row < enemyList.size() && col >= 0 && col < columnNames.size()) {
if (col == 0) {
if (role == Qt::EditRole) {
QString name = value.toString().trimmed();
if (!name.isEmpty()) {
// Проверяем противников с таким же имененм
for (int i = 0, cnt = enemyList.size(); i < cnt; i++) {
if (enemyList.at(i).name == name)
return false;
}
enemyList[row].name = name;
if (row + 1 == enemyList.size()) {
const int newRow = row + 1;
// Добавляем пустую строчку, т.к. заполнили последнюю строку
beginInsertRows(QModelIndex(), newRow, newRow);
enemyList.append(Settings::SpecificEnemy(QString(), false, false));
endInsertRows();
}
emit dataChanged(index, index);
return true;
} else if (row + 1 < enemyList.size()) {
// Не последняя строка и имя пустое. Удаляем.
beginRemoveRows(QModelIndex(), row, row);
enemyList.removeAt(row);
endRemoveRows();
}
}
} else if (role == Qt::CheckStateRole) {
bool flag = (value.toInt() == Qt::Checked);
if (col == 1) {
enemyList[row].mapNotMark = flag;
} else if (col == 2) {
enemyList[row].resetQueue = flag;
}
return true;
}
}
}
return false;
}
bool SpecificEnemiesModel::removeRows(int row, int count, const QModelIndex &/*parent*/)
{
const int cntAll = enemyList.size();
if (row < cntAll) {
int lastRow = row + count - 1;
if (lastRow >= cntAll)
lastRow = cntAll - 1;
beginRemoveRows(QModelIndex(), row, lastRow);
while (row <= lastRow) {
enemyList.removeAt(row);
--lastRow;
}
endRemoveRows();
return true;
}
return false;
}
//----------------------------------------------------------------------
SpecificEnemiesView::SpecificEnemiesView(QWidget *parent) :
QTableView(parent),
model_(NULL)
{
}
/**
* init должен вызываться для каждого переключения аккаунта, так что старые данные нужно обновлять
*/
void SpecificEnemiesView::init()
{
// Прописываем модель
SpecificEnemiesModel *oldModel = model_;
model_ = new SpecificEnemiesModel(this);
setModel(model_);
if (oldModel != NULL)
delete oldModel;
// Настройки поведения таблицы
resizeColumnsToContents();
horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
horizontalHeader()->setResizeMode(1, QHeaderView::ResizeToContents);
horizontalHeader()->setResizeMode(2, QHeaderView::ResizeToContents);
verticalHeader()->setDefaultAlignment( Qt::AlignHCenter );
verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
}
void SpecificEnemiesView::save()
{
if (model_ != NULL)
model_->save();
}
void SpecificEnemiesView::showContextMenu(QPoint /*pos*/)
{
QModelIndex index_ = currentIndex();
if (!index_.isValid())
return;
const int row = index_.row();
QMenu *menu = new QMenu();
QAction *actAppend = new QAction(QString::fromUtf8("Добавить"), menu);
menu->addAction(actAppend);
QAction *actEdit = new QAction(PluginHosts::psiIcon->getIcon("psi/action_templates_edit"), QString::fromUtf8("Редактировать"), menu);
menu->addAction(actEdit);
QAction *actRemove = new QAction(PluginHosts::psiIcon->getIcon("psi/remove"), QString::fromUtf8("Удалить"), menu);
menu->addAction(actRemove);
if (row + 1 >= model_->rowCount(QModelIndex())) {
actRemove->setEnabled(false);
}
QAction *res = menu->exec(QCursor::pos());
if (res != NULL) {
if (res == actAppend) {
QModelIndex newItemIndex = model_->index(model_->rowCount(QModelIndex()) - 1, 0);
setCurrentIndex(newItemIndex);
edit(newItemIndex);
} else if (res == actEdit) {
QModelIndex itemIndex = model_->index(row, 0);
edit(itemIndex);
} else if (res == actRemove) {
model_->removeRow(row, QModelIndex());
}
}
delete menu;
}