Back to index

scribus-ng  1.3.4.dfsg+svn20071115
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 
00016 #include <qstring.h>
00017 #include <qstringlist.h>
00018 #include <qdom.h>
00019 #include <qdir.h>
00020 #include <qfile.h>
00021 #include <qvariant.h>
00022 #include <qpushbutton.h>
00023 #include <qheader.h>
00024 #include <qlistview.h>
00025 #include <qlabel.h>
00026 #include <qspinbox.h>
00027 #include <qcheckbox.h>
00028 #include <qlayout.h>
00029 #include <qtooltip.h>
00030 #include <qwhatsthis.h>
00031 
00032 #include "loremipsum.h"
00033 #include "loremipsum.moc"
00034 
00035 #include "langmgr.h"
00036 #include "pageitem.h"
00037 #include "scribus.h"
00038 #include "scribusdoc.h"
00039 #include "scribusview.h"
00040 #include "scpaths.h"
00041 #include "selection.h"
00042 #include "prefsmanager.h"
00043 #include "commonstrings.h"
00044 #include "hyphenator.h"
00045 #include "util.h"
00046 // #include "gtparagraphstyle.h"
00047 // #include "gtframestyle.h"
00048 // #include "gtwriter.h"
00049 
00050 QString getLoremLocation(QString fname)
00051 {
00052        return QDir::convertSeparators(ScPaths::instance().shareDir() + "/loremipsum/" + fname);
00053 }
00054 
00055 LoremParser::LoremParser(QString fname)
00056 {
00057        name = author = url = "n/a";
00058        correct = false;
00059        QDomDocument doc("loremdoc");
00060        QFile file(getLoremLocation(fname));
00061        if (!file.open(IO_ReadOnly))
00062               return;
00063        if (!doc.setContent(&file))
00064        {
00065               file.close();
00066               return;
00067        }
00068        file.close();
00069 
00070        QDomElement docElement = doc.documentElement();
00071 
00072        QDomNode node = docElement.firstChild();
00073        while(!node.isNull())
00074        {
00075               QDomElement element = node.toElement();
00076               if(!element.isNull())
00077               {
00078                      if (element.tagName() == "name")
00079                             name = element.text();
00080                      if (element.tagName() == "author")
00081                             author = element.text();
00082                      if (element.tagName() == "url")
00083                             url = element.text();
00084                      if (element.tagName() == "p")
00085                             loremIpsum.append(element.text().simplifyWhiteSpace());
00086               }
00087               node = node.nextSibling();
00088        }
00089        if (name != "n/a")
00090               correct = true;
00091 }
00092 
00093 QString LoremParser::createLorem(uint parCount)
00094 {
00095        if (parCount < 1)
00096               return QString::null;
00097        // first paragraph is always the same
00098        QString lorem(loremIpsum[0]);
00099        if (!loremIpsum.isEmpty())
00100               for (uint i = 1; i < parCount + 1; ++i)
00101                      lorem += loremIpsum[rand()%loremIpsum.count()] + SpecialChars::PARSEP;
00102        return lorem.stripWhiteSpace();
00103 }
00104 
00105 
00106 LoremManager::LoremManager(ScribusDoc* doc, QWidget* parent, const char* name, bool modal, WFlags fl)
00107        : QDialog( parent, name, modal, fl )
00108 {
00109        m_Doc=doc;
00110        if ( !name )
00111               setName( "LoremManager" );
00112        LoremManagerLayout = new QGridLayout( this, 1, 1, 11, 6, "LoremManagerLayout");
00113 
00114        layout3 = new QVBoxLayout( 0, 0, 6, "layout3");
00115 
00116        loremList = new QListView( this, "loremList" );
00117        loremList->addColumn( tr( "Select Lorem Ipsum" ) );
00118        loremList->setRootIsDecorated(true);
00119        layout3->addWidget( loremList );
00120 
00121        layout2 = new QHBoxLayout( 0, 0, 6, "layout2");
00122 
00123        paraLabel = new QLabel( this, "paraLabel" );
00124        layout2->addWidget( paraLabel );
00125 
00126        paraBox = new QSpinBox( this, "paraBox" );
00127        paraBox->setMinValue( 1 );
00128        paraBox->setValue(PrefsManager::instance()->appPrefs.paragraphsLI);
00129        layout2->addWidget( paraBox );
00130        paraSpacer = new QSpacerItem( 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00131        layout2->addItem( paraSpacer );
00132        layout3->addLayout( layout2 );
00133 
00134        layout1 = new QHBoxLayout( 0, 0, 6, "layout1");
00135        buttonSpacer = new QSpacerItem( 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00136        layout1->addItem( buttonSpacer );
00137 
00138        okButton = new QPushButton( this, "okButton" );
00139        layout1->addWidget( okButton );
00140 
00141        cancelButton = new QPushButton( this, "cancelButton" );
00142        layout1->addWidget( cancelButton );
00143        layout3->addLayout( layout1 );
00144 
00145        LoremManagerLayout->addLayout( layout3, 0, 0 );
00146        languageChange();
00147        resize( QSize(439, 364).expandedTo(minimumSizeHint()) );
00148        clearWState( WState_Polished );
00149 
00150        // reading lorems
00151        QDir d(getLoremLocation(QString::null), "*.xml");
00152 
00153        const QFileInfoList *list = d.entryInfoList();
00154        QFileInfoListIterator it(*list);
00155        QFileInfo *fi=0;
00156        langmgr=new LanguageManager();
00157        langmgr->init(false);
00158        
00159        while ( (fi = it.current()) != 0 )
00160        {
00161               if (langmgr->getLangFromAbbrev(fi->baseName(), false).isEmpty())
00162               {
00163                      ++it;
00164                      continue;
00165               }
00166               LoremParser *parser = new LoremParser(fi->fileName());
00167               if (!parser->correct)
00168               {
00169                      delete parser;
00170                      ++it;
00171                      continue;
00172               }
00173               availableLorems[parser->name] = fi->fileName();
00174               QListViewItem *item = new QListViewItem(loremList);
00175               if (parser->name=="la")
00176                      item->setText(0,standardloremtext);
00177               else
00178                      item->setText(0, langmgr->getLangFromAbbrev(parser->name, true));
00179               new QListViewItem(item, tr("Author:") + " " + parser->author);
00180               new QListViewItem(item, tr("Get More:") + " " + parser->url);
00181               new QListViewItem(item, tr("XML File:") + " " + fi->fileName());
00182               loremList->insertItem(item);
00183               ++it;
00184               delete parser;
00185        }
00186 
00187        // signals and slots connections
00188        connect( okButton, SIGNAL( clicked() ), this, SLOT( okButton_clicked() ) );
00189        connect( cancelButton, SIGNAL( clicked() ), this, SLOT( cancelButton_clicked() ) );
00190        connect( loremList, SIGNAL(doubleClicked(QListViewItem *, const QPoint &, int)), this, SLOT(okButton_clicked()));
00191 }
00192 
00193 LoremManager::~LoremManager()
00194 {
00195        delete langmgr;
00196 }
00197 
00198 void LoremManager::languageChange()
00199 {
00200        setCaption( tr( "Lorem Ipsum" ) );
00201        paraLabel->setText( tr( "Paragraphs:" ) );
00202        okButton->setText( CommonStrings::tr_OK );
00203        okButton->setAccel( QKeySequence( tr( "Alt+O" ) ) );
00204        cancelButton->setText( CommonStrings::tr_Cancel );
00205        cancelButton->setAccel( QKeySequence( tr( "Alt+C" ) ) );
00206        standardloremtext = tr("Standard Lorem Ipsum");
00207 }
00208 
00209 void LoremManager::okButton_clicked()
00210 {
00211        // only top level items are taken
00212        QListViewItem *li;
00213        if (loremList->currentItem()->parent() == 0)
00214               li = loremList->currentItem();
00215        else
00216               li = loremList->currentItem()->parent();
00217        QString name;
00218        if (li->text(0)==standardloremtext)
00219               name="la";
00220        else
00221               name=langmgr->getAbbrevFromLang(li->text(0), true, false);
00222               
00223        insertLoremIpsum(availableLorems[name], paraBox->value());
00224        accept();
00225 }
00226 
00227 void LoremManager::cancelButton_clicked()
00228 {
00229        reject();
00230 }
00231 
00232 void LoremManager::insertLoremIpsum(QString name, int paraCount)
00233 {
00234        //CB: Avox please make insertText for text frame to nuke all this
00235        // is it really applied?
00236 //     bool done = false;
00237 
00238        for (uint i = 0; i < m_Doc->m_Selection->count(); ++i)
00239        {
00240               PageItem* currItem=m_Doc->m_Selection->itemAt(i);
00241               if (currItem == NULL)
00242                      continue;
00243               if (!currItem->asTextFrame())
00244                      continue;
00245               if (currItem->itemText.length() != 0)
00246               {
00247                      m_Doc->itemSelection_ClearItem();
00248                      /* ClearItem() doesn't return true or false so
00249                      the following test has to be done */
00250                      if (currItem->itemText.length() != 0)
00251                             continue;
00252               }
00253               LoremParser *lp = new LoremParser(name);
00254               if (lp == NULL)
00255               {
00256                      qDebug("LoremManager::okButton_clicked() *lp == NULL");
00257                      return;
00258               }
00259 
00260 #if 0         
00261 //            Set up the gtWriter instance with the selected paragraph style
00262               gtWriter* writer = new gtWriter(false, currItem);
00263               if (writer != NULL)
00264               {
00265                             writer->setUpdateParagraphStyles(false);
00266                             writer->setOverridePStyleFont(false);
00267                             gtFrameStyle* fstyle = writer->getDefaultStyle();
00268                             gtParagraphStyle* pstyle = new gtParagraphStyle(*fstyle);
00269                             pstyle->setName(currItem->currentStyle().name());
00270                             writer->setParagraphStyle(pstyle);
00271                             done = true;
00272                             writer->append(lp->createLorem(paraCount));
00273               }
00274               delete writer;              
00275 #endif
00276               
00277               // K.I.S.S.:
00278               currItem->itemText.insertChars(0, lp->createLorem(paraCount));
00279               delete lp;
00280 
00281               //if (ScMW->view->SelItem.at(i)->Doc->docHyphenator->AutoCheck)
00282               //     ScMW->view->SelItem.at(i)->Doc->docHyphenator->slotHyphenate(ScMW->view->SelItem.at(i));
00283               if (m_Doc->docHyphenator->AutoCheck)
00284                      m_Doc->docHyphenator->slotHyphenate(currItem);
00285        }
00286 //     if (done)
00287 //     {
00288 //            m_Doc->view()->updateContents();
00289               m_Doc->view()->DrawNew();
00290               m_Doc->changed();
00291 //     }
00292 }