1
0

fb2head.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #include "fb2head.hpp"
  2. #include <QtDebug>
  3. #include <QAction>
  4. #include <QHeaderView>
  5. #include <QToolBar>
  6. #include <QWebFrame>
  7. #include <QWebPage>
  8. #include <QWebView>
  9. #include <QItemDelegate>
  10. #include <QTreeView>
  11. #include "fb2view.hpp"
  12. #include "fb2utils.h"
  13. //---------------------------------------------------------------------------
  14. // Fb2Scheme::Fb2
  15. //---------------------------------------------------------------------------
  16. Fb2Scheme::Fb2::Fb2()
  17. {
  18. QFile file(":/fb2/FictionBook2.1.xsd");
  19. if (file.open(QIODevice::ReadOnly)) setContent(&file);
  20. }
  21. //---------------------------------------------------------------------------
  22. // Fb2Scheme
  23. //---------------------------------------------------------------------------
  24. const QDomDocument & Fb2Scheme::fb2()
  25. {
  26. static const Fb2 doc;
  27. return doc;
  28. }
  29. FB2_BEGIN_KEYHASH(Fb2Scheme)
  30. FB2_KEY( Element , "xs:element" );
  31. FB2_KEY( Choice , "xs:choice" );
  32. FB2_KEY( Complex , "xs:complexType" );
  33. FB2_KEY( Sequence , "xs:sequence" );
  34. FB2_END_KEYHASH
  35. QString Fb2Scheme::info() const
  36. {
  37. QDomElement element = *this;
  38. if (element.isNull()) return QString();
  39. element = element.firstChildElement("xs:annotation");
  40. if (element.isNull()) return QString();
  41. element = element.firstChildElement("xs:documentation");
  42. if (element.isNull()) return QString();
  43. return element.text();
  44. }
  45. QString Fb2Scheme::type() const
  46. {
  47. return attribute("type");
  48. }
  49. Fb2Scheme Fb2Scheme::element(const QString &name) const
  50. {
  51. Fb2Scheme parent = *this;
  52. if (parent.isNull()) {
  53. parent = fb2().documentElement();
  54. parent = parent.element("FictionBook");
  55. }
  56. Fb2Scheme child = parent.firstChildElement();
  57. while (!child.isNull()) {
  58. switch (toKeyword(child.tagName())) {
  59. case Element: {
  60. if (child.attribute("name") == name) return child;
  61. } break;
  62. case Choice:
  63. case Complex:
  64. case Sequence: {
  65. Fb2Scheme result = child.element(name);
  66. if (!result.isNull()) return result;
  67. } break;
  68. default: ;
  69. }
  70. child = child.nextSiblingElement();
  71. }
  72. QString type = this->type();
  73. if (type.isEmpty()) return QDomElement();
  74. child = fb2().firstChildElement().firstChildElement();
  75. while (!child.isNull()) {
  76. if (child.tagName() == "xs:complexType") {
  77. if (child.attribute("name") == type) return child.element(name);
  78. }
  79. child = child.nextSiblingElement();
  80. }
  81. return QDomElement();
  82. }
  83. //---------------------------------------------------------------------------
  84. // Fb2HeadItem
  85. //---------------------------------------------------------------------------
  86. Fb2HeadItem::HintHash::HintHash()
  87. {
  88. insert( "title-info" , tr( "Book" ));
  89. insert( "document-info" , tr( "File" ));
  90. insert( "publish-info" , tr( "Publish" ));
  91. insert( "custom-info" , tr( "Add-ons" ));
  92. insert( "genre" , tr( "Genre" ));
  93. insert( "author" , tr( "Author" ));
  94. insert( "book-title" , tr( "Title" ));
  95. insert( "annotation" , tr( "Annotation" ));
  96. insert( "coverpage" , tr( "Cover" ));
  97. insert( "date" , tr( "Date" ));
  98. insert( "lang" , tr( "Language" ));
  99. insert( "translator" , tr( "Translator" ));
  100. insert( "sequence" , tr( "Sequence" ));
  101. insert( "first-name" , tr( "First name" ));
  102. insert( "middle-name" , tr( "Middle name" ));
  103. insert( "last-name" , tr( "Last name" ));
  104. insert( "history" , tr( "History" ));
  105. }
  106. FB2_BEGIN_KEYHASH(Fb2HeadItem)
  107. FB2_KEY( Auth , "author" );
  108. FB2_KEY( Cover , "coverpage" );
  109. FB2_KEY( Image , "img" );
  110. FB2_KEY( Seqn , "sequence" );
  111. FB2_END_KEYHASH
  112. Fb2HeadItem::Fb2HeadItem(QWebElement &element, Fb2HeadItem *parent)
  113. : QObject(parent)
  114. , m_element(element)
  115. , m_parent(parent)
  116. {
  117. m_name = element.tagName().toLower();
  118. m_id = element.attribute("id");
  119. if (m_name == "div") {
  120. QString style = element.attribute("class").toLower();
  121. if (!style.isEmpty()) m_name = style;
  122. if (style == "annotation") return;
  123. if (style == "history") return;
  124. } else if (m_name == "img") {
  125. m_text = element.attribute("alt");
  126. }
  127. addChildren(element);
  128. }
  129. Fb2HeadItem::~Fb2HeadItem()
  130. {
  131. foreach (Fb2HeadItem * item, m_list) {
  132. delete item;
  133. }
  134. }
  135. void Fb2HeadItem::addChildren(QWebElement &parent)
  136. {
  137. QWebElement child = parent.firstChild();
  138. while (!child.isNull()) {
  139. QString tag = child.tagName().toLower();
  140. if (tag == "div") {
  141. m_list << new Fb2HeadItem(child, this);
  142. } else if (tag == "img") {
  143. m_list << new Fb2HeadItem(child, this);
  144. } else {
  145. addChildren(child);
  146. }
  147. child = child.nextSibling();
  148. }
  149. }
  150. Fb2HeadItem * Fb2HeadItem::item(const QModelIndex &index) const
  151. {
  152. int row = index.row();
  153. if (row < 0 || row >= m_list.size()) return NULL;
  154. return m_list[row];
  155. }
  156. Fb2HeadItem * Fb2HeadItem::item(int row) const
  157. {
  158. if (row < 0 || row >= m_list.size()) return NULL;
  159. return m_list[row];
  160. }
  161. QString Fb2HeadItem::text(int col) const
  162. {
  163. switch (col) {
  164. case 0: return QString("<%1> %2").arg(m_name).arg(hint());
  165. case 1: return value();
  166. case 2: return scheme().info();
  167. }
  168. return QString();
  169. }
  170. QString Fb2HeadItem::hint() const
  171. {
  172. static HintHash hints;
  173. HintHash::const_iterator it = hints.find(m_name);
  174. if (it == hints.end()) return QString();
  175. return it.value();
  176. }
  177. QString Fb2HeadItem::value() const
  178. {
  179. switch (toKeyword(m_name)) {
  180. case Auth : {
  181. QString result = sub("last-name");
  182. result += " " + sub("first-name");
  183. result += " " + sub("middle-name");
  184. return result.simplified();
  185. } break;
  186. case Cover : {
  187. QString text;
  188. foreach (Fb2HeadItem * item, m_list) {
  189. if (item->m_name == "img") {
  190. if (!text.isEmpty()) text += ", ";
  191. text += item->value();
  192. }
  193. }
  194. return text;
  195. } break;
  196. case Image : {
  197. return m_element.attribute("src");
  198. } break;
  199. case Seqn : {
  200. QString text = m_element.attribute("fb2.name");
  201. QString numb = m_element.attribute("fb2.number");
  202. if (numb.isEmpty() || numb == "0") return text;
  203. return text + ", " + tr("#") + numb;
  204. } break;
  205. default: ;
  206. }
  207. if (m_list.count()) return QString();
  208. return m_element.toPlainText().simplified();
  209. }
  210. QString Fb2HeadItem::sub(const QString &key) const
  211. {
  212. foreach (Fb2HeadItem * item, m_list) {
  213. if (item->m_name == key) return item->value();
  214. }
  215. return QString();
  216. }
  217. Fb2Scheme Fb2HeadItem::scheme() const
  218. {
  219. Fb2Scheme parent = m_parent ? m_parent->scheme() : Fb2Scheme();
  220. return parent.element(m_name);
  221. }
  222. //---------------------------------------------------------------------------
  223. // Fb2HeadModel
  224. //---------------------------------------------------------------------------
  225. Fb2HeadModel::Fb2HeadModel(QWebView &view, QObject *parent)
  226. : QAbstractItemModel(parent)
  227. , m_view(view)
  228. , m_root(NULL)
  229. {
  230. QWebElement doc = view.page()->mainFrame()->documentElement();
  231. QWebElement head = doc.findFirst("div.description");
  232. if (head.isNull()) return;
  233. m_root = new Fb2HeadItem(head);
  234. }
  235. Fb2HeadModel::~Fb2HeadModel()
  236. {
  237. if (m_root) delete m_root;
  238. }
  239. void Fb2HeadModel::expand(QTreeView *view)
  240. {
  241. QModelIndex parent = QModelIndex();
  242. int count = rowCount(parent);
  243. for (int i = 0; i < count; i++) {
  244. QModelIndex child = index(i, 0, parent);
  245. Fb2HeadItem *node = item(child);
  246. if (!node) continue;
  247. view->expand(child);
  248. int count = rowCount(child);
  249. for (int j = 0; j < count; j++) {
  250. QModelIndex ch = index(j, 0, child);
  251. Fb2HeadItem *node = item(ch);
  252. if (node) view->expand(ch);
  253. }
  254. }
  255. }
  256. Fb2HeadItem * Fb2HeadModel::item(const QModelIndex &index) const
  257. {
  258. if (index.isValid()) {
  259. return static_cast<Fb2HeadItem*>(index.internalPointer());
  260. } else {
  261. return 0;
  262. }
  263. }
  264. int Fb2HeadModel::columnCount(const QModelIndex &parent) const
  265. {
  266. Q_UNUSED(parent);
  267. return 2;
  268. }
  269. QModelIndex Fb2HeadModel::index(int row, int column, const QModelIndex &parent) const
  270. {
  271. if (!m_root || row < 0 || column < 0) return QModelIndex();
  272. if (!parent.isValid() && m_root) {
  273. return createIndex(row, column, (void*)m_root);
  274. }
  275. if (Fb2HeadItem *owner = item(parent)) {
  276. if (Fb2HeadItem *child = owner->item(row)) {
  277. return createIndex(row, column, (void*)child);
  278. }
  279. }
  280. return QModelIndex();
  281. }
  282. QModelIndex Fb2HeadModel::parent(const QModelIndex &child) const
  283. {
  284. if (Fb2HeadItem * node = static_cast<Fb2HeadItem*>(child.internalPointer())) {
  285. if (Fb2HeadItem * parent = node->parent()) {
  286. if (Fb2HeadItem * owner = parent->parent()) {
  287. return createIndex(owner->index(parent), 0, (void*)parent);
  288. } else {
  289. return createIndex(0, 0, (void*)parent);
  290. }
  291. }
  292. }
  293. return QModelIndex();
  294. }
  295. int Fb2HeadModel::rowCount(const QModelIndex &parent) const
  296. {
  297. if (parent.column() > 0) return 0;
  298. if (!parent.isValid()) return m_root ? 1 : 0;
  299. Fb2HeadItem *owner = item(parent);
  300. return owner ? owner->count() : 0;
  301. }
  302. QVariant Fb2HeadModel::data(const QModelIndex &index, int role) const
  303. {
  304. if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
  305. Fb2HeadItem * i = item(index);
  306. return i ? i->text(index.column()) : QVariant();
  307. }
  308. QVariant Fb2HeadModel::headerData(int section, Qt::Orientation orientation, int role) const
  309. {
  310. if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
  311. switch (section) {
  312. case 0: return tr("Key");
  313. case 1: return tr("Value");
  314. }
  315. }
  316. return QVariant();
  317. }
  318. void Fb2HeadModel::select(const QModelIndex &index)
  319. {
  320. Fb2HeadItem *node = item(index);
  321. if (!node || node->id().isEmpty()) return;
  322. m_view.page()->mainFrame()->scrollToAnchor(node->id());
  323. }
  324. void Fb2HeadItem::setText(const QString &text)
  325. {
  326. m_text = text;
  327. }
  328. bool Fb2HeadModel::setData(const QModelIndex &index, const QVariant &value, int role)
  329. {
  330. if (role != Qt::EditRole) return false;
  331. Fb2HeadItem * i = item(index);
  332. if (!i) return false;
  333. i->setText(value.toString());
  334. emit dataChanged(index, index);
  335. return true;
  336. }
  337. Qt::ItemFlags Fb2HeadModel::flags(const QModelIndex &index) const
  338. {
  339. if (!index.isValid()) return 0;
  340. Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  341. if (index.column() == 1) flags |= Qt::ItemIsEditable;
  342. return flags;
  343. }
  344. //---------------------------------------------------------------------------
  345. // Fb2TreeView
  346. //---------------------------------------------------------------------------
  347. Fb2HeadView::Fb2HeadView(Fb2WebView &view, QWidget *parent)
  348. : QTreeView(parent)
  349. , m_view(view)
  350. {
  351. QAction * act;
  352. actionInsert = act = new QAction(FB2::icon("list-add"), tr("&Append"), this);
  353. act->setPriority(QAction::LowPriority);
  354. act->setShortcuts(QKeySequence::New);
  355. actionModify = act = new QAction(FB2::icon("list-add"), tr("&Modify"), this);
  356. act->setPriority(QAction::LowPriority);
  357. actionDelete = act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
  358. act->setPriority(QAction::LowPriority);
  359. act->setShortcuts(QKeySequence::Delete);
  360. //setItemDelegate(new QItemDelegate(this));
  361. setRootIsDecorated(false);
  362. setSelectionBehavior(QAbstractItemView::SelectItems);
  363. setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
  364. connect(&m_view, SIGNAL(loadFinished(bool)), SLOT(updateTree()));
  365. connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
  366. connect(this, SIGNAL(collapsed(QModelIndex)), SLOT(collapsed(QModelIndex)));
  367. header()->setDefaultSectionSize(200);
  368. connect(actionModify, SIGNAL(triggered()), SLOT(editCurrent()));
  369. }
  370. void Fb2HeadView::initToolbar(QToolBar &toolbar)
  371. {
  372. toolbar.addSeparator();
  373. toolbar.addAction(actionInsert);
  374. toolbar.addAction(actionDelete);
  375. }
  376. void Fb2HeadView::updateTree()
  377. {
  378. Fb2HeadModel * model = new Fb2HeadModel(m_view, this);
  379. setModel(model);
  380. model->expand(this);
  381. }
  382. void Fb2HeadView::editCurrent()
  383. {
  384. if (!model()) return;
  385. QModelIndex current = currentIndex();
  386. if (!current.isValid()) return;
  387. QModelIndex parent = model()->parent(current);
  388. QModelIndex index = model()->index(current.row(), 1, parent);
  389. setCurrentIndex(index);
  390. edit(index);
  391. }
  392. void Fb2HeadView::activated(const QModelIndex &index)
  393. {
  394. if (index.isValid() && index.column() == 1) edit(index);
  395. showStatus(index);
  396. }
  397. void Fb2HeadView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
  398. {
  399. QTreeView::currentChanged(current, previous);
  400. showStatus(current);
  401. }
  402. void Fb2HeadView::showStatus(const QModelIndex &current)
  403. {
  404. if (!model()) return;
  405. if (!current.isValid()) return;
  406. QModelIndex parent = model()->parent(current);
  407. QModelIndex index = model()->index(current.row(), 2, parent);
  408. emit status(model()->data(index).toString());
  409. }
  410. void Fb2HeadView::collapsed(const QModelIndex &index)
  411. {
  412. if (model() && !model()->parent(index).isValid()) {
  413. expand(index);
  414. }
  415. }