fb2head.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "fb2head.h"
  2. #include <QtDebug>
  3. #include <QWebFrame>
  4. #include <QWebPage>
  5. #include <QWebView>
  6. #include <QTreeView>
  7. Fb2HeadItem::Fb2HeadItem(QWebElement &element, Fb2HeadItem *parent)
  8. : QObject(parent)
  9. , m_element(element)
  10. , m_parent(parent)
  11. {
  12. m_name = element.tagName().toLower();
  13. QString style = element.attribute("class").toLower();
  14. if (m_name == "div") {
  15. if (style == "title") {
  16. m_text = element.toPlainText().simplified().left(255);
  17. if (m_parent) m_parent->m_text += m_text += " ";
  18. } else if (style == "subtitle") {
  19. m_text = element.toPlainText().simplified().left(255);
  20. }
  21. if (!style.isEmpty()) m_name = style;
  22. } else if (m_name == "img") {
  23. m_text = element.attribute("alt");
  24. }
  25. m_id = element.attribute("id");
  26. addChildren(element);
  27. }
  28. Fb2HeadItem::~Fb2HeadItem()
  29. {
  30. foreach (Fb2HeadItem * item, m_list) {
  31. delete item;
  32. }
  33. }
  34. void Fb2HeadItem::addChildren(QWebElement &parent)
  35. {
  36. QWebElement child = parent.firstChild();
  37. while (!child.isNull()) {
  38. QString tag = child.tagName().toLower();
  39. if (tag == "div") {
  40. QString style = child.attribute("style");
  41. if (style != "display:none") m_list << new Fb2HeadItem(child, this);
  42. } else if (tag == "img") {
  43. m_list << new Fb2HeadItem(child, this);
  44. } else {
  45. addChildren(child);
  46. }
  47. child = child.nextSibling();
  48. }
  49. }
  50. Fb2HeadItem * Fb2HeadItem::item(const QModelIndex &index) const
  51. {
  52. int row = index.row();
  53. if (row < 0 || row >= m_list.size()) return NULL;
  54. return m_list[row];
  55. }
  56. Fb2HeadItem * Fb2HeadItem::item(int row) const
  57. {
  58. if (row < 0 || row >= m_list.size()) return NULL;
  59. return m_list[row];
  60. }
  61. QString Fb2HeadItem::text(int col) const
  62. {
  63. switch (col) {
  64. case 0: return QString("<%1>").arg(m_name);
  65. case 2: if (m_list.count() == 0) return m_element.toPlainText().simplified();
  66. }
  67. return QString();
  68. }
  69. //---------------------------------------------------------------------------
  70. // Fb2HeadModel
  71. //---------------------------------------------------------------------------
  72. Fb2HeadModel::Fb2HeadModel(QWebView &view, QObject *parent)
  73. : QAbstractItemModel(parent)
  74. , m_view(view)
  75. , m_root(NULL)
  76. {
  77. QWebElement doc = view.page()->mainFrame()->documentElement();
  78. QWebElement body = doc.findFirst("div.description");
  79. if (body.isNull()) return;
  80. m_root = new Fb2HeadItem(body);
  81. }
  82. Fb2HeadModel::~Fb2HeadModel()
  83. {
  84. if (m_root) delete m_root;
  85. }
  86. void Fb2HeadModel::expand(QTreeView *view)
  87. {
  88. QModelIndex parent = QModelIndex();
  89. int count = rowCount(parent);
  90. for (int i = 0; i < count; i++) {
  91. QModelIndex child = index(i, 0, parent);
  92. Fb2HeadItem *node = item(child);
  93. if (node && node->name() == "body") view->expand(child);
  94. }
  95. }
  96. Fb2HeadItem * Fb2HeadModel::item(const QModelIndex &index) const
  97. {
  98. if (index.isValid()) {
  99. return static_cast<Fb2HeadItem*>(index.internalPointer());
  100. } else {
  101. return m_root;
  102. }
  103. }
  104. int Fb2HeadModel::columnCount(const QModelIndex &parent) const
  105. {
  106. Q_UNUSED(parent);
  107. return 3;
  108. }
  109. QModelIndex Fb2HeadModel::index(int row, int column, const QModelIndex &parent) const
  110. {
  111. if (!m_root || row < 0 || column < 0) return QModelIndex();
  112. if (Fb2HeadItem *owner = item(parent)) {
  113. if (Fb2HeadItem *child = owner->item(row)) {
  114. return createIndex(row, column, (void*)child);
  115. }
  116. }
  117. return QModelIndex();
  118. }
  119. QModelIndex Fb2HeadModel::parent(const QModelIndex &child) const
  120. {
  121. if (Fb2HeadItem * node = static_cast<Fb2HeadItem*>(child.internalPointer())) {
  122. if (Fb2HeadItem * parent = node->parent()) {
  123. if (Fb2HeadItem * owner = parent->parent()) {
  124. return createIndex(owner->index(parent), 0, (void*)parent);
  125. }
  126. }
  127. }
  128. return QModelIndex();
  129. }
  130. int Fb2HeadModel::rowCount(const QModelIndex &parent) const
  131. {
  132. if (parent.column() > 0) return 0;
  133. Fb2HeadItem *owner = item(parent);
  134. return owner ? owner->count() : 0;
  135. }
  136. QVariant Fb2HeadModel::data(const QModelIndex &index, int role) const
  137. {
  138. if (role != Qt::DisplayRole) return QVariant();
  139. Fb2HeadItem * i = item(index);
  140. return i ? i->text(index.column()) : QVariant();
  141. }
  142. QVariant Fb2HeadModel::headerData(int section, Qt::Orientation orientation, int role) const
  143. {
  144. if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
  145. switch (section) {
  146. case 0: return tr("Tag");
  147. case 1: return tr("Description");
  148. case 2: return tr("Value");
  149. }
  150. }
  151. return QVariant();
  152. }
  153. void Fb2HeadModel::select(const QModelIndex &index)
  154. {
  155. Fb2HeadItem *node = item(index);
  156. if (!node || node->id().isEmpty()) return;
  157. m_view.page()->mainFrame()->scrollToAnchor(node->id());
  158. }