fb2tree.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. m_body = element.attribute("fb2_name");
  31. }
  32. if (!style.isEmpty()) m_name = style;
  33. } else if (m_name == "img") {
  34. m_name = "image";
  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, bool header)
  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. bool h = header;
  58. QString style = child.attribute("class").toLower();
  59. h = h || style == "description" || style == "stylesheet";
  60. if (!h || style == "annotation" || style == "history") {
  61. m_list << new Fb2TreeItem(child, this, direct ? number : -1);
  62. } else {
  63. addChildren(child, false, h);
  64. }
  65. } else if (header) {
  66. // skip
  67. } else if (tag == "img") {
  68. m_list << new Fb2TreeItem(child, this, direct ? number : -1);
  69. } else {
  70. addChildren(child, false, header);
  71. }
  72. child = child.nextSibling();
  73. number++;
  74. }
  75. }
  76. Fb2TreeItem * Fb2TreeItem::item(const QModelIndex &index) const
  77. {
  78. int row = index.row();
  79. if (row < 0 || row >= m_list.size()) return NULL;
  80. return m_list[row];
  81. }
  82. Fb2TreeItem * Fb2TreeItem::item(int row) const
  83. {
  84. if (row < 0 || row >= m_list.size()) return NULL;
  85. return m_list[row];
  86. }
  87. QString Fb2TreeItem::text() const
  88. {
  89. QString name = m_name;
  90. if (!m_body.isEmpty()) name += " name=" + m_body;
  91. return QString("<%1> %2").arg(name).arg(m_text);
  92. }
  93. QString Fb2TreeItem::selector() const
  94. {
  95. QString text = "";
  96. QString selector = ".get(0)";
  97. QWebElement element = m_element;
  98. QWebElement parent = element.parent();
  99. while (!parent.isNull()) {
  100. text.prepend(element.tagName()).prepend("/");
  101. QWebElement child = parent.firstChild();
  102. int index = -1;
  103. while (!child.isNull()) {
  104. index++;
  105. if (child == element) break;
  106. child = child.nextSibling();
  107. }
  108. if (index == -1) return QString();
  109. selector.prepend(QString(".children().eq(%1)").arg(index));
  110. element = parent;
  111. parent = element.parent();
  112. }
  113. return selector.prepend("$('html')");
  114. }
  115. Fb2TreeItem * Fb2TreeItem::content(const Fb2TreeModel &model, int number, QModelIndex &index) const
  116. {
  117. int row = 0;
  118. QList<Fb2TreeItem*>::const_iterator i;
  119. for (i = m_list.constBegin(); i != m_list.constEnd(); ++i) {
  120. if ((*i)->m_number == number) {
  121. index = model.index(row, 0, index);
  122. return *i;
  123. }
  124. row++;
  125. }
  126. return 0;
  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. QModelIndex Fb2TreeModel::move(const QModelIndex &index, int dx, int dy)
  213. {
  214. Fb2TreeItem *child = item(index);
  215. if (!child) return QModelIndex();
  216. Fb2TreeItem *owner = child->parent();
  217. if (!owner) return QModelIndex();
  218. int from = index.row();
  219. QModelIndex parent = this->parent(index);
  220. QModelIndex result;
  221. switch (dx) {
  222. case -1: {
  223. if (!owner || owner == m_root) return QModelIndex();
  224. if (child->name() != "section") return QModelIndex();
  225. if (owner->name() != "section") return QModelIndex();
  226. QModelIndex target = this->parent(parent);
  227. int to = parent.row() + 1;
  228. result = createIndex(to, 0, (void*)child);
  229. beginMoveRows(parent, from, from, target, to);
  230. QWebElement element = child->element().takeFromDocument();
  231. owner->element().appendOutside(element);
  232. owner->takeAt(from);
  233. owner->parent()->insert(child, to);
  234. endMoveRows();
  235. } break;
  236. case +1: {
  237. if (from == 0) return QModelIndex();
  238. Fb2TreeItem * brother = owner->item(from - 1);
  239. if (child->name() != "section") return QModelIndex();
  240. if (brother->name() != "section") return QModelIndex();
  241. QModelIndex target = createIndex(from - 1, 0, (void*)brother);
  242. int to = rowCount(target);
  243. result = createIndex(to, 0, (void*)child);
  244. beginMoveRows(parent, from, from, target, to);
  245. QWebElement element = child->element().takeFromDocument();
  246. brother->element().appendInside(element);
  247. owner->takeAt(from);
  248. brother->insert(child, to);
  249. endMoveRows();
  250. } break;
  251. default: {
  252. int to = from + dy;
  253. if (to < 0 || rowCount(parent) <= to) return QModelIndex();
  254. result = createIndex(to, 0, (void*)child);
  255. if (dy > 0) {
  256. to = index.row();
  257. from = to + dy;
  258. }
  259. Fb2TreeItem * child = owner->item(to);
  260. Fb2TreeItem * brother = owner->item(from);
  261. QString n = child->name();
  262. bool ok = (n == "body" || n == "section") && n == brother->name();
  263. if (!ok) return QModelIndex();
  264. beginMoveRows(parent, from, from, parent, to);
  265. brother = owner->takeAt(from);
  266. owner->insert(brother, to);
  267. QWebElement element = child->element().takeFromDocument();
  268. brother->element().appendOutside(element);
  269. endMoveRows();
  270. } break;
  271. }
  272. return result;
  273. }
  274. bool Fb2TreeModel::removeRows(int row, int count, const QModelIndex &parent)
  275. {
  276. if (row < 0 || count <= 0 || row + count > rowCount(parent)) return false;
  277. Fb2TreeItem * owner = item(parent);
  278. if (!owner) return false;
  279. int last = row + count - 1;
  280. beginRemoveRows(parent, row, last);
  281. for (int i = last; i >= row; i--) {
  282. if (Fb2TreeItem * child = owner->takeAt(i)) {
  283. child->element().removeFromDocument();
  284. delete child;
  285. }
  286. }
  287. endRemoveRows();
  288. return true;
  289. }
  290. //---------------------------------------------------------------------------
  291. // Fb2TreeView
  292. //---------------------------------------------------------------------------
  293. Fb2TreeView::Fb2TreeView(Fb2WebView &view, QWidget *parent)
  294. : QTreeView(parent)
  295. , m_view(view)
  296. {
  297. setHeaderHidden(true);
  298. setContextMenuPolicy(Qt::ActionsContextMenu);
  299. connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
  300. connect(m_view.page(), SIGNAL(loadFinished(bool)), SLOT(updateTree()));
  301. connect(m_view.page(), SIGNAL(contentsChanged()), SLOT(contentsChanged()));
  302. connect(m_view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  303. m_timerSelect.setInterval(1000);
  304. m_timerSelect.setSingleShot(true);
  305. connect(&m_timerSelect, SIGNAL(timeout()), SLOT(selectTree()));
  306. m_timerUpdate.setInterval(1000);
  307. m_timerUpdate.setSingleShot(true);
  308. connect(&m_timerUpdate, SIGNAL(timeout()), SLOT(updateTree()));
  309. QMetaObject::invokeMethod(this, "updateTree", Qt::QueuedConnection);
  310. }
  311. void Fb2TreeView::initToolbar(QToolBar *toolbar)
  312. {
  313. QAction * act;
  314. act = new QAction(FB2::icon("list-add"), tr("&Insert"), this);
  315. act->setShortcutContext(Qt::WidgetShortcut);
  316. act->setShortcut(QKeySequence("Insert"));
  317. act->setPriority(QAction::LowPriority);
  318. connect(act, SIGNAL(triggered()), SLOT(insertNode()));
  319. toolbar->addAction(act);
  320. addAction(act);
  321. act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
  322. act->setShortcutContext(Qt::WidgetShortcut);
  323. act->setShortcut(QKeySequence("Delete"));
  324. act->setPriority(QAction::LowPriority);
  325. connect(act, SIGNAL(triggered()), SLOT(deleteNode()));
  326. toolbar->addAction(act);
  327. addAction(act);
  328. toolbar->addSeparator();
  329. act = new QAction(FB2::icon("go-up"), tr("&Up"), this);
  330. connect(act, SIGNAL(triggered()), SLOT(moveUp()));
  331. toolbar->addAction(act);
  332. addAction(act);
  333. act = new QAction(FB2::icon("go-down"), tr("&Down"), this);
  334. connect(act, SIGNAL(triggered()), SLOT(moveDown()));
  335. toolbar->addAction(act);
  336. addAction(act);
  337. act = new QAction(FB2::icon("go-previous"), tr("&Left"), this);
  338. connect(act, SIGNAL(triggered()), SLOT(moveLeft()));
  339. toolbar->addAction(act);
  340. addAction(act);
  341. act = new QAction(FB2::icon("go-next"), tr("&Right"), this);
  342. connect(act, SIGNAL(triggered()), SLOT(moveRight()));
  343. toolbar->addAction(act);
  344. addAction(act);
  345. }
  346. void Fb2TreeView::selectionChanged()
  347. {
  348. m_timerSelect.start();
  349. }
  350. void Fb2TreeView::contentsChanged()
  351. {
  352. m_timerUpdate.start();
  353. }
  354. void Fb2TreeView::activated(const QModelIndex &index)
  355. {
  356. if (qApp->focusWidget() == &m_view) return;
  357. if (Fb2TreeModel * m = model()) {
  358. m->selectText(index);
  359. }
  360. }
  361. void Fb2TreeView::selectTree()
  362. {
  363. if (qApp->focusWidget() == this) return;
  364. if (Fb2TreeModel * m = model()) {
  365. QWebFrame * frame = m->view().page()->mainFrame();
  366. static const QString javascript = FB2::read(":/js/get_location.js");
  367. QString location = frame->evaluateJavaScript(javascript).toString();
  368. QModelIndex index = m->index(location);
  369. if (!index.isValid()) return;
  370. setCurrentIndex(index);
  371. scrollTo(index);
  372. }
  373. }
  374. void Fb2TreeView::updateTree()
  375. {
  376. Fb2TreeModel * model = new Fb2TreeModel(m_view, this);
  377. setModel(model);
  378. selectTree();
  379. }
  380. void Fb2TreeView::insertNode()
  381. {
  382. }
  383. void Fb2TreeView::deleteNode()
  384. {
  385. if (Fb2TreeModel * m = model()) {
  386. QModelIndex index = currentIndex();
  387. QModelIndex result = m->parent(index);
  388. setCurrentIndex(result);
  389. emit currentChanged(result, index);
  390. emit QTreeView::activated(result);
  391. scrollTo(result);
  392. m->removeRow(index.row(), result);
  393. }
  394. }
  395. Fb2TreeModel * Fb2TreeView::model()
  396. {
  397. return qobject_cast<Fb2TreeModel*>(QTreeView::model());
  398. }
  399. void Fb2TreeView::moveCurrent(int dx, int dy)
  400. {
  401. if (Fb2TreeModel * m = model()) {
  402. QModelIndex index = currentIndex();
  403. QModelIndex result = m->move(index, dx, dy);
  404. if (result.isValid()) {
  405. setCurrentIndex(result);
  406. emit currentChanged(result, index);
  407. emit QTreeView::activated(result);
  408. scrollTo(result);
  409. }
  410. }
  411. }
  412. void Fb2TreeView::moveUp()
  413. {
  414. moveCurrent(0, -1);
  415. }
  416. void Fb2TreeView::moveDown()
  417. {
  418. moveCurrent(0, +1);
  419. }
  420. void Fb2TreeView::moveLeft()
  421. {
  422. moveCurrent(-1, 0);
  423. }
  424. void Fb2TreeView::moveRight()
  425. {
  426. moveCurrent(+1, 0);
  427. }
  428. //---------------------------------------------------------------------------
  429. // Fb2TreeWidget
  430. //---------------------------------------------------------------------------
  431. Fb2TreeWidget::Fb2TreeWidget(Fb2WebView &view, QWidget* parent)
  432. : QWidget(parent)
  433. {
  434. QVBoxLayout * layout = new QVBoxLayout(this);
  435. layout->setSpacing(0);
  436. layout->setContentsMargins(0, 0, 0, 0);
  437. layout->setObjectName(QString::fromUtf8("verticalLayout"));
  438. m_tree = new Fb2TreeView(view, this);
  439. m_tree->setContextMenuPolicy(Qt::ActionsContextMenu);
  440. layout->addWidget(m_tree);
  441. m_tool = new QToolBar(this);
  442. layout->addWidget(m_tool);
  443. m_tree->initToolbar(m_tool);
  444. }