Back to index

scribus-ng  1.3.4.dfsg+svn20071115
fontcombo.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008                           fontcombo.cpp  -  description
00009                              -------------------
00010     begin                : Die Jun 17 2003
00011     copyright            : (C) 2003 by Franz Schmid
00012     email                : Franz.Schmid@altmuehlnet.de
00013  ***************************************************************************/
00014 
00015 /***************************************************************************
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  ***************************************************************************/
00023 #include <qstringlist.h>
00024 #include <qcombobox.h>
00025 #include <qlistbox.h>
00026 #include <qfont.h>
00027 #include <qpainter.h>
00028 
00029 #include "sccombobox.h"
00030 
00031 #include "scribusdoc.h"
00032 #include "fontcombo.h"
00033 #include "fontcombo.moc"
00034 #include "page.h"
00035 #include "prefsmanager.h"
00036 extern QPixmap SCRIBUS_API loadIcon(QString nam);
00037 
00038 FontListItem::FontListItem(QComboBox* parent, QString f, QFont fo) : QListBoxItem(parent->listBox())
00039 {
00040        fontName = f;
00041        iFont = fo;
00042        setText(fontName);
00043 }
00044 
00045 const int FontListItem::width(const QListBox *listbox)
00046 {
00047        return listbox->fontMetrics().width(text()) + 2;
00048 }
00049 
00050 const int FontListItem::height(const QListBox *listbox)
00051 {
00052        QFontMetrics fontMetrics(listbox->fontMetrics());
00053        return fontMetrics.lineSpacing() + 2;
00054 }
00055 
00056 void FontListItem::paint(QPainter *painter)
00057 {
00058        painter->setFont(iFont);
00059        QFontMetrics fontMetrics(painter->fontMetrics());
00060        painter->drawText(3, fontMetrics.ascent() + fontMetrics.leading() / 2, fontName);
00061 }
00062 
00063 FontCombo::FontCombo(QWidget* pa) : QComboBox(true, pa)
00064 {
00065        prefsManager = PrefsManager::instance();
00066        ttfFont = loadIcon("font_truetype16.png");
00067        otfFont = loadIcon("font_otf16.png");
00068        psFont = loadIcon("font_type1_16.png");
00069        substFont = loadIcon("font_subst16.png");
00070        setEditable(false);
00071        QFont f(font());
00072        f.setPointSize(f.pointSize()-1);
00073        setFont(f);
00074        RebuildList(0);
00075 }
00076 
00077 void FontCombo::RebuildList(ScribusDoc *currentDoc, bool forAnnotation, bool forSubstitute)
00078 {
00079        clear();
00080        QMap<QString, QString> rlist;
00081        rlist.clear();
00082        SCFontsIterator it(prefsManager->appPrefs.AvailFonts);
00083        for ( ; it.hasNext(); it.next())
00084        {
00085               if (it.current().usable())
00086               {
00087                      if (currentDoc != NULL)
00088                      {
00089                             if (currentDoc->DocName == it.current().localForDocument() || it.current().localForDocument().isEmpty())
00090                                    rlist.insert(it.currentKey().lower(), it.currentKey());
00091                      }
00092                      else
00093                             rlist.insert(it.currentKey().lower(), it.currentKey());
00094               }
00095        }
00096        for (QMap<QString,QString>::Iterator it2 = rlist.begin(); it2 != rlist.end(); ++it2)
00097        {
00098               ScFace fon = prefsManager->appPrefs.AvailFonts[it2.data()];
00099               if (! fon.usable() )
00100                      continue;
00101               ScFace::FontType type = fon.type();
00102               if ((forAnnotation) && ((type == ScFace::TYPE1) || (type == ScFace::OTF) || fon.subset()))
00103                      continue;
00104               if (forSubstitute && fon.isReplacement())
00105                      continue;
00106               if (fon.isReplacement())
00107                      insertItem(substFont, it2.data());
00108               else if (type == ScFace::OTF)
00109                      insertItem(otfFont, it2.data());
00110               else if (type == ScFace::TYPE1)
00111                      insertItem(psFont, it2.data());
00112               else if (type == ScFace::TTF)
00113                      insertItem(ttfFont, it2.data());
00114        }
00115        listBox()->setMinimumWidth(listBox()->maxItemWidth()+24);
00116 }
00117 
00118 FontComboH::FontComboH(QWidget* parent, bool labels) :
00119               QWidget(parent, "FontComboH"),
00120               fontFaceLabel(0),
00121               fontStyleLabel(0),
00122               showLabels(labels)
00123 {
00124        prefsManager = PrefsManager::instance();
00125        ttfFont = loadIcon("font_truetype16.png");
00126        otfFont = loadIcon("font_otf16.png");
00127        psFont = loadIcon("font_type1_16.png");
00128        substFont = loadIcon("font_subst16.png");
00129        currDoc = 0;
00130        fontComboLayout = new QGridLayout( this, 0, 0);
00131        int col=0;
00132        if (showLabels)
00133        {
00134               fontFaceLabel=new QLabel("", this, "fontFaceLabel");
00135               fontStyleLabel=new QLabel("", this, "fontStyleLabel");
00136               fontComboLayout->addWidget(fontFaceLabel,0,0);
00137               fontComboLayout->addWidget(fontStyleLabel,1,0);
00138               fontComboLayout->setColStretch(1,10);
00139               col=1;
00140        }
00141        fontFamily = new ScComboBox( false, this, "fontFamily" );
00142        fontComboLayout->addWidget(fontFamily,0,col);
00143        fontStyle = new ScComboBox( false, this, "fontStyle" );
00144        fontComboLayout->addWidget(fontStyle,1,col);
00145        RebuildList(0);
00146        connect(fontFamily, SIGNAL(activated(int)), this, SLOT(familySelected(int)));
00147        connect(fontStyle, SIGNAL(activated(int)), this, SLOT(styleSelected(int)));
00148        languageChange();
00149 }
00150 
00151 void FontComboH::languageChange()
00152 {
00153        if(showLabels)
00154        {
00155               fontFaceLabel->setText( tr("Face:"));
00156               fontStyleLabel->setText( tr("Style:"));
00157        }
00158 }
00159 
00160 void FontComboH::familySelected(int id)
00161 {
00162        disconnect(fontStyle, SIGNAL(activated(int)), this, SLOT(styleSelected(int)));
00163        QString curr = fontStyle->currentText();
00164        fontStyle->clear();
00165        QStringList slist = prefsManager->appPrefs.AvailFonts.fontMap[fontFamily->text(id)];
00166        slist.sort();
00167        fontStyle->insertStringList(slist);
00168        if (slist.contains(curr))
00169               fontStyle->setCurrentText(curr);
00170        else if (slist.contains("Regular"))
00171               fontStyle->setCurrentText("Regular");
00172        else if (slist.contains("Roman"))
00173               fontStyle->setCurrentText("Roman");
00174        emit fontSelected(fontFamily->text(id) + " " + fontStyle->currentText());
00175        connect(fontStyle, SIGNAL(activated(int)), this, SLOT(styleSelected(int)));
00176 }
00177 
00178 void FontComboH::styleSelected(int id)
00179 {
00180        emit fontSelected(fontFamily->currentText() + " " + fontStyle->text(id));
00181 }
00182 
00183 QString FontComboH::currentFont()
00184 {
00185        return fontFamily->currentText() + " " + fontStyle->currentText();
00186 }
00187 
00188 void FontComboH::setCurrentFont(QString f)
00189 {
00190        disconnect(fontFamily, SIGNAL(activated(int)), this, SLOT(familySelected(int)));
00191        disconnect(fontStyle, SIGNAL(activated(int)), this, SLOT(styleSelected(int)));
00192        QString family = prefsManager->appPrefs.AvailFonts[f].family();
00193        QString style = prefsManager->appPrefs.AvailFonts[f].style();
00194        fontFamily->setCurrentText(family);
00195        fontStyle->clear();
00196        QStringList slist = prefsManager->appPrefs.AvailFonts.fontMap[family];
00197        slist.sort();
00198        QStringList ilist;
00199        ilist.clear();
00200        if (currDoc != NULL)
00201        {
00202               for (QStringList::ConstIterator it3 = slist.begin(); it3 != slist.end(); ++it3)
00203               {
00204                      if ((currDoc->DocName == prefsManager->appPrefs.AvailFonts[family + " " + *it3].localForDocument()) || (prefsManager->appPrefs.AvailFonts[family + " " + *it3].localForDocument().isEmpty()))
00205                             ilist.append(*it3);
00206               }
00207               fontStyle->insertStringList(ilist);
00208        }
00209        else
00210               fontStyle->insertStringList(slist);
00211        fontStyle->setCurrentText(style);
00212        connect(fontFamily, SIGNAL(activated(int)), this, SLOT(familySelected(int)));
00213        connect(fontStyle, SIGNAL(activated(int)), this, SLOT(styleSelected(int)));
00214 }
00215 
00216 void FontComboH::RebuildList(ScribusDoc *currentDoc, bool forAnnotation, bool forSubstitute)
00217 {
00218        currDoc = currentDoc;
00219        disconnect(fontFamily, SIGNAL(activated(int)), this, SLOT(familySelected(int)));
00220        disconnect(fontStyle, SIGNAL(activated(int)), this, SLOT(styleSelected(int)));
00221        fontFamily->clear();
00222        fontStyle->clear();
00223        QStringList rlist = prefsManager->appPrefs.AvailFonts.fontMap.keys();
00224        QMap<QString, QString> flist;
00225        flist.clear();
00226        for (QStringList::ConstIterator it2 = rlist.begin(); it2 != rlist.end(); ++it2)
00227        {
00228               if (currentDoc != NULL)
00229               {
00230                      QStringList slist = prefsManager->appPrefs.AvailFonts.fontMap[*it2];
00231                      slist.sort();
00232                      QStringList ilist;
00233                      ilist.clear();
00234                      for (QStringList::ConstIterator it3 = slist.begin(); it3 != slist.end(); ++it3)
00235                      {
00236                             if ( prefsManager->appPrefs.AvailFonts.contains(*it2 + " " + *it3))
00237                             {
00238                                    const ScFace& fon(prefsManager->appPrefs.AvailFonts[*it2 + " " + *it3]);
00239                                    if (fon.usable() && !fon.isReplacement() && (currentDoc->DocName == fon.localForDocument() || fon.localForDocument().isEmpty()))
00240                                           ilist.append(*it3);
00241                             }
00242                      }
00243                      if (!ilist.isEmpty())
00244                             flist.insert((*it2).lower(), *it2);
00245               }
00246               else
00247                      flist.insert((*it2).lower(), *it2);
00248        }
00249        for (QMap<QString,QString>::Iterator it2a = flist.begin(); it2a != flist.end(); ++it2a)
00250        {
00251               ScFace fon = prefsManager->appPrefs.AvailFonts[it2a.data()+" "+prefsManager->appPrefs.AvailFonts.fontMap[it2a.data()][0]];
00252               if ( !fon.usable() || fon.isReplacement() )
00253                      continue;
00254               ScFace::FontType type = fon.type();
00255               if ((forAnnotation) && ((type == ScFace::TYPE1) || (type == ScFace::OTF) || (fon.subset())))
00256                      continue;
00257               if ((forSubstitute) && fon.isReplacement())
00258                      continue;
00259               if (fon.isReplacement())
00260                      fontFamily->insertItem(substFont, it2a.data());
00261               else if (type == ScFace::OTF)
00262                      fontFamily->insertItem(otfFont, it2a.data());
00263               else if (type == ScFace::TYPE1)
00264                      fontFamily->insertItem(psFont, it2a.data());
00265               else if (type == ScFace::TTF)
00266                      fontFamily->insertItem(ttfFont, it2a.data());
00267        }
00268        QString family = fontFamily->currentText();
00269        QStringList slist = prefsManager->appPrefs.AvailFonts.fontMap[family];
00270        slist.sort();
00271        QStringList ilist;
00272        ilist.clear();
00273        if (currentDoc != NULL)
00274        {
00275               for (QStringList::ConstIterator it3 = slist.begin(); it3 != slist.end(); ++it3)
00276               {
00277                      if (prefsManager->appPrefs.AvailFonts.contains(family + " " + *it3) && (currentDoc->DocName == prefsManager->appPrefs.AvailFonts[family + " " + *it3].localForDocument() || prefsManager->appPrefs.AvailFonts[family + " " + *it3].localForDocument().isEmpty()))
00278                             ilist.append(*it3);
00279               }
00280               fontStyle->insertStringList(ilist);
00281        }
00282        else
00283               fontStyle->insertStringList(slist);
00284        connect(fontFamily, SIGNAL(activated(int)), this, SLOT(familySelected(int)));
00285        connect(fontStyle, SIGNAL(activated(int)), this, SLOT(styleSelected(int)));
00286 }