fb2read.cpp 18 KB

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