fb2tree.cpp 10 KB

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