Source Code (Use browser search to find items of interest.)

Class Index

kfontmanager'KFontExplorer (./kdebase/kfontmanager/kfontexplorer.h:38)

class KFontExplorer : public QDialog {

    Q_OBJECT

      // Usage of the KFontDialog Class:
      //
      // Case 1) The pointer fontlist is null ( Recommended Usage !!)
      // 
      // In this case KFontDialog will first search
      // for ~/.kde/config/kdefonts. If kdefonts if available
      // it will insert the fonts listed there into the family combo.
      // 
      // Note: ~/.kde/config/kdefonts is managed by kfontmanager. 
      // ~/.kde/config/kdefonts is a newline separated list of font names.
      // Such as: time\nhelvetica\nfixed\n etc.You should however not 
      // manipulate that list -- that is the job of kfontmanager.
      // 
      // If ~/.kde/config/kdefonts doesn't exist, KFontDialog will query
      // the X server and insert all availabe fonts.
      //
      // Case 2) The pointer fontlist is non null. In this cae KFontDialog 
      // will insert the strings of that QStrList into the family combo.
      // 
      // Note: Due to a bug in Qt 1.2 you must 
      // supply at this point at least two fonts in the QStrList that
      // fontlist points to. The bug has been reported and will hopefully
      // be fixed in Qt.1.3. 


public:
    KFontExplorer( QWidget *parent = 0L, const char *name = 0L,
			bool modal = FALSE, const QStrList* fontlist = 0L );

    void setFont( const QFont &font );
    QFont font()	{  return selFont; }

    /*
     * This is probably the function you are looking for.
     * Just call this to pop up a dialog to get the selected font.
     * returns result().
     */

    static int getFont( QFont &theFont );

signals:
	/*
	 * connect to this to monitor the font as it as selected if you are
	 * not running modal.
	 */
	void fontSelected( const QFont &font );

private slots:

      void 	family_chosen_slot(const QString & );
      void      size_chosen_slot(const QString & );
      void      weight_chosen_slot(const QString &);
      void      style_chosen_slot(const QString &);
      void      display_example(const QFont &font);
      void      charset_chosen_slot(int index);
      void 	setColors();

private:

    bool loadKDEInstalledFonts();
    void fill_family_combo();
    void setCombos();
   
    // pointer to an optinally supplied list of fonts to 
    // inserted into the fontdialog font-family combo-box
    QStrList     *fontlist; 

    QLabel	 *actual_family_label_data;
    QLabel	 *actual_size_label_data;
    QLabel       *actual_weight_label_data;
    QLabel       *actual_style_label_data;
    QLabel	 *actual_charset_label_data;

    QComboBox    *family_combo;
    QComboBox    *size_combo;
    QComboBox    *weight_combo;
    QComboBox    *style_combo;
    QComboBox	 *charset_combo;    

    QPushButton	 *ok_button;
    QPushButton	 *cancel_button;

    QLabel       *example_label;
    QFont         selFont;
};


kfontmanager'KFontExplorer::KFontExplorer() (./kdebase/kfontmanager/kfontexplorer.cpp:57)

KFontExplorer::KFontExplorer( QWidget *parent, const char *name, 
   bool modal, const QStrList* fontlist)  : QDialog( parent, name, modal )
{
  QString string;
  QHBoxLayout *hbox;
  QVBoxLayout *vbox;
  QGroupBox *gbox;
  int i;
  
  QVBoxLayout *top = new QVBoxLayout(this,10,10);

  gbox = new QGroupBox(i18n("Requested Font"),this);
  top->addWidget(gbox);

  hbox = new QHBoxLayout(gbox,10,10);
  hbox->addStretch();
  /* 1. column */
  vbox = new QVBoxLayout(hbox);
  vbox->addSpacing(10);
  vbox->addWidget( new QLabel(i18n("Family:"), gbox) ); 
  vbox->addSpacing(10);
  vbox->addWidget( new QLabel(i18n("Weight:"), gbox) ); 
  vbox->addSpacing(10);
  vbox->addWidget( new QLabel(i18n("Charset:"),gbox) ); 
  vbox->addSpacing(10);
  /* 2. column */
  vbox = new QVBoxLayout(hbox);
  vbox->addSpacing(10);
  vbox->addWidget( family_combo = new QComboBox(false, gbox) );
  vbox->addSpacing(10);
  vbox->addWidget( weight_combo = new QComboBox(false, gbox) );
  vbox->addSpacing(10);
  vbox->addWidget(charset_combo = new QComboBox(false, gbox) );
  vbox->addSpacing(10);
  /* 3. column */
  hbox->addStretch();
  vbox = new QVBoxLayout(hbox);
  vbox->addSpacing(10);
  vbox->addWidget( new QLabel(i18n("Size:"),	gbox) ); 
  vbox->addSpacing(10);
  vbox->addWidget( new QLabel(i18n("Style:"),	gbox) ); 
  vbox->addSpacing(10);
  vbox->addWidget( new QLabel("", 		gbox) ); 
  vbox->addSpacing(10);
  /* 4. column */
  vbox = new QVBoxLayout(hbox);
  vbox->addSpacing(10);
  vbox->addWidget( size_combo = new QComboBox(false,  gbox) );
  vbox->addSpacing(10);
  vbox->addWidget( style_combo = new QComboBox(false, gbox) );
  vbox->addSpacing(10);
  vbox->addWidget( new QLabel("", 		      gbox) ); 
  vbox->addSpacing(10);
  hbox->addStretch();


  /* status dialogs... (second (lower) part of the display) */
  gbox = new QGroupBox(i18n("Actual Font"),this);
  top->addWidget(gbox);
  
  hbox = new QHBoxLayout(gbox,10,10);
  hbox->addSpacing(10);
  hbox->addStretch();

  vbox = new QVBoxLayout(hbox);
  vbox->addSpacing(10);
  vbox->addWidget( new QLabel(i18n("Charset:"),gbox) );
  vbox->addWidget( new QLabel(i18n("Family:") ,gbox) );
  vbox->addWidget( new QLabel(i18n("Size:")   ,gbox) );
  vbox->addWidget( new QLabel(i18n("Weight:") ,gbox) );
  vbox->addWidget( new QLabel(i18n("Style:")  ,gbox) );
  vbox->addSpacing(10);

  vbox = new QVBoxLayout(hbox);
  vbox->addSpacing(10);
  vbox->addWidget( actual_charset_label_data = new QLabel("",gbox) );
  vbox->addWidget( actual_family_label_data  = new QLabel("",gbox) );
  vbox->addWidget( actual_size_label_data    = new QLabel("",gbox) );
  vbox->addWidget( actual_weight_label_data  = new QLabel("",gbox) );
  vbox->addWidget( actual_style_label_data   = new QLabel("",gbox) );
  vbox->addSpacing(10);

  hbox->addStretch();
  
  vbox = new QVBoxLayout(hbox,10);
  vbox->addSpacing(10);
  example_label = new QLabel(i18n("Dolor Ipse"),gbox);
  example_label->setFont(selFont);
  example_label->setAlignment(AlignCenter);
  example_label->setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
  example_label->setLineWidth( 1 );
  example_label->setMinimumSize( 200,30 );
  vbox->addWidget( example_label,10 );
  vbox->addSpacing(10);

  hbox->addStretch();
  hbox->addSpacing(10);


  /* preset entries and connect widgets */
  if (fontlist) {
    if (fontlist->count()) {
      family_combo->insertStrList(fontlist, -1);
    }
  }
  else 
    fill_family_combo();
  connect( family_combo, SIGNAL(activated(const QString &)),
	   SLOT(family_chosen_slot(const QString &)) );

  for (i=0; i<ARRAYSIZE(fontsizes); i++)
      size_combo->insertItem( string.setNum((int)fontsizes[i]) );
  connect( size_combo, SIGNAL(activated(const QString &)),
	   SLOT(size_chosen_slot(const QString &)) );

  weight_combo->insertItem( i18n("normal") );
  weight_combo->insertItem( i18n("bold") );
  connect( weight_combo, SIGNAL(activated(const QString &)),
	   SLOT(weight_chosen_slot(const QString &)) );

  style_combo->insertItem( i18n("roman") );
  style_combo->insertItem( i18n("italic") );
  connect( style_combo, SIGNAL(activated(const QString &)),
	   SLOT(style_chosen_slot(const QString &)) );

  CHARSETS_COUNT = 0;
  QFont font;
  while (true) { 
      string = font.encodingName( (QFont::CharSet) CHARSETS_COUNT );
      if (!string.length())
          break;
      charset_combo->insertItem( string );
      CHARSETS_COUNT++;
  };
  connect( charset_combo, SIGNAL(activated(int)),
	   SLOT(charset_chosen_slot(int)) );


  connect(this,SIGNAL(fontSelected( const QFont&  )),
	  this,SLOT(display_example( const QFont&)));

  // let's initialize the display if possible
  if (family_combo->count()) {
    this->setFont(QFont(family_combo->text(0),12,QFont::Normal));
  }
  
  setColors();
  connect(KApplication::kApplication(),SIGNAL(kdisplayPaletteChanged()),
	  this,SLOT(setColors()));
}



kfontmanager'KFontExplorer::charset_chosen_slot() (./kdebase/kfontmanager/kfontexplorer.cpp:209)

void KFontExplorer::charset_chosen_slot(int index){

  selFont.setCharSet( (QFont::CharSet) index );
  emit fontSelected(selFont);
}


kfontmanager'KFontExplorer::getFont() (./kdebase/kfontmanager/kfontexplorer.cpp:215)

int KFontExplorer::getFont( QFont &theFont )
{
	KFontExplorer dlg( 0L, "Font Selector", true );
	dlg.setFont( theFont );
	int result = dlg.exec();

	if ( result == Accepted )
		theFont = dlg.font();

	return result;
}



kfontmanager'KFontExplorer::setFont() (./kdebase/kfontmanager/kfontexplorer.cpp:228)

void KFontExplorer::setFont( const QFont& aFont){

  selFont = aFont;
  setCombos();
  display_example(selFont);
}  



kfontmanager'KFontExplorer::family_chosen_slot() (./kdebase/kfontmanager/kfontexplorer.cpp:236)

void KFontExplorer::family_chosen_slot(const QString &family){

  selFont.setFamily(family);
  // display_example();
  emit fontSelected(selFont);
}


kfontmanager'KFontExplorer::size_chosen_slot() (./kdebase/kfontmanager/kfontexplorer.cpp:243)

void KFontExplorer::size_chosen_slot(const QString & size){
  
  QString size_string = size;

  selFont.setPointSize(size_string.toInt());
  // display_example();
  emit fontSelected(selFont);
}


kfontmanager'KFontExplorer::weight_chosen_slot() (./kdebase/kfontmanager/kfontexplorer.cpp:252)

void KFontExplorer::weight_chosen_slot(const QString &weight){

  QString weight_string = weight;

  if ( weight_string == i18n("normal"))
    selFont.setBold(false);
  if ( weight_string == i18n("bold"))
       selFont.setBold(true);
  // display_example();
  emit fontSelected(selFont);
}


kfontmanager'KFontExplorer::style_chosen_slot() (./kdebase/kfontmanager/kfontexplorer.cpp:264)

void KFontExplorer::style_chosen_slot(const QString &style){


  QString style_string = style;

  if ( style_string == i18n("roman"))
    selFont.setItalic(false);
  if ( style_string == i18n("italic"))
    selFont.setItalic(true);
  //  display_example();
  emit fontSelected(selFont);
}
       


kfontmanager'KFontExplorer::display_example() (./kdebase/kfontmanager/kfontexplorer.cpp:278)

void KFontExplorer::display_example(const QFont& font) {

  QString string;

  example_label->setFont(font);

  QFontInfo info = example_label->fontInfo();
  actual_family_label_data->setText(info.family());

  actual_size_label_data->setNum(info.pointSize());

  string = info.bold() ? i18n("Bold") : i18n("Normal");
  actual_weight_label_data->setText(string);
 
  string = info.italic() ? i18n("italic") : i18n("roman");
  actual_style_label_data->setText(string);
  
  QFont::CharSet charset = info.charSet();
  actual_charset_label_data->setText(font.encodingName(charset) );
}


kfontmanager'KFontExplorer::setCombos() (./kdebase/kfontmanager/kfontexplorer.cpp:299)

void KFontExplorer::setCombos() {

 QString string;
 QComboBox* combo;
 int number_of_entries, i=0; 
 bool found;

 number_of_entries =  family_combo->count(); 
 string = selFont.family();
 combo = family_combo; 
 found = false;

 for (i = 0;i < number_of_entries ; i++){
   //   printf("%s with %s\n",string.data(), ((QString) combo->text(i)).data());
   if ( string.lower() == ((QString) combo->text(i)).lower()){
     combo->setCurrentItem(i);
     //printf("Found Font %s\n",string.data());
     found = true;
     break;
   }
 }

 
 number_of_entries =  size_combo->count(); 
 string.setNum(selFont.pointSize());
 combo = size_combo; 
 found = false;

 for (i = 0;i < number_of_entries - 1; i++){
   if ( string == (QString) combo->text(i)){
     combo->setCurrentItem(i);
     found = true;
     // printf("Found Size %s setting to item %d\n",string.data(),i);
     break;
   }
 }

 if (selFont.bold())
   weight_combo->setCurrentItem(1);
 else
   weight_combo->setCurrentItem(0);

 if (selFont.italic())
   style_combo->setCurrentItem(1);
 else
   style_combo->setCurrentItem(0);

 charset_combo->setCurrentItem( (int)selFont.charSet() );

}


kfontmanager'KFontExplorer::loadKDEInstalledFonts() (./kdebase/kfontmanager/kfontexplorer.cpp:350)

bool KFontExplorer::loadKDEInstalledFonts() {

  QString fontfilename;

  fontfilename =  locate("config", "kdefonts");

  QFile fontfile(fontfilename);

  if (!fontfile.exists())
    return false;

  if(!fontfile.open(IO_ReadOnly)){
    return false;
  }

  if (!fontfile.isReadable())
    return false;
  
  
  QTextStream t(&fontfile);

  while (!t.eof()) {
    QString s(t.readLine());
    s = s.stripWhiteSpace();
    if (!s.isEmpty())
      family_combo->insertItem( s ,-1 );
  }

  fontfile.close();
  
  return true;
}



kfontmanager'KFontExplorer::fill_family_combo() (./kdebase/kfontmanager/kfontexplorer.cpp:384)

void KFontExplorer::fill_family_combo() {

  QStrList	*fontlist;
  QStrList	newlist(true);
  QString	current, qfontname;
  int		dash, dash_two;

  fontlist = KFontList_GetXFonts();

  // if available we are done, the kde fonts are now in the family_combo
  if (loadKDEInstalledFonts())
    return;

  for (fontlist->first(); fontlist->current(); fontlist->next()) {

    qfontname = fontlist->current();
    
    dash = qfontname.find('-', 1, true); // find next dash
    if (dash == -1)	// No such next dash -- this shouldn't happen.
	continue;	// but what do I care -- lets skip it.

    // the font name is between the second and third dash so:
    // let's find the third dash:

    dash_two = qfontname.find('-', dash+1, true); 
    if (dash_two == -1)
	continue;

    // fish the name of the font info string

    qfontname = qfontname.mid(dash+1, dash_two-dash-1);

    if (!qfontname.contains("open look", true))
      if (qfontname != "nil")
        if (newlist.find(qfontname) == -1) {
          family_combo->insertItem(qfontname,-1);
	  newlist.append(qfontname);
	}
	  
  } // end for()

  delete fontlist;
}



kfontmanager'KFontExplorer::setColors() (./kdebase/kfontmanager/kfontexplorer.cpp:429)

void KFontExplorer::setColors() {
  /* this is to the the backgound of a widget to white and the
     text color to black -- some lables such as the one of the
     font manager really shouldn't follow colorschemes The
     primary task of those label is to display the text clearly
     an visibly and not to look pretty ...*/

  QPalette mypalette( example_label->palette() );

  QColorGroup cgrp( mypalette.normal() );
  QColorGroup ncgrp(black,cgrp.background(),
		    cgrp.light(),cgrp.dark(),cgrp.mid(),black,white);

  mypalette.setNormal(ncgrp);
  mypalette.setDisabled(ncgrp);
  mypalette.setActive(ncgrp);

  example_label->setPalette(mypalette);
  example_label->setBackgroundColor(white);
}