serversstatuswidget.cpp
1 //------------------------------------------------------------------------------
2 // serversstatuswidget.h
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #include <QHBoxLayout>
25 #include <QLabel>
26 #include <QMouseEvent>
27 #include <QPainter>
28 #include <cmath>
29 
30 #include "serversstatuswidget.h"
31 #include "gui/serverlist.h"
32 #include "plugins/engineplugin.h"
33 #include "serverapi/masterclient.h"
34 #include "serverapi/playerslist.h"
35 #include "serverapi/server.h"
36 #include "serverapi/serverlistcounttracker.h"
37 
38 ServersStatusWidget::ServersStatusWidget(const EnginePlugin *plugin, const ServerList *serverList)
39  : QLabel(), enabled(false), icon(plugin->icon())
40 {
41  this->plugin = plugin;
42 
43  this->countTracker = new ServerListCountTracker(this);
44  this->countTracker->setPluginFilter(plugin);
45  this->serverList = serverList;
46 
47  countTracker->connect(serverList, SIGNAL(serverRegistered(ServerPtr)),
48  SLOT(registerServer(ServerPtr)));
49  countTracker->connect(serverList, SIGNAL(serverDeregistered(ServerPtr)),
50  SLOT(deregisterServer(ServerPtr)));
51  this->connect(countTracker, SIGNAL(updated()), SLOT(updateDisplay()));
52  this->connect(countTracker, SIGNAL(updated()), SIGNAL(counterUpdated()));
53 
54  // Transform icon to grayscale format for disabled appearance
55  QImage iconImage = icon.toImage();
56  int width = iconImage.width();
57  int height = iconImage.height();
58  for (int x = 0; x < width; ++x)
59  {
60  for (int y = 0; y < height; ++y)
61  {
62  QRgb pixel = iconImage.pixel(x, y);
63  int alpha = qAlpha(pixel);
64  int gray = qGray(pixel);
65 
66  pixel = qRgba(gray, gray, gray, alpha);
67 
68  iconImage.setPixel(x, y, pixel);
69  }
70  }
71 
72  iconDisabled = QPixmap::fromImage(iconImage);
73 
74  // Have an inset frame unless we're on the Mac
75 #ifndef Q_OS_MAC
76  setFrameShape(QFrame::Panel);
77  setFrameShadow(QFrame::Sunken);
78 #else
79  setFrameShape(QFrame::NoFrame);
80 #endif
81 
82  setFixedHeight(22);
83  setToolTip(tr("Players (Humans + Bots) / Servers Refreshed%"));
84 
85  setIndent(22);
86  updateDisplay();
87 }
88 
89 const ServerListCount &ServersStatusWidget::count() const
90 {
91  return countTracker->count();
92 }
93 
94 void ServersStatusWidget::mousePressEvent(QMouseEvent* event)
95 {
96  if (event->button() == Qt::LeftButton)
97  {
98  emit clicked(plugin);
99  }
100 }
101 
102 void ServersStatusWidget::paintEvent(QPaintEvent *event)
103 {
104  QPainter p(this);
105  p.setRenderHint(QPainter::SmoothPixmapTransform);
106  p.drawPixmap(2, 2, 18, 18, enabled ? icon : iconDisabled);
107  p.end();
108 
109  QLabel::paintEvent(event);
110 }
111 
112 QString ServersStatusWidget::refreshedPercentAsText() const
113 {
114  const ServerListCount &count = countTracker->count();
115  if (count.numServers == 0)
116  {
117  return tr("N/A");
118  }
119  else
120  {
121  return tr("%1%").arg(count.refreshedPercent());
122  }
123 }
124 
126 {
127  this->enabled = bEnabled;
128  updateDisplay();
129 }
130 
131 void ServersStatusWidget::updateDisplay()
132 {
133  if (enabled)
134  {
135  const ServerListCount &count = countTracker->count();
136  QString text = tr("%1 (%2+%3) / %4").arg(count.numPlayers).arg(count.numHumanPlayers)
137  .arg(count.numBots).arg(count.numServers);
138  if (count.numRefreshing > 0)
139  {
140  text += tr(" %1").arg(refreshedPercentAsText());
141  }
142  setText(text);
143  }
144  else
145  {
146  setText(tr("N/A"));
147  }
148 }
void setMasterEnabledStatus(bool bEnabled)
Changes the appearance of the widget basing on the boolean value.