fb2head.cpp 5.0 KB

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