fb2read.cpp 11 KB

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