fb2tree.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. for (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 nullptr;
  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 = nullptr;
  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. actionText = act = new QAction(tr("+ Simple text"), this);
  401. connect(act, SIGNAL(triggered()), SLOT(insertText()));
  402. actionDate = act = new QAction(tr("+ Date"), this);
  403. connect(act, SIGNAL(triggered()), SLOT(insertDate()));
  404. actionDelete = act = new QAction(FbIcon("list-remove"), tr("&Delete"), this);
  405. act->setShortcutContext(Qt::WidgetShortcut);
  406. act->setShortcut(Qt::Key_Delete);
  407. act->setPriority(QAction::LowPriority);
  408. connect(act, SIGNAL(triggered()), SLOT(deleteNode()));
  409. toolbar->addAction(act);
  410. actionCut = act = new QAction(FbIcon("edit-cut"), tr("Cu&t"), this);
  411. act->setShortcutContext(Qt::WidgetShortcut);
  412. act->setPriority(QAction::LowPriority);
  413. act->setShortcuts(QKeySequence::Cut);
  414. act->setEnabled(false);
  415. actionCopy = act = new QAction(FbIcon("edit-copy"), tr("&Copy"), this);
  416. act->setShortcutContext(Qt::WidgetShortcut);
  417. act->setPriority(QAction::LowPriority);
  418. act->setShortcuts(QKeySequence::Copy);
  419. act->setEnabled(false);
  420. actionPaste = act = new QAction(FbIcon("edit-paste"), tr("&Paste"), this);
  421. act->setShortcutContext(Qt::WidgetShortcut);
  422. act->setPriority(QAction::LowPriority);
  423. act->setShortcuts(QKeySequence::Paste);
  424. toolbar->addSeparator();
  425. actionMoveUp = act = new QAction(FbIcon("go-up"), tr("&Up"), this);
  426. act->setShortcutContext(Qt::WidgetShortcut);
  427. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
  428. connect(act, SIGNAL(triggered()), SLOT(moveUp()));
  429. toolbar->addAction(act);
  430. actionMoveDown = act = new QAction(FbIcon("go-down"), tr("&Down"), this);
  431. act->setShortcutContext(Qt::WidgetShortcut);
  432. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
  433. connect(act, SIGNAL(triggered()), SLOT(moveDown()));
  434. toolbar->addAction(act);
  435. actionMoveLeft = act = new QAction(FbIcon("go-previous"), tr("&Left"), this);
  436. act->setShortcutContext(Qt::WidgetShortcut);
  437. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
  438. connect(act, SIGNAL(triggered()), SLOT(moveLeft()));
  439. toolbar->addAction(act);
  440. actionMoveRight = act = new QAction(FbIcon("go-next"), tr("&Right"), this);
  441. act->setShortcutContext(Qt::WidgetShortcut);
  442. act->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));
  443. connect(act, SIGNAL(triggered()), SLOT(moveRight()));
  444. toolbar->addAction(act);
  445. }
  446. void FbTreeView::keyPressEvent(QKeyEvent *event)
  447. {
  448. switch (event->modifiers()) {
  449. case Qt::NoModifier:
  450. switch (event->key()) {
  451. case Qt::Key_Insert: insertSection(); return;
  452. case Qt::Key_Delete: deleteNode(); return;
  453. }
  454. break;
  455. case Qt::ControlModifier:
  456. switch (event->key()) {
  457. case Qt::Key_Left : moveCurrent(-1, 0); return;
  458. case Qt::Key_Right : moveCurrent(+1, 0); return;
  459. case Qt::Key_Up : moveCurrent(0, -1); return;
  460. case Qt::Key_Down : moveCurrent(0, +1); return;
  461. }
  462. break;
  463. }
  464. QTreeView::keyPressEvent(event);
  465. }
  466. void FbTreeView::contextMenu(const QPoint &pos)
  467. {
  468. FbTreeModel * m = model();
  469. if (!m) return;
  470. FbTreeItem * i = m->item(currentIndex());
  471. if (!i) return;
  472. FbTextElement e = i->element();
  473. if (e.isTitle()) e = e.parent();
  474. if (e.isNull()) return;
  475. QMenu menu;
  476. menu.addAction(actionSection);
  477. QString tag = e.tagName();
  478. if (tag == "FB:BODY") {
  479. if (!e.hasChild("IMG")) menu.addAction(actionImage);
  480. if (!e.hasTitle()) menu.addAction(actionTitle);
  481. menu.addAction(actionEpigraph);
  482. }
  483. else
  484. if (tag == "FB:SECTION") {
  485. if (!e.hasTitle()) menu.addAction(actionTitle);
  486. menu.addAction(actionEpigraph);
  487. if (!e.hasChild("IMG")) menu.addAction(actionImage);
  488. if (!e.hasChild("FB:ANNOTATION")) menu.addAction(actionAnnot);
  489. menu.addAction(actionText);
  490. }
  491. else
  492. if (tag == "FB:POEM") {
  493. if (!e.hasTitle()) menu.addAction(actionTitle);
  494. menu.addAction(actionEpigraph);
  495. menu.addAction(actionStanza);
  496. menu.addAction(actionAuthor);
  497. if (!e.hasChild("date")) menu.addAction(actionDate);
  498. }
  499. else
  500. if (tag == "FB:STANZA") {
  501. if (!e.hasTitle()) menu.addAction(actionTitle);
  502. }
  503. else
  504. if (tag == "FB:EPIGRAPH") {
  505. menu.addAction(actionAuthor);
  506. }
  507. else
  508. if (tag == "FB:CITE") {
  509. menu.addAction(actionAuthor);
  510. }
  511. menu.addAction(actionDelete);
  512. menu.addSeparator();
  513. menu.addAction(actionCut);
  514. menu.addAction(actionCopy);
  515. menu.addAction(actionPaste);
  516. menu.addSeparator();
  517. menu.addAction(actionMoveUp);
  518. menu.addAction(actionMoveDown);
  519. menu.addAction(actionMoveLeft);
  520. menu.addAction(actionMoveRight);
  521. menu.exec(mapToGlobal(pos));
  522. }
  523. void FbTreeView::selectionChanged()
  524. {
  525. m_timerSelect.start();
  526. }
  527. void FbTreeView::contentsChanged()
  528. {
  529. m_timerUpdate.start();
  530. }
  531. void FbTreeView::activated(const QModelIndex &index)
  532. {
  533. if (qApp->focusWidget() == &m_view) return;
  534. if (FbTreeModel * m = model()) {
  535. m->selectText(index);
  536. }
  537. }
  538. void FbTreeView::selectTree()
  539. {
  540. if (qApp->focusWidget() == this) return;
  541. if (FbTreeModel * m = model()) {
  542. QString location = m->view().page()->location();
  543. QModelIndex index = m->index(location);
  544. if (!index.isValid()) return;
  545. setCurrentIndex(index);
  546. scrollTo(index);
  547. }
  548. }
  549. void FbTreeView::updateTree()
  550. {
  551. if (FbTreeModel * m = model()) {
  552. m->update();
  553. } else {
  554. m = new FbTreeModel(m_view, this);
  555. m->update();
  556. setModel(m);
  557. }
  558. selectTree();
  559. }
  560. void FbTreeView::append(const QModelIndex &parent, FbTextElement element)
  561. {
  562. FbTreeModel * m = model();
  563. if (!m) return;
  564. QModelIndex current = currentIndex();
  565. QModelIndex index = m->append(parent, element);
  566. if (!index.isValid()) return;
  567. setCurrentIndex(index);
  568. emit QTreeView::currentChanged(index, current);
  569. emit QTreeView::activated(index);
  570. scrollTo(index);
  571. }
  572. void FbTreeView::insertSection()
  573. {
  574. FbTreeModel * m = model();
  575. if (!m) return;
  576. QModelIndex index = currentIndex();
  577. FbTreeItem * item = m->item(index);
  578. if (!item) return;
  579. FbTextElement element = item->element();
  580. while (!element.isNull()) {
  581. if (element.isBody() || element.isSection()) {
  582. element = m_view.page()->appendSection(element);
  583. append(index, element);
  584. break;
  585. }
  586. element = element.parent();
  587. index = m->parent(index);
  588. }
  589. }
  590. void FbTreeView::insertTitle()
  591. {
  592. FbTreeModel * m = model();
  593. if (!m) return;
  594. QModelIndex index = currentIndex();
  595. FbTreeItem * item = m->item(index);
  596. if (!item) return;
  597. FbTextElement element = item->element();
  598. if (element.hasTitle()) return;
  599. element = m_view.page()->appendTitle(element);
  600. append(index, element);
  601. }
  602. void FbTreeView::insertText()
  603. {
  604. FbTreeModel * m = model();
  605. if (!m) return;
  606. QModelIndex index = currentIndex();
  607. FbTreeItem * item = m->item(index);
  608. if (!item) return;
  609. FbTextElement element = item->element();
  610. if (!element.isSection()) return;
  611. m_view.page()->appendText(element).select();
  612. }
  613. void FbTreeView::insertAuthor()
  614. {
  615. }
  616. void FbTreeView::insertEpigraph()
  617. {
  618. }
  619. void FbTreeView::insertImage()
  620. {
  621. }
  622. void FbTreeView::insertAnnot()
  623. {
  624. }
  625. void FbTreeView::insertStanza()
  626. {
  627. }
  628. void FbTreeView::insertDate()
  629. {
  630. }
  631. void FbTreeView::deleteNode()
  632. {
  633. if (FbTreeModel * m = model()) {
  634. QModelIndex index = currentIndex();
  635. QModelIndex parent = m->parent(index);
  636. QModelIndex result = parent;
  637. int row = index.row();
  638. int last = m->rowCount(result) - 1;
  639. if (last > 0) {
  640. if (row >= last) row = last;
  641. result = m->index(row, 0, parent);
  642. }
  643. emit currentChanged(result, index);
  644. emit QTreeView::activated(result);
  645. setCurrentIndex(result);
  646. m->removeRow(row, parent);
  647. }
  648. }
  649. FbTreeModel * FbTreeView::model() const
  650. {
  651. return qobject_cast<FbTreeModel*>(QTreeView::model());
  652. }
  653. void FbTreeView::moveCurrent(int dx, int dy)
  654. {
  655. if (FbTreeModel * m = model()) {
  656. QModelIndex index = currentIndex();
  657. QModelIndex result = m->move(index, dx, dy);
  658. if (result.isValid()) {
  659. setCurrentIndex(result);
  660. emit currentChanged(result, index);
  661. emit QTreeView::activated(result);
  662. scrollTo(result);
  663. }
  664. }
  665. }
  666. void FbTreeView::moveUp()
  667. {
  668. moveCurrent(0, -1);
  669. }
  670. void FbTreeView::moveDown()
  671. {
  672. moveCurrent(0, +1);
  673. }
  674. void FbTreeView::moveLeft()
  675. {
  676. moveCurrent(-1, 0);
  677. }
  678. void FbTreeView::moveRight()
  679. {
  680. moveCurrent(+1, 0);
  681. }
  682. //---------------------------------------------------------------------------
  683. // FbTreeWidget
  684. //---------------------------------------------------------------------------
  685. FbTreeWidget::FbTreeWidget(FbTextEdit *view, QWidget* parent)
  686. : QWidget(parent)
  687. {
  688. QVBoxLayout * layout = new QVBoxLayout(this);
  689. layout->setSpacing(0);
  690. layout->setContentsMargins(0, 0, 0, 0);
  691. m_tree = new FbTreeView(*view, this);
  692. layout->addWidget(m_tree);
  693. m_tool = new QToolBar(this);
  694. layout->addWidget(m_tool);
  695. m_tree->initActions(m_tool);
  696. }