1
0

fb2tree.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "fb2tree.hpp"
  2. #include <QtDebug>
  3. #include <QWebFrame>
  4. #include <QWebPage>
  5. #include <QTreeView>
  6. #include <QUrl>
  7. #include "fb2utils.h"
  8. #include "fb2view.hpp"
  9. Fb2TreeItem::Fb2TreeItem(QWebElement &element, Fb2TreeItem *parent, int number)
  10. : QObject(parent)
  11. , m_element(element)
  12. , m_parent(parent)
  13. , m_number(number)
  14. {
  15. m_name = element.tagName().toLower();
  16. QString style = element.attribute("class").toLower();
  17. if (m_name == "div") {
  18. if (style == "title") {
  19. m_text = title(element);
  20. if (m_parent) m_parent->m_text += m_text += " ";
  21. } else if (style == "subtitle") {
  22. m_text = title(element);
  23. } else if (style == "body") {
  24. QString name = element.attribute("name");
  25. if (!name.isEmpty()) style += " name=" + name;
  26. }
  27. if (!style.isEmpty()) m_name = style;
  28. } else if (m_name == "img") {
  29. QUrl url = element.attribute("src");
  30. m_text = url.path();
  31. }
  32. addChildren(element);
  33. }
  34. Fb2TreeItem::~Fb2TreeItem()
  35. {
  36. foreach (Fb2TreeItem * item, m_list) {
  37. delete item;
  38. }
  39. }
  40. QString Fb2TreeItem::title(const QWebElement &element)
  41. {
  42. return element.toPlainText().left(255).simplified();
  43. }
  44. void Fb2TreeItem::addChildren(QWebElement &parent, bool direct)
  45. {
  46. int number = 0;
  47. QWebElement child = parent.firstChild();
  48. while (!child.isNull()) {
  49. QString tag = child.tagName().toLower();
  50. if (tag == "div") {
  51. m_list << new Fb2TreeItem(child, this, direct ? number : -1);
  52. } else if (tag == "img") {
  53. m_list << new Fb2TreeItem(child, this, direct ? number : -1);
  54. } else {
  55. addChildren(child, false);
  56. }
  57. child = child.nextSibling();
  58. number++;
  59. }
  60. }
  61. Fb2TreeItem * Fb2TreeItem::item(const QModelIndex &index) const
  62. {
  63. int row = index.row();
  64. if (row < 0 || row >= m_list.size()) return NULL;
  65. return m_list[row];
  66. }
  67. Fb2TreeItem * Fb2TreeItem::item(int row) const
  68. {
  69. if (row < 0 || row >= m_list.size()) return NULL;
  70. return m_list[row];
  71. }
  72. QString Fb2TreeItem::text() const
  73. {
  74. return QString("<%1> %2").arg(m_name).arg(m_text);
  75. }
  76. QString Fb2TreeItem::selector() const
  77. {
  78. QString text = "";
  79. QString selector = ".get(0)";
  80. QWebElement element = m_element;
  81. QWebElement parent = element.parent();
  82. while (!parent.isNull()) {
  83. text.prepend(element.tagName()).prepend("/");
  84. QWebElement child = parent.firstChild();
  85. int index = -1;
  86. while (!child.isNull()) {
  87. index++;
  88. if (child == element) break;
  89. child = child.nextSibling();
  90. }
  91. if (index == -1) return QString();
  92. selector.prepend(QString(".children().eq(%1)").arg(index));
  93. element = parent;
  94. parent = element.parent();
  95. }
  96. return selector.prepend("$('html')");
  97. }
  98. Fb2TreeItem * Fb2TreeItem::content(const Fb2TreeModel &model, int number, QModelIndex &index) const
  99. {
  100. int row = 0;
  101. QList<Fb2TreeItem*>::const_iterator i;
  102. for (i = m_list.constBegin(); i != m_list.constEnd(); ++i) {
  103. if ((*i)->m_number == number) {
  104. index = model.index(row, 0, index);
  105. return *i;
  106. }
  107. row++;
  108. }
  109. return 0;
  110. }
  111. //---------------------------------------------------------------------------
  112. // Fb2TreeModel
  113. //---------------------------------------------------------------------------
  114. Fb2TreeModel::Fb2TreeModel(Fb2WebView &view, QObject *parent)
  115. : QAbstractItemModel(parent)
  116. , m_view(view)
  117. , m_root(NULL)
  118. {
  119. QWebElement doc = view.page()->mainFrame()->documentElement();
  120. QWebElement body = doc.findFirst("body");
  121. if (body.isNull()) return;
  122. m_root = new Fb2TreeItem(body);
  123. }
  124. Fb2TreeModel::~Fb2TreeModel()
  125. {
  126. if (m_root) delete m_root;
  127. }
  128. Fb2TreeItem * Fb2TreeModel::item(const QModelIndex &index) const
  129. {
  130. if (index.isValid()) {
  131. return static_cast<Fb2TreeItem*>(index.internalPointer());
  132. } else {
  133. return m_root;
  134. }
  135. }
  136. int Fb2TreeModel::columnCount(const QModelIndex &parent) const
  137. {
  138. Q_UNUSED(parent);
  139. return 1;
  140. }
  141. QModelIndex Fb2TreeModel::index(int row, int column, const QModelIndex &parent) const
  142. {
  143. if (!m_root || row < 0 || column < 0) return QModelIndex();
  144. if (Fb2TreeItem *owner = item(parent)) {
  145. if (Fb2TreeItem *child = owner->item(row)) {
  146. return createIndex(row, column, (void*)child);
  147. }
  148. }
  149. return QModelIndex();
  150. }
  151. QModelIndex Fb2TreeModel::parent(const QModelIndex &child) const
  152. {
  153. if (Fb2TreeItem * node = static_cast<Fb2TreeItem*>(child.internalPointer())) {
  154. if (Fb2TreeItem * parent = node->parent()) {
  155. if (Fb2TreeItem * owner = parent->parent()) {
  156. return createIndex(owner->index(parent), 0, (void*)parent);
  157. }
  158. }
  159. }
  160. return QModelIndex();
  161. }
  162. int Fb2TreeModel::rowCount(const QModelIndex &parent) const
  163. {
  164. if (parent.column() > 0) return 0;
  165. Fb2TreeItem *owner = item(parent);
  166. return owner ? owner->count() : 0;
  167. }
  168. QVariant Fb2TreeModel::data(const QModelIndex &index, int role) const
  169. {
  170. if (role != Qt::DisplayRole) return QVariant();
  171. Fb2TreeItem * i = item(index);
  172. return i ? i->text() : QVariant();
  173. }
  174. void Fb2TreeModel::expandBody(QTreeView *view)
  175. {
  176. QModelIndex parent = QModelIndex();
  177. int count = rowCount(parent);
  178. for (int i = 0; i < count; i++) {
  179. QModelIndex child = index(i, 0, parent);
  180. Fb2TreeItem *node = item(child);
  181. if (node && node->name() == "body") view->expand(child);
  182. }
  183. }
  184. void Fb2TreeModel::selectText(const QModelIndex &index)
  185. {
  186. Fb2TreeItem *node = item(index);
  187. if (!node) return;
  188. QWebFrame *frame = m_view.page()->mainFrame();
  189. frame->scroll(0, node->pos().y() - frame->scrollPosition().y());
  190. static const QString setCursor = FB2::read(":/js/set_cursor.js");
  191. QString javascript = QString("var element=%1;").arg(node->selector()) + setCursor;
  192. frame->evaluateJavaScript(javascript);
  193. m_view.setFocus();
  194. }
  195. QModelIndex Fb2TreeModel::index(const QString &location, QModelIndex current) const
  196. {
  197. QModelIndex index;
  198. Fb2TreeItem * parent = m_root;
  199. QStringList list = location.split(",");
  200. QStringListIterator iterator(list);
  201. while (parent && iterator.hasNext()) {
  202. QString str = iterator.next();
  203. if (str.left(5) == "HTML=") continue;
  204. int key = str.mid(str.indexOf("=")+1).toInt();
  205. Fb2TreeItem * child = parent->content(*this, key, index);
  206. parent = child;
  207. }
  208. while (current.isValid()) {
  209. if (current == index) return QModelIndex();
  210. current = this->parent(current);
  211. }
  212. return index;
  213. }
  214. //---------------------------------------------------------------------------
  215. // Fb2TreeView
  216. //---------------------------------------------------------------------------
  217. Fb2TreeView::Fb2TreeView(Fb2WebView &view, QWidget *parent)
  218. : QTreeView(parent)
  219. , m_view(view)
  220. {
  221. setHeaderHidden(true);
  222. connect(&m_view, SIGNAL(loadFinished(bool)), SLOT(updateTree()));
  223. connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
  224. connect(m_view.page(), SIGNAL(contentsChanged()), SLOT(contentsChanged()));
  225. connect(m_view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  226. m_timerSelect.setInterval(1000);
  227. m_timerSelect.setSingleShot(true);
  228. connect(&m_timerSelect, SIGNAL(timeout()), SLOT(selectTree()));
  229. m_timerUpdate.setInterval(1000);
  230. m_timerUpdate.setSingleShot(true);
  231. connect(&m_timerUpdate, SIGNAL(timeout()), SLOT(updateTree()));
  232. }
  233. void Fb2TreeView::selectionChanged()
  234. {
  235. m_timerSelect.start();
  236. }
  237. void Fb2TreeView::contentsChanged()
  238. {
  239. m_timerUpdate.start();
  240. }
  241. void Fb2TreeView::activated(const QModelIndex &index)
  242. {
  243. if (!model()) return;
  244. Fb2TreeModel *model = static_cast<Fb2TreeModel*>(this->model());
  245. disconnect(m_view.page(), SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
  246. model->selectText(index);
  247. connect(m_view.page(), SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
  248. }
  249. void Fb2TreeView::selectTree()
  250. {
  251. if (model() == 0) return;
  252. Fb2TreeModel * model = static_cast<Fb2TreeModel*>(this->model());
  253. QWebFrame * frame = model->view().page()->mainFrame();
  254. static const QString javascript = FB2::read(":/js/get_location.js");
  255. QString location = frame->evaluateJavaScript(javascript).toString();
  256. QModelIndex index = model->index(location, currentIndex());
  257. if (!index.isValid()) return;
  258. disconnect(this, SIGNAL(activated(QModelIndex)), this, SLOT(activated(QModelIndex)));
  259. setCurrentIndex(index);
  260. scrollTo(index);
  261. connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(activated(QModelIndex)));
  262. }
  263. void Fb2TreeView::updateTree()
  264. {
  265. Fb2TreeModel * model = new Fb2TreeModel(m_view, this);
  266. setModel(model);
  267. model->expandBody(this);
  268. selectTree();
  269. }