Rev 2716 | Go to most recent revision | Details | 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" |
||
31 | |||
32 | extern ScribusApp *ScApp; |
||
33 | |||
34 | QString getLoremLocation(QString fname) |
||
35 | { |
||
36 | return QDir::convertSeparators(ScPaths::instance().shareDir() + "/loremipsum/" + fname); |
||
37 | } |
||
38 | |||
39 | LoremParser::LoremParser(QString fname) |
||
40 | { |
||
41 | name = author = url = "n/a"; |
||
42 | QDomDocument doc("loremdoc"); |
||
43 | QFile file(getLoremLocation(fname)); |
||
44 | if (!file.open(IO_ReadOnly)) |
||
45 | return; |
||
46 | if (!doc.setContent(&file)) |
||
47 | { |
||
48 | file.close(); |
||
49 | return; |
||
50 | } |
||
51 | file.close(); |
||
52 | |||
53 | QDomElement docElement = doc.documentElement(); |
||
54 | |||
55 | QDomNode node = docElement.firstChild(); |
||
56 | while(!node.isNull()) |
||
57 | { |
||
58 | QDomElement element = node.toElement(); |
||
59 | if(!element.isNull()) |
||
60 | { |
||
61 | if (element.tagName() == "name") |
||
62 | name = element.text(); |
||
63 | if (element.tagName() == "author") |
||
64 | author = element.text(); |
||
65 | if (element.tagName() == "url") |
||
66 | url = element.text(); |
||
67 | if (element.tagName() == "p") |
||
68 | loremIpsum.append(element.text()); |
||
69 | } |
||
70 | node = node.nextSibling(); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | QString LoremParser::createLorem(uint parCount) |
||
75 | { |
||
76 | if (parCount < 1) |
||
77 | return QString::null; |
||
78 | // first paragraph is always the same |
||
79 | QString lorem(loremIpsum[0]); |
||
80 | for (uint i = 1; i < parCount + 1; ++i) |
||
81 | lorem += loremIpsum[rand()%loremIpsum.count()] + '\n'; |
||
82 | return lorem.stripWhiteSpace(); |
||
83 | } |
||
84 | |||
85 | |||
86 | LoremManager::LoremManager(QWidget* parent, const char* name, bool modal, WFlags fl) |
||
87 | : QDialog( parent, name, modal, fl ) |
||
88 | { |
||
89 | // setup checking |
||
90 | if (!ScApp->HaveDoc) |
||
91 | return; |
||
92 | if (ScApp->view->SelItem.count() == 0) |
||
93 | { |
||
94 | ScApp->mainWindowStatusLabel->setText(tr("Select any text item to apply dummy text")); |
||
95 | return; |
||
96 | } |
||
97 | |||
98 | // UI construction |
||
99 | if ( !name ) |
||
100 | setName( "LoremManager" ); |
||
101 | LoremManagerLayout = new QGridLayout( this, 1, 1, 11, 6, "LoremManagerLayout"); |
||
102 | |||
103 | layout3 = new QVBoxLayout( 0, 0, 6, "layout3"); |
||
104 | |||
105 | loremList = new QListView( this, "loremList" ); |
||
106 | loremList->addColumn( tr( "Select Lorem Ipsum" ) ); |
||
107 | loremList->setRootIsDecorated(true); |
||
108 | layout3->addWidget( loremList ); |
||
109 | |||
110 | layout2 = new QHBoxLayout( 0, 0, 6, "layout2"); |
||
111 | |||
112 | paraLabel = new QLabel( this, "paraLabel" ); |
||
113 | layout2->addWidget( paraLabel ); |
||
114 | |||
115 | paraBox = new QSpinBox( this, "paraBox" ); |
||
116 | paraBox->setMinValue( 1 ); |
||
117 | paraBox->setValue( 4 ); |
||
118 | layout2->addWidget( paraBox ); |
||
119 | paraSpacer = new QSpacerItem( 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ); |
||
120 | layout2->addItem( paraSpacer ); |
||
121 | layout3->addLayout( layout2 ); |
||
122 | |||
123 | alwaysCheckBox = new QCheckBox( this, "alwaysCheckBox" ); |
||
124 | layout3->addWidget( alwaysCheckBox ); |
||
125 | |||
126 | layout1 = new QHBoxLayout( 0, 0, 6, "layout1"); |
||
127 | buttonSpacer = new QSpacerItem( 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ); |
||
128 | layout1->addItem( buttonSpacer ); |
||
129 | |||
130 | okButton = new QPushButton( this, "okButton" ); |
||
131 | layout1->addWidget( okButton ); |
||
132 | |||
133 | cancelButton = new QPushButton( this, "cancelButton" ); |
||
134 | layout1->addWidget( cancelButton ); |
||
135 | layout3->addLayout( layout1 ); |
||
136 | |||
137 | LoremManagerLayout->addLayout( layout3, 0, 0 ); |
||
138 | languageChange(); |
||
139 | resize( QSize(439, 364).expandedTo(minimumSizeHint()) ); |
||
140 | clearWState( WState_Polished ); |
||
141 | |||
142 | // reading lorems |
||
143 | QDir d(getLoremLocation(QString::null), "*.xml"); |
||
144 | |||
145 | const QFileInfoList *list = d.entryInfoList(); |
||
146 | QFileInfoListIterator it(*list); |
||
147 | QFileInfo *fi; |
||
148 | |||
149 | while ( (fi = it.current()) != 0 ) |
||
150 | { |
||
151 | LoremParser *parser = new LoremParser(fi->fileName()); |
||
152 | availableLorems[parser->name] = fi->fileName(); |
||
153 | QListViewItem *item = new QListViewItem(loremList); |
||
154 | item->setText(0, parser->name); |
||
155 | new QListViewItem(item, tr("Author:") + " " + parser->author); |
||
156 | new QListViewItem(item, tr("Get More:") + " " + parser->url); |
||
157 | new QListViewItem(item, tr("XML File:") + " " + fi->fileName()); |
||
158 | loremList->insertItem(item); |
||
159 | ++it; |
||
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:" ) ); |
||
171 | alwaysCheckBox->setText( tr( "&Always Use Selected Lorem Ipsum" ) ); |
||
172 | alwaysCheckBox->setAccel( QKeySequence( tr( "Alt+A" ) ) ); |
||
173 | okButton->setText( tr( "&OK" ) ); |
||
174 | okButton->setAccel( QKeySequence( tr( "Alt+O" ) ) ); |
||
175 | cancelButton->setText( tr( "&Cancel" ) ); |
||
176 | cancelButton->setAccel( QKeySequence( tr( "Alt+C" ) ) ); |
||
177 | } |
||
178 | |||
179 | void LoremManager::okButton_clicked() |
||
180 | { |
||
181 | QListViewItem *li = loremList->currentItem(); |
||
182 | LoremParser *lp = new LoremParser(availableLorems[li->text(0)]); |
||
183 | |||
184 | for (uint i = 0; i < ScApp->view->SelItem.count(); ++i) |
||
185 | { |
||
186 | if (ScApp->view->SelItem.at(i) == NULL) |
||
187 | continue; |
||
188 | if (ScApp->view->SelItem.at(i)->itemType() != PageItem::TextFrame) |
||
189 | continue; |
||
190 | if (ScApp->view->SelItem.at(i)->itemText.count() != 0) |
||
191 | { |
||
192 | QString text = "<qt>" + tr("Do you really want to replace all your text\nin the frame named %1 with sample text?") + "</qt>"; |
||
193 | int t = QMessageBox::warning(ScApp, tr("Warning"), |
||
194 | QString(text).arg(ScApp->view->SelItem.at(i)->itemName()), |
||
195 | QMessageBox::No, QMessageBox::Yes, QMessageBox::NoButton); |
||
196 | if (t == QMessageBox::No) |
||
197 | continue; |
||
198 | } |
||
199 | Serializer *ss = new Serializer(""); |
||
200 | if (ss != NULL) |
||
201 | { |
||
202 | ss->Objekt = lp->createLorem(paraBox->value()); |
||
203 | int st = ScApp->view->SelItem.at(i)->Doc->currentParaStyle; |
||
204 | if (st > 5) |
||
205 | 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); |
||
206 | else |
||
207 | ss->GetText(ScApp->view->SelItem.at(i), st, ScApp->view->SelItem.at(i)->IFont, ScApp->view->SelItem.at(i)->ISize, true); |
||
208 | delete ss; |
||
209 | } |
||
210 | if (ScApp->view->SelItem.at(i)->Doc->docHyphenator->AutoCheck) |
||
211 | ScApp->view->SelItem.at(i)->Doc->docHyphenator->slotHyphenate(ScApp->view->SelItem.at(i)); |
||
212 | } |
||
213 | ScApp->view->updateContents(); |
||
214 | ScApp->slotDocCh(); |
||
215 | accept(); |
||
216 | } |
||
217 | |||
218 | void LoremManager::cancelButton_clicked() |
||
219 | { |
||
220 | reject(); |
||
221 | } |
||
222 |