fb2tree.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include "fb2tree.hpp"
  2. #include <QtDebug>
  3. #include <QAction>
  4. #include <QApplication>
  5. #include <QVBoxLayout>
  6. #include <QWebFrame>
  7. #include <QWebPage>
  8. #include <QTreeView>
  9. #include <QUrl>
  10. #include "fb2utils.h"
  11. #include "fb2view.hpp"
  12. //---------------------------------------------------------------------------
  13. // Fb2WebElement
  14. //---------------------------------------------------------------------------
  15. void Fb2WebElement::select()
  16. {
  17. static const QString javascript = FB2::read(":/js/set_cursor.js");
  18. evaluateJavaScript(javascript);
  19. }
  20. //---------------------------------------------------------------------------
  21. // Fb2TreeItem
  22. //---------------------------------------------------------------------------
  23. Fb2TreeItem::Fb2TreeItem(QWebElement &element, Fb2TreeItem *parent, int number)
  24. : QObject(parent)
  25. , m_element(element)
  26. , m_parent(parent)
  27. , m_number(number)
  28. {
  29. m_name = element.tagName().toLower();
  30. QString style = element.attribute("class").toLower();
  31. if (m_name == "div") {
  32. if (style == "title") {
  33. m_text = title(element);
  34. if (m_parent) m_parent->m_text += m_text += " ";
  35. } else if (style == "subtitle") {
  36. m_text = title(element);
  37. } else if (style == "body") {
  38. QString name = element.attribute("fb2_name");
  39. if (!name.isEmpty()) style += " name=" + name;
  40. }
  41. if (!style.isEmpty()) m_name = style;
  42. } else if (m_name == "img") {
  43. m_name = "image";
  44. QUrl url = element.attribute("src");
  45. m_text = url.path();
  46. }
  47. addChildren(element);
  48. }
  49. Fb2TreeItem::~Fb2TreeItem()
  50. {
  51. foreach (Fb2TreeItem * item, m_list) {
  52. delete item;
  53. }
  54. }
  55. QString Fb2TreeItem::title(const QWebElement &element)
  56. {
  57. return element.toPlainText().left(255).simplified();
  58. }
  59. void Fb2TreeItem::addChildren(QWebElement &parent, bool direct)
  60. {
  61. int number = 0;
  62. QWebElement child = parent.firstChild();
  63. while (!child.isNull()) {
  64. QString tag = child.tagName().toLower();
  65. if (tag == "div") {
  66. m_list << new Fb2TreeItem(child, this, direct ? number : -1);
  67. } else if (tag == "img") {
  68. m_list << new Fb2TreeItem(child, this, direct ? number : -1);
  69. } else {
  70. addChildren(child, false);
  71. }
  72. child = child.nextSibling();
  73. number++;
  74. }
  75. }
  76. Fb2TreeItem * Fb2TreeItem::item(const QModelIndex &index) const
  77. {
  78. int row = index.row();
  79. if (row < 0 || row >= m_list.size()) return NULL;
  80. return m_list[row];
  81. }
  82. Fb2TreeItem * Fb2TreeItem::item(int row) const
  83. {
  84. if (row < 0 || row >= m_list.size()) return NULL;
  85. return m_list[row];
  86. }
  87. QString Fb2TreeItem::text() const
  88. {
  89. return QString("<%1> %2").arg(m_name).arg(m_text);
  90. }
  91. QString Fb2TreeItem::selector() const
  92. {
  93. QString text = "";
  94. QString selector = ".get(0)";
  95. QWebElement element = m_element;
  96. QWebElement parent = element.parent();
  97. while (!parent.isNull()) {
  98. text.prepend(element.tagName()).prepend("/");
  99. QWebElement child = parent.firstChild();
  100. int index = -1;
  101. while (!child.isNull()) {
  102. index++;
  103. if (child == element) break;
  104. child = child.nextSibling();
  105. }
  106. if (index == -1) return QString();
  107. selector.prepend(QString(".children().eq(%1)").arg(index));
  108. element = parent;
  109. parent = element.parent();
  110. }
  111. return selector.prepend("$('html')");
  112. }
  113. Fb2TreeItem * Fb2TreeItem::content(const Fb2TreeModel &model, int number, QModelIndex &index) const
  114. {
  115. int row = 0;
  116. QList<Fb2TreeItem*>::const_iterator i;
  117. for (i = m_list.constBegin(); i != m_list.constEnd(); ++i) {
  118. if ((*i)->m_number == number) {
  119. index = model.index(row, 0, index);
  120. return *i;
  121. }
  122. row++;
  123. }
  124. return 0;
  125. }
  126. //---------------------------------------------------------------------------
  127. // Fb2TreeModel
  128. //---------------------------------------------------------------------------
  129. Fb2TreeModel::Fb2TreeModel(Fb2WebView &view, QObject *parent)
  130. : QAbstractItemModel(parent)
  131. , m_view(view)
  132. , m_root(NULL)
  133. {
  134. QWebElement doc = view.page()->mainFrame()->documentElement();
  135. QWebElement body = doc.findFirst("body");
  136. if (body.isNull()) return;
  137. m_root = new Fb2TreeItem(body);
  138. }
  139. Fb2TreeModel::~Fb2TreeModel()
  140. {
  141. if (m_root) delete m_root;
  142. }
  143. Fb2TreeItem * Fb2TreeModel::item(const QModelIndex &index) const
  144. {
  145. if (index.isValid()) {
  146. return static_cast<Fb2TreeItem*>(index.internalPointer());
  147. } else {
  148. return m_root;
  149. }
  150. }
  151. int Fb2TreeModel::columnCount(const QModelIndex &parent) const
  152. {
  153. Q_UNUSED(parent);
  154. return 1;
  155. }
  156. QModelIndex Fb2TreeModel::index(int row, int column, const QModelIndex &parent) const
  157. {
  158. if (!m_root || row < 0 || column < 0) return QModelIndex();
  159. if (Fb2TreeItem *owner = item(parent)) {
  160. if (Fb2TreeItem *child = owner->item(row)) {
  161. return createIndex(row, column, (void*)child);
  162. }
  163. }
  164. return QModelIndex();
  165. }
  166. QModelIndex Fb2TreeModel::parent(const QModelIndex &child) const
  167. {
  168. if (Fb2TreeItem * node = static_cast<Fb2TreeItem*>(child.internalPointer())) {
  169. if (Fb2TreeItem * parent = node->parent()) {
  170. if (Fb2TreeItem * owner = parent->parent()) {
  171. return createIndex(owner->index(parent), 0, (void*)parent);
  172. }
  173. }
  174. }
  175. return QModelIndex();
  176. }
  177. int Fb2TreeModel::rowCount(const QModelIndex &parent) const
  178. {
  179. if (parent.column() > 0) return 0;
  180. Fb2TreeItem *owner = item(parent);
  181. return owner ? owner->count() : 0;
  182. }
  183. QVariant Fb2TreeModel::data(const QModelIndex &index, int role) const
  184. {
  185. if (role != Qt::DisplayRole) return QVariant();
  186. Fb2TreeItem * i = item(index);
  187. return i ? i->text() : QVariant();
  188. }
  189. void Fb2TreeModel::selectText(const QModelIndex &index)
  190. {
  191. if (Fb2TreeItem *node = item(index)) {
  192. node->element().select();
  193. }
  194. }
  195. QModelIndex Fb2TreeModel::index(const QString &location) const
  196. {
  197. QModelIndex index;
  198. Fb2TreeItem * parent = m_root;
  199. QStringList list = location.split(",");
  200. QStringListIterator iterator(list);
  201. while (parent && iterator.hasNext()) {
  202. QString str = iterator.next();
  203. if (str.left(5) == "HTML=") continue;
  204. int key = str.mid(str.indexOf("=")+1).toInt();
  205. Fb2TreeItem * child = parent->content(*this, key, index);
  206. parent = child;
  207. }
  208. return index;
  209. }
  210. //---------------------------------------------------------------------------
  211. // Fb2TreeView
  212. //---------------------------------------------------------------------------
  213. Fb2TreeView::Fb2TreeView(Fb2WebView &view, QWidget *parent)
  214. : QTreeView(parent)
  215. , m_view(view)
  216. {
  217. setHeaderHidden(true);
  218. setContextMenuPolicy(Qt::ActionsContextMenu);
  219. connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
  220. connect(m_view.page(), SIGNAL(loadFinished(bool)), SLOT(updateTree()));
  221. connect(m_view.page(), SIGNAL(contentsChanged()), SLOT(contentsChanged()));
  222. connect(m_view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  223. m_timerSelect.setInterval(1000);
  224. m_timerSelect.setSingleShot(true);
  225. connect(&m_timerSelect, SIGNAL(timeout()), SLOT(selectTree()));
  226. m_timerUpdate.setInterval(1000);
  227. m_timerUpdate.setSingleShot(true);
  228. connect(&m_timerUpdate, SIGNAL(timeout()), SLOT(updateTree()));
  229. QMetaObject::invokeMethod(this, "updateTree", Qt::QueuedConnection);
  230. }
  231. void Fb2TreeView::initToolbar(QToolBar *toolbar)
  232. {
  233. QAction * act;
  234. act = new QAction(FB2::icon("list-add"), tr("&Insert"), this);
  235. connect(act, SIGNAL(triggered()), SLOT(insertNode()));
  236. toolbar->addAction(act);
  237. addAction(act);
  238. act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
  239. connect(act, SIGNAL(triggered()), SLOT(deleteNode()));
  240. toolbar->addAction(act);
  241. addAction(act);
  242. toolbar->addSeparator();
  243. act = new QAction(FB2::icon("go-up"), tr("&Up"), this);
  244. connect(act, SIGNAL(triggered()), SLOT(moveUp()));
  245. toolbar->addAction(act);
  246. addAction(act);
  247. act = new QAction(FB2::icon("go-down"), tr("&Down"), this);
  248. connect(act, SIGNAL(triggered()), SLOT(moveDown()));
  249. toolbar->addAction(act);
  250. addAction(act);
  251. act = new QAction(FB2::icon("go-previous"), tr("&Left"), this);
  252. connect(act, SIGNAL(triggered()), SLOT(moveLeft()));
  253. toolbar->addAction(act);
  254. addAction(act);
  255. act = new QAction(FB2::icon("go-next"), tr("&Right"), this);
  256. connect(act, SIGNAL(triggered()), SLOT(moveRight()));
  257. toolbar->addAction(act);
  258. addAction(act);
  259. }
  260. void Fb2TreeView::selectionChanged()
  261. {
  262. m_timerSelect.start();
  263. }
  264. void Fb2TreeView::contentsChanged()
  265. {
  266. m_timerUpdate.start();
  267. }
  268. void Fb2TreeView::activated(const QModelIndex &index)
  269. {
  270. if (qApp->focusWidget() == &m_view) return;
  271. if (!model()) return;
  272. Fb2TreeModel *model = static_cast<Fb2TreeModel*>(this->model());
  273. model->selectText(index);
  274. }
  275. void Fb2TreeView::selectTree()
  276. {
  277. if (qApp->focusWidget() == this) return;
  278. if (model() == 0) return;
  279. Fb2TreeModel * model = static_cast<Fb2TreeModel*>(this->model());
  280. QWebFrame * frame = model->view().page()->mainFrame();
  281. static const QString javascript = FB2::read(":/js/get_location.js");
  282. QString location = frame->evaluateJavaScript(javascript).toString();
  283. QModelIndex index = model->index(location);
  284. if (!index.isValid()) return;
  285. setCurrentIndex(index);
  286. scrollTo(index);
  287. }
  288. void Fb2TreeView::updateTree()
  289. {
  290. Fb2TreeModel * model = new Fb2TreeModel(m_view, this);
  291. setModel(model);
  292. selectTree();
  293. }
  294. void Fb2TreeView::insertNode()
  295. {
  296. }
  297. void Fb2TreeView::deleteNode()
  298. {
  299. }
  300. void Fb2TreeView::moveUp()
  301. {
  302. }
  303. void Fb2TreeView::moveDown()
  304. {
  305. }
  306. void Fb2TreeView::moveLeft()
  307. {
  308. }
  309. void Fb2TreeView::moveRight()
  310. {
  311. }
  312. //---------------------------------------------------------------------------
  313. // Fb2TreeWidget
  314. //---------------------------------------------------------------------------
  315. Fb2TreeWidget::Fb2TreeWidget(Fb2WebView &view, QWidget* parent)
  316. : QWidget(parent)
  317. {
  318. QVBoxLayout * layout = new QVBoxLayout(this);
  319. layout->setSpacing(0);
  320. layout->setContentsMargins(0, 0, 0, 0);
  321. layout->setObjectName(QString::fromUtf8("verticalLayout"));
  322. m_tree = new Fb2TreeView(view, this);
  323. m_tree->setContextMenuPolicy(Qt::ActionsContextMenu);
  324. layout->addWidget(m_tree);
  325. m_tool = new QToolBar(this);
  326. layout->addWidget(m_tool);
  327. m_tree->initToolbar(m_tool);
  328. }