Rev 3205 | Rev 3625 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2707 | subik | 1 | /*************************************************************************** |
2 | * * |
||
3 | * This program is free software; you can redistribute it and/or modify * |
||
4 | * it under the terms of the GNU General Public License as published by * |
||
5 | * the Free Software Foundation; either version 2 of the License, or * |
||
6 | * (at your option) any later version. * |
||
7 | * * |
||
8 | ***************************************************************************/ |
||
9 | |||
10 | #include <qstring.h> |
||
11 | #include <qstringlist.h> |
||
12 | #include <qdom.h> |
||
13 | #include <qdir.h> |
||
14 | #include <qfile.h> |
||
15 | #include <qvariant.h> |
||
16 | #include <qpushbutton.h> |
||
17 | #include <qheader.h> |
||
18 | #include <qlistview.h> |
||
19 | #include <qlabel.h> |
||
20 | #include <qspinbox.h> |
||
21 | #include <qcheckbox.h> |
||
22 | #include <qlayout.h> |
||
23 | #include <qtooltip.h> |
||
24 | #include <qwhatsthis.h> |
||
25 | |||
26 | #include "loremipsum.h" |
||
27 | #include "loremipsum.moc" |
||
28 | #include "scribus.h" |
||
29 | #include "scpaths.h" |
||
30 | #include "serializer.h" |
||
2834 | cbradney | 31 | #include "prefsmanager.h" |
2952 | cbradney | 32 | #include "commonstrings.h" |
3252 | craig | 33 | #include "hyphenator.h" |
2707 | subik | 34 | |
35 | |||
3205 | craig | 36 | |
2707 | subik | 37 | QString getLoremLocation(QString fname) |
38 | { |
||
39 | return QDir::convertSeparators(ScPaths::instance().shareDir() + "/loremipsum/" + fname); |
||
40 | } |
||
41 | |||
42 | LoremParser::LoremParser(QString fname) |
||
43 | { |
||
44 | name = author = url = "n/a"; |
||
2716 | subik | 45 | correct = false; |
2707 | subik | 46 | QDomDocument doc("loremdoc"); |
47 | QFile file(getLoremLocation(fname)); |
||
48 | if (!file.open(IO_ReadOnly)) |
||
49 | return; |
||
50 | if (!doc.setContent(&file)) |
||
51 | { |
||
52 | file.close(); |
||
53 | return; |
||
54 | } |
||
55 | file.close(); |
||
56 | |||
57 | QDomElement docElement = doc.documentElement(); |
||
58 | |||
59 | QDomNode node = docElement.firstChild(); |
||
60 | while(!node.isNull()) |
||
61 | { |
||
62 | QDomElement element = node.toElement(); |
||
63 | if(!element.isNull()) |
||
64 | { |
||
65 | if (element.tagName() == "name") |
||
66 | name = element.text(); |
||
67 | if (element.tagName() == "author") |
||
68 | author = element.text(); |
||
69 | if (element.tagName() == "url") |
||
70 | url = element.text(); |
||
71 | if (element.tagName() == "p") |
||
2784 | subik | 72 | loremIpsum.append(element.text().simplifyWhiteSpace()); |
2707 | subik | 73 | } |
74 | node = node.nextSibling(); |
||
75 | } |
||
2716 | subik | 76 | if (name != "n/a") |
77 | correct = true; |
||
2707 | subik | 78 | } |
79 | |||
80 | QString LoremParser::createLorem(uint parCount) |
||
81 | { |
||
82 | if (parCount < 1) |
||
83 | return QString::null; |
||
84 | // first paragraph is always the same |
||
85 | QString lorem(loremIpsum[0]); |
||
86 | for (uint i = 1; i < parCount + 1; ++i) |
||
87 | lorem += loremIpsum[rand()%loremIpsum.count()] + '\n'; |
||
88 | return lorem.stripWhiteSpace(); |
||
89 | } |
||
90 | |||
91 | |||
92 | LoremManager::LoremManager(QWidget* parent, const char* name, bool modal, WFlags fl) |
||
93 | : QDialog( parent, name, modal, fl ) |
||
94 | { |
||
95 | if ( !name ) |
||
96 | setName( "LoremManager" ); |
||
97 | LoremManagerLayout = new QGridLayout( this, 1, 1, 11, 6, "LoremManagerLayout"); |
||
98 | |||
99 | layout3 = new QVBoxLayout( 0, 0, 6, "layout3"); |
||
100 | |||
101 | loremList = new QListView( this, "loremList" ); |
||
102 | loremList->addColumn( tr( "Select Lorem Ipsum" ) ); |
||
103 | loremList->setRootIsDecorated(true); |
||
104 | layout3->addWidget( loremList ); |
||
105 | |||
106 | layout2 = new QHBoxLayout( 0, 0, 6, "layout2"); |
||
107 | |||
108 | paraLabel = new QLabel( this, "paraLabel" ); |
||
109 | layout2->addWidget( paraLabel ); |
||
110 | |||
111 | paraBox = new QSpinBox( this, "paraBox" ); |
||
112 | paraBox->setMinValue( 1 ); |
||
2834 | cbradney | 113 | paraBox->setValue(PrefsManager::instance()->appPrefs.paragraphsLI); |
2707 | subik | 114 | layout2->addWidget( paraBox ); |
115 | paraSpacer = new QSpacerItem( 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ); |
||
116 | layout2->addItem( paraSpacer ); |
||
117 | layout3->addLayout( layout2 ); |
||
118 | |||
119 | layout1 = new QHBoxLayout( 0, 0, 6, "layout1"); |
||
120 | buttonSpacer = new QSpacerItem( 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ); |
||
121 | layout1->addItem( buttonSpacer ); |
||
122 | |||
123 | okButton = new QPushButton( this, "okButton" ); |
||
124 | layout1->addWidget( okButton ); |
||
125 | |||
126 | cancelButton = new QPushButton( this, "cancelButton" ); |
||
127 | layout1->addWidget( cancelButton ); |
||
128 | layout3->addLayout( layout1 ); |
||
129 | |||
130 | LoremManagerLayout->addLayout( layout3, 0, 0 ); |
||
131 | languageChange(); |
||
132 | resize( QSize(439, 364).expandedTo(minimumSizeHint()) ); |
||
133 | clearWState( WState_Polished ); |
||
134 | |||
135 | // reading lorems |
||
136 | QDir d(getLoremLocation(QString::null), "*.xml"); |
||
137 | |||
138 | const QFileInfoList *list = d.entryInfoList(); |
||
139 | QFileInfoListIterator it(*list); |
||
140 | QFileInfo *fi; |
||
141 | |||
142 | while ( (fi = it.current()) != 0 ) |
||
143 | { |
||
144 | LoremParser *parser = new LoremParser(fi->fileName()); |
||
2716 | subik | 145 | if (!parser->correct) |
146 | { |
||
147 | delete parser; |
||
148 | ++it; |
||
149 | continue; |
||
150 | } |
||
2707 | subik | 151 | availableLorems[parser->name] = fi->fileName(); |
152 | QListViewItem *item = new QListViewItem(loremList); |
||
153 | item->setText(0, parser->name); |
||
154 | new QListViewItem(item, tr("Author:") + " " + parser->author); |
||
155 | new QListViewItem(item, tr("Get More:") + " " + parser->url); |
||
156 | new QListViewItem(item, tr("XML File:") + " " + fi->fileName()); |
||
157 | loremList->insertItem(item); |
||
158 | ++it; |
||
2716 | subik | 159 | delete parser; |
2707 | subik | 160 | } |
161 | |||
162 | // signals and slots connections |
||
163 | connect( okButton, SIGNAL( clicked() ), this, SLOT( okButton_clicked() ) ); |
||
164 | connect( cancelButton, SIGNAL( clicked() ), this, SLOT( cancelButton_clicked() ) ); |
||
165 | } |
||
166 | |||
167 | void LoremManager::languageChange() |
||
168 | { |
||
169 | setCaption( tr( "Lorem Ipsum" ) ); |
||
170 | paraLabel->setText( tr( "Paragraphs:" ) ); |
||
2952 | cbradney | 171 | okButton->setText( CommonStrings::tr_OK ); |
2707 | subik | 172 | okButton->setAccel( QKeySequence( tr( "Alt+O" ) ) ); |
2952 | cbradney | 173 | cancelButton->setText( CommonStrings::tr_Cancel ); |
2707 | subik | 174 | cancelButton->setAccel( QKeySequence( tr( "Alt+C" ) ) ); |
175 | } |
||
176 | |||
177 | void LoremManager::okButton_clicked() |
||
178 | { |
||
2716 | subik | 179 | // only top level items are taken |
180 | QListViewItem *li; |
||
181 | if (loremList->currentItem()->parent() == 0) |
||
182 | li = loremList->currentItem(); |
||
183 | else |
||
184 | li = loremList->currentItem()->parent(); |
||
185 | |||
2784 | subik | 186 | insertLoremIpsum(availableLorems[li->text(0)], paraBox->value()); |
187 | accept(); |
||
188 | } |
||
2707 | subik | 189 | |
2784 | subik | 190 | void LoremManager::cancelButton_clicked() |
191 | { |
||
192 | reject(); |
||
193 | } |
||
194 | |||
195 | void LoremManager::insertLoremIpsum(QString name, int paraCount) |
||
196 | { |
||
197 | // is it really applied? |
||
198 | bool done = false; |
||
199 | |||
2707 | subik | 200 | for (uint i = 0; i < ScApp->view->SelItem.count(); ++i) |
201 | { |
||
202 | if (ScApp->view->SelItem.at(i) == NULL) |
||
203 | continue; |
||
204 | if (ScApp->view->SelItem.at(i)->itemType() != PageItem::TextFrame) |
||
205 | continue; |
||
206 | if (ScApp->view->SelItem.at(i)->itemText.count() != 0) |
||
207 | { |
||
2793 | subik | 208 | ScApp->view->ClearItem(); |
209 | /* ClearItem() doesn't return true or false so |
||
210 | the following test has to be done */ |
||
211 | if (ScApp->view->SelItem.at(i)->itemText.count() != 0) |
||
2707 | subik | 212 | continue; |
213 | } |
||
2784 | subik | 214 | |
215 | LoremParser *lp = new LoremParser(name); |
||
216 | if (lp == NULL) |
||
217 | { |
||
218 | qDebug("LoremManager::okButton_clicked() *lp == NULL"); |
||
219 | return; |
||
220 | } |
||
2707 | subik | 221 | Serializer *ss = new Serializer(""); |
222 | if (ss != NULL) |
||
223 | { |
||
2784 | subik | 224 | done = true; |
225 | ss->Objekt = lp->createLorem(paraCount); |
||
2707 | subik | 226 | int st = ScApp->view->SelItem.at(i)->Doc->currentParaStyle; |
227 | if (st > 5) |
||
228 | ss->GetText(ScApp->view->SelItem.at(i), st, ScApp->view->SelItem.at(i)->Doc->docParagraphStyles[st].Font, ScApp->view->SelItem.at(i)->Doc->docParagraphStyles[st].FontSize, true); |
||
229 | else |
||
230 | ss->GetText(ScApp->view->SelItem.at(i), st, ScApp->view->SelItem.at(i)->IFont, ScApp->view->SelItem.at(i)->ISize, true); |
||
231 | delete ss; |
||
232 | } |
||
233 | if (ScApp->view->SelItem.at(i)->Doc->docHyphenator->AutoCheck) |
||
234 | ScApp->view->SelItem.at(i)->Doc->docHyphenator->slotHyphenate(ScApp->view->SelItem.at(i)); |
||
235 | } |
||
2784 | subik | 236 | if (done) |
237 | { |
||
238 | ScApp->view->updateContents(); |
||
239 | ScApp->slotDocCh(); |
||
240 | } |
||
2707 | subik | 241 | } |