fb2tree.cpp 22 KB

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