2 * Copyright (C) 2003-2006 Andriy Rysin (rysin@kde.org)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <kstandarddirs.h>
31 #include "kxkbconfig.h"
34 static const int FLAG_MAX_WIDTH
= 21;
35 static const int FLAG_MAX_HEIGHT
= 14;
37 const QString
LayoutIcon::flagTemplate("l10n/%1/flag.png");
38 const QString
LayoutIcon::ERROR_CODE("error");
39 LayoutIcon
* LayoutIcon::instance
;
42 LayoutIcon
& LayoutIcon::getInstance() {
43 if( instance
== NULL
) {
44 instance
= new LayoutIcon();
49 LayoutIcon::LayoutIcon():
53 m_labelFont
.setPixelSize(10);
54 m_labelFont
.setWeight(QFont::Bold
);
58 LayoutIcon::findPixmap(const QString
& code_
, bool showFlag
, const QString
& displayName_
)
60 //kDebug() << "pixmap for" << code_ << displayName_;
63 if( code_
== ERROR_CODE
) {
64 pm
= m_pixmapCache
[ERROR_CODE
];
66 pm
= createErrorPixmap();
67 m_pixmapCache
.insert(ERROR_CODE
, pm
);
72 QString
displayName(displayName_
);
74 if( displayName
.isEmpty() ) {
75 displayName
= LayoutUnit::getDefaultDisplayName(code_
);
77 if( displayName
.length() > MAX_LABEL_LEN
)
78 displayName
= displayName
.left(MAX_LABEL_LEN
);
80 const QString
pixmapKey( showFlag
? code_
+ '.' + displayName
: displayName
);
82 pm
= m_pixmapCache
[pixmapKey
];
88 QString countryCode
= getCountryFromLayoutName( code_
);
89 flag
= KStandardDirs::locate("locale", flagTemplate
.arg(countryCode
));
92 if( flag
.isEmpty() ) {
93 pm
= new QPixmap(FLAG_MAX_WIDTH
, FLAG_MAX_HEIGHT
);
97 pm
= new QPixmap(flag
);
101 if( pm
->height() < FLAG_MAX_HEIGHT
) {
102 QPixmap
* pix
= new QPixmap(FLAG_MAX_WIDTH
, FLAG_MAX_HEIGHT
);
103 pix
->fill( Qt::lightGray
);
104 // pix->fill( QColor(qRgba(127,127,127,255)) );
107 // pix->setMask(mask);
109 int dy
= (pix
->height() - pm
->height()) / 2;
110 copyBlt( pix
, 0, dy
, pm
, 0, 0, -1, -1 );
111 // QPixmap* px = new QPixmap(21, 14);
112 // px->convertFromImage(img);*/
120 p
.setFont(m_labelFont
);
123 p
.drawText(1, 1, pm
->width(), pm
->height()-2, Qt::AlignCenter
, displayName
);
125 p
.drawText(0, 0, pm
->width(), pm
->height()-2, Qt::AlignCenter
, displayName
);
127 m_pixmapCache
.insert(pixmapKey
, pm
);
133 @brief Try to get country code from layout name in xkb before xorg 6.9.0
135 QString
LayoutIcon::getCountryFromLayoutName(const QString
& layoutName
)
139 if( layoutName
== "mkd" )
142 if( layoutName
== "srp" ) {
143 QString csFlagFile
= KStandardDirs::locate("locale", flagTemplate
.arg("cs"));
144 flag
= csFlagFile
.isEmpty() ? "yu" : "cs";
147 if( layoutName
.endsWith("/jp") )
150 if( layoutName
== "trq" || layoutName
== "trf" || layoutName
== "tralt" )
153 if( layoutName
.length() > 2 )
162 void LayoutIcon::dimPixmap(QPixmap
& pm
)
164 QImage image
= pm
.toImage();
165 for (int y
=0; y
<image
.height(); y
++)
166 for(int x
=0; x
<image
.width(); x
++)
168 QRgb rgb
= image
.pixel(x
,y
);
169 QRgb
dimRgb(qRgb(qRed(rgb
)*3/4, qGreen(rgb
)*3/4, qBlue(rgb
)*3/4));
170 image
.setPixel(x
, y
, dimRgb
);
172 pm
= QPixmap::fromImage(image
);
175 static const char* ERROR_LABEL
= "err";
178 QPixmap
* LayoutIcon::createErrorPixmap()
180 QPixmap
* pm
= new QPixmap(21, 14);
185 p
.setFont(m_labelFont
);
187 p
.drawText(1, 1, pm
->width(), pm
->height()-2, Qt::AlignCenter
, ERROR_LABEL
);
189 p
.drawText(0, 0, pm
->width(), pm
->height()-2, Qt::AlignCenter
, ERROR_LABEL
);
190 m_pixmapCache
.insert(ERROR_CODE
, pm
);