fb2read.cpp 8.9 KB

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