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>
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.
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.
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.
24
#include <config-workspace.h>
25
#include <config-kdm.h>
27
#include "kdmthemer.h"
31
#include <kmacroexpander.h>
35
#include <QFontMetrics>
45
#include <sys/utsname.h>
46
#if !defined(HAVE_GETDOMAINNAME) && defined(HAVE_SYS_SYSTEMINFO)
47
# include <sys/systeminfo.h>
50
KdmLabel::KdmLabel(QObject *parent, const QDomNode &node)
51
: KdmItem(parent, node)
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) :
64
label.active.present = false;
65
label.prelight.present = false;
67
const QString locale = KGlobal::locale()->language();
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();
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", "");
93
label.text = el.text();
94
} else if (tagName == "stock") {
95
label.text = lookupStock(el.attribute("type", ""));
100
label.isTimer = label.text.indexOf("%c") >= 0;
102
timer = new QTimer(this);
104
connect(timer, SIGNAL(timeout()), SLOT(update()));
107
zeroWidth = QFontMetrics(label.normal.font.font).width('0');
109
label.text.replace('\n', ' ');
110
setCText(lookupText(label.text));
114
KdmLabel::setText(const QString &txt)
117
label.text.replace('\n', ' ');
122
KdmLabel::setCText(const QString &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()));
133
pText.remove(pAccelOff, 1);
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)))
141
newSize.rwidth() += zeroWidth;
143
emit needPlacement();
145
pTextIndent = bbox.left();
154
emit activated(cp->objectName());
157
cp = qobject_cast<KdmItem *>(cp->parent());
159
if (!buddy.isEmpty())
164
KdmLabel::doPlugActions(bool plug)
167
QWidget *w = themer()->widget();
169
w->addAction(action);
171
w->removeAction(action);
182
KdmLabel::drawContents(QPainter *p, const QRect &r)
184
// choose the correct label class
185
struct LabelStruct::LabelClass *l = &label.normal;
186
if (state == Sactive && label.active.present)
188
else if (state == Sprelight && label.prelight.present)
191
p->setFont(l->font.font);
194
if (pAccelOff != -1) {
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);
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));
210
p->drawText(area, 0, cText);
212
p->setClipping(false);
216
KdmLabel::statusChanged(bool descend)
218
KdmItem::statusChanged(descend);
219
if (!label.active.present && !label.prelight.present)
221
if ((state == Sprelight && !label.prelight.present) ||
222
(state == Sactive && !label.active.present))
224
updateWidgetAttribs();
232
QString text = lookupText(label.text);
240
#define I18N_NOOP(t) 0, t
242
#define I18N_NOOP2(c,t) c, t
244
static const struct {
245
const char *type, *comment, *text;
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") }
267
KdmLabel::lookupStock(const QString &stock)
269
QString type(stock.toLower());
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);
276
return i18n(stocks[i].text);
279
kWarning() << "Invalid <stock> element '" << stock << "'. Check your theme!";
283
QString KdmLabel::timedUser = QString();
284
int KdmLabel::timedDelay = -1;
285
QHash<QChar, QString> KdmLabel::expandoMap;
286
bool KdmLabel::dateFormatSet = false;
289
KdmLabel::expandMacro(QChar chr, QStringList &ret)
291
switch (chr.unicode()) {
293
ret << i18ncp("will login in ...", "1 second", "%1 seconds", timedDelay);
299
if (!dateFormatSet) {
300
// xgettext:no-c-format
301
KGlobal::locale()->setDateFormat(i18nc("date format", "%a %d %B"));
302
dateFormatSet = true;
304
ret << KGlobal::locale()->formatDateTime(QDateTime::currentDateTime(), KLocale::LongDate);
308
if (expandoMap.isEmpty()) {
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);
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);
325
QHash<QChar, QString>::const_iterator mi = expandoMap.constFind(chr);
326
if (mi != expandoMap.constEnd()) {
335
KdmLabel::lookupText(const QString &t)
343
KdmLabel::setWidget(QWidget *widget)
345
KdmItem::setWidget(widget);
346
updateWidgetAttribs();
350
KdmLabel::updateWidgetAttribs()
354
struct LabelStruct::LabelClass *l = &label.normal;
355
if (state == Sactive && label.active.present)
357
else if (state == Sprelight && label.prelight.present)
359
myWidget->setFont(l->font.font);
361
p.setColor(QPalette::WindowText, l->color);
362
myWidget->setPalette(p);
365
#include "kdmlabel.moc"