fb2html.cpp 2.9 KB

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