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