fb2html.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "fb2html.h"
  2. #include "fb2utils.h"
  3. #include "fb2text.hpp"
  4. //---------------------------------------------------------------------------
  5. // FbTextElement
  6. //---------------------------------------------------------------------------
  7. void FbTextElement::getChildren(FbElementList &list)
  8. {
  9. FbTextElement 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 FbTextElement::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 FbTextElement::select()
  28. {
  29. static const QString javascript = FB2::read(":/js/set_cursor.js");
  30. evaluateJavaScript(javascript);
  31. }
  32. bool FbTextElement::isBody() const
  33. {
  34. return tagName() == "DIV" && attribute("class").toLower() == "body";
  35. }
  36. bool FbTextElement::isSection() const
  37. {
  38. return tagName() == "DIV" && attribute("class").toLower() == "section";
  39. }
  40. bool FbTextElement::hasTitle() const
  41. {
  42. return FbTextElement(firstChild()).isTitle();
  43. }
  44. bool FbTextElement::isTitle() const
  45. {
  46. return tagName() == "DIV" && attribute("class").toLower() == "title";
  47. }
  48. //---------------------------------------------------------------------------
  49. // FbInsertCmd
  50. //---------------------------------------------------------------------------
  51. FbInsertCmd::FbInsertCmd(const FbTextElement &element)
  52. : QUndoCommand()
  53. , m_element(element)
  54. , m_parent(element.previousSibling())
  55. , m_inner(false)
  56. {
  57. if (m_parent.isNull()) {
  58. m_parent = m_element.parent();
  59. m_inner = true;
  60. }
  61. }
  62. void FbInsertCmd::redo()
  63. {
  64. if (m_inner) {
  65. m_parent.prependInside(m_element);
  66. } else {
  67. m_parent.appendOutside(m_element);
  68. }
  69. m_element.select();
  70. }
  71. void FbInsertCmd::undo()
  72. {
  73. m_element.takeFromDocument();
  74. }
  75. //---------------------------------------------------------------------------
  76. // FbDeleteCmd
  77. //---------------------------------------------------------------------------
  78. FbDeleteCmd::FbDeleteCmd(const FbTextElement &element)
  79. : QUndoCommand()
  80. , m_element(element)
  81. , m_parent(element.previousSibling())
  82. , m_inner(false)
  83. {
  84. if (m_parent.isNull()) {
  85. m_parent = element.parent();
  86. m_inner = true;
  87. }
  88. }
  89. void FbDeleteCmd::redo()
  90. {
  91. m_element.takeFromDocument();
  92. }
  93. void FbDeleteCmd::undo()
  94. {
  95. if (m_inner) {
  96. m_parent.prependInside(m_element);
  97. } else {
  98. m_parent.appendOutside(m_element);
  99. }
  100. m_element.select();
  101. }
  102. //---------------------------------------------------------------------------
  103. // FbMoveUpCmd
  104. //---------------------------------------------------------------------------
  105. FbMoveUpCmd::FbMoveUpCmd(const FbTextElement &element)
  106. : QUndoCommand()
  107. , m_element(element)
  108. {
  109. }
  110. void FbMoveUpCmd::redo()
  111. {
  112. FbTextElement subling = m_element.previousSibling();
  113. subling.prependOutside(m_element.takeFromDocument());
  114. }
  115. void FbMoveUpCmd::undo()
  116. {
  117. FbTextElement subling = m_element.nextSibling();
  118. subling.appendOutside(m_element.takeFromDocument());
  119. }