fb2html.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "fb2html.h"
  2. #include "fb2utils.h"
  3. #include "fb2text.hpp"
  4. //---------------------------------------------------------------------------
  5. // Fb2TextElement
  6. //---------------------------------------------------------------------------
  7. void Fb2TextElement::getChildren(Fb2ElementList &list)
  8. {
  9. Fb2TextElement child = firstChild();
  10. while (!child.isNull()) {
  11. QString tag = child.tagName().toLower();
  12. if (tag == "div") {
  13. if (child.hasAttribute("class")) list << child;
  14. } else if (tag == "img") {
  15. list << child;
  16. } else {
  17. child.getChildren(list);
  18. }
  19. child = child.nextSibling();
  20. }
  21. }
  22. QString Fb2TextElement::location()
  23. {
  24. static const QString javascript = FB2::read(":/js/get_location.js").prepend("var element=this;");
  25. return evaluateJavaScript(javascript).toString();
  26. }
  27. void Fb2TextElement::select()
  28. {
  29. static const QString javascript = FB2::read(":/js/set_cursor.js");
  30. evaluateJavaScript(javascript);
  31. }
  32. bool Fb2TextElement::isSection() const
  33. {
  34. return tagName() == "DIV" && attribute("class").toLower() == "section";
  35. }
  36. bool Fb2TextElement::isTitle() const
  37. {
  38. return tagName() == "DIV" && attribute("class").toLower() == "title";
  39. }
  40. //---------------------------------------------------------------------------
  41. // Fb2AddBodyCmd
  42. //---------------------------------------------------------------------------
  43. void Fb2AddBodyCmd::undo()
  44. {
  45. m_page.body().lastChild().removeFromDocument();
  46. Fb2TextElement(m_page.body().lastChild()).select();
  47. m_page.update();
  48. }
  49. void Fb2AddBodyCmd::redo()
  50. {
  51. m_page.body().appendInside("<div class=body><div class=section><p>text</p></div></div>");
  52. Fb2TextElement(m_page.body().lastChild()).select();
  53. m_page.update();
  54. }
  55. //---------------------------------------------------------------------------
  56. // Fb2SubtitleCmd
  57. //---------------------------------------------------------------------------
  58. void Fb2SubtitleCmd::redo()
  59. {
  60. QString html = "<div class=subtitle><p><br/></p></div>";
  61. Fb2TextElement element = m_page.element(m_location);
  62. if (m_element.isNull()) {
  63. element.appendOutside(html);
  64. } else {
  65. element.appendOutside(m_element);
  66. }
  67. element = element.nextSibling();
  68. m_position = element.location();
  69. element.select();
  70. m_page.update();
  71. }
  72. void Fb2SubtitleCmd::undo()
  73. {
  74. Fb2TextElement element = m_page.element(m_position);
  75. Fb2TextElement parent = element.parent();
  76. m_element = element.takeFromDocument();
  77. parent.select();
  78. m_page.update();
  79. }
  80. //---------------------------------------------------------------------------
  81. // Fb2DeleteCmd
  82. //---------------------------------------------------------------------------
  83. Fb2DeleteCmd::Fb2DeleteCmd(Fb2TextPage &page, const Fb2TextElement &element, QUndoCommand *parent)
  84. : QUndoCommand(parent)
  85. , m_element(element)
  86. , m_page(page)
  87. , m_inner(false)
  88. {
  89. m_parent = element.previousSibling();
  90. if (m_parent.isNull()) {
  91. m_parent = element.parent();
  92. m_inner = true;
  93. }
  94. }
  95. void Fb2DeleteCmd::redo()
  96. {
  97. m_element.takeFromDocument();
  98. m_page.update();
  99. }
  100. void Fb2DeleteCmd::undo()
  101. {
  102. if (m_inner) {
  103. m_parent.prependInside(m_element);
  104. } else {
  105. m_parent.appendOutside(m_element);
  106. }
  107. m_page.update();
  108. }