fb2tree.cpp 22 KB

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