serversstatuswidget.cpp
1 //------------------------------------------------------------------------------
2 // serversstatuswidget.h
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 "Blzut3" <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #include <QHBoxLayout>
25 #include <QLabel>
26 #include <QMouseEvent>
27 #include <QPainter>
28 
29 #include "serversstatuswidget.h"
30 #include "serverapi/masterclient.h"
31 #include "serverapi/playerslist.h"
32 #include "serverapi/server.h"
33 
34 ServersStatusWidget::ServersStatusWidget(const QPixmap &icon, MasterClient *serverList) : QLabel(),
35  bMasterIsEnabled(false), icon(icon), numBots(0), numPlayers(0), serverList(serverList)
36 {
37  // Transform icon to grayscale format for disabled appearance
38  QImage iconImage = icon.toImage();
39  int width = iconImage.width();
40  int height = iconImage.height();
41  for (int x = 0; x < width; ++x)
42  {
43  for (int y = 0; y < height; ++y)
44  {
45  QRgb pixel = iconImage.pixel(x, y);
46  int alpha = qAlpha(pixel);
47  int gray = qGray(pixel);
48 
49  pixel = qRgba(gray, gray, gray, alpha);
50 
51  iconImage.setPixel(x, y, pixel);
52  }
53  }
54 
55  iconDisabled = QPixmap::fromImage(iconImage);
56 
57  // Have an inset frame unless we're on the Mac
58 #ifndef Q_OS_MAC
59  setFrameShape(QFrame::Panel);
60  setFrameShadow(QFrame::Sunken);
61 #else
62  setFrameShape(QFrame::NoFrame);
63 #endif
64 
65  setFixedHeight(22);
66  setToolTip(tr("Players-Bots Servers"));
67 
68  setIndent(22);
69  updateDisplay();
70 
71  registerServers();
72 
73  connect(serverList, SIGNAL(listUpdated()), this, SLOT(registerServers()));
74 }
75 
76 void ServersStatusWidget::addServer(const ServerPtr &server)
77 {
78  const PlayersList &players = server->players();
79  numPlayers += players.numClients();
80  numBots += players.numBots();
81  updateDisplay();
82 }
83 
84 void ServersStatusWidget::mousePressEvent(QMouseEvent* event)
85 {
86  if (event->button() == Qt::LeftButton)
87  {
88  emit clicked(serverList);
89  }
90 }
91 
92 void ServersStatusWidget::paintEvent(QPaintEvent *event)
93 {
94  QPainter p(this);
95  p.setRenderHint(QPainter::SmoothPixmapTransform);
96  p.drawPixmap(2, 2, 18, 18, bMasterIsEnabled ? icon : iconDisabled);
97  p.end();
98 
99  QLabel::paintEvent(event);
100 }
101 
102 void ServersStatusWidget::registerServers()
103 {
104  // Since this is done when the list changes we should reset some values
105  numPlayers = 0;
106  numBots = 0;
107 
108  if (serverList != NULL)
109  {
110  foreach(ServerPtr server, serverList->servers())
111  {
112  connect(server.data(), SIGNAL(begunRefreshing(ServerPtr)), this, SLOT(removeServer(ServerPtr)), Qt::DirectConnection);
113  connect(server.data(), SIGNAL(updated(ServerPtr, int)), this, SLOT(addServer(ServerPtr)), Qt::DirectConnection);
114  }
115  }
116 }
117 
118 void ServersStatusWidget::removeServer(const ServerPtr &server)
119 {
120  const PlayersList &players = server->players();
121  numPlayers -= players.numClients();
122  numBots -= players.numBots();
123  updateDisplay();
124 }
125 
127 {
128  this->bMasterIsEnabled = bEnabled;
129  updateDisplay();
130 }
131 
132 void ServersStatusWidget::updateDisplay()
133 {
134  if (bMasterIsEnabled)
135  {
136  setText(QString("%1-%2 %3").arg(numPlayers).arg(numBots).arg(serverList != NULL ? serverList->numServers() : 0));
137  }
138  else
139  {
140  setText("N/A");
141  }
142 }
void setMasterEnabledStatus(bool bEnabled)
Changes the appearance of the widget basing on the boolean value.
Abstract base for all MasterClients.
Definition: masterclient.h:49
int numClients() const
Overall number of people and bots on the server.