serverdetailsdock.cpp
1 //------------------------------------------------------------------------------
2 // serverdetailsdock.cpp
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) 2014 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #include <QAction>
25 #include <QBoxLayout>
26 
27 #include "serverapi/playerslist.h"
28 #include "serverapi/server.h"
29 #include "serverapi/serverptr.h"
31 #include "serverapi/tooltips/tooltipgenerator.h"
32 #include "serverdetailsdock.h"
33 #include "ui_serverdetailsdock.h"
34 
35 DClass<ServerDetailsDock> : public Ui::ServerDetailsDock
36 {
37 };
38 
39 DPointered(ServerDetailsDock)
40 
41 ServerDetailsDock::ServerDetailsDock(QWidget *parent) : QDockWidget(parent)
42 {
43  d->setupUi(this);
44  this->toggleViewAction()->setIcon(QIcon(":/icons/server_details.png"));
45 
46  clear();
47 }
48 
49 ServerDetailsDock::~ServerDetailsDock()
50 {
51 }
52 
53 void ServerDetailsDock::clear()
54 {
55  d->lblServer->setText("");
56  d->sbArea->setText("");
57  d->detailsArea->setText("");
58 }
59 
60 void ServerDetailsDock::displaySelection(QList<ServerPtr> &selectedServers)
61 {
62  ServerPtr server = selectServer(selectedServers);
63  if (server == nullptr)
64  {
65  clear();
66  return;
67  }
68 
69  d->lblServer->setText(server->name());
70  TooltipGenerator *tooltipGenerator = server->tooltipGenerator();
71  if (server->players().numClients() != 0)
72  d->sbArea->setText(tooltipGenerator->playerTableHTML());
73  else
74  d->sbArea->setText(QString());
75  d->detailsArea->setText(QString("<div>%1</div>%2").arg(server->customDetails()).arg(tooltipGenerator->dmflagsHTML()));
76  delete tooltipGenerator;
77 }
78 
79 void ServerDetailsDock::reorientContentsBasingOnDimensions()
80 {
81  const int TOPTOBOTTOM_PREFERENCE_THRESHOLD = 100;
82  if (height() + TOPTOBOTTOM_PREFERENCE_THRESHOLD > width())
83  static_cast<QBoxLayout *>(d->dataLayout)->setDirection(QBoxLayout::TopToBottom);
84  else
85  static_cast<QBoxLayout *>(d->dataLayout)->setDirection(QBoxLayout::LeftToRight);
86 }
87 
88 void ServerDetailsDock::resizeEvent(QResizeEvent *event)
89 {
90  Q_UNUSED(event);
91  reorientContentsBasingOnDimensions();
92 }
93 
94 ServerPtr ServerDetailsDock::selectServer(QList<ServerPtr> &selectedServers)
95 {
96  if (selectedServers.count() == 0)
97  return ServerPtr();
98  if (!selectedServers[0]->isKnown())
99  return ServerPtr();
100  return selectedServers[0];
101 }
virtual QString dmflagsHTML()
DMFLAGS listing for the current server.
virtual QString playerTableHTML()
Player table that is created when cursor hovers over players column.