fb2tree.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. void Fb2TreeItem::move(Fb2TreeItem * child, int delta)
  119. {
  120. int x = index(child);
  121. int i = x; if (delta < 0) i += delta;
  122. int j = x; if (delta > 0) j += delta;
  123. if (i < 0 || j >= count()) return;
  124. QWebElement element = m_list[i]->element().takeFromDocument();
  125. m_list[j]->element().appendOutside(element);
  126. m_list.move(i, j);
  127. }
  128. //---------------------------------------------------------------------------
  129. // Fb2TreeModel
  130. //---------------------------------------------------------------------------
  131. Fb2TreeModel::Fb2TreeModel(Fb2WebView &view, QObject *parent)
  132. : QAbstractItemModel(parent)
  133. , m_view(view)
  134. , m_root(NULL)
  135. {
  136. QWebElement doc = view.page()->mainFrame()->documentElement();
  137. QWebElement body = doc.findFirst("body");
  138. if (body.isNull()) return;
  139. m_root = new Fb2TreeItem(body);
  140. }
  141. Fb2TreeModel::~Fb2TreeModel()
  142. {
  143. if (m_root) delete m_root;
  144. }
  145. Fb2TreeItem * Fb2TreeModel::item(const QModelIndex &index) const
  146. {
  147. if (index.isValid()) {
  148. return static_cast<Fb2TreeItem*>(index.internalPointer());
  149. } else {
  150. return m_root;
  151. }
  152. }
  153. int Fb2TreeModel::columnCount(const QModelIndex &parent) const
  154. {
  155. Q_UNUSED(parent);
  156. return 1;
  157. }
  158. QModelIndex Fb2TreeModel::index(int row, int column, const QModelIndex &parent) const
  159. {
  160. if (!m_root || row < 0 || column < 0) return QModelIndex();
  161. if (Fb2TreeItem *owner = item(parent)) {
  162. if (Fb2TreeItem *child = owner->item(row)) {
  163. return createIndex(row, column, (void*)child);
  164. }
  165. }
  166. return QModelIndex();
  167. }
  168. QModelIndex Fb2TreeModel::parent(const QModelIndex &child) const
  169. {
  170. if (Fb2TreeItem * node = static_cast<Fb2TreeItem*>(child.internalPointer())) {
  171. if (Fb2TreeItem * parent = node->parent()) {
  172. if (Fb2TreeItem * owner = parent->parent()) {
  173. return createIndex(owner->index(parent), 0, (void*)parent);
  174. }
  175. }
  176. }
  177. return QModelIndex();
  178. }
  179. int Fb2TreeModel::rowCount(const QModelIndex &parent) const
  180. {
  181. if (parent.column() > 0) return 0;
  182. Fb2TreeItem *owner = item(parent);
  183. return owner ? owner->count() : 0;
  184. }
  185. QVariant Fb2TreeModel::data(const QModelIndex &index, int role) const
  186. {
  187. if (role != Qt::DisplayRole) return QVariant();
  188. Fb2TreeItem * i = item(index);
  189. return i ? i->text() : QVariant();
  190. }
  191. void Fb2TreeModel::selectText(const QModelIndex &index)
  192. {
  193. if (Fb2TreeItem *node = item(index)) {
  194. node->element().select();
  195. }
  196. }
  197. QModelIndex Fb2TreeModel::index(const QString &location) const
  198. {
  199. QModelIndex index;
  200. Fb2TreeItem * parent = m_root;
  201. QStringList list = location.split(",");
  202. QStringListIterator iterator(list);
  203. while (parent && iterator.hasNext()) {
  204. QString str = iterator.next();
  205. if (str.left(5) == "HTML=") continue;
  206. int key = str.mid(str.indexOf("=")+1).toInt();
  207. Fb2TreeItem * child = parent->content(*this, key, index);
  208. parent = child;
  209. }
  210. return index;
  211. }
  212. //---------------------------------------------------------------------------
  213. // Fb2TreeView
  214. //---------------------------------------------------------------------------
  215. Fb2TreeView::Fb2TreeView(Fb2WebView &view, QWidget *parent)
  216. : QTreeView(parent)
  217. , m_view(view)
  218. {
  219. setHeaderHidden(true);
  220. setContextMenuPolicy(Qt::ActionsContextMenu);
  221. connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
  222. connect(m_view.page(), SIGNAL(loadFinished(bool)), SLOT(updateTree()));
  223. connect(m_view.page(), SIGNAL(contentsChanged()), SLOT(contentsChanged()));
  224. connect(m_view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  225. m_timerSelect.setInterval(1000);
  226. m_timerSelect.setSingleShot(true);
  227. connect(&m_timerSelect, SIGNAL(timeout()), SLOT(selectTree()));
  228. m_timerUpdate.setInterval(1000);
  229. m_timerUpdate.setSingleShot(true);
  230. connect(&m_timerUpdate, SIGNAL(timeout()), SLOT(updateTree()));
  231. QMetaObject::invokeMethod(this, "updateTree", Qt::QueuedConnection);
  232. }
  233. void Fb2TreeView::initToolbar(QToolBar *toolbar)
  234. {
  235. QAction * act;
  236. act = new QAction(FB2::icon("list-add"), tr("&Insert"), this);
  237. connect(act, SIGNAL(triggered()), SLOT(insertNode()));
  238. toolbar->addAction(act);
  239. addAction(act);
  240. act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
  241. connect(act, SIGNAL(triggered()), SLOT(deleteNode()));
  242. toolbar->addAction(act);
  243. addAction(act);
  244. toolbar->addSeparator();
  245. act = new QAction(FB2::icon("go-up"), tr("&Up"), this);
  246. connect(act, SIGNAL(triggered()), SLOT(moveUp()));
  247. toolbar->addAction(act);
  248. addAction(act);
  249. act = new QAction(FB2::icon("go-down"), tr("&Down"), this);
  250. connect(act, SIGNAL(triggered()), SLOT(moveDown()));
  251. toolbar->addAction(act);
  252. addAction(act);
  253. act = new QAction(FB2::icon("go-previous"), tr("&Left"), this);
  254. connect(act, SIGNAL(triggered()), SLOT(moveLeft()));
  255. toolbar->addAction(act);
  256. addAction(act);
  257. act = new QAction(FB2::icon("go-next"), tr("&Right"), this);
  258. connect(act, SIGNAL(triggered()), SLOT(moveRight()));
  259. toolbar->addAction(act);
  260. addAction(act);
  261. }
  262. void Fb2TreeView::selectionChanged()
  263. {
  264. m_timerSelect.start();
  265. }
  266. void Fb2TreeView::contentsChanged()
  267. {
  268. m_timerUpdate.start();
  269. }
  270. void Fb2TreeView::activated(const QModelIndex &index)
  271. {
  272. if (qApp->focusWidget() == &m_view) return;
  273. if (Fb2TreeModel * m = model()) {
  274. m->selectText(index);
  275. }
  276. }
  277. void Fb2TreeView::selectTree()
  278. {
  279. if (qApp->focusWidget() == this) return;
  280. if (Fb2TreeModel * m = model()) {
  281. QWebFrame * frame = m->view().page()->mainFrame();
  282. static const QString javascript = FB2::read(":/js/get_location.js");
  283. QString location = frame->evaluateJavaScript(javascript).toString();
  284. QModelIndex index = m->index(location);
  285. if (!index.isValid()) return;
  286. setCurrentIndex(index);
  287. scrollTo(index);
  288. }
  289. }
  290. void Fb2TreeView::updateTree()
  291. {
  292. Fb2TreeModel * model = new Fb2TreeModel(m_view, this);
  293. setModel(model);
  294. selectTree();
  295. }
  296. void Fb2TreeView::insertNode()
  297. {
  298. }
  299. void Fb2TreeView::deleteNode()
  300. {
  301. }
  302. Fb2TreeModel * Fb2TreeView::model()
  303. {
  304. return qobject_cast<Fb2TreeModel*>(QTreeView::model());
  305. }
  306. QModelIndex Fb2TreeModel::move(const QModelIndex &index, int dx, int dy)
  307. {
  308. Fb2TreeItem *child = item(index);
  309. if (!child) return QModelIndex();
  310. Fb2TreeItem *owner = child->parent();
  311. if (!owner) return QModelIndex();
  312. int from = index.row();
  313. QModelIndex parent = this->parent(index);
  314. QModelIndex result;
  315. switch (dx) {
  316. case -1: {
  317. if (owner == m_root) return QModelIndex();
  318. QModelIndex target = this->parent(parent);
  319. int to = parent.row() + 1;
  320. result = createIndex(to, 0, (void*)child);
  321. beginMoveRows(parent, from, from, target, to);
  322. QWebElement element = child->element().takeFromDocument();
  323. owner->element().appendOutside(element);
  324. owner->takeAt(from);
  325. owner->parent()->insert(child, to);
  326. endMoveRows();
  327. } break;
  328. case +1: {
  329. if (from == 0) return QModelIndex();
  330. Fb2TreeItem * brother = owner->item(from - 1);
  331. QModelIndex target = createIndex(from - 1, 0, (void*)brother);
  332. int to = rowCount(target);
  333. result = createIndex(to, 0, (void*)child);
  334. beginMoveRows(parent, from, from, target, to);
  335. QWebElement element = child->element().takeFromDocument();
  336. brother->element().appendInside(element);
  337. owner->takeAt(from);
  338. brother->insert(child, to);
  339. endMoveRows();
  340. } break;
  341. default: {
  342. int to = from + dy;
  343. if (to < 0 || rowCount(parent) <= to) return QModelIndex();
  344. result = createIndex(to, 0, (void*)child);
  345. if (dy > 0) {
  346. from = to;
  347. to = index.row();
  348. }
  349. beginMoveRows(parent, from, from, parent, to);
  350. owner->move(child, dy);
  351. endMoveRows();
  352. } break;
  353. }
  354. return result;
  355. }
  356. void Fb2TreeView::moveCurrent(int dx, int dy)
  357. {
  358. if (Fb2TreeModel * m = model()) {
  359. QModelIndex index = currentIndex();
  360. QModelIndex result = m->move(index, dx, dy);
  361. if (result.isValid()) {
  362. setCurrentIndex(result);
  363. emit currentChanged(result, index);
  364. emit QTreeView::activated(result);
  365. scrollTo(result);
  366. }
  367. }
  368. }
  369. void Fb2TreeView::moveUp()
  370. {
  371. moveCurrent(0, -1);
  372. }
  373. void Fb2TreeView::moveDown()
  374. {
  375. moveCurrent(0, +1);
  376. }
  377. void Fb2TreeView::moveLeft()
  378. {
  379. moveCurrent(-1, 0);
  380. }
  381. void Fb2TreeView::moveRight()
  382. {
  383. moveCurrent(+1, 0);
  384. }
  385. //---------------------------------------------------------------------------
  386. // Fb2TreeWidget
  387. //---------------------------------------------------------------------------
  388. Fb2TreeWidget::Fb2TreeWidget(Fb2WebView &view, QWidget* parent)
  389. : QWidget(parent)
  390. {
  391. QVBoxLayout * layout = new QVBoxLayout(this);
  392. layout->setSpacing(0);
  393. layout->setContentsMargins(0, 0, 0, 0);
  394. layout->setObjectName(QString::fromUtf8("verticalLayout"));
  395. m_tree = new Fb2TreeView(view, this);
  396. m_tree->setContextMenuPolicy(Qt::ActionsContextMenu);
  397. layout->addWidget(m_tree);
  398. m_tool = new QToolBar(this);
  399. layout->addWidget(m_tool);
  400. m_tree->initToolbar(m_tool);
  401. }