fb2tree.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. #include "fb2tree.hpp"
  2. #include <QtDebug>
  3. #include <QAction>
  4. #include <QApplication>
  5. #include <QVBoxLayout>
  6. #include <QUndoStack>
  7. #include <QWebFrame>
  8. #include <QWebPage>
  9. #include <QTreeView>
  10. #include <QUrl>
  11. #include "fb2text.hpp"
  12. #include "fb2html.h"
  13. #include "fb2utils.h"
  14. //---------------------------------------------------------------------------
  15. // FbTreeItem
  16. //---------------------------------------------------------------------------
  17. FbTreeItem::FbTreeItem(QWebElement &element, FbTreeItem *parent, int number)
  18. : QObject(parent)
  19. , m_element(element)
  20. , m_parent(parent)
  21. , m_number(number)
  22. {
  23. init();
  24. }
  25. FbTreeItem::~FbTreeItem()
  26. {
  27. foreach (FbTreeItem * item, m_list) {
  28. delete item;
  29. }
  30. }
  31. void FbTreeItem::init()
  32. {
  33. m_text = QString();
  34. m_name = m_element.tagName().toLower();
  35. QString style = m_element.attribute("class").toLower();
  36. if (m_name == "div") {
  37. if (style == "title") {
  38. m_text = title();
  39. if (m_parent) m_parent->m_text += m_text += " ";
  40. } else if (style == "subtitle") {
  41. m_text = title();
  42. } else if (style == "body") {
  43. m_body = m_element.attribute("fb2_name");
  44. }
  45. if (!style.isEmpty()) m_name = style;
  46. } else if (m_name == "img") {
  47. m_name = "image";
  48. QUrl url = m_element.attribute("src");
  49. m_text = url.fragment();
  50. }
  51. }
  52. QString FbTreeItem::title()
  53. {
  54. return m_element.toPlainText().left(255).simplified();
  55. }
  56. FbTreeItem * FbTreeItem::item(const QModelIndex &index) const
  57. {
  58. int row = index.row();
  59. if (row < 0 || row >= m_list.size()) return NULL;
  60. return m_list[row];
  61. }
  62. FbTreeItem * FbTreeItem::item(int row) const
  63. {
  64. if (row < 0 || row >= m_list.size()) return NULL;
  65. return m_list[row];
  66. }
  67. QString FbTreeItem::text() const
  68. {
  69. QString name = m_name;
  70. if (!m_body.isEmpty()) name += " name=" + m_body;
  71. return QString("<%1> %2").arg(name).arg(m_text);
  72. }
  73. QString FbTreeItem::selector() const
  74. {
  75. QString text = "";
  76. QString selector = ".get(0)";
  77. QWebElement element = m_element;
  78. QWebElement parent = element.parent();
  79. while (!parent.isNull()) {
  80. text.prepend(element.tagName()).prepend("/");
  81. QWebElement child = parent.firstChild();
  82. int index = -1;
  83. while (!child.isNull()) {
  84. index++;
  85. if (child == element) break;
  86. child = child.nextSibling();
  87. }
  88. if (index == -1) return QString();
  89. selector.prepend(QString(".children().eq(%1)").arg(index));
  90. element = parent;
  91. parent = element.parent();
  92. }
  93. return selector.prepend("$('html')");
  94. }
  95. FbTreeItem * FbTreeItem::content(int number) const
  96. {
  97. FbTextElement element = m_element.firstChild();
  98. while (number-- > 0) element = element.nextSibling();
  99. FbTreeList::const_iterator it;
  100. for (it = m_list.constBegin(); it != m_list.constEnd(); it++) {
  101. if ((*it)->element() == element) return *it;
  102. }
  103. return 0;
  104. }
  105. //---------------------------------------------------------------------------
  106. // FbTreeModel
  107. //---------------------------------------------------------------------------
  108. FbTreeModel::FbTreeModel(FbTextEdit &view, QObject *parent)
  109. : QAbstractItemModel(parent)
  110. , m_view(view)
  111. , m_root(NULL)
  112. {
  113. QWebElement doc = view.page()->mainFrame()->documentElement();
  114. QWebElement body = doc.findFirst("body");
  115. if (body.isNull()) return;
  116. m_root = new FbTreeItem(body);
  117. }
  118. FbTreeModel::~FbTreeModel()
  119. {
  120. if (m_root) delete m_root;
  121. }
  122. FbTreeItem * FbTreeModel::item(const QModelIndex &index) const
  123. {
  124. if (index.isValid()) {
  125. return static_cast<FbTreeItem*>(index.internalPointer());
  126. } else {
  127. return m_root;
  128. }
  129. }
  130. int FbTreeModel::columnCount(const QModelIndex &parent) const
  131. {
  132. Q_UNUSED(parent);
  133. return 1;
  134. }
  135. QModelIndex FbTreeModel::index(FbTreeItem *item, int column) const
  136. {
  137. FbTreeItem *parent = item->parent();
  138. return parent ? createIndex(parent->index(item), column, (void*)item) : QModelIndex();
  139. }
  140. bool FbTreeModel::hasChildren(const QModelIndex &parent) const
  141. {
  142. FbTreeItem *owner = item(parent);
  143. return owner ? owner->hasChildren() : false;
  144. }
  145. QModelIndex FbTreeModel::index(int row, int column, const QModelIndex &parent) const
  146. {
  147. if (!m_root || row < 0 || column < 0) return QModelIndex();
  148. if (FbTreeItem *owner = item(parent)) {
  149. if (FbTreeItem *child = owner->item(row)) {
  150. return createIndex(row, column, (void*)child);
  151. }
  152. }
  153. return QModelIndex();
  154. }
  155. QModelIndex FbTreeModel::parent(const QModelIndex &child) const
  156. {
  157. if (FbTreeItem * node = static_cast<FbTreeItem*>(child.internalPointer())) {
  158. if (FbTreeItem * parent = node->parent()) {
  159. if (FbTreeItem * owner = parent->parent()) {
  160. return createIndex(owner->index(parent), 0, (void*)parent);
  161. }
  162. }
  163. }
  164. return QModelIndex();
  165. }
  166. int FbTreeModel::rowCount(const QModelIndex &parent) const
  167. {
  168. if (parent.column() > 0) return 0;
  169. FbTreeItem *owner = item(parent);
  170. return owner ? owner->count() : 0;
  171. }
  172. QVariant FbTreeModel::data(const QModelIndex &index, int role) const
  173. {
  174. if (role != Qt::DisplayRole) return QVariant();
  175. FbTreeItem * i = item(index);
  176. return i ? i->text() : QVariant();
  177. }
  178. void FbTreeModel::selectText(const QModelIndex &index)
  179. {
  180. if (FbTreeItem *node = item(index)) {
  181. node->element().select();
  182. }
  183. }
  184. QModelIndex FbTreeModel::index(const QString &location) const
  185. {
  186. QModelIndex result;
  187. FbTreeItem * parent = m_root;
  188. QStringList list = location.split(",");
  189. QStringListIterator iterator(list);
  190. while (parent && iterator.hasNext()) {
  191. QString str = iterator.next();
  192. if (str.left(5) == "HTML=") continue;
  193. int key = str.mid(str.indexOf("=")+1).toInt();
  194. FbTreeItem * child = parent->content(key);
  195. if (child) result = index(child);
  196. parent = child;
  197. }
  198. return result;
  199. }
  200. QModelIndex FbTreeModel::move(const QModelIndex &index, int dx, int dy)
  201. {
  202. FbTreeItem *child = item(index);
  203. if (!child) return QModelIndex();
  204. FbTreeItem *owner = child->parent();
  205. if (!owner) return QModelIndex();
  206. int from = index.row();
  207. QModelIndex parent = this->parent(index);
  208. QModelIndex result;
  209. switch (dx) {
  210. case -1: {
  211. if (!owner || owner == m_root) return QModelIndex();
  212. if (!child->element().isSection()) return QModelIndex();
  213. if (!owner->element().isSection()) return QModelIndex();
  214. QModelIndex target = this->parent(parent);
  215. int to = parent.row() + 1;
  216. result = createIndex(to, 0, (void*)child);
  217. beginMoveRows(parent, from, from, target, to);
  218. owner->takeAt(from);
  219. owner->parent()->insert(child, to);
  220. endMoveRows();
  221. QUndoCommand * command = new FbMoveLeftCmd(child->element());
  222. m_view.page()->push(command, tr("Move section"));
  223. } break;
  224. case +1: {
  225. if (from == 0) return QModelIndex();
  226. FbTreeItem * brother = owner->item(from - 1);
  227. if (!child->element().isSection()) return QModelIndex();
  228. if (!brother->element().isSection()) return QModelIndex();
  229. QModelIndex target = createIndex(from - 1, 0, (void*)brother);
  230. int to = rowCount(target);
  231. result = createIndex(to, 0, (void*)child);
  232. beginMoveRows(parent, from, from, target, to);
  233. owner->takeAt(from);
  234. brother->insert(child, to);
  235. endMoveRows();
  236. QUndoCommand * command = new FbMoveRightCmd(child->element());
  237. m_view.page()->push(command, tr("Move section"));
  238. } break;
  239. default: {
  240. int to = from + dy;
  241. if (to < 0 || rowCount(parent) <= to) return QModelIndex();
  242. result = createIndex(to, 0, (void*)child);
  243. if (dy > 0) {
  244. to = index.row();
  245. from = to + dy;
  246. }
  247. FbTreeItem * child = owner->item(to);
  248. FbTreeItem * brother = owner->item(from);
  249. QString n = child->name();
  250. bool ok = (n == "body" || n == "section") && n == brother->name();
  251. if (!ok) return QModelIndex();
  252. beginMoveRows(parent, from, from, parent, to);
  253. brother = owner->takeAt(from);
  254. owner->insert(brother, to);
  255. endMoveRows();
  256. QUndoCommand * command = new FbMoveUpCmd(brother->element());
  257. m_view.page()->push(command, tr("Move section"));
  258. } break;
  259. }
  260. return result;
  261. }
  262. bool FbTreeModel::removeRows(int row, int count, const QModelIndex &parent)
  263. {
  264. if (row < 0 || count <= 0 || row + count > rowCount(parent)) return false;
  265. FbTreeItem * owner = item(parent);
  266. if (!owner) return false;
  267. int last = row + count - 1;
  268. beginRemoveRows(parent, row, last);
  269. for (int i = last; i >= row; i--) {
  270. if (FbTreeItem * child = owner->takeAt(i)) {
  271. QUndoCommand * command = new FbDeleteCmd(child->element());
  272. m_view.page()->push(command, "Delete element");
  273. delete child;
  274. }
  275. }
  276. endRemoveRows();
  277. return true;
  278. }
  279. void FbTreeModel::update(FbTreeItem &owner)
  280. {
  281. owner.init();
  282. FbElementList list;
  283. owner.element().getChildren(list);
  284. int pos = 0;
  285. QModelIndex index = this->index(&owner);
  286. for (FbElementList::iterator it = list.begin(); it != list.end(); it++) {
  287. FbTreeItem * child = 0;
  288. QWebElement element = *it;
  289. int count = owner.count();
  290. for (int i = pos; i < count; i++) {
  291. if (owner.item(i)->element() == element) {
  292. child = owner.item(i);
  293. if (i > pos) {
  294. beginMoveRows(index, i, i, index, pos);
  295. owner.insert(owner.takeAt(i), pos);
  296. endMoveRows();
  297. break;
  298. }
  299. }
  300. }
  301. if (child) {
  302. QString old = child->text();
  303. update(*child);
  304. if (old != child->text()) {
  305. QModelIndex i = this->index(child);
  306. emit dataChanged(i, i);
  307. }
  308. } else {
  309. FbTreeItem * child = new FbTreeItem(element);
  310. beginInsertRows(index, pos, pos);
  311. owner.insert(child, pos);
  312. endInsertRows();
  313. update(*child);
  314. }
  315. pos++;
  316. }
  317. int last = owner.count() - 1;
  318. if (pos <= last) {
  319. beginRemoveRows(index, pos, last);
  320. for (int i = last; i >= pos; i--) delete owner.takeAt(i);
  321. endRemoveRows();
  322. }
  323. }
  324. void FbTreeModel::update()
  325. {
  326. QWebElement doc = m_view.page()->mainFrame()->documentElement();
  327. QWebElement body = doc.findFirst("body");
  328. if (m_root) {
  329. if (m_root->element() != body) *m_root = body;
  330. update(*m_root);
  331. } else {
  332. if (!body.isNull()) {
  333. m_root = new FbTreeItem(body);
  334. update(*m_root);
  335. }
  336. }
  337. }
  338. //---------------------------------------------------------------------------
  339. // FbTreeView
  340. //---------------------------------------------------------------------------
  341. FbTreeView::FbTreeView(FbTextEdit &view, QWidget *parent)
  342. : QTreeView(parent)
  343. , m_view(view)
  344. {
  345. setHeaderHidden(true);
  346. setContextMenuPolicy(Qt::CustomContextMenu);
  347. connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
  348. connect(m_view.page(), SIGNAL(loadFinished(bool)), SLOT(updateTree()));
  349. connect(m_view.page(), SIGNAL(contentsChanged()), SLOT(contentsChanged()));
  350. connect(m_view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  351. connect(m_view.page()->undoStack(), SIGNAL(indexChanged(int)), SLOT(contentsChanged()));
  352. connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(contextMenu(QPoint)));
  353. m_timerSelect.setInterval(1000);
  354. m_timerSelect.setSingleShot(true);
  355. connect(&m_timerSelect, SIGNAL(timeout()), SLOT(selectTree()));
  356. m_timerUpdate.setInterval(1000);
  357. m_timerUpdate.setSingleShot(true);
  358. connect(&m_timerUpdate, SIGNAL(timeout()), SLOT(updateTree()));
  359. QMetaObject::invokeMethod(this, "updateTree", Qt::QueuedConnection);
  360. }
  361. void FbTreeView::initActions(QToolBar *toolbar)
  362. {
  363. QAction * act;
  364. actionSection = act = new QAction(FbIcon("list-add"), tr("&Add section"), this);
  365. act->setShortcutContext(Qt::WidgetShortcut);
  366. act->setShortcut(Qt::Key_Insert);
  367. act->setPriority(QAction::LowPriority);
  368. connect(act, SIGNAL(triggered()), SLOT(insertSection()));
  369. toolbar->addAction(act);
  370. actionTitle = act = new QAction(tr("+ Title"), this);
  371. connect(act, SIGNAL(triggered()), SLOT(insertTitle()));
  372. actionEpigraph = act = new QAction(tr("+ Epigraph"), this);
  373. connect(act, SIGNAL(triggered()), SLOT(insertEpigraph()));
  374. actionImage = act = new QAction(tr("+ Image"), this);
  375. connect(act, SIGNAL(triggered()), SLOT(insertImage()));
  376. actionAnnot = act = new QAction(tr("+ Annotation"), this);
  377. connect(act, SIGNAL(triggered()), SLOT(insertAnnot()));
  378. actionStanza = act = new QAction(tr("+ Stanza"), this);
  379. connect(act, SIGNAL(triggered()), SLOT(insertStanza()));
  380. actionAuthor = act = new QAction(tr("+ Author"), this);
  381. connect(act, SIGNAL(triggered()), SLOT(insertAuthor()));
  382. actionDate = act = new QAction(tr("+ Date"), this);
  383. connect(act, SIGNAL(triggered()), SLOT(insertDate()));
  384. actionDelete = act = new QAction(FbIcon("list-remove"), tr("&Delete"), this);
  385. act->setShortcutContext(Qt::WidgetShortcut);
  386. act->setShortcut(Qt::Key_Delete);
  387. act->setPriority(QAction::LowPriority);
  388. connect(act, SIGNAL(triggered()), SLOT(deleteNode()));
  389. toolbar->addAction(act);
  390. actionCut = act = new QAction(FbIcon("edit-cut"), tr("Cu&t"), this);
  391. act->setShortcutContext(Qt::WidgetShortcut);
  392. act->setPriority(QAction::LowPriority);
  393. act->setShortcuts(QKeySequence::Cut);
  394. act->setEnabled(false);
  395. actionCopy = act = new QAction(FbIcon("edit-copy"), tr("&Copy"), this);
  396. act->setShortcutContext(Qt::WidgetShortcut);
  397. act->setPriority(QAction::LowPriority);
  398. act->setShortcuts(QKeySequence::Copy);
  399. act->setEnabled(false);
  400. actionPaste = act = new QAction(FbIcon("edit-paste"), tr("&Paste"), this);
  401. act->setShortcutContext(Qt::WidgetShortcut);
  402. act->setPriority(QAction::LowPriority);
  403. act->setShortcuts(QKeySequence::Paste);
  404. toolbar->addSeparator();
  405. actionMoveUp = act = new QAction(FbIcon("go-up"), tr("&Up"), this);
  406. act->setShortcutContext(Qt::WidgetShortcut);
  407. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
  408. connect(act, SIGNAL(triggered()), SLOT(moveUp()));
  409. toolbar->addAction(act);
  410. actionMoveDown = act = new QAction(FbIcon("go-down"), tr("&Down"), this);
  411. act->setShortcutContext(Qt::WidgetShortcut);
  412. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
  413. connect(act, SIGNAL(triggered()), SLOT(moveDown()));
  414. toolbar->addAction(act);
  415. actionMoveLeft = act = new QAction(FbIcon("go-previous"), tr("&Left"), this);
  416. act->setShortcutContext(Qt::WidgetShortcut);
  417. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
  418. connect(act, SIGNAL(triggered()), SLOT(moveLeft()));
  419. toolbar->addAction(act);
  420. actionMoveRight = act = new QAction(FbIcon("go-next"), tr("&Right"), this);
  421. act->setShortcutContext(Qt::WidgetShortcut);
  422. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));
  423. connect(act, SIGNAL(triggered()), SLOT(moveRight()));
  424. toolbar->addAction(act);
  425. }
  426. void FbTreeView::keyPressEvent(QKeyEvent *event)
  427. {
  428. switch (event->modifiers()) {
  429. case Qt::NoModifier:
  430. switch (event->key()) {
  431. case Qt::Key_Insert: insertSection(); return;
  432. case Qt::Key_Delete: deleteNode(); return;
  433. }
  434. break;
  435. case Qt::ControlModifier:
  436. switch (event->key()) {
  437. case Qt::Key_Left : moveCurrent(-1, 0); return;
  438. case Qt::Key_Right : moveCurrent(+1, 0); return;
  439. case Qt::Key_Up : moveCurrent(0, -1); return;
  440. case Qt::Key_Down : moveCurrent(0, +1); return;
  441. }
  442. break;
  443. }
  444. QTreeView::keyPressEvent(event);
  445. }
  446. void FbTreeView::contextMenu(const QPoint &pos)
  447. {
  448. FbTreeModel * m = model();
  449. if (!m) return;
  450. FbTreeItem * i = m->item(currentIndex());
  451. if (!i) return;
  452. FbTextElement e = i->element();
  453. if (e.isTitle()) e = e.parent();
  454. if (e.isNull()) return;
  455. QMenu menu;
  456. menu.addAction(actionSection);
  457. if (e.isBody()) {
  458. if (!e.hasChild("image")) menu.addAction(actionImage);
  459. if (!e.hasChild("title")) menu.addAction(actionTitle);
  460. menu.addAction(actionEpigraph);
  461. }
  462. if (e.isSection()) {
  463. if (!e.hasChild("title")) menu.addAction(actionTitle);
  464. menu.addAction(actionEpigraph);
  465. if (!e.hasChild("image")) menu.addAction(actionImage);
  466. if (!e.hasChild("annotetion")) menu.addAction(actionAnnot);
  467. }
  468. if (e.isDiv("poem")) {
  469. if (!e.hasChild("title")) menu.addAction(actionTitle);
  470. menu.addAction(actionEpigraph);
  471. menu.addAction(actionStanza);
  472. menu.addAction(actionAuthor);
  473. if (!e.hasChild("date")) menu.addAction(actionDate);
  474. }
  475. if (e.isDiv("stanza")) {
  476. if (!e.hasChild("title")) menu.addAction(actionTitle);
  477. }
  478. if (e.isDiv("epigraph")) {
  479. menu.addAction(actionAuthor);
  480. }
  481. if (e.isDiv("cite")) {
  482. menu.addAction(actionAuthor);
  483. }
  484. menu.addAction(actionDelete);
  485. menu.addSeparator();
  486. menu.addAction(actionCut);
  487. menu.addAction(actionCopy);
  488. menu.addAction(actionPaste);
  489. menu.addSeparator();
  490. menu.addAction(actionMoveUp);
  491. menu.addAction(actionMoveDown);
  492. menu.addAction(actionMoveLeft);
  493. menu.addAction(actionMoveRight);
  494. menu.exec(mapToGlobal(pos));
  495. }
  496. void FbTreeView::selectionChanged()
  497. {
  498. m_timerSelect.start();
  499. }
  500. void FbTreeView::contentsChanged()
  501. {
  502. m_timerUpdate.start();
  503. }
  504. void FbTreeView::activated(const QModelIndex &index)
  505. {
  506. if (qApp->focusWidget() == &m_view) return;
  507. if (FbTreeModel * m = model()) {
  508. m->selectText(index);
  509. }
  510. }
  511. void FbTreeView::selectTree()
  512. {
  513. if (qApp->focusWidget() == this) return;
  514. if (FbTreeModel * m = model()) {
  515. QString location = m->view().page()->location();
  516. QModelIndex index = m->index(location);
  517. if (!index.isValid()) return;
  518. setCurrentIndex(index);
  519. scrollTo(index);
  520. }
  521. }
  522. void FbTreeView::updateTree()
  523. {
  524. if (FbTreeModel * m = model()) {
  525. m->update();
  526. } else {
  527. m = new FbTreeModel(m_view, this);
  528. m->update();
  529. setModel(m);
  530. }
  531. selectTree();
  532. }
  533. QModelIndex FbTreeModel::append(const QModelIndex &parent, FbTextElement element)
  534. {
  535. FbTreeItem * owner = item(parent);
  536. if (!owner || owner == m_root) return QModelIndex();
  537. int row = owner->count();
  538. FbTreeItem * child = new FbTreeItem(element);
  539. beginInsertRows(parent, row, row);
  540. owner->insert(child, row);
  541. endInsertRows();
  542. return createIndex(row, 0, (void*)child);
  543. }
  544. void FbTreeView::insertSection()
  545. {
  546. if (FbTreeModel * m = model()) {
  547. QModelIndex index = currentIndex();
  548. FbTreeItem * item = m->item(index);
  549. if (!item) return;
  550. FbTextElement element = item->element();
  551. while (!element.isNull()) {
  552. if (element.isSection() || element.isBody()) {
  553. m_view.page()->appendSection(element);
  554. QModelIndex result = m->append(index, element.lastChild());
  555. if (!result.isValid()) return;
  556. setCurrentIndex(result);
  557. emit QTreeView::currentChanged(result, index);
  558. emit QTreeView::activated(result);
  559. scrollTo(result);
  560. break;
  561. }
  562. element = element.parent();
  563. index = m->parent(index);
  564. }
  565. }
  566. }
  567. void FbTreeView::insertTitle()
  568. {
  569. }
  570. void FbTreeView::insertAuthor()
  571. {
  572. }
  573. void FbTreeView::insertEpigraph()
  574. {
  575. }
  576. void FbTreeView::insertImage()
  577. {
  578. }
  579. void FbTreeView::insertAnnot()
  580. {
  581. }
  582. void FbTreeView::insertStanza()
  583. {
  584. }
  585. void FbTreeView::insertDate()
  586. {
  587. }
  588. void FbTreeView::deleteNode()
  589. {
  590. if (FbTreeModel * m = model()) {
  591. QModelIndex index = currentIndex();
  592. QModelIndex parent = m->parent(index);
  593. QModelIndex result = parent;
  594. int row = index.row();
  595. int last = m->rowCount(result) - 1;
  596. if (last > 0) {
  597. if (row >= last) row = last;
  598. result = m->index(row, 0, parent);
  599. }
  600. emit currentChanged(result, index);
  601. emit QTreeView::activated(result);
  602. setCurrentIndex(result);
  603. m->removeRow(row, parent);
  604. }
  605. }
  606. FbTreeModel * FbTreeView::model()
  607. {
  608. return qobject_cast<FbTreeModel*>(QTreeView::model());
  609. }
  610. void FbTreeView::moveCurrent(int dx, int dy)
  611. {
  612. if (FbTreeModel * m = model()) {
  613. QModelIndex index = currentIndex();
  614. QModelIndex result = m->move(index, dx, dy);
  615. if (result.isValid()) {
  616. setCurrentIndex(result);
  617. emit currentChanged(result, index);
  618. emit QTreeView::activated(result);
  619. scrollTo(result);
  620. }
  621. }
  622. }
  623. void FbTreeView::moveUp()
  624. {
  625. moveCurrent(0, -1);
  626. }
  627. void FbTreeView::moveDown()
  628. {
  629. moveCurrent(0, +1);
  630. }
  631. void FbTreeView::moveLeft()
  632. {
  633. moveCurrent(-1, 0);
  634. }
  635. void FbTreeView::moveRight()
  636. {
  637. moveCurrent(+1, 0);
  638. }
  639. //---------------------------------------------------------------------------
  640. // FbTreeWidget
  641. //---------------------------------------------------------------------------
  642. FbTreeWidget::FbTreeWidget(FbTextEdit &view, QWidget* parent)
  643. : QWidget(parent)
  644. {
  645. QVBoxLayout * layout = new QVBoxLayout(this);
  646. layout->setSpacing(0);
  647. layout->setContentsMargins(0, 0, 0, 0);
  648. layout->setObjectName(QString::fromUtf8("verticalLayout"));
  649. m_tree = new FbTreeView(view, this);
  650. layout->addWidget(m_tree);
  651. m_tool = new QToolBar(this);
  652. layout->addWidget(m_tool);
  653. m_tree->initActions(m_tool);
  654. }