-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
statuswidget.cpp
115 lines (96 loc) · 2.97 KB
/
statuswidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/** @file statuswidget.cpp Widget for status information.
*
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "statuswidget.h"
#include <QTimer>
#include <de/shell/TextRootWidget>
using namespace de;
using namespace de::shell;
struct StatusWidget::Instance
{
StatusWidget &self;
Link *link;
QTimer *updateTimer;
Instance(StatusWidget &i) : self(i), link(0)
{
updateTimer = new QTimer(&self);
}
};
StatusWidget::StatusWidget(String const &name)
: TextWidget(name), d(new Instance(*this))
{
connect(d->updateTimer, SIGNAL(timeout()), this, SLOT(redraw()));
}
StatusWidget::~StatusWidget()
{
delete d;
}
void StatusWidget::setShellLink(Link *link)
{
d->link = link;
if(link)
{
// Observe changes in link status.
connect(link, SIGNAL(connected()), this, SLOT(linkConnected()));
connect(link, SIGNAL(disconnected()), this, SLOT(linkDisconnected()));
}
root().requestDraw();
}
void StatusWidget::draw()
{
if(!targetCanvas()) return;
Rectanglei pos = rule().recti();
TextCanvas buf(pos.size());
if(!d->link || d->link->status() == Link::Disconnected)
{
String msg = "Not connected to a server";
buf.drawText(Vector2i(buf.size().x/2 - msg.size()/2), msg, TextCanvas::Char::Bold);
}
else if(d->link->status() == Link::Connecting)
{
String msg = "Connecting to " + d->link->address().asText();
buf.drawText(Vector2i(buf.size().x/2 - msg.size()/2), msg);
}
else if(d->link->status() == Link::Connected)
{
TimeDelta elapsed = d->link->connectedAt().since();
String time = String("| %1:%2:%3")
.arg(int(elapsed.asHours()))
.arg(int(elapsed.asMinutes()) % 60)
.arg(int(elapsed) % 60);
String host = "| " + d->link->address().asText();
int x = buf.size().x - time.size() - 1;
buf.drawText(x, time);
x -= host.size() + 1;
buf.drawText(x, host);
}
buf.blit(*targetCanvas(), pos.topLeft);
}
void StatusWidget::redraw()
{
drawAndShow();
}
void StatusWidget::linkConnected()
{
d->updateTimer->start(1000);
redraw();
}
void StatusWidget::linkDisconnected()
{
d->updateTimer->stop();
redraw();
}