Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

loremipsum.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  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 #include "loremipsum.h"
00016 
00017 #include <QDir>
00018 #include <QDomDocument>
00019 #include <QEvent>
00020 #include <QFile>
00021 #include <QGridLayout>
00022 #include <QHBoxLayout>
00023 #include <QHeaderView>
00024 #include <QLabel>
00025 #include <QPushButton>
00026 #include <QSpacerItem>
00027 #include <QSpinBox>
00028 #include <QString>
00029 #include <QStringList>
00030 #include <QTreeWidget>
00031 #include <QTreeWidgetItem>
00032 #include <QVBoxLayout>
00033 
00034 
00035 #include "langmgr.h"
00036 #include "pageitem.h"
00037 #include "pageitem_textframe.h"
00038 #include "scribus.h"
00039 #include "scribusdoc.h"
00040 #include "scribusview.h"
00041 #include "scpaths.h"
00042 #include "selection.h"
00043 #include "prefsmanager.h"
00044 #include "commonstrings.h"
00045 #include "hyphenator.h"
00046 #include "util_icon.h"
00047 
00048 QString getLoremLocation(QString fname)
00049 {
00050     return QDir::convertSeparators(ScPaths::instance().shareDir() + "/loremipsum/" + fname);
00051 }
00052 
00053 LoremParser::LoremParser(QString fname)
00054 {
00055     name = author = url = "n/a";
00056     correct = false;
00057     QDomDocument doc("loremdoc");
00058     QFile file(getLoremLocation(fname));
00059     if (!file.open(QIODevice::ReadOnly))
00060         return;
00061     if (!doc.setContent(&file))
00062     {
00063         file.close();
00064         return;
00065     }
00066     file.close();
00067 
00068     QDomElement docElement = doc.documentElement();
00069 
00070     QDomNode node = docElement.firstChild();
00071     while(!node.isNull())
00072     {
00073         QDomElement element = node.toElement();
00074         if(!element.isNull())
00075         {
00076             if (element.tagName() == "name")
00077                 name = element.text();
00078             if (element.tagName() == "author")
00079                 author = element.text();
00080             if (element.tagName() == "url")
00081                 url = element.text();
00082             if (element.tagName() == "p")
00083             {
00084                 QString txt = element.text().simplified();
00085                 txt.replace(SpecialChars::OLD_NBSPACE, SpecialChars::NBSPACE);
00086                 txt.replace(SpecialChars::OLD_NBHYPHEN, SpecialChars::NBHYPHEN);    
00087                 loremIpsum.append(txt);
00088             }
00089         }
00090         node = node.nextSibling();
00091     }
00092     if (name != "n/a")
00093         correct = true;
00094 }
00095 
00096 QString LoremParser::createLorem(uint parCount)
00097 {
00098     if (parCount < 1)
00099         return QString::null;
00100     // first paragraph is always the same
00101     QString lorem(loremIpsum[0]);
00102     if (parCount > 1)
00103     {
00104         if (!loremIpsum.isEmpty())
00105         {
00106             for (uint i = 1; i < parCount; ++i)
00107                 lorem += SpecialChars::PARSEP + loremIpsum[rand()%loremIpsum.count()];
00108         }
00109     }
00110     return lorem.trimmed();
00111 }
00112 
00113 
00114 LoremManager::LoremManager(ScribusDoc* doc, QWidget* parent) : QDialog( parent )
00115 {
00116     m_Doc=doc;
00117     setModal(true);
00118     setWindowTitle( tr( "Lorem Ipsum" ) );
00119     setWindowIcon(QIcon(loadIcon ( "AppIcon.png" )));
00120     LoremManagerLayout = new QGridLayout(this) ;
00121     LoremManagerLayout->setMargin(10);
00122     LoremManagerLayout->setSpacing(5);
00123 
00124     layout3 = new QVBoxLayout;
00125     layout3->setMargin(0);
00126     layout3->setSpacing(5);
00127 
00128     loremList = new QTreeWidget( this );
00129     loremList->setRootIsDecorated(true);
00130     loremList->setColumnCount(1);
00131     loremList->setHeaderLabel( tr("Select Lorem Ipsum"));
00132     loremList->header()->setClickable( false );
00133     loremList->header()->setResizeMode( QHeaderView::ResizeToContents );
00134     loremList->setSelectionMode(QAbstractItemView::SingleSelection);
00135     layout3->addWidget( loremList );
00136 
00137     layout2 = new QHBoxLayout;
00138     layout2->setMargin(0);
00139     layout2->setSpacing(5);
00140 
00141     paraLabel = new QLabel( this );
00142     layout2->addWidget( paraLabel );
00143 
00144     paraBox = new QSpinBox( this );
00145     paraBox->setMinimum( 1 );
00146     paraBox->setValue(PrefsManager::instance()->appPrefs.paragraphsLI);
00147     layout2->addWidget( paraBox );
00148     paraSpacer = new QSpacerItem( 2, 2, QSizePolicy::Expanding, QSizePolicy::Minimum );
00149     layout2->addItem( paraSpacer );
00150     layout3->addLayout( layout2 );
00151 
00152     layout1 = new QHBoxLayout;
00153     layout1->setMargin(0);
00154     layout1->setSpacing(5);
00155     buttonSpacer = new QSpacerItem( 2, 2, QSizePolicy::Expanding, QSizePolicy::Minimum );
00156     layout1->addItem( buttonSpacer );
00157 
00158     okButton = new QPushButton( this );
00159     layout1->addWidget( okButton );
00160 
00161     cancelButton = new QPushButton( this );
00162     layout1->addWidget( cancelButton );
00163     layout3->addLayout( layout1 );
00164 
00165     LoremManagerLayout->addLayout( layout3, 0, 0 );
00166     languageChange();
00167 
00168     // reading lorems
00169     QDir d(getLoremLocation(QString::null), "*.xml");
00170 
00171     QFileInfoList list = d.entryInfoList();
00172     QListIterator<QFileInfo> it(list);
00173     QFileInfo fi;
00174     langmgr = new LanguageManager();
00175     langmgr->init(false);
00176 
00177     while (it.hasNext())
00178     {
00179         fi = it.next();
00180         if (langmgr->getLangFromAbbrev(fi.baseName(), false).isEmpty())
00181             continue;
00182         LoremParser *parser = new LoremParser(fi.fileName());
00183         if (!parser->correct)
00184         {
00185             delete parser;
00186             continue;
00187         }
00188         availableLorems[parser->name] = fi.fileName();
00189         QTreeWidgetItem *item = new QTreeWidgetItem(loremList);
00190         if (parser->name=="la")
00191             item->setText(0, standardloremtext);
00192         else
00193             item->setText(0, langmgr->getLangFromAbbrev(parser->name, true));
00194         QTreeWidgetItem *subItem;
00195         subItem = new QTreeWidgetItem(item);
00196         subItem->setText(0, tr("Author:") + " " + parser->author);
00197         subItem = new QTreeWidgetItem(item);
00198         subItem->setText(0, tr("Get More:") + " " + parser->url);
00199         subItem = new QTreeWidgetItem(item);
00200         subItem->setText(0, tr("XML File:") + " " + fi.fileName());
00201         delete parser;
00202     }
00203     loremList->sortItems(0, Qt::AscendingOrder);
00204     loremList->setSortingEnabled(false);
00205     resize( QSize(320, 340).expandedTo(minimumSizeHint()) );
00206     // signals and slots connections
00207     connect( okButton, SIGNAL( clicked() ), this, SLOT( okButton_clicked() ) );
00208     connect( cancelButton, SIGNAL( clicked() ), this, SLOT( cancelButton_clicked() ) );
00209     connect( loremList, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(okButton_clicked()));
00210 }
00211 
00212 LoremManager::~LoremManager()
00213 {
00214     delete langmgr;
00215 }
00216 
00217 void LoremManager::changeEvent(QEvent *e)
00218 {
00219     if (e->type() == QEvent::LanguageChange)
00220     {
00221         languageChange();
00222     }
00223     else
00224         QWidget::changeEvent(e);
00225 }
00226 
00227 void LoremManager::languageChange()
00228 {
00229     setWindowTitle( tr( "Lorem Ipsum" ) );
00230     paraLabel->setText( tr( "Paragraphs:" ) );
00231     okButton->setText( CommonStrings::tr_OK );
00232     okButton->setShortcut( QKeySequence( tr( "Alt+O" ) ) );
00233     cancelButton->setText( CommonStrings::tr_Cancel );
00234     cancelButton->setShortcut( QKeySequence( tr( "Alt+C" ) ) );
00235     standardloremtext = tr("Standard Lorem Ipsum");
00236 }
00237 
00238 void LoremManager::okButton_clicked()
00239 {
00240     // only top level items are taken
00241     QTreeWidgetItem *li;
00242     if (loremList->currentItem()->parent() == 0)
00243         li = loremList->currentItem();
00244     else
00245         li = loremList->currentItem()->parent();
00246     QString name;
00247     if (li->text(0)==standardloremtext)
00248         name="la";
00249     else
00250         name=langmgr->getAbbrevFromLang(li->text(0), true, false);
00251         
00252     insertLoremIpsum(availableLorems[name], paraBox->value());
00253     accept();
00254 }
00255 
00256 void LoremManager::cancelButton_clicked()
00257 {
00258     reject();
00259 }
00260 
00261 void LoremManager::insertLoremIpsum(QString name, int paraCount)
00262 {
00263     //CB: Avox please make insertText for text frame to nuke all this
00264     // is it really applied?
00265 //  bool done = false;
00266 
00267     for (int i = 0; i < m_Doc->m_Selection->count(); ++i)
00268     {
00269         PageItem* currItem=m_Doc->m_Selection->itemAt(i);
00270         if (currItem == NULL)
00271             continue;
00272         if (!currItem->asTextFrame())
00273             continue;
00274         if (currItem->itemText.length() != 0)
00275         {
00276             m_Doc->itemSelection_ClearItem();
00277             /* ClearItem() doesn't return true or false so
00278             the following test has to be done */
00279             if (currItem->itemText.length() != 0)
00280                 continue;
00281         }
00282         LoremParser *lp = new LoremParser(name);
00283         if (lp == NULL)
00284         {
00285             qDebug("LoremManager::okButton_clicked() *lp == NULL");
00286             return;
00287         }
00288 
00289 #if 0       
00290 //      Set up the gtWriter instance with the selected paragraph style
00291         gtWriter* writer = new gtWriter(false, currItem);
00292         if (writer != NULL)
00293         {
00294                 writer->setUpdateParagraphStyles(false);
00295                 writer->setOverridePStyleFont(false);
00296                 gtFrameStyle* fstyle = writer->getDefaultStyle();
00297                 gtParagraphStyle* pstyle = new gtParagraphStyle(*fstyle);
00298                 pstyle->setName(currItem->currentStyle().name());
00299                 writer->setParagraphStyle(pstyle);
00300                 done = true;
00301                 writer->append(lp->createLorem(paraCount));
00302         }
00303         delete writer;      
00304 #endif
00305         
00306         // K.I.S.S.:
00307         currItem->itemText.insertChars(0, lp->createLorem(paraCount));
00308         delete lp;
00309 
00310         //if (ScMW->view->SelItem.at(i)->Doc->docHyphenator->AutoCheck)
00311         //  ScMW->view->SelItem.at(i)->Doc->docHyphenator->slotHyphenate(ScMW->view->SelItem.at(i));
00312         if (m_Doc->docHyphenator->AutoCheck)
00313             m_Doc->docHyphenator->slotHyphenate(currItem);
00314         currItem->invalidateLayout();
00315     }
00316 //  if (done)
00317 //  {
00318         m_Doc->regionsChanged()->update(QRectF());
00319         m_Doc->changed();
00320 //  }
00321 }
Created with personalized Doxygen. This is documentation for GPL software Scribus - Open Source Desktop Publishing.