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