fb2tree.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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(&m_view, SIGNAL(loadFinished(bool)), SLOT(updateTree()));
  348. connect(&m_view, SIGNAL(loadFinished(bool)), SLOT(connectPage()));
  349. connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
  350. connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(contextMenu(QPoint)));
  351. connectPage();
  352. m_timerSelect.setInterval(1000);
  353. m_timerSelect.setSingleShot(true);
  354. connect(&m_timerSelect, SIGNAL(timeout()), SLOT(selectTree()));
  355. m_timerUpdate.setInterval(1000);
  356. m_timerUpdate.setSingleShot(true);
  357. connect(&m_timerUpdate, SIGNAL(timeout()), SLOT(updateTree()));
  358. QMetaObject::invokeMethod(this, "updateTree", Qt::QueuedConnection);
  359. }
  360. void FbTreeView::connectPage()
  361. {
  362. QWebPage *page = m_view.page();
  363. connect(page, SIGNAL(contentsChanged()), SLOT(contentsChanged()));
  364. connect(page, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  365. connect(page->undoStack(), SIGNAL(indexChanged(int)), SLOT(contentsChanged()));
  366. }
  367. void FbTreeView::initActions(QToolBar *toolbar)
  368. {
  369. QAction * act;
  370. actionSection = act = new QAction(FbIcon("list-add"), tr("&Add section"), this);
  371. act->setShortcutContext(Qt::WidgetShortcut);
  372. act->setShortcut(Qt::Key_Insert);
  373. act->setPriority(QAction::LowPriority);
  374. connect(act, SIGNAL(triggered()), SLOT(insertSection()));
  375. toolbar->addAction(act);
  376. actionTitle = act = new QAction(tr("+ Title"), this);
  377. connect(act, SIGNAL(triggered()), SLOT(insertTitle()));
  378. actionEpigraph = act = new QAction(tr("+ Epigraph"), this);
  379. connect(act, SIGNAL(triggered()), SLOT(insertEpigraph()));
  380. actionImage = act = new QAction(tr("+ Image"), this);
  381. connect(act, SIGNAL(triggered()), SLOT(insertImage()));
  382. actionAnnot = act = new QAction(tr("+ Annotation"), this);
  383. connect(act, SIGNAL(triggered()), SLOT(insertAnnot()));
  384. actionStanza = act = new QAction(tr("+ Stanza"), this);
  385. connect(act, SIGNAL(triggered()), SLOT(insertStanza()));
  386. actionAuthor = act = new QAction(tr("+ Author"), this);
  387. connect(act, SIGNAL(triggered()), SLOT(insertAuthor()));
  388. actionDate = act = new QAction(tr("+ Date"), this);
  389. connect(act, SIGNAL(triggered()), SLOT(insertDate()));
  390. actionDelete = act = new QAction(FbIcon("list-remove"), tr("&Delete"), this);
  391. act->setShortcutContext(Qt::WidgetShortcut);
  392. act->setShortcut(Qt::Key_Delete);
  393. act->setPriority(QAction::LowPriority);
  394. connect(act, SIGNAL(triggered()), SLOT(deleteNode()));
  395. toolbar->addAction(act);
  396. actionCut = act = new QAction(FbIcon("edit-cut"), tr("Cu&t"), this);
  397. act->setShortcutContext(Qt::WidgetShortcut);
  398. act->setPriority(QAction::LowPriority);
  399. act->setShortcuts(QKeySequence::Cut);
  400. act->setEnabled(false);
  401. actionCopy = act = new QAction(FbIcon("edit-copy"), tr("&Copy"), this);
  402. act->setShortcutContext(Qt::WidgetShortcut);
  403. act->setPriority(QAction::LowPriority);
  404. act->setShortcuts(QKeySequence::Copy);
  405. act->setEnabled(false);
  406. actionPaste = act = new QAction(FbIcon("edit-paste"), tr("&Paste"), this);
  407. act->setShortcutContext(Qt::WidgetShortcut);
  408. act->setPriority(QAction::LowPriority);
  409. act->setShortcuts(QKeySequence::Paste);
  410. toolbar->addSeparator();
  411. actionMoveUp = act = new QAction(FbIcon("go-up"), tr("&Up"), this);
  412. act->setShortcutContext(Qt::WidgetShortcut);
  413. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
  414. connect(act, SIGNAL(triggered()), SLOT(moveUp()));
  415. toolbar->addAction(act);
  416. actionMoveDown = act = new QAction(FbIcon("go-down"), tr("&Down"), this);
  417. act->setShortcutContext(Qt::WidgetShortcut);
  418. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
  419. connect(act, SIGNAL(triggered()), SLOT(moveDown()));
  420. toolbar->addAction(act);
  421. actionMoveLeft = act = new QAction(FbIcon("go-previous"), tr("&Left"), this);
  422. act->setShortcutContext(Qt::WidgetShortcut);
  423. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
  424. connect(act, SIGNAL(triggered()), SLOT(moveLeft()));
  425. toolbar->addAction(act);
  426. actionMoveRight = act = new QAction(FbIcon("go-next"), tr("&Right"), this);
  427. act->setShortcutContext(Qt::WidgetShortcut);
  428. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));
  429. connect(act, SIGNAL(triggered()), SLOT(moveRight()));
  430. toolbar->addAction(act);
  431. }
  432. void FbTreeView::keyPressEvent(QKeyEvent *event)
  433. {
  434. switch (event->modifiers()) {
  435. case Qt::NoModifier:
  436. switch (event->key()) {
  437. case Qt::Key_Insert: insertSection(); return;
  438. case Qt::Key_Delete: deleteNode(); return;
  439. }
  440. break;
  441. case Qt::ControlModifier:
  442. switch (event->key()) {
  443. case Qt::Key_Left : moveCurrent(-1, 0); return;
  444. case Qt::Key_Right : moveCurrent(+1, 0); return;
  445. case Qt::Key_Up : moveCurrent(0, -1); return;
  446. case Qt::Key_Down : moveCurrent(0, +1); return;
  447. }
  448. break;
  449. }
  450. QTreeView::keyPressEvent(event);
  451. }
  452. void FbTreeView::contextMenu(const QPoint &pos)
  453. {
  454. FbTreeModel * m = model();
  455. if (!m) return;
  456. FbTreeItem * i = m->item(currentIndex());
  457. if (!i) return;
  458. FbTextElement e = i->element();
  459. if (e.isTitle()) e = e.parent();
  460. if (e.isNull()) return;
  461. QMenu menu;
  462. menu.addAction(actionSection);
  463. if (e.isBody()) {
  464. if (!e.hasChild("image")) menu.addAction(actionImage);
  465. if (!e.hasChild("title")) menu.addAction(actionTitle);
  466. menu.addAction(actionEpigraph);
  467. }
  468. if (e.isSection()) {
  469. if (!e.hasChild("title")) menu.addAction(actionTitle);
  470. menu.addAction(actionEpigraph);
  471. if (!e.hasChild("image")) menu.addAction(actionImage);
  472. if (!e.hasChild("annotetion")) menu.addAction(actionAnnot);
  473. }
  474. if (e.isDiv("poem")) {
  475. if (!e.hasChild("title")) menu.addAction(actionTitle);
  476. menu.addAction(actionEpigraph);
  477. menu.addAction(actionStanza);
  478. menu.addAction(actionAuthor);
  479. if (!e.hasChild("date")) menu.addAction(actionDate);
  480. }
  481. if (e.isDiv("stanza")) {
  482. if (!e.hasChild("title")) menu.addAction(actionTitle);
  483. }
  484. if (e.isDiv("epigraph")) {
  485. menu.addAction(actionAuthor);
  486. }
  487. if (e.isDiv("cite")) {
  488. menu.addAction(actionAuthor);
  489. }
  490. menu.addAction(actionDelete);
  491. menu.addSeparator();
  492. menu.addAction(actionCut);
  493. menu.addAction(actionCopy);
  494. menu.addAction(actionPaste);
  495. menu.addSeparator();
  496. menu.addAction(actionMoveUp);
  497. menu.addAction(actionMoveDown);
  498. menu.addAction(actionMoveLeft);
  499. menu.addAction(actionMoveRight);
  500. menu.exec(mapToGlobal(pos));
  501. }
  502. void FbTreeView::selectionChanged()
  503. {
  504. m_timerSelect.start();
  505. }
  506. void FbTreeView::contentsChanged()
  507. {
  508. m_timerUpdate.start();
  509. }
  510. void FbTreeView::activated(const QModelIndex &index)
  511. {
  512. if (qApp->focusWidget() == &m_view) return;
  513. if (FbTreeModel * m = model()) {
  514. m->selectText(index);
  515. }
  516. }
  517. void FbTreeView::selectTree()
  518. {
  519. if (qApp->focusWidget() == this) return;
  520. if (FbTreeModel * m = model()) {
  521. QString location = m->view().page()->location();
  522. QModelIndex index = m->index(location);
  523. if (!index.isValid()) return;
  524. setCurrentIndex(index);
  525. scrollTo(index);
  526. }
  527. }
  528. void FbTreeView::updateTree()
  529. {
  530. if (FbTreeModel * m = model()) {
  531. m->update();
  532. } else {
  533. m = new FbTreeModel(m_view, this);
  534. m->update();
  535. setModel(m);
  536. }
  537. selectTree();
  538. }
  539. QModelIndex FbTreeModel::append(const QModelIndex &parent, FbTextElement element)
  540. {
  541. FbTreeItem * owner = item(parent);
  542. if (!owner || owner == m_root) return QModelIndex();
  543. int row = owner->count();
  544. FbTreeItem * child = new FbTreeItem(element);
  545. beginInsertRows(parent, row, row);
  546. owner->insert(child, row);
  547. endInsertRows();
  548. return createIndex(row, 0, (void*)child);
  549. }
  550. void FbTreeView::insertSection()
  551. {
  552. if (FbTreeModel * m = model()) {
  553. QModelIndex index = currentIndex();
  554. FbTreeItem * item = m->item(index);
  555. if (!item) return;
  556. FbTextElement element = item->element();
  557. while (!element.isNull()) {
  558. if (element.isSection() || element.isBody()) {
  559. m_view.page()->appendSection(element);
  560. QModelIndex result = m->append(index, element.lastChild());
  561. if (!result.isValid()) return;
  562. setCurrentIndex(result);
  563. emit QTreeView::currentChanged(result, index);
  564. emit QTreeView::activated(result);
  565. scrollTo(result);
  566. break;
  567. }
  568. element = element.parent();
  569. index = m->parent(index);
  570. }
  571. }
  572. }
  573. void FbTreeView::insertTitle()
  574. {
  575. }
  576. void FbTreeView::insertAuthor()
  577. {
  578. }
  579. void FbTreeView::insertEpigraph()
  580. {
  581. }
  582. void FbTreeView::insertImage()
  583. {
  584. }
  585. void FbTreeView::insertAnnot()
  586. {
  587. }
  588. void FbTreeView::insertStanza()
  589. {
  590. }
  591. void FbTreeView::insertDate()
  592. {
  593. }
  594. void FbTreeView::deleteNode()
  595. {
  596. if (FbTreeModel * m = model()) {
  597. QModelIndex index = currentIndex();
  598. QModelIndex parent = m->parent(index);
  599. QModelIndex result = parent;
  600. int row = index.row();
  601. int last = m->rowCount(result) - 1;
  602. if (last > 0) {
  603. if (row >= last) row = last;
  604. result = m->index(row, 0, parent);
  605. }
  606. emit currentChanged(result, index);
  607. emit QTreeView::activated(result);
  608. setCurrentIndex(result);
  609. m->removeRow(row, parent);
  610. }
  611. }
  612. FbTreeModel * FbTreeView::model()
  613. {
  614. return qobject_cast<FbTreeModel*>(QTreeView::model());
  615. }
  616. void FbTreeView::moveCurrent(int dx, int dy)
  617. {
  618. if (FbTreeModel * m = model()) {
  619. QModelIndex index = currentIndex();
  620. QModelIndex result = m->move(index, dx, dy);
  621. if (result.isValid()) {
  622. setCurrentIndex(result);
  623. emit currentChanged(result, index);
  624. emit QTreeView::activated(result);
  625. scrollTo(result);
  626. }
  627. }
  628. }
  629. void FbTreeView::moveUp()
  630. {
  631. moveCurrent(0, -1);
  632. }
  633. void FbTreeView::moveDown()
  634. {
  635. moveCurrent(0, +1);
  636. }
  637. void FbTreeView::moveLeft()
  638. {
  639. moveCurrent(-1, 0);
  640. }
  641. void FbTreeView::moveRight()
  642. {
  643. moveCurrent(+1, 0);
  644. }
  645. //---------------------------------------------------------------------------
  646. // FbTreeWidget
  647. //---------------------------------------------------------------------------
  648. FbTreeWidget::FbTreeWidget(FbTextEdit *view, QWidget* parent)
  649. : QWidget(parent)
  650. {
  651. QVBoxLayout * layout = new QVBoxLayout(this);
  652. layout->setSpacing(0);
  653. layout->setContentsMargins(0, 0, 0, 0);
  654. m_tree = new FbTreeView(*view, this);
  655. layout->addWidget(m_tree);
  656. m_tool = new QToolBar(this);
  657. layout->addWidget(m_tool);
  658. m_tree->initActions(m_tool);
  659. }