| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2009 by The qGo Project * |
| 3 | * * |
| 4 | * This file is part of qGo. * |
| 5 | * * |
| 6 | * qGo is free software: you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/> * |
| 18 | * or write to the Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | #include "listviews.h" |
| 24 | #include "network/messages.h" |
| 25 | #include "playergamelistings.h" |
| 26 | #include "gamedata.h" |
| 27 | #include <QColor> |
| 28 | |
| 29 | PlayerListModel::PlayerListModel() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | PlayerListModel::~PlayerListModel() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | int PlayerListModel::columnCount(const QModelIndex &) const |
| 38 | { |
| 39 | return P_TOTALCOLUMNS; |
| 40 | } |
| 41 | |
| 42 | QVariant PlayerListModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 43 | { |
| 44 | if(orientation == Qt::Horizontal && role == Qt::DisplayRole) |
| 45 | switch(section) |
| 46 | { |
| 47 | case PC_STATUS: |
| 48 | return QVariant("Stat"); |
| 49 | case PC_NAME: |
| 50 | return QVariant("Name"); |
| 51 | case PC_RANK: |
| 52 | return QVariant("Rank"); |
| 53 | case PC_PLAYING: |
| 54 | return QVariant("Play"); |
| 55 | case PC_OBSERVING: |
| 56 | return QVariant("Obs"); |
| 57 | case PC_WINS: |
| 58 | return QVariant("Won"); |
| 59 | case PC_LOSSES: |
| 60 | return QVariant("Lost"); |
| 61 | case PC_IDLE: |
| 62 | return QVariant("Idle"); |
| 63 | case PC_COUNTRY: |
| 64 | return QVariant("Country"); |
| 65 | case PC_MATCHPREFS: |
| 66 | return QVariant("Match prefs"); |
| 67 | } |
| 68 | return QVariant(); |
| 69 | } |
| 70 | |
| 71 | void PlayerListModel::removeListing(PlayerListing * const l) |
| 72 | { |
| 73 | int i = items.indexOf(l); |
| 74 | if (i != -1) |
| 75 | { |
| 76 | beginRemoveRows(QModelIndex(), i, i); |
| 77 | items.removeAt(i); |
| 78 | endRemoveRows(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void PlayerListModel::clearList(void) |
| 83 | { |
| 84 | if(items.count() == 0) |
| 85 | return; |
| 86 | beginResetModel(); |
| 87 | items.clear(); |
| 88 | endResetModel(); |
| 89 | } |
| 90 | |
| 91 | QVariant PlayerListModel::data(const QModelIndex & index, int role) const |
| 92 | { |
| 93 | if(!index.isValid()) |
| 94 | return QVariant(); |
| 95 | |
| 96 | PlayerListing * item = static_cast<PlayerListing*>(index.internalPointer()); |
| 97 | |
| 98 | if (role == Qt::ForegroundRole) |
| 99 | { |
| 100 | if(item->name == account_name) |
| 101 | return QColor(Qt::blue); |
| 102 | else |
| 103 | return QVariant(); |
| 104 | } |
| 105 | else if ((role == Qt::DisplayRole) || (role == LIST_SORT_ROLE)) |
| 106 | { |
| 107 | switch(index.column()) |
| 108 | { |
| 109 | case PC_STATUS: |
| 110 | return QVariant(item->info); |
| 111 | break; |
| 112 | case PC_NAME: |
| 113 | return QVariant(item->name); |
| 114 | break; |
| 115 | case PC_RANK: |
| 116 | if (role == LIST_SORT_ROLE) |
| 117 | return QVariant(item->rank_score); |
| 118 | else |
| 119 | return QVariant(item->rank); |
| 120 | case PC_PLAYING: |
| 121 | if(item->playing == 0) |
| 122 | return QVariant("-"); |
| 123 | else |
| 124 | return QVariant(item->playing); |
| 125 | case PC_OBSERVING: |
| 126 | if(item->observing == 0 || |
| 127 | item->observing == item->playing) |
| 128 | return QVariant("-"); |
| 129 | else |
| 130 | return QVariant(item->observing); |
| 131 | case PC_WINS: |
| 132 | return QVariant(item->wins); |
| 133 | case PC_LOSSES: |
| 134 | return QVariant(item->losses); |
| 135 | case PC_IDLE: |
| 136 | if (role == LIST_SORT_ROLE) |
| 137 | return QVariant(item->seconds_idle); |
| 138 | else |
| 139 | return QVariant(item->idletime); |
| 140 | case PC_COUNTRY: |
| 141 | return QVariant(item->country); |
| 142 | case PC_MATCHPREFS: |
| 143 | //is this right? FIXME takes up too much space, should go with simple numbers, abbrev. |
| 144 | return QVariant(item->nmatch_settings); |
| 145 | case PC_NOTIFY: //was used for SimplePlayerListModel |
| 146 | return QVariant(item->notify); |
| 147 | case PC_ONLINE: |
| 148 | return QVariant(item->online); |
| 149 | default: |
| 150 | return QVariant(); |
| 151 | } |
| 152 | } |
| 153 | return QVariant(); |
| 154 | } |
| 155 | |
| 156 | PlayerListing * PlayerListModel::playerListingFromIndex(const QModelIndex & index) |
| 157 | { |
| 158 | return static_cast<PlayerListing*>(index.internalPointer()); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | PlayerListing * PlayerListModel::getEntry(unsigned int id) |
| 163 | { |
| 164 | PlayerListing * result; |
| 165 | for (int i=0; i < items.count(); i++) |
| 166 | { |
| 167 | result = static_cast<PlayerListing*>(items[i]); |
| 168 | if (result->id == id) |
| 169 | { |
| 170 | return result; |
| 171 | } |
| 172 | } |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | PlayerListing * PlayerListModel::getEntry(const QString &name) |
| 177 | { |
| 178 | PlayerListing * result; |
| 179 | for (int i=0; i < items.count(); i++) |
| 180 | { |
| 181 | result = static_cast<PlayerListing*>(items[i]); |
| 182 | if (result->name == name) |
| 183 | { |
| 184 | return result; |
| 185 | } |
| 186 | } |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | PlayerListing * PlayerListModel::getPlayerFromNotNickName(const QString & notnickname) |
| 191 | { |
| 192 | PlayerListing * result; |
| 193 | for (int i=0; i < items.count(); i++) |
| 194 | { |
| 195 | result = static_cast<PlayerListing*>(items[i]); |
| 196 | if (result->notnickname == notnickname) |
| 197 | return result; |
| 198 | } |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | PlayerListing * PlayerListModel::updateEntry(PlayerListing * listing) |
| 203 | { |
| 204 | PlayerListing * result; |
| 205 | for (int i=0; i < items.count(); i++) |
| 206 | { |
| 207 | result = static_cast<PlayerListing*>(items[i]); |
| 208 | if (result == listing) |
| 209 | { |
| 210 | emit dataChanged(createIndex(i, 0), createIndex(i, columnCount() - 1)); |
| 211 | return result; |
| 212 | } |
| 213 | } |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | void PlayerListModel::insertListing(PlayerListing *item) |
| 218 | { |
| 219 | beginInsertRows(QModelIndex(), items.count(), items.count()); |
| 220 | items.append(item); |
| 221 | endInsertRows(); |
| 222 | } |
| 223 | |
| 224 | int PlayerListModel::rowCount(const QModelIndex &) const |
| 225 | { |
| 226 | return items.count(); |
| 227 | } |
| 228 | |
| 229 | QModelIndex PlayerListModel::index ( int row, int column, const QModelIndex & ) const |
| 230 | { |
| 231 | return createIndex(row,column,(void*)items[row]); |
| 232 | } |
| 233 | |
| 234 | Qt::ItemFlags PlayerListModel::flags(const QModelIndex & index) const |
| 235 | { |
| 236 | if(!index.isValid()) |
| 237 | return Qt::ItemIsEnabled; |
| 238 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
| 239 | } |
| 240 | |
| 241 | int ObserverListModel::columnCount(const QModelIndex &) const |
| 242 | { |
| 243 | return O_TOTALCOLUMNS; |
| 244 | } |
| 245 | |
| 246 | QVariant ObserverListModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 247 | { |
| 248 | if(orientation == Qt::Horizontal && role == Qt::DisplayRole) |
| 249 | switch(section) |
| 250 | { |
| 251 | case OC_NAME: |
| 252 | return QVariant("Name"); |
| 253 | case OC_RANK: |
| 254 | return QVariant("Rank"); |
| 255 | } |
| 256 | return QVariant(); |
| 257 | } |
| 258 | |
| 259 | QVariant ObserverListModel::data(const QModelIndex & index, int role) const |
| 260 | { |
| 261 | if(!index.isValid()) |
| 262 | return QVariant(); |
| 263 | |
| 264 | PlayerListing * item = static_cast<PlayerListing*>(index.internalPointer()); |
| 265 | if(role == Qt::DisplayRole) |
| 266 | { |
| 267 | switch(index.column()) |
| 268 | { |
| 269 | case OC_NAME: |
| 270 | return item->name; |
| 271 | case OC_RANK: |
| 272 | return item->rank; |
| 273 | default: |
| 274 | return QVariant(); |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | else if(role == Qt::ForegroundRole) |
| 279 | { |
| 280 | if(item->name == account_name) |
| 281 | return QColor(Qt::blue); |
| 282 | else |
| 283 | return QVariant(); |
| 284 | } |
| 285 | else |
| 286 | return QVariant(); |
| 287 | } |
| 288 | |
| 289 | SimplePlayerListModel::SimplePlayerListModel(bool _notify_column) |
| 290 | { |
| 291 | notify_column = _notify_column; |
| 292 | } |
| 293 | |
| 294 | int SimplePlayerListModel::columnCount(const QModelIndex &) const |
| 295 | { |
| 296 | if (notify_column) |
| 297 | return 2; |
| 298 | else |
| 299 | return 1; |
| 300 | } |
| 301 | |
| 302 | QVariant SimplePlayerListModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 303 | { |
| 304 | if(orientation == Qt::Horizontal && role == Qt::DisplayRole) |
| 305 | switch(section) |
| 306 | { |
| 307 | case 0: |
| 308 | return QVariant("Name"); |
| 309 | case 1: |
| 310 | return QVariant("Notify"); |
| 311 | } |
| 312 | return QVariant(); |
| 313 | } |
| 314 | |
| 315 | QVariant SimplePlayerListModel::data(const QModelIndex & index, int role) const |
| 316 | { |
| 317 | if(!index.isValid()) |
| 318 | return QVariant(); |
| 319 | |
| 320 | PlayerListing * item = static_cast<PlayerListing*>(index.internalPointer()); |
| 321 | if(role == Qt::DisplayRole) |
| 322 | { |
| 323 | switch(index.column()) |
| 324 | { |
| 325 | case SPC_NAME: |
| 326 | return item->name; |
| 327 | case SPC_NOTIFY: |
| 328 | return item->notify; |
| 329 | default: |
| 330 | return QVariant(); |
| 331 | } |
| 332 | } |
| 333 | else |
| 334 | return QVariant(); |
| 335 | } |
| 336 | |
| 337 | GamesListModel::GamesListModel() |
| 338 | { |
| 339 | } |
| 340 | |
| 341 | GamesListModel::~GamesListModel() |
| 342 | { |
| 343 | clearList(); |
| 344 | } |
| 345 | |
| 346 | int GamesListModel::columnCount(const QModelIndex &) const |
| 347 | { |
| 348 | return G_TOTALCOLUMNS; |
| 349 | } |
| 350 | |
| 351 | QVariant GamesListModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 352 | { |
| 353 | if(orientation == Qt::Horizontal && role == Qt::DisplayRole) |
| 354 | switch(section) |
| 355 | { |
| 356 | case GC_ID: |
| 357 | return QVariant("Id"); |
| 358 | case GC_WHITENAME: |
| 359 | return QVariant("White"); |
| 360 | case GC_WHITERANK: |
| 361 | return QVariant("WR"); |
| 362 | case GC_BLACKNAME: |
| 363 | return QVariant("Black"); |
| 364 | case GC_BLACKRANK: |
| 365 | return QVariant("BR"); |
| 366 | case GC_MOVES: |
| 367 | return QVariant("Mv"); |
| 368 | case GC_SIZE: |
| 369 | return QVariant("Sz"); |
| 370 | case GC_HANDICAP: |
| 371 | return QVariant("H"); |
| 372 | case GC_KOMI: |
| 373 | return QVariant("K"); |
| 374 | case GC_BYOMI: |
| 375 | return QVariant("B"); |
| 376 | case GC_FLAGS: |
| 377 | return QVariant("Flags"); |
| 378 | case GC_OBSERVERS: |
| 379 | return QVariant("Obs"); |
| 380 | } |
| 381 | return QVariant(); |
| 382 | } |
| 383 | |
| 384 | GameListing * GamesListModel::getEntry(unsigned int id) |
| 385 | { |
| 386 | for (int i=0; i < items.count(); i++) |
| 387 | { |
| 388 | if (items[i]->number == id) |
| 389 | return items[i]; |
| 390 | } |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | void GamesListModel::updateListing(GameListing * listing) |
| 395 | { |
| 396 | int i = items.indexOf(listing); // Returns index if found or -1 if not found |
| 397 | if (i != -1) |
| 398 | emit dataChanged(createIndex(i, 0), createIndex(i, columnCount() - 1)); |
| 399 | } |
| 400 | |
| 401 | void GamesListModel::clearList(void) |
| 402 | { |
| 403 | beginRemoveRows(QModelIndex(), 0, items.count() - 1); |
| 404 | items.clear(); |
| 405 | endRemoveRows(); |
| 406 | } |
| 407 | |
| 408 | QVariant GamesListModel::data(const QModelIndex & index, int role) const |
| 409 | { |
| 410 | if(!index.isValid()) |
| 411 | return QVariant(); |
| 412 | |
| 413 | GameListing * item = static_cast<GameListing*>(index.internalPointer()); |
| 414 | if ((role == Qt::DisplayRole) || (role == LIST_SORT_ROLE)) |
| 415 | { |
| 416 | switch(index.column()) |
| 417 | { |
| 418 | case GC_ID: |
| 419 | return QVariant(item->number); |
| 420 | case GC_WHITENAME: |
| 421 | return QVariant(item->white_name()); |
| 422 | case GC_WHITERANK: |
| 423 | if (role == LIST_SORT_ROLE) |
| 424 | return QVariant(item->white_rank_score()); |
| 425 | else |
| 426 | return QVariant(item->white_rank()); |
| 427 | case GC_BLACKNAME: |
| 428 | return QVariant(item->black_name()); |
| 429 | case GC_BLACKRANK: |
| 430 | if (role == LIST_SORT_ROLE) |
| 431 | return QVariant(item->black_rank_score()); |
| 432 | else |
| 433 | return QVariant(item->black_rank()); |
| 434 | case GC_MOVES: |
| 435 | if(item->moves == (unsigned)-1) |
| 436 | return QVariant("-"); |
| 437 | return QVariant(item->moves); |
| 438 | case GC_SIZE: |
| 439 | return QVariant(item->board_size); |
| 440 | case GC_HANDICAP: |
| 441 | return QVariant(item->handicap); |
| 442 | case GC_KOMI: |
| 443 | return QVariant(item->komi); |
| 444 | case GC_BYOMI: |
| 445 | return QVariant(item->By); |
| 446 | case GC_FLAGS: |
| 447 | return QVariant(item->FR); |
| 448 | case GC_OBSERVERS: |
| 449 | return QVariant(item->observers); |
| 450 | case GC_RUNNING: |
| 451 | return QVariant(item->running); |
| 452 | default: |
| 453 | return QVariant(); |
| 454 | } |
| 455 | } |
| 456 | return QVariant(); |
| 457 | } |
| 458 | |
| 459 | GameListing * GamesListModel::gameListingFromIndex(const QModelIndex & index) |
| 460 | { |
| 461 | return static_cast<GameListing*>(index.internalPointer()); |
| 462 | } |
| 463 | |
| 464 | void GamesListModel::insertListing(GameListing *item) |
| 465 | { |
| 466 | beginInsertRows(QModelIndex(), items.count(), items.count()); |
| 467 | items.append(item); |
| 468 | endInsertRows(); |
| 469 | emit countChanged(items.count()); |
| 470 | } |
| 471 | |
| 472 | int GamesListModel::rowCount(const QModelIndex &) const |
| 473 | { |
| 474 | return items.count(); |
| 475 | } |
| 476 | |
| 477 | QModelIndex GamesListModel::index(int row, int column, const QModelIndex &) const |
| 478 | { |
| 479 | return createIndex(row,column,(void*)items[row]); |
| 480 | } |
| 481 | |
| 482 | PlayerListSortFilterProxyModel::PlayerListSortFilterProxyModel(QObject * parent) : |
| 483 | QSortFilterProxyModel(parent), rankMin(0), rankMax(100000), open(false), friends(false), fans(false), noblock(false) |
| 484 | { |
| 485 | setSortCaseSensitivity(Qt::CaseInsensitive); |
| 486 | setFilterCaseSensitivity(Qt::CaseInsensitive); |
| 487 | setFilterKeyColumn(PC_NAME); |
| 488 | this->setSortRole(LIST_SORT_ROLE); |
| 489 | } |
| 490 | |
| 491 | bool PlayerListSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const |
| 492 | { |
| 493 | if (! QSortFilterProxyModel::filterAcceptsRow(sourceRow,sourceParent)) |
| 494 | return false; |
| 495 | |
| 496 | const PlayerListing * p; |
| 497 | p = static_cast<PlayerListing*>(static_cast<PlayerListModel*>(sourceModel())->items[sourceRow]); |
| 498 | |
| 499 | if(!(p->online) || p->hidden) |
| 500 | return false; |
| 501 | if(!(noblock)) |
| 502 | { |
| 503 | if(p->friendWatchType == PlayerListing::blocked) |
| 504 | return false; |
| 505 | } |
| 506 | if(open) |
| 507 | { |
| 508 | if(p->info.contains("X") || p->playing != 0) |
| 509 | return false; |
| 510 | } |
| 511 | if(p->rank_score > (unsigned int)rankMax || p->rank_score + 1 < (unsigned int)rankMin) |
| 512 | return false; |
| 513 | if(friends || fans) |
| 514 | { |
| 515 | if(friends && p->friendWatchType == PlayerListing::friended) |
| 516 | return true; |
| 517 | if(fans && p->friendWatchType == PlayerListing::watched) |
| 518 | return true; |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | void PlayerListSortFilterProxyModel::setFilterOpen(bool state) |
| 526 | { |
| 527 | if (open != state) |
| 528 | { |
| 529 | open = state; |
| 530 | invalidateFilter(); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | void PlayerListSortFilterProxyModel::setFilterFriends(bool state) |
| 535 | { |
| 536 | if (friends != state) |
| 537 | { |
| 538 | friends = state; |
| 539 | invalidateFilter(); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | void PlayerListSortFilterProxyModel::setFilterFans(bool state) |
| 544 | { |
| 545 | if (fans != state) |
| 546 | { |
| 547 | fans = state; |
| 548 | invalidateFilter(); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | void PlayerListSortFilterProxyModel::setFilterMinRank(int rank) |
| 553 | { |
| 554 | rankMin = rank; |
| 555 | invalidateFilter(); |
| 556 | } |
| 557 | |
| 558 | void PlayerListSortFilterProxyModel::setFilterMaxRank(int rank) |
| 559 | { |
| 560 | rankMax = rank; |
| 561 | invalidateFilter(); |
| 562 | } |
| 563 | |
| 564 | GamesListSortFilterProxyModel::GamesListSortFilterProxyModel(QObject * parent) : |
| 565 | QSortFilterProxyModel(parent), watches(false) |
| 566 | { |
| 567 | setSortCaseSensitivity(Qt::CaseInsensitive); |
| 568 | setSortRole(LIST_SORT_ROLE); |
| 569 | } |
| 570 | |
| 571 | void GamesListSortFilterProxyModel::setFilterWatch(bool state) |
| 572 | { |
| 573 | if (watches != state) |
| 574 | { |
| 575 | watches = state; |
| 576 | invalidateFilter(); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | bool GamesListSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &) const |
| 581 | { |
| 582 | const GameListing * g = static_cast<GamesListModel*>(sourceModel())->items[sourceRow]; |
| 583 | if(!(g->running) || (g->isRoomOnly)) // It is very weird that rooms are saved as games, FIXME |
| 584 | return false; |
| 585 | if(watches) |
| 586 | { |
| 587 | return ((g->black && g->black->friendWatchType == PlayerListing::watched) || |
| 588 | (g->white && g->white->friendWatchType == PlayerListing::watched)); |
| 589 | } |
| 590 | return true; |
| 591 | } |