~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kdm/kfrontend/themer/kdmlabel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2003 by Unai Garro <ugarro@users.sourceforge.net>
 
3
 *  Copyright (C) 2004 by Enrico Ros <rosenric@dei.unipd.it>
 
4
 *  Copyright (C) 2004 by Stephan Kulow <coolo@kde.org>
 
5
 *  Copyright (C) 2004 by Oswald Buddenhagen <ossi@kde.org>
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
20
 */
 
21
 
 
22
#include "kdmlabel.h"
 
23
 
 
24
#include <config-workspace.h>
 
25
#include <config-kdm.h>
 
26
 
 
27
#include "kdmthemer.h"
 
28
 
 
29
#include <kglobal.h>
 
30
#include <klocale.h>
 
31
#include <kmacroexpander.h>
 
32
 
 
33
#include <QAction>
 
34
#include <QDateTime>
 
35
#include <QFontMetrics>
 
36
#include <QHash>
 
37
#include <QPainter>
 
38
#include <QTimer>
 
39
#include <QX11Info>
 
40
 
 
41
#include <X11/Xlib.h>
 
42
#include <fixx11h.h>
 
43
 
 
44
#include <unistd.h>
 
45
#include <sys/utsname.h>
 
46
#if !defined(HAVE_GETDOMAINNAME) && defined(HAVE_SYS_SYSTEMINFO)
 
47
# include <sys/systeminfo.h>
 
48
#endif
 
49
 
 
50
KdmLabel::KdmLabel(QObject *parent, const QDomNode &node)
 
51
    : KdmItem(parent, node)
 
52
    , action(0)
 
53
{
 
54
    itemType = "label";
 
55
    if (!isVisible())
 
56
        return;
 
57
 
 
58
    // Set default values for label (note: strings are already Null)
 
59
    label.normal.font = label.active.font = label.prelight.font = style.font;
 
60
    label.normal.color = label.active.color = label.prelight.color =
 
61
        style.palette.isBrushSet(QPalette::Normal, QPalette::WindowText) ?
 
62
            style.palette.color(QPalette::Normal, QPalette::WindowText) :
 
63
            QColor(Qt::white);
 
64
    label.active.present = false;
 
65
    label.prelight.present = false;
 
66
 
 
67
    const QString locale = KGlobal::locale()->language();
 
68
 
 
69
    // Read LABEL TAGS
 
70
    QDomNodeList childList = node.childNodes();
 
71
    bool stockUsed = false;
 
72
    for (int nod = 0; nod < childList.count(); nod++) {
 
73
        QDomNode child = childList.item(nod);
 
74
        QDomElement el = child.toElement();
 
75
        QString tagName = el.tagName();
 
76
 
 
77
        if (tagName == "normal") {
 
78
            parseColor(el, label.normal.color);
 
79
            parseFont(el, label.normal.font);
 
80
        } else if (tagName == "active") {
 
81
            label.active.present = true;
 
82
            parseColor(el, label.active.color);
 
83
            parseFont(el, label.active.font);
 
84
        } else if (tagName == "prelight") {
 
85
            label.prelight.present = true;
 
86
            parseColor(el, label.prelight.color);
 
87
            parseFont(el, label.prelight.font);
 
88
        } else if (tagName == "text" && el.attributes().count() == 0 && !stockUsed) {
 
89
            label.text = el.text();
 
90
        } else if (tagName == "text" && !stockUsed) {
 
91
            QString lang = el.attribute("xml:lang", "");
 
92
            if (lang == locale)
 
93
                label.text = el.text();
 
94
        } else if (tagName == "stock") {
 
95
            label.text = lookupStock(el.attribute("type", ""));
 
96
            stockUsed = true;
 
97
        }
 
98
    }
 
99
 
 
100
    label.isTimer = label.text.indexOf("%c") >= 0;
 
101
    if (label.isTimer) {
 
102
        timer = new QTimer(this);
 
103
        timer->start(1000);
 
104
        connect(timer, SIGNAL(timeout()), SLOT(update()));
 
105
    }
 
106
 
 
107
    zeroWidth = QFontMetrics(label.normal.font.font).width('0');
 
108
 
 
109
    label.text.replace('\n', ' ');
 
110
    setCText(lookupText(label.text));
 
111
}
 
112
 
 
113
void
 
114
KdmLabel::setText(const QString &txt)
 
115
{
 
116
    label.text = txt;
 
117
    label.text.replace('\n', ' ');
 
118
    update();
 
119
}
 
120
 
 
121
void
 
122
KdmLabel::setCText(const QString &txt)
 
123
{
 
124
    delete action;
 
125
    action = 0;
 
126
    pText = cText = txt;
 
127
    pAccelOff = txt.indexOf('_');
 
128
    if (pAccelOff >= 0) {
 
129
        action = new QAction(this);
 
130
        action->setShortcut(Qt::ALT + txt[pAccelOff + 1].unicode());
 
131
        connect(action, SIGNAL(triggered(bool)), SLOT(activate()));
 
132
        emit needPlugging();
 
133
        pText.remove(pAccelOff, 1);
 
134
    }
 
135
    QRect bbox = QFontMetrics(label.normal.font.font).boundingRect(pText);
 
136
    QSize newSize = bbox.size();
 
137
    if (newSize.width() > pTextSize.width() ||
 
138
        (newSize.width() < (pTextSize.width() - zeroWidth)))
 
139
    {
 
140
        if (label.isTimer)
 
141
            newSize.rwidth() += zeroWidth;
 
142
        pTextSize = newSize;
 
143
        emit needPlacement();
 
144
    }
 
145
    pTextIndent = bbox.left();
 
146
}
 
147
 
 
148
void
 
149
KdmLabel::activate()
 
150
{
 
151
    KdmItem *cp = this;
 
152
    do {
 
153
        if (cp->isButton) {
 
154
            emit activated(cp->objectName());
 
155
            return;
 
156
        }
 
157
        cp = qobject_cast<KdmItem *>(cp->parent());
 
158
    } while (cp);
 
159
    if (!buddy.isEmpty())
 
160
        activateBuddy();
 
161
}
 
162
 
 
163
void
 
164
KdmLabel::doPlugActions(bool plug)
 
165
{
 
166
    if (action) {
 
167
        QWidget *w = themer()->widget();
 
168
        if (plug)
 
169
            w->addAction(action);
 
170
        else
 
171
            w->removeAction(action);
 
172
    }
 
173
}
 
174
 
 
175
QSize
 
176
KdmLabel::sizeHint()
 
177
{
 
178
    return pTextSize;
 
179
}
 
180
 
 
181
void
 
182
KdmLabel::drawContents(QPainter *p, const QRect &r)
 
183
{
 
184
    // choose the correct label class
 
185
    struct LabelStruct::LabelClass *l = &label.normal;
 
186
    if (state == Sactive && label.active.present)
 
187
        l = &label.active;
 
188
    else if (state == Sprelight && label.prelight.present)
 
189
        l = &label.prelight;
 
190
    // draw the label
 
191
    p->setFont(l->font.font);
 
192
    p->setPen(l->color);
 
193
    p->setClipRect(r);
 
194
    if (pAccelOff != -1) {
 
195
        QRect tarea(area);
 
196
        tarea.setLeft(tarea.left() - pTextIndent);
 
197
        QFontMetrics fm(l->font.font);
 
198
        QString left = pText.left(pAccelOff);
 
199
        p->drawText(area, 0, left);
 
200
        tarea.setLeft(tarea.left() + fm.width(left));
 
201
        QFont f(l->font.font);
 
202
        f.setUnderline(true);
 
203
        p->setFont(f);
 
204
        QString acc(pText[pAccelOff]);
 
205
        p->drawText(tarea, 0, acc);
 
206
        tarea.setLeft(tarea.left() + fm.width(acc));
 
207
        p->setFont(l->font.font);
 
208
        p->drawText(tarea, 0, pText.mid(pAccelOff + 1));
 
209
    } else {
 
210
        p->drawText(area, 0, cText);
 
211
    }
 
212
    p->setClipping(false);
 
213
}
 
214
 
 
215
void
 
216
KdmLabel::statusChanged(bool descend)
 
217
{
 
218
    KdmItem::statusChanged(descend);
 
219
    if (!label.active.present && !label.prelight.present)
 
220
        return;
 
221
    if ((state == Sprelight && !label.prelight.present) ||
 
222
        (state == Sactive && !label.active.present))
 
223
        return;
 
224
    updateWidgetAttribs();
 
225
    needUpdate();
 
226
}
 
227
 
 
228
void
 
229
KdmLabel::update()
 
230
{
 
231
    KdmItem::update();
 
232
    QString text = lookupText(label.text);
 
233
    if (text != cText) {
 
234
        setCText(text);
 
235
        needUpdate();
 
236
    }
 
237
}
 
238
 
 
239
#undef I18N_NOOP
 
240
#define I18N_NOOP(t) 0, t
 
241
#undef I18N_NOOP2
 
242
#define I18N_NOOP2(c,t) c, t
 
243
 
 
244
static const struct {
 
245
    const char *type, *comment, *text;
 
246
} stocks[] = {
 
247
    { "language",          I18N_NOOP2("@action:button", "Lan_guage") },
 
248
    { "session",           I18N_NOOP2("@action:button", "Session _Type") },
 
249
    { "system",            I18N_NOOP2("@action:button", "_Menu") }, // i18n("Actions");
 
250
    { "disconnect",        I18N_NOOP2("@action:button ... from XDMCP server", "Disconn_ect") },
 
251
    { "quit",              I18N_NOOP2("@action:button", "_Quit") },
 
252
    { "halt",              I18N_NOOP2("@action:button", "Power o_ff") },
 
253
//    { "suspend",           I18N_NOOP2("@action:button", "_Suspend") },
 
254
    { "reboot",            I18N_NOOP2("@action:button", "Re_boot") },
 
255
    { "chooser",           I18N_NOOP2("@action:button", "_Remote login") },
 
256
    { "caps-lock-warning", I18N_NOOP("Caps Lock is enabled") },
 
257
    { "timed-label",       I18N_NOOP("User %u will log in in %t") },
 
258
    { "welcome-label",     I18N_NOOP("Welcome to %h") }, // _greetString
 
259
    { "domain-label",      I18N_NOOP("_Domain:") },
 
260
    { "username-label",    I18N_NOOP("_Username:") },
 
261
    { "password-label",    I18N_NOOP("_Password:") },
 
262
    { "login",             I18N_NOOP2("@action:button", "_Login") }
 
263
};
 
264
 
 
265
// public static
 
266
QString
 
267
KdmLabel::lookupStock(const QString &stock)
 
268
{
 
269
    QString type(stock.toLower());
 
270
 
 
271
    for (uint i = 0; i < sizeof(stocks) / sizeof(stocks[0]); i++)
 
272
        if (type == stocks[i].type) {
 
273
            if (stocks[i].comment)
 
274
                return i18nc(stocks[i].comment, stocks[i].text);
 
275
            else
 
276
                return i18n(stocks[i].text);
 
277
        }
 
278
 
 
279
    kWarning() << "Invalid <stock> element '" << stock << "'. Check your theme!";
 
280
    return stock;
 
281
}
 
282
 
 
283
QString KdmLabel::timedUser = QString();
 
284
int KdmLabel::timedDelay = -1;
 
285
QHash<QChar, QString> KdmLabel::expandoMap;
 
286
bool KdmLabel::dateFormatSet = false;
 
287
 
 
288
bool
 
289
KdmLabel::expandMacro(QChar chr, QStringList &ret)
 
290
{
 
291
    switch (chr.unicode()) {
 
292
    case 't':
 
293
        ret << i18ncp("will login in ...", "1 second", "%1 seconds", timedDelay);
 
294
        return true;
 
295
    case 'u':
 
296
        ret << timedUser;
 
297
        return true;
 
298
    case 'c':
 
299
        if (!dateFormatSet) {
 
300
            // xgettext:no-c-format
 
301
            KGlobal::locale()->setDateFormat(i18nc("date format", "%a %d %B"));
 
302
            dateFormatSet = true;
 
303
        }
 
304
        ret << KGlobal::locale()->formatDateTime(QDateTime::currentDateTime(), KLocale::LongDate);
 
305
        return true;
 
306
    }
 
307
 
 
308
    if (expandoMap.isEmpty()) {
 
309
        struct utsname uts;
 
310
        uname(&uts);
 
311
        expandoMap['d'] = QString::fromLocal8Bit(DisplayString(QX11Info::display()));
 
312
        expandoMap['n'] = QString::fromLocal8Bit(uts.nodename);
 
313
        expandoMap['s'] = QString::fromLocal8Bit(uts.sysname);
 
314
        expandoMap['r'] = QString::fromLocal8Bit(uts.release);
 
315
        expandoMap['m'] = QString::fromLocal8Bit(uts.machine);
 
316
        char buf[256];
 
317
        buf[sizeof(buf) - 1] = '\0';
 
318
        expandoMap['h'] = gethostname(buf, sizeof(buf) - 1) ? "localhost" : QString::fromLocal8Bit(buf);
 
319
#ifdef HAVE_GETDOMAINNAME
 
320
        expandoMap['o'] = getdomainname(buf, sizeof(buf) - 1) ? "localdomain" : QString::fromLocal8Bit(buf);
 
321
#elif defined(HAVE_SYS_SYSTEMINFO)
 
322
        expandoMap['o'] = (unsigned)sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf)) > sizeof(buf) ? "localdomain" : QString::fromLocal8Bit(buf);
 
323
#endif
 
324
    }
 
325
    QHash<QChar, QString>::const_iterator mi = expandoMap.constFind(chr);
 
326
    if (mi != expandoMap.constEnd()) {
 
327
        ret << mi.value();
 
328
        return true;
 
329
    }
 
330
 
 
331
    return false;
 
332
}
 
333
 
 
334
QString
 
335
KdmLabel::lookupText(const QString &t)
 
336
{
 
337
    QString text = t;
 
338
    expandMacros(text);
 
339
    return text;
 
340
}
 
341
 
 
342
void
 
343
KdmLabel::setWidget(QWidget *widget)
 
344
{
 
345
    KdmItem::setWidget(widget);
 
346
    updateWidgetAttribs();
 
347
}
 
348
 
 
349
void
 
350
KdmLabel::updateWidgetAttribs()
 
351
{
 
352
    if (!myWidget)
 
353
        return;
 
354
    struct LabelStruct::LabelClass *l = &label.normal;
 
355
    if (state == Sactive && label.active.present)
 
356
        l = &label.active;
 
357
    else if (state == Sprelight && label.prelight.present)
 
358
        l = &label.prelight;
 
359
    myWidget->setFont(l->font.font);
 
360
    QPalette p;
 
361
    p.setColor(QPalette::WindowText, l->color);
 
362
    myWidget->setPalette(p);
 
363
}
 
364
 
 
365
#include "kdmlabel.moc"