fb2tree.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #include "fb2tree.hpp"
  2. #include <QtDebug>
  3. #include <QAction>
  4. #include <QApplication>
  5. #include <QCursor>
  6. #include <QVBoxLayout>
  7. #include <QWebFrame>
  8. #include <QWebPage>
  9. #include <QTreeView>
  10. #include <QUrl>
  11. #include "fb2utils.h"
  12. #include "fb2view.hpp"
  13. //---------------------------------------------------------------------------
  14. // Fb2TreeItem
  15. //---------------------------------------------------------------------------
  16. Fb2TreeItem::Fb2TreeItem(QWebElement &element, Fb2TreeItem *parent, int number)
  17. : QObject(parent)
  18. , m_element(element)
  19. , m_parent(parent)
  20. , m_number(number)
  21. {
  22. m_name = element.tagName().toLower();
  23. QString style = element.attribute("class").toLower();
  24. if (m_name == "div") {
  25. if (style == "title") {
  26. m_text = title(element);
  27. if (m_parent) m_parent->m_text += m_text += " ";
  28. } else if (style == "subtitle") {
  29. m_text = title(element);
  30. } else if (style == "body") {
  31. m_body = element.attribute("fb2_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, bool header)
  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. bool h = header;
  59. QString style = child.attribute("class").toLower();
  60. h = h || style == "description" || style == "stylesheet";
  61. if (!h || style == "annotation" || style == "history") {
  62. m_list << new Fb2TreeItem(child, this, direct ? number : -1);
  63. } else {
  64. addChildren(child, false, h);
  65. }
  66. } else if (header) {
  67. // skip
  68. } else if (tag == "img") {
  69. m_list << new Fb2TreeItem(child, this, direct ? number : -1);
  70. } else {
  71. addChildren(child, false, header);
  72. }
  73. child = child.nextSibling();
  74. number++;
  75. }
  76. }
  77. Fb2TreeItem * Fb2TreeItem::item(const QModelIndex &index) const
  78. {
  79. int row = index.row();
  80. if (row < 0 || row >= m_list.size()) return NULL;
  81. return m_list[row];
  82. }
  83. Fb2TreeItem * Fb2TreeItem::item(int row) const
  84. {
  85. if (row < 0 || row >= m_list.size()) return NULL;
  86. return m_list[row];
  87. }
  88. QString Fb2TreeItem::text() const
  89. {
  90. QString name = m_name;
  91. if (!m_body.isEmpty()) name += " name=" + m_body;
  92. return QString("<%1> %2").arg(name).arg(m_text);
  93. }
  94. QString Fb2TreeItem::selector() const
  95. {
  96. QString text = "";
  97. QString selector = ".get(0)";
  98. QWebElement element = m_element;
  99. QWebElement parent = element.parent();
  100. while (!parent.isNull()) {
  101. text.prepend(element.tagName()).prepend("/");
  102. QWebElement child = parent.firstChild();
  103. int index = -1;
  104. while (!child.isNull()) {
  105. index++;
  106. if (child == element) break;
  107. child = child.nextSibling();
  108. }
  109. if (index == -1) return QString();
  110. selector.prepend(QString(".children().eq(%1)").arg(index));
  111. element = parent;
  112. parent = element.parent();
  113. }
  114. return selector.prepend("$('html')");
  115. }
  116. Fb2TreeItem * Fb2TreeItem::content(const Fb2TreeModel &model, int number, QModelIndex &index) const
  117. {
  118. int row = 0;
  119. QList<Fb2TreeItem*>::const_iterator i;
  120. for (i = m_list.constBegin(); i != m_list.constEnd(); ++i) {
  121. if ((*i)->m_number == number) {
  122. index = model.index(row, 0, index);
  123. return *i;
  124. }
  125. row++;
  126. }
  127. return 0;
  128. }
  129. //---------------------------------------------------------------------------
  130. // Fb2TreeModel
  131. //---------------------------------------------------------------------------
  132. Fb2TreeModel::Fb2TreeModel(Fb2TextEdit &view, QObject *parent)
  133. : QAbstractItemModel(parent)
  134. , m_view(view)
  135. , m_root(NULL)
  136. {
  137. QWebElement doc = view.page()->mainFrame()->documentElement();
  138. QWebElement body = doc.findFirst("body");
  139. if (body.isNull()) return;
  140. m_root = new Fb2TreeItem(body);
  141. }
  142. Fb2TreeModel::~Fb2TreeModel()
  143. {
  144. if (m_root) delete m_root;
  145. }
  146. Fb2TreeItem * Fb2TreeModel::item(const QModelIndex &index) const
  147. {
  148. if (index.isValid()) {
  149. return static_cast<Fb2TreeItem*>(index.internalPointer());
  150. } else {
  151. return m_root;
  152. }
  153. }
  154. int Fb2TreeModel::columnCount(const QModelIndex &parent) const
  155. {
  156. Q_UNUSED(parent);
  157. return 1;
  158. }
  159. QModelIndex Fb2TreeModel::index(int row, int column, const QModelIndex &parent) const
  160. {
  161. if (!m_root || row < 0 || column < 0) return QModelIndex();
  162. if (Fb2TreeItem *owner = item(parent)) {
  163. if (Fb2TreeItem *child = owner->item(row)) {
  164. return createIndex(row, column, (void*)child);
  165. }
  166. }
  167. return QModelIndex();
  168. }
  169. QModelIndex Fb2TreeModel::parent(const QModelIndex &child) const
  170. {
  171. if (Fb2TreeItem * node = static_cast<Fb2TreeItem*>(child.internalPointer())) {
  172. if (Fb2TreeItem * parent = node->parent()) {
  173. if (Fb2TreeItem * owner = parent->parent()) {
  174. return createIndex(owner->index(parent), 0, (void*)parent);
  175. }
  176. }
  177. }
  178. return QModelIndex();
  179. }
  180. int Fb2TreeModel::rowCount(const QModelIndex &parent) const
  181. {
  182. if (parent.column() > 0) return 0;
  183. Fb2TreeItem *owner = item(parent);
  184. return owner ? owner->count() : 0;
  185. }
  186. QVariant Fb2TreeModel::data(const QModelIndex &index, int role) const
  187. {
  188. if (role != Qt::DisplayRole) return QVariant();
  189. Fb2TreeItem * i = item(index);
  190. return i ? i->text() : QVariant();
  191. }
  192. void Fb2TreeModel::selectText(const QModelIndex &index)
  193. {
  194. if (Fb2TreeItem *node = item(index)) {
  195. node->element().select();
  196. }
  197. }
  198. QModelIndex Fb2TreeModel::index(const QString &location) const
  199. {
  200. QModelIndex index;
  201. Fb2TreeItem * parent = m_root;
  202. QStringList list = location.split(",");
  203. QStringListIterator iterator(list);
  204. while (parent && iterator.hasNext()) {
  205. QString str = iterator.next();
  206. if (str.left(5) == "HTML=") continue;
  207. int key = str.mid(str.indexOf("=")+1).toInt();
  208. Fb2TreeItem * child = parent->content(*this, key, index);
  209. parent = child;
  210. }
  211. return index;
  212. }
  213. QModelIndex Fb2TreeModel::move(const QModelIndex &index, int dx, int dy)
  214. {
  215. Fb2TreeItem *child = item(index);
  216. if (!child) return QModelIndex();
  217. Fb2TreeItem *owner = child->parent();
  218. if (!owner) return QModelIndex();
  219. int from = index.row();
  220. QModelIndex parent = this->parent(index);
  221. QModelIndex result;
  222. switch (dx) {
  223. case -1: {
  224. if (!owner || owner == m_root) return QModelIndex();
  225. if (child->name() != "section") return QModelIndex();
  226. if (owner->name() != "section") return QModelIndex();
  227. QModelIndex target = this->parent(parent);
  228. int to = parent.row() + 1;
  229. result = createIndex(to, 0, (void*)child);
  230. beginMoveRows(parent, from, from, target, to);
  231. QWebElement element = child->element().takeFromDocument();
  232. owner->element().appendOutside(element);
  233. owner->takeAt(from);
  234. owner->parent()->insert(child, to);
  235. endMoveRows();
  236. } break;
  237. case +1: {
  238. if (from == 0) return QModelIndex();
  239. Fb2TreeItem * brother = owner->item(from - 1);
  240. if (child->name() != "section") return QModelIndex();
  241. if (brother->name() != "section") return QModelIndex();
  242. QModelIndex target = createIndex(from - 1, 0, (void*)brother);
  243. int to = rowCount(target);
  244. result = createIndex(to, 0, (void*)child);
  245. beginMoveRows(parent, from, from, target, to);
  246. QWebElement element = child->element().takeFromDocument();
  247. brother->element().appendInside(element);
  248. owner->takeAt(from);
  249. brother->insert(child, to);
  250. endMoveRows();
  251. } break;
  252. default: {
  253. int to = from + dy;
  254. if (to < 0 || rowCount(parent) <= to) return QModelIndex();
  255. result = createIndex(to, 0, (void*)child);
  256. if (dy > 0) {
  257. to = index.row();
  258. from = to + dy;
  259. }
  260. Fb2TreeItem * child = owner->item(to);
  261. Fb2TreeItem * brother = owner->item(from);
  262. QString n = child->name();
  263. bool ok = (n == "body" || n == "section") && n == brother->name();
  264. if (!ok) return QModelIndex();
  265. beginMoveRows(parent, from, from, parent, to);
  266. brother = owner->takeAt(from);
  267. owner->insert(brother, to);
  268. QWebElement element = child->element().takeFromDocument();
  269. brother->element().appendOutside(element);
  270. endMoveRows();
  271. } break;
  272. }
  273. return result;
  274. }
  275. bool Fb2TreeModel::removeRows(int row, int count, const QModelIndex &parent)
  276. {
  277. if (row < 0 || count <= 0 || row + count > rowCount(parent)) return false;
  278. Fb2TreeItem * owner = item(parent);
  279. if (!owner) return false;
  280. int last = row + count - 1;
  281. beginRemoveRows(parent, row, last);
  282. for (int i = last; i >= row; i--) {
  283. if (Fb2TreeItem * child = owner->takeAt(i)) {
  284. child->element().removeFromDocument();
  285. delete child;
  286. }
  287. }
  288. endRemoveRows();
  289. return true;
  290. }
  291. //---------------------------------------------------------------------------
  292. // Fb2TreeView
  293. //---------------------------------------------------------------------------
  294. Fb2TreeView::Fb2TreeView(Fb2TextEdit &view, QWidget *parent)
  295. : QTreeView(parent)
  296. , m_view(view)
  297. {
  298. setHeaderHidden(true);
  299. setContextMenuPolicy(Qt::CustomContextMenu);
  300. connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
  301. connect(m_view.page(), SIGNAL(loadFinished(bool)), SLOT(updateTree()));
  302. connect(m_view.page(), SIGNAL(contentsChanged()), SLOT(contentsChanged()));
  303. connect(m_view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  304. connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(contextMenu(QPoint)));
  305. m_timerSelect.setInterval(1000);
  306. m_timerSelect.setSingleShot(true);
  307. connect(&m_timerSelect, SIGNAL(timeout()), SLOT(selectTree()));
  308. m_timerUpdate.setInterval(1000);
  309. m_timerUpdate.setSingleShot(true);
  310. connect(&m_timerUpdate, SIGNAL(timeout()), SLOT(updateTree()));
  311. QMetaObject::invokeMethod(this, "updateTree", Qt::QueuedConnection);
  312. }
  313. void Fb2TreeView::initActions(QToolBar *toolbar)
  314. {
  315. QAction * act;
  316. act = new QAction(FB2::icon("list-add"), tr("&Insert"), this);
  317. act->setShortcutContext(Qt::WidgetShortcut);
  318. act->setShortcut(Qt::Key_Insert);
  319. act->setPriority(QAction::LowPriority);
  320. connect(act, SIGNAL(triggered()), SLOT(insertNode()));
  321. toolbar->addAction(act);
  322. m_menu.addAction(act);
  323. act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
  324. act->setShortcutContext(Qt::WidgetShortcut);
  325. act->setShortcut(Qt::Key_Delete);
  326. act->setPriority(QAction::LowPriority);
  327. connect(act, SIGNAL(triggered()), SLOT(deleteNode()));
  328. toolbar->addAction(act);
  329. m_menu.addAction(act);
  330. m_menu.addSeparator();
  331. actionCut = act = new QAction(FB2::icon("edit-cut"), tr("Cu&t"), this);
  332. act->setShortcutContext(Qt::WidgetShortcut);
  333. act->setPriority(QAction::LowPriority);
  334. act->setShortcuts(QKeySequence::Cut);
  335. act->setEnabled(false);
  336. m_menu.addAction(act);
  337. actionCopy = act = new QAction(FB2::icon("edit-copy"), tr("&Copy"), this);
  338. act->setShortcutContext(Qt::WidgetShortcut);
  339. act->setPriority(QAction::LowPriority);
  340. act->setShortcuts(QKeySequence::Copy);
  341. act->setEnabled(false);
  342. m_menu.addAction(act);
  343. actionPaste = act = new QAction(FB2::icon("edit-paste"), tr("&Paste"), this);
  344. act->setShortcutContext(Qt::WidgetShortcut);
  345. act->setPriority(QAction::LowPriority);
  346. act->setShortcuts(QKeySequence::Paste);
  347. m_menu.addAction(act);
  348. toolbar->addSeparator();
  349. m_menu.addSeparator();
  350. act = new QAction(FB2::icon("go-up"), tr("&Up"), this);
  351. act->setShortcutContext(Qt::WidgetShortcut);
  352. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
  353. connect(act, SIGNAL(triggered()), SLOT(moveUp()));
  354. toolbar->addAction(act);
  355. m_menu.addAction(act);
  356. act = new QAction(FB2::icon("go-down"), tr("&Down"), this);
  357. act->setShortcutContext(Qt::WidgetShortcut);
  358. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
  359. connect(act, SIGNAL(triggered()), SLOT(moveDown()));
  360. toolbar->addAction(act);
  361. m_menu.addAction(act);
  362. act = new QAction(FB2::icon("go-previous"), tr("&Left"), this);
  363. act->setShortcutContext(Qt::WidgetShortcut);
  364. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
  365. connect(act, SIGNAL(triggered()), SLOT(moveLeft()));
  366. toolbar->addAction(act);
  367. m_menu.addAction(act);
  368. act = new QAction(FB2::icon("go-next"), tr("&Right"), this);
  369. act->setShortcutContext(Qt::WidgetShortcut);
  370. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));
  371. connect(act, SIGNAL(triggered()), SLOT(moveRight()));
  372. toolbar->addAction(act);
  373. m_menu.addAction(act);
  374. }
  375. void Fb2TreeView::keyPressEvent(QKeyEvent *event)
  376. {
  377. if (event->modifiers() == Qt::NoModifier) {
  378. switch (event->key()) {
  379. case Qt::Key_Insert: insertNode(); return;
  380. case Qt::Key_Delete: deleteNode(); return;
  381. }
  382. }
  383. QTreeView::keyPressEvent(event);
  384. }
  385. void Fb2TreeView::contextMenu(const QPoint &pos)
  386. {
  387. m_menu.exec(QCursor::pos());
  388. }
  389. void Fb2TreeView::selectionChanged()
  390. {
  391. m_timerSelect.start();
  392. }
  393. void Fb2TreeView::contentsChanged()
  394. {
  395. m_timerUpdate.start();
  396. }
  397. void Fb2TreeView::activated(const QModelIndex &index)
  398. {
  399. if (qApp->focusWidget() == &m_view) return;
  400. if (Fb2TreeModel * m = model()) {
  401. m->selectText(index);
  402. }
  403. }
  404. void Fb2TreeView::selectTree()
  405. {
  406. if (qApp->focusWidget() == this) return;
  407. if (Fb2TreeModel * m = model()) {
  408. QString location = m->view().location();
  409. QModelIndex index = m->index(location);
  410. if (!index.isValid()) return;
  411. setCurrentIndex(index);
  412. scrollTo(index);
  413. }
  414. }
  415. void Fb2TreeView::updateTree()
  416. {
  417. Fb2TreeModel * model = new Fb2TreeModel(m_view, this);
  418. setModel(model);
  419. selectTree();
  420. }
  421. QModelIndex Fb2TreeModel::append(const QModelIndex &parent)
  422. {
  423. Fb2TreeItem * owner = item(parent);
  424. if (!owner || owner == m_root) return QModelIndex();
  425. int row = owner->count();
  426. owner->element().appendInside("<div class=section><div class=title><p><br/></p></div><p><br/></p></div>");
  427. QWebElement element = owner->element().lastChild();
  428. Fb2TreeItem * child = new Fb2TreeItem(element);
  429. beginInsertRows(parent, row, row);
  430. owner->insert(child, row);
  431. endInsertRows();
  432. return createIndex(row, 0, (void*)child);
  433. }
  434. void Fb2TreeView::insertNode()
  435. {
  436. if (Fb2TreeModel * m = model()) {
  437. QModelIndex index = currentIndex();
  438. Fb2TreeItem * item = m->item(index);
  439. if (!item) return;
  440. QString n = item->name();
  441. if (n != "section" && n != "body") {
  442. index = m->parent(index);
  443. item = item->parent();
  444. n = item->name();
  445. if (n != "section" && n != "body") return;
  446. }
  447. QModelIndex result = m->append(index);
  448. if (!result.isValid()) return;
  449. setCurrentIndex(result);
  450. emit QTreeView::currentChanged(result, index);
  451. emit QTreeView::activated(result);
  452. scrollTo(result);
  453. }
  454. }
  455. void Fb2TreeView::deleteNode()
  456. {
  457. if (Fb2TreeModel * m = model()) {
  458. QModelIndex index = currentIndex();
  459. QModelIndex result = m->parent(index);
  460. setCurrentIndex(result);
  461. emit currentChanged(result, index);
  462. emit QTreeView::activated(result);
  463. m->removeRow(index.row(), result);
  464. scrollTo(result);
  465. }
  466. }
  467. Fb2TreeModel * Fb2TreeView::model()
  468. {
  469. return qobject_cast<Fb2TreeModel*>(QTreeView::model());
  470. }
  471. void Fb2TreeView::moveCurrent(int dx, int dy)
  472. {
  473. if (Fb2TreeModel * m = model()) {
  474. QModelIndex index = currentIndex();
  475. QModelIndex result = m->move(index, dx, dy);
  476. if (result.isValid()) {
  477. setCurrentIndex(result);
  478. emit currentChanged(result, index);
  479. emit QTreeView::activated(result);
  480. scrollTo(result);
  481. }
  482. }
  483. }
  484. void Fb2TreeView::moveUp()
  485. {
  486. moveCurrent(0, -1);
  487. }
  488. void Fb2TreeView::moveDown()
  489. {
  490. moveCurrent(0, +1);
  491. }
  492. void Fb2TreeView::moveLeft()
  493. {
  494. moveCurrent(-1, 0);
  495. }
  496. void Fb2TreeView::moveRight()
  497. {
  498. moveCurrent(+1, 0);
  499. }
  500. //---------------------------------------------------------------------------
  501. // Fb2TreeWidget
  502. //---------------------------------------------------------------------------
  503. Fb2TreeWidget::Fb2TreeWidget(Fb2TextEdit &view, QWidget* parent)
  504. : QWidget(parent)
  505. {
  506. QVBoxLayout * layout = new QVBoxLayout(this);
  507. layout->setSpacing(0);
  508. layout->setContentsMargins(0, 0, 0, 0);
  509. layout->setObjectName(QString::fromUtf8("verticalLayout"));
  510. m_tree = new Fb2TreeView(view, this);
  511. layout->addWidget(m_tree);
  512. m_tool = new QToolBar(this);
  513. layout->addWidget(m_tool);
  514. m_tree->initActions(m_tool);
  515. }