fb2read.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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_feed(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("empty-line", Emptyline);
  165. insert("poem", Poem);
  166. insert("stanza", Stanza);
  167. insert("v", Verse);
  168. }
  169. Fb2Handler::SectionHandler::SectionHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes)
  170. : ContentHandler(parent)
  171. , m_name(name)
  172. , m_feed(false)
  173. {
  174. m_frame = cursor().currentFrame();
  175. QTextFrameFormat frameFormat;
  176. frameFormat.setBorder(1);
  177. frameFormat.setPadding(8);
  178. frameFormat.setTopMargin(4);
  179. frameFormat.setBottomMargin(4);
  180. cursor().insertFrame(frameFormat);
  181. QTextBlockFormat blockFormat;
  182. blockFormat.setTopMargin(4);
  183. blockFormat.setBottomMargin(4);
  184. cursor().setBlockFormat(blockFormat);
  185. }
  186. Fb2Handler::SectionHandler::Keyword Fb2Handler::SectionHandler::toKeyword(const QString &name)
  187. {
  188. static KeywordHash map;
  189. KeywordHash::const_iterator i = map.find(name);
  190. return i == map.end() ? None : i.value();
  191. }
  192. bool Fb2Handler::SectionHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  193. {
  194. if (m_handler) return m_handler->doStart(name, attributes);
  195. Keyword keyword = toKeyword(name);
  196. switch (keyword) {
  197. case Paragraph:
  198. case Emptyline:
  199. case Image:
  200. if (m_feed) cursor().insertBlock();
  201. m_feed = true;
  202. break;
  203. default:
  204. m_feed = false;
  205. }
  206. switch (keyword) {
  207. case Emptyline : m_handler = new ContentHandler(*this); break;
  208. case Paragraph : m_handler = new TextHandler(*this, name); break;
  209. case Section : m_handler = new SectionHandler(*this, name, attributes); break;
  210. case Title : m_handler = new SectionHandler(*this, name, attributes); break;
  211. case Poem : m_handler = new SectionHandler(*this, name, attributes); break;
  212. case Stanza : m_handler = new SectionHandler(*this, name, attributes); break;
  213. case Image : m_handler = new ImageHandler(*this, attributes); break;
  214. default : m_handler = new TextHandler(*this, name); break;
  215. }
  216. return true;
  217. }
  218. bool Fb2Handler::SectionHandler::doText(const QString &text)
  219. {
  220. if (m_handler) return m_handler->doText(text);
  221. cursor().insertText(text);
  222. return true;
  223. }
  224. //---------------------------------------------------------------------------
  225. // Fb2Handler::TextHandler
  226. //---------------------------------------------------------------------------
  227. Fb2Handler::TextHandler::TextHandler(ContentHandler &parent, const QString &name)
  228. : ContentHandler(parent)
  229. , m_name(name)
  230. {
  231. QTextBlockFormat blockFormat;
  232. blockFormat.setTopMargin(4);
  233. blockFormat.setBottomMargin(4);
  234. cursor().setBlockFormat(blockFormat);
  235. }
  236. Fb2Handler::TextHandler::KeywordHash::KeywordHash()
  237. {
  238. insert("strong" , Strong);
  239. insert("emphasis" , Emphasis);
  240. insert("style" , Style);
  241. insert("a" , Anchor);
  242. insert("strikethrough" , Strikethrough);
  243. insert("sub" , Sub);
  244. insert("sup" , Sup);
  245. insert("code" , Code);
  246. insert("image" , Image);
  247. }
  248. Fb2Handler::TextHandler::Keyword Fb2Handler::TextHandler::toKeyword(const QString &name)
  249. {
  250. static KeywordHash map;
  251. KeywordHash::const_iterator i = map.find(name);
  252. return i == map.end() ? None : i.value();
  253. }
  254. bool Fb2Handler::TextHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  255. {
  256. if (m_handler) return m_handler->doStart(name, attributes);
  257. switch (toKeyword(name)) {
  258. default : m_handler = new ContentHandler(*this); break;
  259. }
  260. return true;
  261. }
  262. bool Fb2Handler::TextHandler::doText(const QString &text)
  263. {
  264. if (m_handler) return m_handler->doText(text);
  265. cursor().insertText(text);
  266. return true;
  267. }
  268. //---------------------------------------------------------------------------
  269. // Fb2Handler::ImageHandler
  270. //---------------------------------------------------------------------------
  271. Fb2Handler::ImageHandler::ImageHandler(ContentHandler &parent, const QXmlAttributes &attributes)
  272. : ContentHandler(parent)
  273. {
  274. QString image = Value(attributes, "href");
  275. while (image.left(1) == "#") image.remove(0, 1);
  276. if (!image.isEmpty()) cursor().insertImage(image);
  277. }
  278. bool Fb2Handler::ImageHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  279. {
  280. Q_UNUSED(name);
  281. Q_UNUSED(attributes);
  282. return false;
  283. }
  284. //---------------------------------------------------------------------------
  285. // Fb2Handler::BinaryHandler
  286. //---------------------------------------------------------------------------
  287. Fb2Handler::BinaryHandler::BinaryHandler(ContentHandler &parent, const QXmlAttributes &attributes)
  288. : ContentHandler(parent), m_name(Value(attributes, "id"))
  289. {
  290. }
  291. bool Fb2Handler::BinaryHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  292. {
  293. Q_UNUSED(name);
  294. Q_UNUSED(attributes);
  295. return false;
  296. }
  297. bool Fb2Handler::BinaryHandler::doText(const QString &text)
  298. {
  299. m_text += text;
  300. return true;
  301. }
  302. bool Fb2Handler::BinaryHandler::doEnd(const QString &name, bool & exit)
  303. {
  304. Q_UNUSED(name);
  305. QByteArray in; in.append(m_text);
  306. QImage img = QImage::fromData(QByteArray::fromBase64(in));
  307. if (!m_name.isEmpty()) m_owner.document().addResource(QTextDocument::ImageResource, QUrl(m_name), img);
  308. exit = true;
  309. return true;
  310. }
  311. //---------------------------------------------------------------------------
  312. // Fb2Handler
  313. //---------------------------------------------------------------------------
  314. Fb2Handler::Fb2Handler(QTextDocument & document)
  315. : m_document(document)
  316. , m_cursor(&document)
  317. , m_handler(NULL)
  318. {
  319. m_cursor.beginEditBlock();
  320. document.clear();
  321. m_cursor.movePosition(QTextCursor::Start);
  322. }
  323. Fb2Handler::~Fb2Handler()
  324. {
  325. if (m_handler) delete m_handler;
  326. m_cursor.endEditBlock();
  327. }
  328. bool Fb2Handler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
  329. {
  330. Q_UNUSED(namespaceURI);
  331. Q_UNUSED(localName);
  332. const QString name = qName.toLower();
  333. if (m_handler) return m_handler->doStart(name, attributes);
  334. if (name == "fictionbook") {
  335. m_handler = new RootHandler(*this);
  336. return true;
  337. } else {
  338. m_error = QObject::tr("The file is not an FB2 file.");
  339. return false;
  340. }
  341. }
  342. static bool isWhiteSpace(const QString &str)
  343. {
  344. return str.simplified().isEmpty();
  345. }
  346. bool Fb2Handler::characters(const QString &str)
  347. {
  348. QString s = str.simplified();
  349. if (s.isEmpty()) return true;
  350. if (isWhiteSpace(str.left(1))) s.prepend(" ");
  351. if (isWhiteSpace(str.right(1))) s.append(" ");
  352. return m_handler && m_handler->doText(s);
  353. }
  354. bool Fb2Handler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
  355. {
  356. Q_UNUSED(namespaceURI);
  357. Q_UNUSED(localName);
  358. bool exit = false;
  359. return m_handler && m_handler->doEnd(qName.toLower(), exit);
  360. }
  361. bool Fb2Handler::fatalError(const QXmlParseException &exception)
  362. {
  363. qCritical() << QObject::tr("Parse error at line %1, column %2:\n%3")
  364. .arg(exception.lineNumber())
  365. .arg(exception.columnNumber())
  366. .arg(exception.message());
  367. return false;
  368. }
  369. QString Fb2Handler::errorString() const
  370. {
  371. return m_error;
  372. }
  373. /*
  374. QMessageBox::information(
  375. m_editor->window(),
  376. QObject::tr("fb2edit"),
  377. QObject::tr("%1=%2\n%1=%3")
  378. .arg(attributes.localName(i))
  379. .arg(attributes.value(i))
  380. .arg(image)
  381. );
  382. //! [2]
  383. QTextCursor cursor(editor->textCursor());
  384. cursor.movePosition(QTextCursor::Start);
  385. //! [2] //! [3]
  386. QTextFrame *topFrame = cursor.currentFrame();
  387. QTextFrameFormat topFrameFormat = topFrame->frameFormat();
  388. topFrameFormat.setPadding(16);
  389. topFrame->setFrameFormat(topFrameFormat);
  390. QTextCharFormat textFormat;
  391. QTextCharFormat boldFormat;
  392. boldFormat.setFontWeight(QFont::Bold);
  393. QTextFrameFormat referenceFrameFormat;
  394. referenceFrameFormat.setBorder(1);
  395. referenceFrameFormat.setPadding(8);
  396. referenceFrameFormat.setPosition(QTextFrameFormat::FloatRight);
  397. referenceFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 40));
  398. cursor.insertFrame(referenceFrameFormat);
  399. cursor.insertText("A company", boldFormat);
  400. cursor.insertBlock();
  401. cursor.insertText("321 City Street");
  402. cursor.insertBlock();
  403. cursor.insertText("Industry Park");
  404. cursor.insertBlock();
  405. cursor.insertText("Another country");
  406. //! [3]
  407. //! [4]
  408. cursor.setPosition(topFrame->lastPosition());
  409. cursor.insertText(name, textFormat);
  410. QString line;
  411. foreach (line, address.split("\n")) {
  412. cursor.insertBlock();
  413. cursor.insertText(line);
  414. }
  415. //! [4] //! [5]
  416. cursor.insertBlock();
  417. cursor.insertBlock();
  418. QDate date = QDate::currentDate();
  419. cursor.insertText(tr("Date: %1").arg(date.toString("d MMMM yyyy")),
  420. textFormat);
  421. cursor.insertBlock();
  422. QTextFrameFormat bodyFrameFormat;
  423. bodyFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
  424. cursor.insertFrame(bodyFrameFormat);
  425. //! [5]
  426. //! [6]
  427. cursor.insertText(tr("I would like to place an order for the following "
  428. "items:"), textFormat);
  429. cursor.insertBlock();
  430. //! [6] //! [7]
  431. cursor.insertBlock();
  432. //! [7]
  433. //! [8]
  434. QTextTableFormat orderTableFormat;
  435. orderTableFormat.setAlignment(Qt::AlignHCenter);
  436. QTextTable *orderTable = cursor.insertTable(1, 2, orderTableFormat);
  437. QTextFrameFormat orderFrameFormat = cursor.currentFrame()->frameFormat();
  438. orderFrameFormat.setBorder(1);
  439. cursor.currentFrame()->setFrameFormat(orderFrameFormat);
  440. //! [8]
  441. //! [9]
  442. cursor = orderTable->cellAt(0, 0).firstCursorPosition();
  443. cursor.insertText(tr("Product"), boldFormat);
  444. cursor = orderTable->cellAt(0, 1).firstCursorPosition();
  445. cursor.insertText(tr("Quantity"), boldFormat);
  446. //! [9]
  447. //! [10]
  448. for (int i = 0; i < orderItems.count(); ++i) {
  449. QPair<QString,int> item = orderItems[i];
  450. int row = orderTable->rows();
  451. orderTable->insertRows(row, 1);
  452. cursor = orderTable->cellAt(row, 0).firstCursorPosition();
  453. cursor.insertText(item.first, textFormat);
  454. cursor = orderTable->cellAt(row, 1).firstCursorPosition();
  455. cursor.insertText(QString("%1").arg(item.second), textFormat);
  456. }
  457. //! [10]
  458. //! [11]
  459. cursor.setPosition(topFrame->lastPosition());
  460. cursor.insertBlock();
  461. //! [11] //! [12]
  462. cursor.insertText(tr("Please update my records to take account of the "
  463. "following privacy information:"));
  464. cursor.insertBlock();
  465. //! [12]
  466. //! [13]
  467. QTextTable *offersTable = cursor.insertTable(2, 2);
  468. cursor = offersTable->cellAt(0, 1).firstCursorPosition();
  469. cursor.insertText(tr("I want to receive more information about your "
  470. "company's products and special offers."), textFormat);
  471. cursor = offersTable->cellAt(1, 1).firstCursorPosition();
  472. cursor.insertText(tr("I do not want to receive any promotional information "
  473. "from your company."), textFormat);
  474. if (sendOffers)
  475. cursor = offersTable->cellAt(0, 0).firstCursorPosition();
  476. else
  477. cursor = offersTable->cellAt(1, 0).firstCursorPosition();
  478. cursor.insertText("X", boldFormat);
  479. //! [13]
  480. //! [14]
  481. cursor.setPosition(topFrame->lastPosition());
  482. cursor.insertBlock();
  483. cursor.insertText(tr("Sincerely,"), textFormat);
  484. cursor.insertBlock();
  485. cursor.insertBlock();
  486. cursor.insertBlock();
  487. cursor.insertText(name);
  488. printAction->setEnabled(true);
  489. }
  490. //! [14]
  491. */