fb2read.cpp 11 KB

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