fb2head.cpp 13 KB

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