[kde-edu]: Icons for special characters

Peter Hedlund peter at peterandlinda.com
Fri Apr 30 04:15:50 CEST 2004


Hi,

I have written the function below to generate an icon for an arbitrary 
character. It's used in KWordQuiz to provide a toolbar for special 
characters. The code can be used in a KAction constructor or in 
KAction::setIcon(). IMHO the generated icons scale very well from 16 to 48, 
the sizes usually available for KToolbar.

If you like the idea the code could be used in e.g. KHangman and KLettres, 
programs that also provide toolbars for special characters. Then there would 
be no need to distribute the ready-made icons currently included with these 
programs.

Comments?

Peter

QString KWordQuizApp::charIcon(const QChar & c)
{
  ///Create a name and path for the icon
  QString s = locateLocal("icon", "char" + QString::number(c.unicode()) + 
".png");
  
  ///No need to redraw if it already exists
  if (KStandardDirs::exists(s))
    return s;
  
  QRect r(4, 4, 120, 120);

  ///A font to draw the character with
  QFont font("sans");
  font.setPixelSize(100);
  font.setWeight(QFont::Bold);
  
  ///Create the pixmap        
  QPixmap pm(128, 128);
  pm.fill(Qt::white);
  QPainter p(&pm);
  p.setFont(font);
  p.setPen(Qt::blue);
  p.drawText(r, Qt::AlignCenter, (QString) c);
  
  ///Create transparency mask
  QBitmap bm(128, 128);
  bm.fill(Qt::color0);
  QPainter b(&bm);
  b.setFont(font);
  b.setPen(Qt::color1);
  b.drawText(r, Qt::AlignCenter, (QString) c);
  
  ///Mask the pixmap
  pm.setMask(bm); 
  
  ///Save the icon to disk
  pm.save(s, "PNG");
  
  return s;
}


More information about the kde-edu mailing list