fb2read.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. #include <QtGui>
  2. #include <QTextEdit>
  3. #include <QtDebug>
  4. #include "fb2read.h"
  5. static QString Value(const QXmlAttributes &attributes, const QString &name)
  6. {
  7. int count = attributes.count();
  8. for (int i = 0; i < count; i++ ) {
  9. if (attributes.localName(i).compare(name, Qt::CaseInsensitive) == 0) {
  10. return attributes.value(i);
  11. }
  12. }
  13. return QString();
  14. }
  15. //---------------------------------------------------------------------------
  16. // Fb2Handler::ContentHandler
  17. //---------------------------------------------------------------------------
  18. Fb2Handler::ContentHandler::ContentHandler(Fb2Handler &owner)
  19. : m_owner(owner)
  20. , m_handler(NULL)
  21. , m_frame(NULL)
  22. {
  23. }
  24. Fb2Handler::ContentHandler::ContentHandler(ContentHandler &parent)
  25. : m_owner(parent.m_owner)
  26. , m_handler(NULL)
  27. , m_frame(NULL)
  28. {
  29. }
  30. Fb2Handler::ContentHandler::~ContentHandler()
  31. {
  32. if (m_handler) delete m_handler;
  33. }
  34. bool Fb2Handler::ContentHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  35. {
  36. if (m_handler) return m_handler->doStart(name, attributes);
  37. m_handler = new ContentHandler(*this);
  38. }
  39. bool Fb2Handler::ContentHandler::doText(const QString &text)
  40. {
  41. if (m_handler) return m_handler->doText(text);
  42. return true;
  43. }
  44. bool Fb2Handler::ContentHandler::doEnd(const QString &name, bool & exit)
  45. {
  46. if (m_handler) {
  47. bool ok = m_handler->doEnd(name, exit);
  48. if (exit) {
  49. delete m_handler;
  50. m_handler = NULL;
  51. exit = false;
  52. }
  53. return ok;
  54. } else {
  55. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  56. exit = true;
  57. return true;
  58. }
  59. }
  60. //---------------------------------------------------------------------------
  61. // Fb2Handler::RootHandler
  62. //---------------------------------------------------------------------------
  63. Fb2Handler::RootHandler::KeywordHash::KeywordHash()
  64. {
  65. insert("stylesheet", Style);
  66. insert("description", Descr);
  67. insert("body", Body);
  68. insert("binary", Binary);
  69. }
  70. Fb2Handler::RootHandler::RootHandler(Fb2Handler & owner)
  71. : ContentHandler(owner)
  72. {
  73. }
  74. Fb2Handler::RootHandler::Keyword Fb2Handler::RootHandler::toKeyword(const QString &name)
  75. {
  76. static KeywordHash map;
  77. KeywordHash::const_iterator i = map.find(name);
  78. return i == map.end() ? None : i.value();
  79. }
  80. bool Fb2Handler::RootHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  81. {
  82. if (m_handler) return m_handler->doStart(name, attributes);
  83. switch (toKeyword(name)) {
  84. case Descr : return m_handler = new DescrHandler(*this);
  85. case Body : return m_handler = new BodyHandler(*this);
  86. case Binary : return m_handler = new BinaryHandler(*this, attributes);
  87. }
  88. qCritical() << QObject::tr("Unknown XML tag: %1").arg(name);
  89. return false;
  90. }
  91. //---------------------------------------------------------------------------
  92. // Fb2Handler::DescrHandler
  93. //---------------------------------------------------------------------------
  94. bool Fb2Handler::DescrHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  95. {
  96. Q_UNUSED(name);
  97. Q_UNUSED(attributes);
  98. return true;
  99. }
  100. bool Fb2Handler::DescrHandler::doEnd(const QString &name, bool & exit)
  101. {
  102. Q_UNUSED(name);
  103. if (name == "description") exit = true;
  104. return true;
  105. }
  106. //---------------------------------------------------------------------------
  107. // Fb2Handler::BodyHandler
  108. //---------------------------------------------------------------------------
  109. Fb2Handler::BodyHandler::KeywordHash::KeywordHash()
  110. {
  111. insert("image", Image);
  112. insert("title", Title);
  113. insert("epigraph", Epigraph);
  114. insert("section", Section);
  115. insert("p", Paragraph);
  116. insert("poem", Poem);
  117. insert("stanza", Stanza);
  118. insert("v", Verse);
  119. }
  120. Fb2Handler::BodyHandler::BodyHandler(ContentHandler &parent)
  121. : ContentHandler(parent)
  122. , m_empty(true)
  123. {
  124. QTextBlockFormat blockFormat;
  125. blockFormat.setTopMargin(4);
  126. blockFormat.setBottomMargin(4);
  127. cursor().setBlockFormat(blockFormat);
  128. }
  129. Fb2Handler::BodyHandler::Keyword Fb2Handler::BodyHandler::toKeyword(const QString &name)
  130. {
  131. static KeywordHash map;
  132. KeywordHash::const_iterator i = map.find(name);
  133. return i == map.end() ? None : i.value();
  134. }
  135. bool Fb2Handler::BodyHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  136. {
  137. if (m_handler) return m_handler->doStart(name, attributes);
  138. switch (toKeyword(name)) {
  139. case Paragraph : m_handler = new TextHandler(*this, name); break;
  140. case Image : m_handler = new ImageHandler(*this, attributes); break;
  141. case Section : m_handler = new SectionHandler(*this, name, attributes); break;
  142. case Title : m_handler = new SectionHandler(*this, name, attributes); break;
  143. case Poem : m_handler = new SectionHandler(*this, name, attributes); break;
  144. case Stanza : m_handler = new SectionHandler(*this, name, attributes); break;
  145. default : m_handler = new TextHandler(*this, name); break;
  146. }
  147. return true;
  148. }
  149. bool Fb2Handler::BodyHandler::doText(const QString &text)
  150. {
  151. if (m_handler) return m_handler->doText(text);
  152. cursor().insertText(text);
  153. return true;
  154. }
  155. //---------------------------------------------------------------------------
  156. // Fb2Handler::SectionHandler
  157. //---------------------------------------------------------------------------
  158. Fb2Handler::SectionHandler::KeywordHash::KeywordHash()
  159. {
  160. insert("image", Image);
  161. insert("section", Section);
  162. insert("title", Title);
  163. insert("p", Paragraph);
  164. insert("poem", Poem);
  165. insert("stanza", Stanza);
  166. insert("v", Verse);
  167. }
  168. Fb2Handler::SectionHandler::SectionHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes)
  169. : ContentHandler(parent)
  170. , m_name(name)
  171. , m_empty(true)
  172. {
  173. m_frame = cursor().currentFrame();
  174. QTextFrameFormat frameFormat;
  175. frameFormat.setBorder(1);
  176. frameFormat.setPadding(8);
  177. frameFormat.setTopMargin(4);
  178. frameFormat.setBottomMargin(4);
  179. cursor().insertFrame(frameFormat);
  180. QTextBlockFormat blockFormat;
  181. blockFormat.setTopMargin(4);
  182. blockFormat.setBottomMargin(4);
  183. cursor().setBlockFormat(blockFormat);
  184. }
  185. Fb2Handler::SectionHandler::Keyword Fb2Handler::SectionHandler::toKeyword(const QString &name)
  186. {
  187. static KeywordHash map;
  188. KeywordHash::const_iterator i = map.find(name);
  189. return i == map.end() ? None : i.value();
  190. }
  191. bool Fb2Handler::SectionHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  192. {
  193. if (m_handler) return m_handler->doStart(name, attributes);
  194. switch (toKeyword(name)) {
  195. case Paragraph : m_handler = new TextHandler(*this, name); break;
  196. case Image : m_handler = new ImageHandler(*this, attributes); break;
  197. case Section : m_handler = new SectionHandler(*this, name, attributes); break;
  198. case Title : m_handler = new SectionHandler(*this, name, attributes); break;
  199. case Poem : m_handler = new SectionHandler(*this, name, attributes); break;
  200. case Stanza : m_handler = new SectionHandler(*this, name, attributes); break;
  201. default : m_handler = new TextHandler(*this, name); break;
  202. }
  203. return true;
  204. }
  205. bool Fb2Handler::SectionHandler::doText(const QString &text)
  206. {
  207. if (m_handler) return m_handler->doText(text);
  208. cursor().insertText(text);
  209. return true;
  210. }
  211. //---------------------------------------------------------------------------
  212. // Fb2Handler::TextHandler
  213. //---------------------------------------------------------------------------
  214. Fb2Handler::TextHandler::TextHandler(ContentHandler &parent, const QString &name)
  215. : ContentHandler(parent)
  216. , m_name(name)
  217. {
  218. QTextBlockFormat blockFormat;
  219. blockFormat.setTopMargin(4);
  220. blockFormat.setBottomMargin(4);
  221. cursor().setBlockFormat(blockFormat);
  222. }
  223. Fb2Handler::TextHandler::KeywordHash::KeywordHash()
  224. {
  225. insert("strong" , Strong);
  226. insert("emphasis" , Emphasis);
  227. insert("style" , Style);
  228. insert("a" , Anchor);
  229. insert("strikethrough" , Strikethrough);
  230. insert("sub" , Sub);
  231. insert("sup" , Sup);
  232. insert("code" , Code);
  233. insert("image" , Image);
  234. }
  235. Fb2Handler::TextHandler::Keyword Fb2Handler::TextHandler::toKeyword(const QString &name)
  236. {
  237. static KeywordHash map;
  238. KeywordHash::const_iterator i = map.find(name);
  239. return i == map.end() ? None : i.value();
  240. }
  241. bool Fb2Handler::TextHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  242. {
  243. if (m_handler) return m_handler->doStart(name, attributes);
  244. switch (toKeyword(name)) {
  245. default : m_handler = new ContentHandler(*this); break;
  246. }
  247. return true;
  248. }
  249. bool Fb2Handler::TextHandler::doText(const QString &text)
  250. {
  251. if (m_handler) return m_handler->doText(text);
  252. cursor().insertText(text);
  253. return true;
  254. }
  255. //---------------------------------------------------------------------------
  256. // Fb2Handler::ImageHandler
  257. //---------------------------------------------------------------------------
  258. Fb2Handler::ImageHandler::ImageHandler(ContentHandler &parent, const QXmlAttributes &attributes)
  259. : ContentHandler(parent)
  260. {
  261. QString image = Value(attributes, "href");
  262. while (image.left(1) == "#") image.remove(0, 1);
  263. if (!image.isEmpty()) cursor().insertImage(image);
  264. }
  265. bool Fb2Handler::ImageHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  266. {
  267. Q_UNUSED(name);
  268. Q_UNUSED(attributes);
  269. return false;
  270. }
  271. //---------------------------------------------------------------------------
  272. // Fb2Handler::BinaryHandler
  273. //---------------------------------------------------------------------------
  274. Fb2Handler::BinaryHandler::BinaryHandler(ContentHandler &parent, const QXmlAttributes &attributes)
  275. : ContentHandler(parent), m_name(Value(attributes, "id"))
  276. {
  277. }
  278. bool Fb2Handler::BinaryHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  279. {
  280. Q_UNUSED(name);
  281. Q_UNUSED(attributes);
  282. return false;
  283. }
  284. bool Fb2Handler::BinaryHandler::doText(const QString &text)
  285. {
  286. m_text += text;
  287. return true;
  288. }
  289. bool Fb2Handler::BinaryHandler::doEnd(const QString &name, bool & exit)
  290. {
  291. Q_UNUSED(name);
  292. QByteArray in; in.append(m_text);
  293. QImage img = QImage::fromData(QByteArray::fromBase64(in));
  294. if (!m_name.isEmpty()) m_owner.document().addResource(QTextDocument::ImageResource, QUrl(m_name), img);
  295. exit = true;
  296. return true;
  297. }
  298. //---------------------------------------------------------------------------
  299. // Fb2Handler
  300. //---------------------------------------------------------------------------
  301. Fb2Handler::Fb2Handler(QTextDocument & document)
  302. : m_document(document)
  303. , m_cursor(&document)
  304. , m_handler(NULL)
  305. {
  306. m_cursor.beginEditBlock();
  307. document.clear();
  308. m_cursor.movePosition(QTextCursor::Start);
  309. }
  310. Fb2Handler::~Fb2Handler()
  311. {
  312. if (m_handler) delete m_handler;
  313. m_cursor.endEditBlock();
  314. }
  315. bool Fb2Handler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
  316. {
  317. Q_UNUSED(namespaceURI);
  318. Q_UNUSED(localName);
  319. const QString name = qName.toLower();
  320. if (m_handler) return m_handler->doStart(name, attributes);
  321. if (name == "fictionbook") {
  322. m_handler = new RootHandler(*this);
  323. return true;
  324. } else {
  325. m_error = QObject::tr("The file is not an FB2 file.");
  326. return false;
  327. }
  328. }
  329. static bool isWhiteSpace(const QString &str)
  330. {
  331. return str.simplified().isEmpty();
  332. }
  333. bool Fb2Handler::characters(const QString &str)
  334. {
  335. QString s = str.simplified();
  336. if (s.isEmpty()) return true;
  337. if (isWhiteSpace(str.left(1))) s.prepend(" ");
  338. if (isWhiteSpace(str.right(1))) s.append(" ");
  339. return m_handler && m_handler->doText(s);
  340. }
  341. bool Fb2Handler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
  342. {
  343. Q_UNUSED(namespaceURI);
  344. Q_UNUSED(localName);
  345. bool exit = false;
  346. return m_handler && m_handler->doEnd(qName.toLower(), exit);
  347. }
  348. bool Fb2Handler::fatalError(const QXmlParseException &exception)
  349. {
  350. qCritical() << QObject::tr("Parse error at line %1, column %2:\n%3")
  351. .arg(exception.lineNumber())
  352. .arg(exception.columnNumber())
  353. .arg(exception.message());
  354. return false;
  355. }
  356. QString Fb2Handler::errorString() const
  357. {
  358. return m_error;
  359. }
  360. /*
  361. QMessageBox::information(
  362. m_editor->window(),
  363. QObject::tr("fb2edit"),
  364. QObject::tr("%1=%2\n%1=%3")
  365. .arg(attributes.localName(i))
  366. .arg(attributes.value(i))
  367. .arg(image)
  368. );
  369. //! [2]
  370. QTextCursor cursor(editor->textCursor());
  371. cursor.movePosition(QTextCursor::Start);
  372. //! [2] //! [3]
  373. QTextFrame *topFrame = cursor.currentFrame();
  374. QTextFrameFormat topFrameFormat = topFrame->frameFormat();
  375. topFrameFormat.setPadding(16);
  376. topFrame->setFrameFormat(topFrameFormat);
  377. QTextCharFormat textFormat;
  378. QTextCharFormat boldFormat;
  379. boldFormat.setFontWeight(QFont::Bold);
  380. QTextFrameFormat referenceFrameFormat;
  381. referenceFrameFormat.setBorder(1);
  382. referenceFrameFormat.setPadding(8);
  383. referenceFrameFormat.setPosition(QTextFrameFormat::FloatRight);
  384. referenceFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 40));
  385. cursor.insertFrame(referenceFrameFormat);
  386. cursor.insertText("A company", boldFormat);
  387. cursor.insertBlock();
  388. cursor.insertText("321 City Street");
  389. cursor.insertBlock();
  390. cursor.insertText("Industry Park");
  391. cursor.insertBlock();
  392. cursor.insertText("Another country");
  393. //! [3]
  394. //! [4]
  395. cursor.setPosition(topFrame->lastPosition());
  396. cursor.insertText(name, textFormat);
  397. QString line;
  398. foreach (line, address.split("\n")) {
  399. cursor.insertBlock();
  400. cursor.insertText(line);
  401. }
  402. //! [4] //! [5]
  403. cursor.insertBlock();
  404. cursor.insertBlock();
  405. QDate date = QDate::currentDate();
  406. cursor.insertText(tr("Date: %1").arg(date.toString("d MMMM yyyy")),
  407. textFormat);
  408. cursor.insertBlock();
  409. QTextFrameFormat bodyFrameFormat;
  410. bodyFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
  411. cursor.insertFrame(bodyFrameFormat);
  412. //! [5]
  413. //! [6]
  414. cursor.insertText(tr("I would like to place an order for the following "
  415. "items:"), textFormat);
  416. cursor.insertBlock();
  417. //! [6] //! [7]
  418. cursor.insertBlock();
  419. //! [7]
  420. //! [8]
  421. QTextTableFormat orderTableFormat;
  422. orderTableFormat.setAlignment(Qt::AlignHCenter);
  423. QTextTable *orderTable = cursor.insertTable(1, 2, orderTableFormat);
  424. QTextFrameFormat orderFrameFormat = cursor.currentFrame()->frameFormat();
  425. orderFrameFormat.setBorder(1);
  426. cursor.currentFrame()->setFrameFormat(orderFrameFormat);
  427. //! [8]
  428. //! [9]
  429. cursor = orderTable->cellAt(0, 0).firstCursorPosition();
  430. cursor.insertText(tr("Product"), boldFormat);
  431. cursor = orderTable->cellAt(0, 1).firstCursorPosition();
  432. cursor.insertText(tr("Quantity"), boldFormat);
  433. //! [9]
  434. //! [10]
  435. for (int i = 0; i < orderItems.count(); ++i) {
  436. QPair<QString,int> item = orderItems[i];
  437. int row = orderTable->rows();
  438. orderTable->insertRows(row, 1);
  439. cursor = orderTable->cellAt(row, 0).firstCursorPosition();
  440. cursor.insertText(item.first, textFormat);
  441. cursor = orderTable->cellAt(row, 1).firstCursorPosition();
  442. cursor.insertText(QString("%1").arg(item.second), textFormat);
  443. }
  444. //! [10]
  445. //! [11]
  446. cursor.setPosition(topFrame->lastPosition());
  447. cursor.insertBlock();
  448. //! [11] //! [12]
  449. cursor.insertText(tr("Please update my records to take account of the "
  450. "following privacy information:"));
  451. cursor.insertBlock();
  452. //! [12]
  453. //! [13]
  454. QTextTable *offersTable = cursor.insertTable(2, 2);
  455. cursor = offersTable->cellAt(0, 1).firstCursorPosition();
  456. cursor.insertText(tr("I want to receive more information about your "
  457. "company's products and special offers."), textFormat);
  458. cursor = offersTable->cellAt(1, 1).firstCursorPosition();
  459. cursor.insertText(tr("I do not want to receive any promotional information "
  460. "from your company."), textFormat);
  461. if (sendOffers)
  462. cursor = offersTable->cellAt(0, 0).firstCursorPosition();
  463. else
  464. cursor = offersTable->cellAt(1, 0).firstCursorPosition();
  465. cursor.insertText("X", boldFormat);
  466. //! [13]
  467. //! [14]
  468. cursor.setPosition(topFrame->lastPosition());
  469. cursor.insertBlock();
  470. cursor.insertText(tr("Sincerely,"), textFormat);
  471. cursor.insertBlock();
  472. cursor.insertBlock();
  473. cursor.insertBlock();
  474. cursor.insertText(name);
  475. printAction->setEnabled(true);
  476. }
  477. //! [14]
  478. */