fb2read.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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::RootHandler
  17. //---------------------------------------------------------------------------
  18. Fb2Handler::RootHandler::KeywordHash::KeywordHash()
  19. {
  20. insert("stylesheet", Style);
  21. insert("description", Descr);
  22. insert("body", Body);
  23. insert("binary", Binary);
  24. }
  25. Fb2Handler::RootHandler::Keyword Fb2Handler::RootHandler::toKeyword(const QString &name)
  26. {
  27. static KeywordHash map;
  28. KeywordHash::const_iterator i = map.find(name);
  29. return i == map.end() ? None : i.value();
  30. }
  31. bool Fb2Handler::RootHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  32. {
  33. switch (toKeyword(name)) {
  34. case Descr : return m_owner.setHandler(new DescrHandler(this));
  35. case Body : return m_owner.setHandler(new BodyHandler(this));
  36. case Binary : return m_owner.setHandler(new BinaryHandler(this, attributes));
  37. }
  38. qCritical() << QObject::tr("Unknown XML tag: %1").arg(name);
  39. return false;
  40. }
  41. //---------------------------------------------------------------------------
  42. // Fb2Handler::DescrHandler
  43. //---------------------------------------------------------------------------
  44. bool Fb2Handler::DescrHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  45. {
  46. Q_UNUSED(name);
  47. Q_UNUSED(attributes);
  48. return true;
  49. }
  50. bool Fb2Handler::DescrHandler::doText(const QString &text)
  51. {
  52. Q_UNUSED(text);
  53. return true;
  54. }
  55. bool Fb2Handler::DescrHandler::doEnd(const QString &name, bool & exit)
  56. {
  57. exit = name == "description";
  58. return true;
  59. }
  60. //---------------------------------------------------------------------------
  61. // Fb2Handler::BodyHandler
  62. //---------------------------------------------------------------------------
  63. Fb2Handler::BodyHandler::KeywordHash::KeywordHash()
  64. {
  65. insert("body", Body);
  66. insert("image", Image);
  67. insert("p", Paragraph);
  68. insert("section", Section);
  69. insert("title", Title);
  70. insert("poem", Poem);
  71. insert("stanza", Stanza);
  72. insert("v", Verse);
  73. }
  74. Fb2Handler::BodyHandler::Keyword Fb2Handler::BodyHandler::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. /*
  81. Keyword m_keyword;
  82. QTextBlockFormat m_blockFormat;
  83. QTextCharFormat m_charFormat;
  84. QTextFrame * m_frame;
  85. */
  86. bool Fb2Handler::BodyHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  87. {
  88. switch (toKeyword(name)) {
  89. case Image: {
  90. QString image = Value(attributes, "href");
  91. while (image.left(1) == "#") image.remove(0, 1);
  92. if (!image.isEmpty()) m_cursor.insertImage(image);
  93. } break;
  94. case Paragraph: {
  95. QTextBlockFormat blockFormat = m_cursor.blockFormat();
  96. blockFormat.setTopMargin(4);
  97. blockFormat.setBottomMargin(4);
  98. m_cursor.setBlockFormat(blockFormat);
  99. if (m_empty) {
  100. m_cursor.setBlockFormat(blockFormat);
  101. m_empty = false;
  102. } else {
  103. m_cursor.insertBlock(blockFormat);
  104. }
  105. } break;
  106. case Poem: {
  107. } break;
  108. case Section: {
  109. m_frames << m_cursor.currentFrame();
  110. QTextFrameFormat frameFormat;
  111. frameFormat.setBorder(1);
  112. frameFormat.setPadding(8);
  113. frameFormat.setTopMargin(4);
  114. frameFormat.setBottomMargin(4);
  115. m_cursor.insertFrame(frameFormat);
  116. m_empty = true;
  117. } break;
  118. }
  119. return true;
  120. }
  121. bool Fb2Handler::BodyHandler::doText(const QString &text)
  122. {
  123. m_cursor.insertText(text);
  124. return true;
  125. }
  126. bool Fb2Handler::BodyHandler::doEnd(const QString &name, bool & exit)
  127. {
  128. switch (toKeyword(name)) {
  129. case Body: {
  130. exit = true;
  131. }; break;
  132. case Section: {
  133. if (!m_frames.isEmpty()) {
  134. m_cursor.setPosition(m_frames.last()->lastPosition());
  135. m_frames.removeLast();
  136. }
  137. }; break;
  138. }
  139. return true;
  140. }
  141. //---------------------------------------------------------------------------
  142. // Fb2Handler::BinaryHandler
  143. //---------------------------------------------------------------------------
  144. Fb2Handler::BinaryHandler::BinaryHandler(ContentHandler * parent, const QXmlAttributes &attributes)
  145. : ContentHandler(parent), m_name(Value(attributes, "id"))
  146. {
  147. }
  148. bool Fb2Handler::BinaryHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  149. {
  150. Q_UNUSED(name);
  151. Q_UNUSED(attributes);
  152. return false;
  153. }
  154. bool Fb2Handler::BinaryHandler::doText(const QString &text)
  155. {
  156. m_text += text;
  157. return true;
  158. }
  159. bool Fb2Handler::BinaryHandler::doEnd(const QString &name, bool & exit)
  160. {
  161. Q_UNUSED(name);
  162. QByteArray in; in.append(m_text);
  163. QImage img = QImage::fromData(QByteArray::fromBase64(in));
  164. if (!m_name.isEmpty()) m_owner.getDocument().addResource(QTextDocument::ImageResource, QUrl(m_name), img);
  165. exit = true;
  166. return true;
  167. }
  168. //---------------------------------------------------------------------------
  169. // Fb2Handler
  170. //---------------------------------------------------------------------------
  171. Fb2Handler::Fb2Handler(QTextDocument & document)
  172. : m_document(document)
  173. , m_cursor(&document)
  174. , m_handler(NULL)
  175. {
  176. m_cursor.beginEditBlock();
  177. document.clear();
  178. m_cursor.movePosition(QTextCursor::Start);
  179. }
  180. Fb2Handler::~Fb2Handler()
  181. {
  182. while (m_handler) m_handler = doDelete(m_handler);
  183. m_cursor.endEditBlock();
  184. }
  185. bool Fb2Handler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
  186. {
  187. Q_UNUSED(namespaceURI);
  188. Q_UNUSED(localName);
  189. const QString name = qName.toLower();
  190. if (m_handler) return m_handler->doStart(name, attributes);
  191. if (name == "fictionbook") {
  192. m_handler = new RootHandler(*this);
  193. return true;
  194. } else {
  195. m_error = QObject::tr("The file is not an FB2 file.");
  196. return false;
  197. }
  198. }
  199. static bool isWhiteSpace(const QString &str)
  200. {
  201. int size = str.size();
  202. for (int i = 0; i < size; i++) if (str[i] != ' ') return false;
  203. return true;
  204. }
  205. bool Fb2Handler::characters(const QString &str)
  206. {
  207. QString s = str;
  208. s.replace(QRegExp("[\t\n\v\f\r]"), " ");
  209. if (isWhiteSpace(s)) return true;
  210. return m_handler && m_handler->doText(s);
  211. }
  212. Fb2Handler::ContentHandler * Fb2Handler::doDelete(Fb2Handler::ContentHandler * handler)
  213. {
  214. if (!handler) return NULL;
  215. ContentHandler * parent = handler->getParent();
  216. delete handler;
  217. return parent;
  218. }
  219. bool Fb2Handler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
  220. {
  221. Q_UNUSED(namespaceURI);
  222. Q_UNUSED(localName);
  223. bool exit = false;
  224. bool ok = m_handler && m_handler->doEnd(qName.toLower(), exit);
  225. if (exit) m_handler = doDelete(m_handler);
  226. return ok;
  227. }
  228. bool Fb2Handler::fatalError(const QXmlParseException &exception)
  229. {
  230. qCritical() << QObject::tr("Parse error at line %1, column %2:\n%3")
  231. .arg(exception.lineNumber())
  232. .arg(exception.columnNumber())
  233. .arg(exception.message());
  234. return false;
  235. }
  236. QString Fb2Handler::errorString() const
  237. {
  238. return m_error;
  239. }
  240. /*
  241. QMessageBox::information(
  242. m_editor->window(),
  243. QObject::tr("fb2edit"),
  244. QObject::tr("%1=%2\n%1=%3")
  245. .arg(attributes.localName(i))
  246. .arg(attributes.value(i))
  247. .arg(image)
  248. );
  249. //! [2]
  250. QTextCursor cursor(editor->textCursor());
  251. cursor.movePosition(QTextCursor::Start);
  252. //! [2] //! [3]
  253. QTextFrame *topFrame = cursor.currentFrame();
  254. QTextFrameFormat topFrameFormat = topFrame->frameFormat();
  255. topFrameFormat.setPadding(16);
  256. topFrame->setFrameFormat(topFrameFormat);
  257. QTextCharFormat textFormat;
  258. QTextCharFormat boldFormat;
  259. boldFormat.setFontWeight(QFont::Bold);
  260. QTextFrameFormat referenceFrameFormat;
  261. referenceFrameFormat.setBorder(1);
  262. referenceFrameFormat.setPadding(8);
  263. referenceFrameFormat.setPosition(QTextFrameFormat::FloatRight);
  264. referenceFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 40));
  265. cursor.insertFrame(referenceFrameFormat);
  266. cursor.insertText("A company", boldFormat);
  267. cursor.insertBlock();
  268. cursor.insertText("321 City Street");
  269. cursor.insertBlock();
  270. cursor.insertText("Industry Park");
  271. cursor.insertBlock();
  272. cursor.insertText("Another country");
  273. //! [3]
  274. //! [4]
  275. cursor.setPosition(topFrame->lastPosition());
  276. cursor.insertText(name, textFormat);
  277. QString line;
  278. foreach (line, address.split("\n")) {
  279. cursor.insertBlock();
  280. cursor.insertText(line);
  281. }
  282. //! [4] //! [5]
  283. cursor.insertBlock();
  284. cursor.insertBlock();
  285. QDate date = QDate::currentDate();
  286. cursor.insertText(tr("Date: %1").arg(date.toString("d MMMM yyyy")),
  287. textFormat);
  288. cursor.insertBlock();
  289. QTextFrameFormat bodyFrameFormat;
  290. bodyFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
  291. cursor.insertFrame(bodyFrameFormat);
  292. //! [5]
  293. //! [6]
  294. cursor.insertText(tr("I would like to place an order for the following "
  295. "items:"), textFormat);
  296. cursor.insertBlock();
  297. //! [6] //! [7]
  298. cursor.insertBlock();
  299. //! [7]
  300. //! [8]
  301. QTextTableFormat orderTableFormat;
  302. orderTableFormat.setAlignment(Qt::AlignHCenter);
  303. QTextTable *orderTable = cursor.insertTable(1, 2, orderTableFormat);
  304. QTextFrameFormat orderFrameFormat = cursor.currentFrame()->frameFormat();
  305. orderFrameFormat.setBorder(1);
  306. cursor.currentFrame()->setFrameFormat(orderFrameFormat);
  307. //! [8]
  308. //! [9]
  309. cursor = orderTable->cellAt(0, 0).firstCursorPosition();
  310. cursor.insertText(tr("Product"), boldFormat);
  311. cursor = orderTable->cellAt(0, 1).firstCursorPosition();
  312. cursor.insertText(tr("Quantity"), boldFormat);
  313. //! [9]
  314. //! [10]
  315. for (int i = 0; i < orderItems.count(); ++i) {
  316. QPair<QString,int> item = orderItems[i];
  317. int row = orderTable->rows();
  318. orderTable->insertRows(row, 1);
  319. cursor = orderTable->cellAt(row, 0).firstCursorPosition();
  320. cursor.insertText(item.first, textFormat);
  321. cursor = orderTable->cellAt(row, 1).firstCursorPosition();
  322. cursor.insertText(QString("%1").arg(item.second), textFormat);
  323. }
  324. //! [10]
  325. //! [11]
  326. cursor.setPosition(topFrame->lastPosition());
  327. cursor.insertBlock();
  328. //! [11] //! [12]
  329. cursor.insertText(tr("Please update my records to take account of the "
  330. "following privacy information:"));
  331. cursor.insertBlock();
  332. //! [12]
  333. //! [13]
  334. QTextTable *offersTable = cursor.insertTable(2, 2);
  335. cursor = offersTable->cellAt(0, 1).firstCursorPosition();
  336. cursor.insertText(tr("I want to receive more information about your "
  337. "company's products and special offers."), textFormat);
  338. cursor = offersTable->cellAt(1, 1).firstCursorPosition();
  339. cursor.insertText(tr("I do not want to receive any promotional information "
  340. "from your company."), textFormat);
  341. if (sendOffers)
  342. cursor = offersTable->cellAt(0, 0).firstCursorPosition();
  343. else
  344. cursor = offersTable->cellAt(1, 0).firstCursorPosition();
  345. cursor.insertText("X", boldFormat);
  346. //! [13]
  347. //! [14]
  348. cursor.setPosition(topFrame->lastPosition());
  349. cursor.insertBlock();
  350. cursor.insertText(tr("Sincerely,"), textFormat);
  351. cursor.insertBlock();
  352. cursor.insertBlock();
  353. cursor.insertBlock();
  354. cursor.insertText(name);
  355. printAction->setEnabled(true);
  356. }
  357. //! [14]
  358. */