fb2read.cpp 11 KB

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