📄 kfontdialog.cpp
字号:
}void KFontDialog::charset_chosen_slot(const char *chset){ KCharset(chset).setQFont(selFont); emit fontSelected(selFont);}int KFontDialog::getFont( QFont &theFont ){ KFontDialog dlg( 0L, "Font Selector", TRUE ); dlg.setFont( theFont ); int result = dlg.exec(); if ( result == Accepted ) theFont = dlg.font(); return result;}int KFontDialog::getFontAndText( QFont &theFont, QString &theString ){ KFontDialog dlg( 0L, "Font and Text Selector", TRUE ); dlg.setFont( theFont ); int result = dlg.exec(); if( result == Accepted ) { theFont = dlg.font(); theString = dlg.example_edit->text(); } return result;}void KFontDialog::setFont( const QFont& aFont){ selFont = aFont; setCombos(); display_example(selFont);} void KFontDialog::family_chosen_slot(const char* family){ selFont.setFamily(family); // Re-create displayable charsets list KCharsets *charsets=KApplication::getKApplication()->getCharsets(); QStrList lst=charsets->displayable(selFont.family()); charset_combo->clear(); for(const char * chset=lst.first();chset;chset=lst.next()) charset_combo->insertItem( chset ); charset_combo->insertItem( "any" ); //display_example(); emit fontSelected(selFont);}void KFontDialog::size_chosen_slot(const char* size){ QString size_string = size; selFont.setPointSize(size_string.toInt()); //display_example(); emit fontSelected(selFont);}void KFontDialog::weight_chosen_slot(const char* weight){ QString weight_string = weight; if ( weight_string == QString(klocale->translate("normal"))) selFont.setBold(false); if ( weight_string == QString(klocale->translate("bold"))) selFont.setBold(true); // display_example(); emit fontSelected(selFont);}void KFontDialog::style_chosen_slot(const char* style){ QString style_string = style; if ( style_string == QString(klocale->translate("roman"))) selFont.setItalic(false); if ( style_string == QString(klocale->translate("italic"))) selFont.setItalic(true); // display_example(); emit fontSelected(selFont);} void KFontDialog::display_example(const QFont& font){ QString string; example_edit->setFont(font); QFontInfo info = example_edit->fontInfo(); actual_family_label_data->setText(info.family()); string.setNum(info.pointSize()); actual_size_label_data->setText(string); if (info.bold()) actual_weight_label_data->setText(klocale->translate("Bold")); else actual_weight_label_data->setText(klocale->translate("Normal")); if (info.italic()) actual_style_label_data->setText(klocale->translate("italic")); else actual_style_label_data->setText(klocale->translate("roman")); KCharsets *charsets=KApplication::getKApplication()->getCharsets(); const char * charset=charsets->name(selFont); actual_charset_label_data->setText(charset);}void KFontDialog::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(0); weight_combo->setCurrentItem(1); }else weight_combo->setCurrentItem(0); if (selFont.italic()) style_combo->setCurrentItem(1); else style_combo->setCurrentItem(0); // Re-create displayable charsets list KCharsets *charsets=KApplication::getKApplication()->getCharsets(); const char * charset=charsets->name(selFont); QStrList lst=charsets->displayable(selFont.family()); charset_combo->clear(); i = 0; for(const char * chset=lst.first();chset;chset=lst.next(),++i) { charset_combo->insertItem( chset ); if (strcmp(chset, charset) == 0) charset_combo->setCurrentItem(i); } charset_combo->insertItem( "any" ); }bool KFontDialog::loadKDEInstalledFonts(){ QString fontfilename; //TODO replace by QDir::homePath(); fontfilename = KApplication::localkdedir() + "/share/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;}void KFontDialog::fill_family_combo(){ int numFonts; Display *kde_display; char** fontNames; char** fontNames_copy; QString qfontname; QStrList fontlist(TRUE); kde_display = XOpenDisplay( 0L ); // now try to load the KDE fonts bool have_installed = loadKDEInstalledFonts(); // if available we are done, the kde fonts are now in the family_combo if (have_installed) return; fontNames = XListFonts(kde_display, "*", 32767, &numFonts); fontNames_copy = fontNames; for(int i = 0; i < numFonts; i++){ if (**fontNames != '-'){ // The font name doesn't start with a dash -- an alias // so we ignore it. It is debatable whether this is the right // behaviour so I leave the following snippet of code around. // Just uncomment it if you want those aliases to be inserted as well. /* qfontname = ""; qfontname = *fontNames; if(fontlist.find(qfontname) == -1) fontlist.inSort(qfontname); */ fontNames ++; continue; }; qfontname = ""; qfontname = *fontNames; int dash = qfontname.find ('-', 1, TRUE); // find next dash if (dash == -1) { // No such next dash -- this shouldn't happen. // but what do I care -- lets skip it. fontNames ++; continue; } // the font name is between the second and third dash so: // let's find the third dash: int dash_two = qfontname.find ('-', dash + 1 , TRUE); if (dash == -1) { // No such next dash -- this shouldn't happen. // But what do I care -- lets skip it. fontNames ++; 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(fontlist.find(qfontname) == -1) fontlist.inSort(qfontname); } } fontNames ++; } for(fontlist.first(); fontlist.current(); fontlist.next()) family_combo->insertItem(fontlist.current(),-1); XFreeFontNames(fontNames_copy); XCloseDisplay(kde_display);}void KFontDialog::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_edit->palette()).copy(); 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_edit->setPalette(mypalette); example_edit->setBackgroundColor(white); }QString KFontDialog::getXLFD( const QFont& font ){ QFontInfo fi( font ); QString xlfd = "-*-"; // foundry xlfd += fi.family(); // family xlfd += "-"; switch( fi.weight() ) { // weight case QFont::Light: xlfd += "light-"; break; case QFont::Normal: xlfd += "normal-"; break; case QFont::DemiBold: xlfd += "demi-"; break; case QFont::Bold: xlfd += "bold-"; break; case QFont::Black: xlfd += "black-"; break; default: xlfd += "*-"; } if( fi.italic() ) xlfd += "i-"; // slant else xlfd += "r-"; // slant xlfd += "*-"; // set width xlfd += "*-"; // pixels (we cannot know portably, because this // depends on the screen resolution xlfd += fi.pointSize()*10; // points xlfd += "-"; xlfd += "*-"; // horz. resolution xlfd += "*-"; // vert. resolution if( fi.fixedPitch() ) xlfd += "m-"; // spacing: monospaced else xlfd += "p-"; // spacing: proportional xlfd += "*-"; // average width // charset switch( fi.charSet() ) { case QFont::AnyCharSet: xlfd += "*"; break; case QFont::Latin1: xlfd += "iso8859-1"; break; case QFont::Latin2: xlfd += "iso8859-2"; break; case QFont::Latin3: xlfd += "iso8859-3"; break; case QFont::Latin4: xlfd += "iso8859-4"; break; case QFont::Latin5: xlfd += "iso8859-5"; break; case QFont::Latin6: xlfd += "iso8859-6"; break; case QFont::Latin7: xlfd += "iso8859-7"; break; case QFont::Latin8: xlfd += "iso8859-8"; break; case QFont::Latin9: xlfd += "iso8859-9"; break; default: xlfd += "*"; } return xlfd;}#include "kfontdialog.moc"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -