fb2tree.cpp 10 KB

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