fb2tree.cpp 10 KB

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