summaryrefslogtreecommitdiffstats
path: root/wifi/kwireless/kwirelesswidget.cpp
blob: e7b6a719384ba925a152dc4cf3354418e8078d75 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
  $ Author: Mirko Boehm $
  $ License: This code is licensed under the LGPL $
  $ Copyright: (C) 1996-2003, Mirko Boehm $
  $ Contact: Mirko Boehm <[email protected]>
         http://www.kde.org
         http://www.hackerbuero.org $
*/

#include <tqtimer.h>
#include <tqpainter.h>
#include <tqpen.h>
#include <tqcolor.h>
#include <tqpointarray.h>
#include <tdeglobalsettings.h>
#include <kdebug.h>
#include <kpassivepopup.h>
#include <tdelocale.h>

#include "kwirelesswidget.h"
#include "propertytable.h"
#include "linuxwirelesswidget.h"

extern "C" {
#include <math.h>
}

TQPtrList<DeviceInfo> KWireLessWidget::deviceInfo;
TQMutex KWireLessWidget::mutex;
TQTimer *KWireLessWidget::timer;
int KWireLessWidget::m_instances;

DeviceInfo::DeviceInfo(TQString _device, TQString _essid, TQString _encr,
                       float _quality, float _signal, float _noise,
                       int _bitrate)
    : m_device(_device),
      m_essid(_essid),
      m_quality(_quality),
      m_noise(_noise),
      m_signal(_signal),
      m_bitrate(_bitrate),
      m_encr(_encr)
{
}

float DeviceInfo::quality()
{
    return m_quality;
}

TQString DeviceInfo::qualityString()
{
    return i18n("%1%").arg(TQString::number(m_quality*100, 'f', 0));
}

float DeviceInfo::signal()
{
    return m_signal;
}

TQString DeviceInfo::signalString()
{
    return i18n("%1%").arg(TQString::number(m_signal*100, 'f', 0));
}

float DeviceInfo::noise()
{
    return m_noise ;
}

TQString DeviceInfo::noiseString()
{
    return i18n("%1%").arg(TQString::number(m_noise*100, 'f', 0));
}

const TQString& DeviceInfo::device()
{
    return m_device;
}

const TQString& DeviceInfo::essid()
{
    return m_essid;
}


TQString DeviceInfo::bitrateString()
{
    TQString bitrate;
    TQTextOStream b(&bitrate);
    b.precision(2);

    switch((int)log10(m_bitrate))
    {
    case 0:
    case 1:
    case 2:
        b << m_bitrate << " bit/s";
        break;
    case 3:
    case 4:
    case 5:
        b << m_bitrate/1000 << " kbit/s";
        break;
    case 6:
    case 7:
    case 8:
        b << m_bitrate/1000000 << " Mbit/s";
        break;
    case 9:
    case 10:
    case 11:
        b << m_bitrate/1000000000 << " Gbit/s";
        break;
    default:
        b << m_bitrate << " bit/s (!)";
    };
    return bitrate;
}

bool DeviceInfo::usesEncryption()
{
    return !m_encr.isEmpty();
}

TQString DeviceInfo::encrString()
{
    if(m_encr.isEmpty())
    {
        return i18n("unknown");
    } else {
        // we rely on the information provided (that is, it has to
        // translated already!):
        return m_encr;
    }
}

KWireLessWidget::KWireLessWidget(TQWidget *parent, const char* name)
    : TQWidget(parent, name),
      mode(Horizontal),
      frameWidth(1),
      qualityBarWidth(6),
      signalBarWidth(3),
      noiseBarWidth(3)
{
    ++m_instances;
    deviceInfo.setAutoDelete(true);
    // set up the poll timer:
    if(timer == 0)
    {   // this way, only the poll() method of the first instance will
        // ever be called (intended behaviour):
        timer = new TQTimer(this);
        connect(timer, TQT_SIGNAL(timeout()), TQT_SLOT(poll()));
        // single shot, will be restartet at the end of poll ()
        timer->start(100, true);
    }
}

KWireLessWidget::~KWireLessWidget()
{
    --m_instances;
}

int KWireLessWidget::preferredHeight()
{
    return preferredWidth();
}

int KWireLessWidget::preferredWidth()
{
    return 4*frameWidth + qualityBarWidth + signalBarWidth + noiseBarWidth;
}

int KWireLessWidget::instances()
{
    return m_instances;
}

void KWireLessWidget::setMode(KWireLessWidget::Mode _mode)
{
    mode = _mode;
}

void KWireLessWidget::mousePressEvent(TQMouseEvent *e)
{
    if( e->button() == Qt::LeftButton)
    {
        PropertiesDialog dialog(this);
        connect(this, TQT_SIGNAL(updateDeviceInfo(TQPtrList<DeviceInfo> *)),
                &dialog, TQT_SLOT(update(TQPtrList<DeviceInfo> *)));
        dialog.exec();
    }
}

void KWireLessWidget::paintEvent(TQPaintEvent*)
{
    // WORK_TO_DO: paint other devices, too:
    // for quick repaints, we need a buffered painter!
    int w, h, space;
    const int bevel = qualityBarWidth/2;
    TQPainter painter(this);
    TQPointArray points;
    TQColor color, brush;

    // do stuff that does not rely on device being valid (e.g.,
    // non-nil):
    space = 3 * frameWidth + qualityBarWidth + signalBarWidth + noiseBarWidth+1;

    if(mode ==Qt::Horizontal)
    {
        h = space;
        w = width();
    } else {
        // Vertical
        h = height();
        w = space;
    }

    painter.setPen(TQPen(TQt::black, frameWidth));
    painter.setBrush(TDEGlobalSettings::baseColor());
    painter.drawRect(0, 0, w, h);

    int x = qualityBarWidth + frameWidth;
    mode ==Qt::Vertical
          ? painter.drawLine(x, frameWidth, x, h-frameWidth)
          : painter.drawLine(frameWidth, x, w-frameWidth, x);
    x += signalBarWidth + frameWidth;
    mode ==Qt::Vertical
          ? painter.drawLine(x, frameWidth, x, h-frameWidth)
          : painter.drawLine(frameWidth, x, w-frameWidth, x);

    DeviceInfo *device = deviceInfo.getFirst();
    if(device != 0)
    {
        // draw the quality bar:
        switch((int)(device->quality()*3))
        {
        case 0:
            color = TQt::darkRed;
            brush = TQt::red;
            break;
        case 1:
            color = TQt::darkYellow;
            brush = TQt::yellow;
            break;
        default:
            color = TQt::darkGreen;
            brush = TQt::green;
        };
        if(mode ==Qt::Vertical)
        {
            int position = (int)(((float)(h-2*frameWidth))*(1-device->quality())+0.5);

            points.putPoints
                (0, 4,
                 frameWidth, h - 2*frameWidth,
                 frameWidth, position + bevel/2,
                 qualityBarWidth, TQMAX(position - bevel/2, frameWidth),
                 qualityBarWidth, h - 2*frameWidth);
        } else {
            int position = (int)(((float)(w-2*frameWidth))*device->quality()+0.5);

            points.putPoints
                (0, 4,
                 frameWidth, frameWidth,
                 TQMIN(position - bevel/2, w-frameWidth), frameWidth,
                 TQMIN(position + bevel/2, w-frameWidth), frameWidth+qualityBarWidth-1,
                 frameWidth, frameWidth+qualityBarWidth-1);
        }
        painter.setPen(TQPen(color));
        painter.setBrush(brush);
        painter.drawPolygon(points);

        // draw the signal bar:
        painter.setPen(TQPen(TQt::darkRed));
        painter.setBrush(TQt::red);

        if(mode ==Qt::Vertical)
        {
            int x = 2 * frameWidth + qualityBarWidth;
            int h1 = (int)(((float)(h-2*frameWidth))*(1-device->signal())+0.5);

            painter.drawRect(x, h1, signalBarWidth, h - frameWidth - h1);
        } else {
            int x = frameWidth;
            int y = 2 * frameWidth + qualityBarWidth;
            int w1 = (int)(((float)(w-2*frameWidth))*device->signal()+0.5);

            painter.drawRect(x, y, w1, signalBarWidth);
        }
        // draw the noise bar:
        painter.setPen(TQPen(TQt::gray));
        painter.setBrush(TQt::lightGray);

        if(mode ==Qt::Vertical)
        {
            int x = 3 * frameWidth + qualityBarWidth + signalBarWidth;
            int h1 = (int)(((float)(h-2*frameWidth))*(1-device->noise())+0.5);

            painter.drawRect(x, h1, signalBarWidth, h - frameWidth - h1);
        } else {
            int x = frameWidth;
            int y = 3 * frameWidth + qualityBarWidth + signalBarWidth;
            int w1 = (int)(((float)(w-2*frameWidth))*device->noise()+0.5);

            painter.drawRect(x, y, w1, signalBarWidth);
        }
    }
}

KWireLessWidget* KWireLessWidget::makeWireLessWidget(TQWidget *parent,
                                                     const char *name)
{
#if 1
    // defined linuxwirelesswidget
    return new LinuxWireLessWidget(parent, name);
#else
#error KWireLess is not available for your OS
#endif
    // just to shut the compiler up:
    parent = parent; name = name; return 0;
}

#include "kwirelesswidget.moc"