Back to index

scribus-ng  1.3.4.dfsg+svn20071115
fontpreview.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 #include <qvariant.h>
00008 #include <qpushbutton.h>
00009 #include <qlistview.h>
00010 #include <qlabel.h>
00011 #include <qlayout.h>
00012 #include <qtooltip.h>
00013 #include <qwhatsthis.h>
00014 #include <qstring.h>
00015 #include <qspinbox.h>
00016 #include <qregexp.h>
00017 
00018 #include "fontpreview.h"
00019 #include "fontpreview.moc"
00020 #include "scribus.h"
00021 #include "prefsfile.h"
00022 #include "commonstrings.h"
00023 #include "prefsmanager.h"
00024 #include "selection.h"
00025 #include "sampleitem.h"
00026 
00027 
00028 extern QPixmap SCRIBUS_API loadIcon(QString nam);
00029 
00030 
00031 FontPreview::FontPreview(QString fontName, QWidget* parent, ScribusDoc* doc)
00032        : FontPreviewBase(parent, "FontPreview", true, 0)
00033 {
00034        setIcon(loadIcon("AppIcon.png"));
00035        m_Doc=doc;
00036 
00037        sampleItem = new SampleItem(m_Doc);
00038 
00039        languageChange();
00040 
00041        fontList->setAllColumnsShowFocus(true);
00042        fontList->setShowSortIndicator(true);
00043        fontList->setColumnAlignment(1, Qt::AlignCenter);
00044        fontList->setColumnAlignment(3, Qt::AlignCenter);
00045        resetDisplayButton->setPixmap(loadIcon("u_undo16.png"));
00046 
00047        ttfFont = loadIcon("font_truetype16.png");
00048        otfFont = loadIcon("font_otf16.png");
00049        psFont = loadIcon("font_type1_16.png");
00050        okIcon = loadIcon("ok.png");
00051 
00052        updateFontList("");
00053 
00054        // scribus config
00055        defaultStr = tr("Woven silk pyjamas exchanged for blue quartz", "font preview");
00056        prefs = PrefsManager::instance()->prefsFile->getPluginContext("fontpreview");
00057        sortColumn = prefs->getUInt("sortColumn", 0);
00058        fontList->setSorting(sortColumn);
00059        xsize = prefs->getUInt("xsize", 640);
00060        ysize = prefs->getUInt("ysize", 480);
00061        sizeSpin->setValue(prefs->getUInt("fontSize", 18));
00062        QString ph = prefs->get("phrase", defaultStr);
00063        displayEdit->setText(ph);
00064        displayButton_clicked();
00065        resize(QSize(xsize, ysize).expandedTo(minimumSizeHint()));
00066 
00067        // set initial listitem
00068        QListViewItem *item;
00069        if (!fontName.isEmpty())
00070               item = fontList->findItem(fontName, 0);
00071        else
00072        {
00073               Q_ASSERT(m_Doc!=0);
00074               if (m_Doc->m_Selection->count() != 0)
00075                      item = fontList->findItem(m_Doc->currentStyle.charStyle().font().scName(), 0);
00076               else
00077                      item = fontList->findItem(PrefsManager::instance()->appPrefs.toolSettings.defFont, 0);
00078        }
00079        if (item != 0)
00080        {
00081               fontList->setCurrentItem(item);
00082               paintSample(item);
00083               fontList->center(0, fontList->currentItem()->itemPos());
00084 //            qDebug("1");
00085        }
00086 }
00087 
00091 FontPreview::~FontPreview()
00092 {
00093        prefs->set("sortColumn", fontList->sortColumn());
00094        prefs->set("xsize", width());
00095        prefs->set("ysize", height());
00096        prefs->set("fontSize", sizeSpin->value());
00097        prefs->set("phrase", displayEdit->text());
00098        sampleItem->cleanupTemporary(); // just to be sure
00099 }
00100 
00105 void FontPreview::languageChange()
00106 {
00107        QToolTip::add(okButton, "<qt>" + tr("Append selected font into Style, Font menu", "font preview") + "</qt>");
00108        QToolTip::add(cancelButton,tr("Leave preview", "font preview"));
00109        QToolTip::add(searchEdit, "<qt>" + tr("Typing the text here provides quick searching in the font names. Searching is case insensitive. You can provide a common wild cards (*, ?, [...]) in your phrase. Examples: t* will list all fonts starting with t or T. *bold* will list all fonts with word bold, bolder etc. in the name.") + "</qt>");
00110        QToolTip::add(searchButton, tr("Start searching"));
00111        QToolTip::add(sizeSpin, tr("Size of the selected font"));
00112 }
00113 
00114 bool FontPreview::allowSample()
00115 {
00116        if (fontList->childCount() != 0)
00117               return true;
00118        fontPreview->setText("No font selected");
00119        return false;
00120 }
00121 
00122 void FontPreview::keyReleaseEvent(QKeyEvent *k)
00123 {
00124        if (k->isAutoRepeat())
00125        {
00126               fontPreview->setText( tr("Sample will be shown after key release"));
00127               return;
00128        }
00129        if (allowSample())
00130               paintSample(fontList->currentItem());
00131 }
00132 
00133 //void FontPreview::fontList_currentChanged(QListViewItem * item)
00134 void FontPreview::fontList_mouseButtonClicked( int, QListViewItem *item, const QPoint &, int )
00135 {
00136        if (allowSample())
00137               paintSample(item);
00138 }
00139 
00140 void FontPreview::paintSample(QListViewItem *item)
00141 {
00142        if (!item)
00143               return;
00144        sampleItem->setFontSize(sizeSpin->value() * 10, true);
00145        sampleItem->setFont(item->text(0));
00146        QPixmap pixmap = sampleItem->getSample(fontPreview->maximumWidth(),
00147                                                                          fontPreview->maximumHeight());
00148        fontPreview->clear();
00149        if (!pixmap.isNull())
00150               fontPreview->setPixmap(pixmap);
00151 }
00152 
00153 void FontPreview::updateFontList(QString searchStr)
00154 {
00155        fontList->clear();
00156        if (searchStr.find('*') == -1)
00157               searchStr = '*' + searchStr + '*';
00158        QRegExp re(searchStr);
00159        re.setCaseSensitive(false);
00160        re.setWildcard(true);
00161 
00162        for (SCFontsIterator fontIter(PrefsManager::instance()->appPrefs.AvailFonts);
00163                      fontIter.hasNext(); fontIter.next())
00164        {
00165               if (searchStr.length()!=0 & !re.exactMatch(fontIter.current().scName()))
00166                      continue;
00167 
00168               if (fontIter.current().usable())
00169               {
00170                      QListViewItem *row = new QListViewItem(fontList);
00171                      ScFace::FontType type = fontIter.current().type();
00172 
00173                      row->setText(0, fontIter.current().scName());
00174                      // searching
00175 
00176                      if (m_Doc->UsedFonts.contains(fontIter.current().scName()))
00177                             row->setPixmap(1, okIcon);
00178 
00179                      if (type == ScFace::OTF)
00180                      {
00181                             row->setPixmap(2, otfFont);
00182                             row->setText(2, "OpenType");
00183                      }
00184                      else
00185                             if (fontIter.current().subset())
00186                                    row->setPixmap(3, okIcon);
00187 
00188                      if (type == ScFace::TYPE1) // type1
00189                      {
00190                             row->setPixmap(2, psFont);
00191                             row->setText(2, "Type1");
00192                      }
00193 
00194                      if (type == ScFace::TTF)
00195                      {
00196                             row->setPixmap(2, ttfFont);
00197                             row->setText(2, "TrueType");
00198                      }
00199 
00200                      QFileInfo fi(fontIter.current().fontFilePath());
00201                      fi.absFilePath().contains(QDir::homeDirPath()) ?
00202                                    row->setText(4, tr("User", "font preview")):
00203                                    row->setText(4, tr("System", "font preview"));
00204 
00205                      fontList->insertItem(row);
00206               }
00207        } // for fontIter
00208 }
00209 
00210 void FontPreview::searchEdit_textChanged(const QString &s)
00211 {
00212        if (s.length()==0)
00213               updateFontList(s);
00214 }
00215 
00216 void FontPreview::searchButton_clicked()
00217 {
00218        updateFontList(searchEdit->text());
00219 }
00220 
00221 QString FontPreview::getCurrentFont()
00222 {
00223        return fontList->currentItem()->text(0);
00224 }
00225 
00226 void FontPreview::displayButton_clicked()
00227 {
00228        sampleItem->setText(displayEdit->text());
00229        if (allowSample())
00230               paintSample(fontList->currentItem());
00231 }
00232 
00233 void FontPreview::okButton_clicked()
00234 {
00235        accept();
00236 }
00237 
00238 void FontPreview::cancelButton_clicked()
00239 {
00240        reject();
00241 }
00242 
00243 void FontPreview::resetDisplayButton_clicked()
00244 {
00245        displayEdit->setText(defaultStr);
00246        displayButton_clicked();
00247 }
00248 
00249 void FontPreview::sizeSpin_valueChanged( int )
00250 {
00251        if (allowSample())
00252               paintSample(fontList->currentItem());
00253 }