not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / kxkb / pixmap.cpp
blobd0a094860110f74d1f27b80ce93bfaab12695103
1 /*
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.
19 #include <QImage>
20 #include <QFont>
21 #include <QPainter>
22 #include <QRegExp>
23 #include <QHash>
25 #include <kstandarddirs.h>
26 #include <klocale.h>
27 #include <kdebug.h>
29 #include "pixmap.h"
30 #include "rules.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();
46 return *instance;
49 LayoutIcon::LayoutIcon():
50 m_pixmapCache(),
51 m_labelFont("sans")
53 m_labelFont.setPixelSize(10);
54 m_labelFont.setWeight(QFont::Bold);
57 const QPixmap&
58 LayoutIcon::findPixmap(const QString& code_, bool showFlag, const QString& displayName_)
60 //kDebug() << "pixmap for" << code_ << displayName_;
61 QPixmap* pm = NULL;
63 if( code_ == ERROR_CODE ) {
64 pm = m_pixmapCache[ERROR_CODE];
65 if( pm == NULL ) {
66 pm = createErrorPixmap();
67 m_pixmapCache.insert(ERROR_CODE, pm);
69 return *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];
83 if( pm )
84 return *pm;
86 QString flag;
87 if( showFlag ) {
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);
94 pm->fill(Qt::gray);
96 else {
97 pm = new QPixmap(flag);
98 dimPixmap( *pm );
100 #if 0
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)) );
105 // QBitmap mask;
106 // mask.fill(1);
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);*/
113 delete pm;
114 pm = pix;
116 #endif
119 QPainter p(pm);
120 p.setFont(m_labelFont);
122 p.setPen(Qt::black);
123 p.drawText(1, 1, pm->width(), pm->height()-2, Qt::AlignCenter, displayName);
124 p.setPen(Qt::white);
125 p.drawText(0, 0, pm->width(), pm->height()-2, Qt::AlignCenter, displayName);
127 m_pixmapCache.insert(pixmapKey, pm);
129 return *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)
137 QString flag;
139 if( layoutName == "mkd" )
140 flag = "mk";
141 else
142 if( layoutName == "srp" ) {
143 QString csFlagFile = KStandardDirs::locate("locale", flagTemplate.arg("cs"));
144 flag = csFlagFile.isEmpty() ? "yu" : "cs";
146 else
147 if( layoutName.endsWith("/jp") )
148 flag = "jp";
149 else
150 if( layoutName == "trq" || layoutName == "trf" || layoutName == "tralt" )
151 flag = "tr";
152 else
153 if( layoutName.length() > 2 )
154 flag = "";
155 else
156 flag = layoutName;
158 return flag;
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";
177 //private
178 QPixmap* LayoutIcon::createErrorPixmap()
180 QPixmap* pm = new QPixmap(21, 14);
181 pm->fill(Qt::white);
183 QPainter p(pm);
185 p.setFont(m_labelFont);
186 p.setPen(Qt::red);
187 p.drawText(1, 1, pm->width(), pm->height()-2, Qt::AlignCenter, ERROR_LABEL);
188 p.setPen(Qt::blue);
189 p.drawText(0, 0, pm->width(), pm->height()-2, Qt::AlignCenter, ERROR_LABEL);
190 m_pixmapCache.insert(ERROR_CODE, pm);
192 return pm;