fb2read.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <QtGui>
  2. #include <QTextEdit>
  3. #include "fb2read.h"
  4. Fb2Reader::Fb2Reader(QTextEdit * editor)
  5. : m_editor(editor)
  6. , m_cursor(editor->textCursor())
  7. , m_section(None)
  8. {
  9. m_cursor.beginEditBlock();
  10. editor->clear();
  11. m_cursor.movePosition(QTextCursor::Start);
  12. }
  13. Fb2Reader::~Fb2Reader()
  14. {
  15. m_cursor.endEditBlock();
  16. }
  17. bool Fb2Reader::startElement(const QString & /* namespaceURI */,
  18. const QString & /* localName */,
  19. const QString &qName,
  20. const QXmlAttributes &attributes)
  21. {
  22. const QString name = qName.toLower();
  23. switch (m_tags.count()) {
  24. case 0: {
  25. if (name != "fictionbook") {
  26. m_error = QObject::tr("The file is not an FB2 file.");
  27. return false;
  28. };
  29. } break;
  30. case 1: {
  31. if (name == "body") {
  32. m_section = Body;
  33. } else if (name == "description") {
  34. m_section = Descr;
  35. } else if (name == "binary") {
  36. m_section = Bynary;
  37. } else {
  38. m_section = None;
  39. }
  40. } break;
  41. default: {
  42. if (name == "p") m_cursor.insertBlock();
  43. } break;
  44. }
  45. m_text.clear();
  46. m_tags << name;
  47. return true;
  48. }
  49. bool Fb2Reader::endElement(const QString & /* namespaceURI */,
  50. const QString & /* localName */,
  51. const QString &qName)
  52. {
  53. const QString name = qName.toLower();
  54. int index = m_tags.lastIndexOf(name);
  55. int count = m_tags.count();
  56. for (int i = index; i < count; i++) m_tags.removeLast();
  57. if (m_tags.count() < 2) m_section = None;
  58. return true;
  59. }
  60. bool Fb2Reader::characters(const QString &str)
  61. {
  62. switch (m_section) {
  63. case Body: {
  64. m_cursor.insertText(str);
  65. } break;
  66. default: {
  67. m_text += str;
  68. }
  69. }
  70. return true;
  71. }
  72. bool Fb2Reader::fatalError(const QXmlParseException &exception)
  73. {
  74. QMessageBox::information(
  75. m_editor->window(),
  76. QObject::tr("fb2edit"),
  77. QObject::tr("Parse error at line %1, column %2:\n%3")
  78. .arg(exception.lineNumber())
  79. .arg(exception.columnNumber())
  80. .arg(exception.message())
  81. );
  82. return false;
  83. }
  84. QString Fb2Reader::errorString() const
  85. {
  86. return m_error;
  87. }
  88. /*
  89. //! [2]
  90. QTextCursor cursor(editor->textCursor());
  91. cursor.movePosition(QTextCursor::Start);
  92. //! [2] //! [3]
  93. QTextFrame *topFrame = cursor.currentFrame();
  94. QTextFrameFormat topFrameFormat = topFrame->frameFormat();
  95. topFrameFormat.setPadding(16);
  96. topFrame->setFrameFormat(topFrameFormat);
  97. QTextCharFormat textFormat;
  98. QTextCharFormat boldFormat;
  99. boldFormat.setFontWeight(QFont::Bold);
  100. QTextFrameFormat referenceFrameFormat;
  101. referenceFrameFormat.setBorder(1);
  102. referenceFrameFormat.setPadding(8);
  103. referenceFrameFormat.setPosition(QTextFrameFormat::FloatRight);
  104. referenceFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 40));
  105. cursor.insertFrame(referenceFrameFormat);
  106. cursor.insertText("A company", boldFormat);
  107. cursor.insertBlock();
  108. cursor.insertText("321 City Street");
  109. cursor.insertBlock();
  110. cursor.insertText("Industry Park");
  111. cursor.insertBlock();
  112. cursor.insertText("Another country");
  113. //! [3]
  114. //! [4]
  115. cursor.setPosition(topFrame->lastPosition());
  116. cursor.insertText(name, textFormat);
  117. QString line;
  118. foreach (line, address.split("\n")) {
  119. cursor.insertBlock();
  120. cursor.insertText(line);
  121. }
  122. //! [4] //! [5]
  123. cursor.insertBlock();
  124. cursor.insertBlock();
  125. QDate date = QDate::currentDate();
  126. cursor.insertText(tr("Date: %1").arg(date.toString("d MMMM yyyy")),
  127. textFormat);
  128. cursor.insertBlock();
  129. QTextFrameFormat bodyFrameFormat;
  130. bodyFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
  131. cursor.insertFrame(bodyFrameFormat);
  132. //! [5]
  133. //! [6]
  134. cursor.insertText(tr("I would like to place an order for the following "
  135. "items:"), textFormat);
  136. cursor.insertBlock();
  137. //! [6] //! [7]
  138. cursor.insertBlock();
  139. //! [7]
  140. //! [8]
  141. QTextTableFormat orderTableFormat;
  142. orderTableFormat.setAlignment(Qt::AlignHCenter);
  143. QTextTable *orderTable = cursor.insertTable(1, 2, orderTableFormat);
  144. QTextFrameFormat orderFrameFormat = cursor.currentFrame()->frameFormat();
  145. orderFrameFormat.setBorder(1);
  146. cursor.currentFrame()->setFrameFormat(orderFrameFormat);
  147. //! [8]
  148. //! [9]
  149. cursor = orderTable->cellAt(0, 0).firstCursorPosition();
  150. cursor.insertText(tr("Product"), boldFormat);
  151. cursor = orderTable->cellAt(0, 1).firstCursorPosition();
  152. cursor.insertText(tr("Quantity"), boldFormat);
  153. //! [9]
  154. //! [10]
  155. for (int i = 0; i < orderItems.count(); ++i) {
  156. QPair<QString,int> item = orderItems[i];
  157. int row = orderTable->rows();
  158. orderTable->insertRows(row, 1);
  159. cursor = orderTable->cellAt(row, 0).firstCursorPosition();
  160. cursor.insertText(item.first, textFormat);
  161. cursor = orderTable->cellAt(row, 1).firstCursorPosition();
  162. cursor.insertText(QString("%1").arg(item.second), textFormat);
  163. }
  164. //! [10]
  165. //! [11]
  166. cursor.setPosition(topFrame->lastPosition());
  167. cursor.insertBlock();
  168. //! [11] //! [12]
  169. cursor.insertText(tr("Please update my records to take account of the "
  170. "following privacy information:"));
  171. cursor.insertBlock();
  172. //! [12]
  173. //! [13]
  174. QTextTable *offersTable = cursor.insertTable(2, 2);
  175. cursor = offersTable->cellAt(0, 1).firstCursorPosition();
  176. cursor.insertText(tr("I want to receive more information about your "
  177. "company's products and special offers."), textFormat);
  178. cursor = offersTable->cellAt(1, 1).firstCursorPosition();
  179. cursor.insertText(tr("I do not want to receive any promotional information "
  180. "from your company."), textFormat);
  181. if (sendOffers)
  182. cursor = offersTable->cellAt(0, 0).firstCursorPosition();
  183. else
  184. cursor = offersTable->cellAt(1, 0).firstCursorPosition();
  185. cursor.insertText("X", boldFormat);
  186. //! [13]
  187. //! [14]
  188. cursor.setPosition(topFrame->lastPosition());
  189. cursor.insertBlock();
  190. cursor.insertText(tr("Sincerely,"), textFormat);
  191. cursor.insertBlock();
  192. cursor.insertBlock();
  193. cursor.insertBlock();
  194. cursor.insertText(name);
  195. printAction->setEnabled(true);
  196. }
  197. //! [14]
  198. */