Rev 8483 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4430 | cbradney | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
2707 | subik | 7 | /*************************************************************************** |
8 | * * |
||
9 | * This program is free software; you can redistribute it and/or modify * |
||
10 | * it under the terms of the GNU General Public License as published by * |
||
11 | * the Free Software Foundation; either version 2 of the License, or * |
||
12 | * (at your option) any later version. * |
||
13 | * * |
||
14 | ***************************************************************************/ |
||
15 | |||
16 | #include <qstring.h> |
||
17 | #include <qstringlist.h> |
||
18 | #include <qdom.h> |
||
19 | #include <qdir.h> |
||
20 | #include <qfile.h> |
||
21 | #include <qvariant.h> |
||
22 | #include <qpushbutton.h> |
||
23 | #include <qheader.h> |
||
24 | #include <qlistview.h> |
||
25 | #include <qlabel.h> |
||
26 | #include <qspinbox.h> |
||
27 | #include <qcheckbox.h> |
||
28 | #include <qlayout.h> |
||
29 | #include <qtooltip.h> |
||
30 | #include <qwhatsthis.h> |
||
31 | |||
32 | #include "loremipsum.h" |
||
33 | #include "loremipsum.moc" |
||
7617 | cbradney | 34 | |
35 | #include "langmgr.h" |
||
7633 | cbradney | 36 | #include "pageitem.h" |
2707 | subik | 37 | #include "scribus.h" |
3698 | cbradney | 38 | #include "scribusdoc.h" |
39 | #include "scribusview.h" |
||
2707 | subik | 40 | #include "scpaths.h" |
3934 | cbradney | 41 | #include "selection.h" |
2834 | cbradney | 42 | #include "prefsmanager.h" |
2952 | cbradney | 43 | #include "commonstrings.h" |
3252 | craig | 44 | #include "hyphenator.h" |
5569 | avox | 45 | #include "util.h" |
8066 | cbradney | 46 | // #include "gtparagraphstyle.h" |
47 | // #include "gtframestyle.h" |
||
48 | // #include "gtwriter.h" |
||
2707 | subik | 49 | |
50 | QString getLoremLocation(QString fname) |
||
51 | { |
||
52 | return QDir::convertSeparators(ScPaths::instance().shareDir() + "/loremipsum/" + fname); |
||
53 | } |
||
54 | |||
55 | LoremParser::LoremParser(QString fname) |
||
56 | { |
||
57 | name = author = url = "n/a"; |
||
2716 | subik | 58 | correct = false; |
2707 | subik | 59 | QDomDocument doc("loremdoc"); |
60 | QFile file(getLoremLocation(fname)); |
||
61 | if (!file.open(IO_ReadOnly)) |
||
62 | return; |
||
63 | if (!doc.setContent(&file)) |
||
64 | { |
||
65 | file.close(); |
||
66 | return; |
||
67 | } |
||
68 | file.close(); |
||
69 | |||
70 | QDomElement docElement = doc.documentElement(); |
||
71 | |||
72 | QDomNode node = docElement.firstChild(); |
||
73 | while(!node.isNull()) |
||
74 | { |
||
75 | QDomElement element = node.toElement(); |
||
76 | if(!element.isNull()) |
||
77 | { |
||
78 | if (element.tagName() == "name") |
||
79 | name = element.text(); |
||
80 | if (element.tagName() == "author") |
||
81 | author = element.text(); |
||
82 | if (element.tagName() == "url") |
||
83 | url = element.text(); |
||
84 | if (element.tagName() == "p") |
||
2784 | subik | 85 | loremIpsum.append(element.text().simplifyWhiteSpace()); |
2707 | subik | 86 | } |
87 | node = node.nextSibling(); |
||
88 | } |
||
2716 | subik | 89 | if (name != "n/a") |
90 | correct = true; |
||
2707 | subik | 91 | } |
92 | |||
93 | QString LoremParser::createLorem(uint parCount) |
||
94 | { |
||
95 | if (parCount < 1) |
||
96 | return QString::null; |
||
97 | // first paragraph is always the same |
||
98 | QString lorem(loremIpsum[0]); |
||
6021 | cbradney | 99 | if (!loremIpsum.isEmpty()) |
100 | for (uint i = 1; i < parCount + 1; ++i) |
||
8064 | avox | 101 | lorem += loremIpsum[rand()%loremIpsum.count()] + SpecialChars::PARSEP; |
2707 | subik | 102 | return lorem.stripWhiteSpace(); |
103 | } |
||
104 | |||
105 | |||
5781 | cbradney | 106 | LoremManager::LoremManager(ScribusDoc* doc, QWidget* parent, const char* name, bool modal, WFlags fl) |
2707 | subik | 107 | : QDialog( parent, name, modal, fl ) |
108 | { |
||
5781 | cbradney | 109 | m_Doc=doc; |
2707 | subik | 110 | if ( !name ) |
111 | setName( "LoremManager" ); |
||
112 | LoremManagerLayout = new QGridLayout( this, 1, 1, 11, 6, "LoremManagerLayout"); |
||
113 | |||
114 | layout3 = new QVBoxLayout( 0, 0, 6, "layout3"); |
||
115 | |||
116 | loremList = new QListView( this, "loremList" ); |
||
117 | loremList->addColumn( tr( "Select Lorem Ipsum" ) ); |
||
118 | loremList->setRootIsDecorated(true); |
||
119 | layout3->addWidget( loremList ); |
||
120 | |||
121 | layout2 = new QHBoxLayout( 0, 0, 6, "layout2"); |
||
122 | |||
123 | paraLabel = new QLabel( this, "paraLabel" ); |
||
124 | layout2->addWidget( paraLabel ); |
||
125 | |||
126 | paraBox = new QSpinBox( this, "paraBox" ); |
||
127 | paraBox->setMinValue( 1 ); |
||
2834 | cbradney | 128 | paraBox->setValue(PrefsManager::instance()->appPrefs.paragraphsLI); |
2707 | subik | 129 | layout2->addWidget( paraBox ); |
130 | paraSpacer = new QSpacerItem( 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ); |
||
131 | layout2->addItem( paraSpacer ); |
||
132 | layout3->addLayout( layout2 ); |
||
133 | |||
134 | layout1 = new QHBoxLayout( 0, 0, 6, "layout1"); |
||
135 | buttonSpacer = new QSpacerItem( 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ); |
||
136 | layout1->addItem( buttonSpacer ); |
||
137 | |||
138 | okButton = new QPushButton( this, "okButton" ); |
||
139 | layout1->addWidget( okButton ); |
||
140 | |||
141 | cancelButton = new QPushButton( this, "cancelButton" ); |
||
142 | layout1->addWidget( cancelButton ); |
||
143 | layout3->addLayout( layout1 ); |
||
144 | |||
145 | LoremManagerLayout->addLayout( layout3, 0, 0 ); |
||
146 | languageChange(); |
||
147 | resize( QSize(439, 364).expandedTo(minimumSizeHint()) ); |
||
148 | clearWState( WState_Polished ); |
||
149 | |||
150 | // reading lorems |
||
151 | QDir d(getLoremLocation(QString::null), "*.xml"); |
||
152 | |||
153 | const QFileInfoList *list = d.entryInfoList(); |
||
154 | QFileInfoListIterator it(*list); |
||
7646 | cbradney | 155 | QFileInfo *fi=0; |
7617 | cbradney | 156 | langmgr=new LanguageManager(); |
7619 | cbradney | 157 | langmgr->init(false); |
158 | |||
2707 | subik | 159 | while ( (fi = it.current()) != 0 ) |
160 | { |
||
7629 | cbradney | 161 | if (langmgr->getLangFromAbbrev(fi->baseName(), false).isEmpty()) |
162 | { |
||
163 | ++it; |
||
164 | continue; |
||
165 | } |
||
2707 | subik | 166 | LoremParser *parser = new LoremParser(fi->fileName()); |
2716 | subik | 167 | if (!parser->correct) |
168 | { |
||
169 | delete parser; |
||
170 | ++it; |
||
171 | continue; |
||
172 | } |
||
2707 | subik | 173 | availableLorems[parser->name] = fi->fileName(); |
174 | QListViewItem *item = new QListViewItem(loremList); |
||
7617 | cbradney | 175 | if (parser->name=="la") |
176 | item->setText(0,standardloremtext); |
||
177 | else |
||
178 | item->setText(0, langmgr->getLangFromAbbrev(parser->name, true)); |
||
2707 | subik | 179 | new QListViewItem(item, tr("Author:") + " " + parser->author); |
180 | new QListViewItem(item, tr("Get More:") + " " + parser->url); |
||
181 | new QListViewItem(item, tr("XML File:") + " " + fi->fileName()); |
||
182 | loremList->insertItem(item); |
||
183 | ++it; |
||
2716 | subik | 184 | delete parser; |
2707 | subik | 185 | } |
186 | |||
187 | // signals and slots connections |
||
188 | connect( okButton, SIGNAL( clicked() ), this, SLOT( okButton_clicked() ) ); |
||
189 | connect( cancelButton, SIGNAL( clicked() ), this, SLOT( cancelButton_clicked() ) ); |
||
6217 | fschmid | 190 | connect( loremList, SIGNAL(doubleClicked(QListViewItem *, const QPoint &, int)), this, SLOT(okButton_clicked())); |
2707 | subik | 191 | } |
192 | |||
7660 | cbradney | 193 | LoremManager::~LoremManager() |
194 | { |
||
195 | delete langmgr; |
||
196 | } |
||
197 | |||
2707 | subik | 198 | void LoremManager::languageChange() |
199 | { |
||
200 | setCaption( tr( "Lorem Ipsum" ) ); |
||
201 | paraLabel->setText( tr( "Paragraphs:" ) ); |
||
2952 | cbradney | 202 | okButton->setText( CommonStrings::tr_OK ); |
2707 | subik | 203 | okButton->setAccel( QKeySequence( tr( "Alt+O" ) ) ); |
2952 | cbradney | 204 | cancelButton->setText( CommonStrings::tr_Cancel ); |
2707 | subik | 205 | cancelButton->setAccel( QKeySequence( tr( "Alt+C" ) ) ); |
7619 | cbradney | 206 | standardloremtext = tr("Standard Lorem Ipsum"); |
2707 | subik | 207 | } |
208 | |||
209 | void LoremManager::okButton_clicked() |
||
210 | { |
||
2716 | subik | 211 | // only top level items are taken |
212 | QListViewItem *li; |
||
213 | if (loremList->currentItem()->parent() == 0) |
||
214 | li = loremList->currentItem(); |
||
215 | else |
||
216 | li = loremList->currentItem()->parent(); |
||
7617 | cbradney | 217 | QString name; |
218 | if (li->text(0)==standardloremtext) |
||
219 | name="la"; |
||
220 | else |
||
221 | name=langmgr->getAbbrevFromLang(li->text(0), true, false); |
||
222 | |||
223 | insertLoremIpsum(availableLorems[name], paraBox->value()); |
||
2784 | subik | 224 | accept(); |
225 | } |
||
2707 | subik | 226 | |
2784 | subik | 227 | void LoremManager::cancelButton_clicked() |
228 | { |
||
229 | reject(); |
||
230 | } |
||
231 | |||
232 | void LoremManager::insertLoremIpsum(QString name, int paraCount) |
||
233 | { |
||
5781 | cbradney | 234 | //CB: Avox please make insertText for text frame to nuke all this |
2784 | subik | 235 | // is it really applied? |
8066 | cbradney | 236 | // bool done = false; |
2784 | subik | 237 | |
5781 | cbradney | 238 | for (uint i = 0; i < m_Doc->m_Selection->count(); ++i) |
2707 | subik | 239 | { |
5781 | cbradney | 240 | PageItem* currItem=m_Doc->m_Selection->itemAt(i); |
3934 | cbradney | 241 | if (currItem == NULL) |
2707 | subik | 242 | continue; |
3934 | cbradney | 243 | if (!currItem->asTextFrame()) |
2707 | subik | 244 | continue; |
5184 | avox | 245 | if (currItem->itemText.length() != 0) |
2707 | subik | 246 | { |
5781 | cbradney | 247 | m_Doc->itemSelection_ClearItem(); |
2793 | subik | 248 | /* ClearItem() doesn't return true or false so |
249 | the following test has to be done */ |
||
5184 | avox | 250 | if (currItem->itemText.length() != 0) |
2707 | subik | 251 | continue; |
252 | } |
||
2784 | subik | 253 | LoremParser *lp = new LoremParser(name); |
254 | if (lp == NULL) |
||
255 | { |
||
256 | qDebug("LoremManager::okButton_clicked() *lp == NULL"); |
||
257 | return; |
||
258 | } |
||
8059 | avox | 259 | |
260 | #if 0 |
||
8039 | cbradney | 261 | // Set up the gtWriter instance with the selected paragraph style |
262 | gtWriter* writer = new gtWriter(false, currItem); |
||
263 | if (writer != NULL) |
||
264 | { |
||
265 | writer->setUpdateParagraphStyles(false); |
||
266 | writer->setOverridePStyleFont(false); |
||
267 | gtFrameStyle* fstyle = writer->getDefaultStyle(); |
||
268 | gtParagraphStyle* pstyle = new gtParagraphStyle(*fstyle); |
||
269 | pstyle->setName(currItem->currentStyle().name()); |
||
270 | writer->setParagraphStyle(pstyle); |
||
271 | done = true; |
||
272 | writer->append(lp->createLorem(paraCount)); |
||
273 | } |
||
8059 | avox | 274 | delete writer; |
275 | #endif |
||
276 | |||
277 | // K.I.S.S.: |
||
278 | currItem->itemText.insertChars(0, lp->createLorem(paraCount)); |
||
5447 | cbradney | 279 | delete lp; |
8059 | avox | 280 | |
4026 | craig | 281 | //if (ScMW->view->SelItem.at(i)->Doc->docHyphenator->AutoCheck) |
282 | // ScMW->view->SelItem.at(i)->Doc->docHyphenator->slotHyphenate(ScMW->view->SelItem.at(i)); |
||
5781 | cbradney | 283 | if (m_Doc->docHyphenator->AutoCheck) |
284 | m_Doc->docHyphenator->slotHyphenate(currItem); |
||
2707 | subik | 285 | } |
8066 | cbradney | 286 | // if (done) |
287 | // { |
||
9212 | cbradney | 288 | // m_Doc->view()->updateContents(); |
289 | m_Doc->view()->DrawNew(); |
||
5781 | cbradney | 290 | m_Doc->changed(); |
8066 | cbradney | 291 | // } |
2707 | subik | 292 | } |