fb2tree.cpp 22 KB

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