serverlistmodel.cpp
1 //------------------------------------------------------------------------------
2 // serverlistmodel.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "gui/models/serverlistmodel.h"
24 #include "gui/models/serverlistcolumn.h"
25 #include "gui/models/serverlistrowhandler.h"
26 #include "gui/helpers/playersdiagram.h"
27 #include "gui/widgets/serverlistview.h"
28 #include "gui/serverlist.h"
29 #include "configuration/doomseekerconfig.h"
30 #include "serverapi/server.h"
31 #include "log.h"
32 #include <QItemDelegate>
33 #include <QTime>
34 
35 using namespace ServerListColumnId;
36 
38 
39 ServerListModel::ServerListModel(ServerList* parent)
40 : QStandardItemModel(parent),
41  parentHandler(parent)
42 {
43  setSortRole(SLDT_SORT);
44 }
45 
46 int ServerListModel::addServer(ServerPtr server)
47 {
48  QList<QStandardItem*> columns = ServerListColumns::generateListOfCells();
49  appendRow(columns);
50 
51  // Country flag is set only once. Set it here and avoid setting it in
52  // updateServer() method.
53 
54  QModelIndex index = indexFromItem(columns[0]);
55  ServerListRowHandler rowHandler(this, index.row(), server);
56  if (parentHandler->getMainWindow()->isActiveWindow())
57  {
58  rowHandler.setCountryFlag();
59  }
60 
61  return rowHandler.updateServer();
62 }
63 
65 {
66  if (server != NULL)
67  {
68  for (int i = 0; i < rowCount(); ++i)
69  {
70  ServerCPtr savedServ = serverFromList(i);
71  if (server == savedServ)
72  {
73  return i;
74  }
75  }
76  }
77  return -1;
78 }
79 
81 {
82  ServerPtr server = serverFromList(row);
83  ServerListRowHandler rowHandler(this, row, server);
84  rowHandler.redraw();
85 }
86 
87 void ServerListModel::redrawAll()
88 {
89  int slotstyle = gConfig.doomseeker.slotStyle;
90  PlayersDiagram::loadImages(slotstyle);
91 
92  for (int i = 0; i < rowCount(); ++i)
93  {
94  redraw(i);
95  }
96 }
97 
98 QList<ServerPtr> ServerListModel::customServers() const
99 {
100  QList<ServerPtr> servers;
101  for (int i = 0; i < rowCount(); ++i)
102  {
103  ServerPtr server = serverFromList(i);
104  if (server->isCustom())
105  {
106  servers << server;
107  }
108  }
109  return servers;
110 }
111 
112 QList<ServerPtr> ServerListModel::nonSpecialServers() const
113 {
114  QList<ServerPtr> servers;
115  for (int i = 0; i < rowCount(); ++i)
116  {
117  ServerPtr server = serverFromList(i);
118  if (!server->isCustom() && !server->isLan())
119  {
120  servers << server;
121  }
122  }
123  return servers;
124 }
125 
126 QList<ServerPtr> ServerListModel::servers() const
127 {
128  QList<ServerPtr> servers;
129  for (int i = 0; i < rowCount(); ++i)
130  {
131  servers << serverFromList(i);
132  }
133  return servers;
134 }
135 
136 QList<ServerPtr> ServerListModel::serversForPlugin(const EnginePlugin *plugin) const
137 {
138  QList<ServerPtr> servers;
139  for (int i = 0; i < rowCount(); ++i)
140  {
141  ServerPtr server = serverFromList(i);
142  if (server->plugin() == plugin)
143  {
144  servers << server;
145  }
146  }
147  return servers;
148 }
149 
150 void ServerListModel::removeServer(const ServerPtr &server)
151 {
152  int index = findServerOnTheList(server.data());
153  if (index >= 0)
154  {
155  removeRow(index);
156  }
157 }
158 
159 ServerPtr ServerListModel::serverFromList(int rowIndex) const
160 {
161  return ServerListRowHandler::serverFromList(this, rowIndex);
162 }
163 
164 ServerPtr ServerListModel::serverFromList(const QModelIndex& index) const
165 {
166  return ServerListRowHandler::serverFromList(this, index.row());
167 }
168 
169 ServerListModel::ServerGroup ServerListModel::serverGroup(int row)
170 {
171  QStandardItem* qstdItem = item(row, IDHiddenGroup);
172  return static_cast<ServerListModel::ServerGroup>(qstdItem->data(SLDT_SORT).toInt());
173 }
174 
175 void ServerListModel::setRefreshing(ServerPtr server)
176 {
177  int rowIndex = findServerOnTheList(server.data());
178  if (rowIndex >= 0)
179  {
180  ServerListRowHandler rowHandler(this, rowIndex, server);
181  rowHandler.setRefreshing();
182  }
183 }
184 
185 void ServerListModel::prepareHeaders()
186 {
187  setHorizontalHeaderLabels(ServerListColumns::generateColumnHeaderLabels());
188 }
189 
190 int ServerListModel::updateServer(int row, ServerPtr server)
191 {
192  ServerListRowHandler rowHandler(this, row, server);
193  rowHandler.updateServer();
194 
195  return row;
196 }
197 
198 QVariant ServerListModel::columnSortData(int row, int column)
199 {
200  QStandardItem* it = item(row, column);
201  return it->data(SLDT_SORT);
202 }
203 
204 void ServerListModel::updateFlag(int row, bool force)
205 {
206  ServerPtr server = serverFromList(row);
207  ServerListRowHandler rowHandler(this, row, server);
208  QStandardItem* itm = item(row, IDServerName);
209 
210  if (force || itm->icon().isNull())
211  {
212  rowHandler.setCountryFlag();
213  }
214 }
static void loadImages(int style)
int addServer(ServerPtr server)
void redraw(int row)
A representation of a server for a given game.
Definition: server.h:93
int updateServer(int row, ServerPtr server)
int findServerOnTheList(const Server *server)
Finds index of the row where server is contained.
void updateFlag(int row, bool force=true)