fb2head.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "fb2head.hpp"
  2. #include <QtDebug>
  3. #include <QWebFrame>
  4. #include <QWebPage>
  5. #include <QWebView>
  6. #include <QTreeView>
  7. #include "fb2view.hpp"
  8. Fb2HeadItem::HintHash::HintHash()
  9. {
  10. insert( "title-info" , tr( "Book" ));
  11. insert( "document-info" , tr( "File" ));
  12. insert( "publish-info" , tr( "Publish" ));
  13. insert( "custom-info" , tr( "Add-ons" ));
  14. insert( "genre" , tr( "Genre" ));
  15. insert( "author" , tr( "Author" ));
  16. insert( "book-title" , tr( "Title" ));
  17. insert( "annotation" , tr( "Annotation" ));
  18. insert( "coverpage" , tr( "Cover" ));
  19. insert( "date" , tr( "Date" ));
  20. insert( "lang" , tr( "Language" ));
  21. insert( "translator" , tr( "Translator" ));
  22. insert( "sequence" , tr( "Sequence" ));
  23. insert( "first-name" , tr( "First name" ));
  24. insert( "middle-name" , tr( "Middle name" ));
  25. insert( "last-name" , tr( "Last name" ));
  26. insert( "history" , tr( "History" ));
  27. }
  28. FB2_BEGIN_KEYHASH(Fb2HeadItem)
  29. FB2_KEY( Auth , "author" );
  30. FB2_KEY( Cover , "coverpage" );
  31. FB2_KEY( Image , "img" );
  32. FB2_KEY( Seqn , "sequence" );
  33. FB2_END_KEYHASH
  34. Fb2HeadItem::Fb2HeadItem(QWebElement &element, Fb2HeadItem *parent)
  35. : QObject(parent)
  36. , m_element(element)
  37. , m_parent(parent)
  38. {
  39. m_name = element.tagName().toLower();
  40. QString style = element.attribute("class").toLower();
  41. if (m_name == "div") {
  42. if (!style.isEmpty()) m_name = style;
  43. } else if (m_name == "img") {
  44. m_text = element.attribute("alt");
  45. }
  46. m_id = element.attribute("id");
  47. addChildren(element);
  48. }
  49. Fb2HeadItem::~Fb2HeadItem()
  50. {
  51. foreach (Fb2HeadItem * item, m_list) {
  52. delete item;
  53. }
  54. }
  55. void Fb2HeadItem::addChildren(QWebElement &parent)
  56. {
  57. QWebElement child = parent.firstChild();
  58. while (!child.isNull()) {
  59. QString tag = child.tagName().toLower();
  60. if (tag == "div") {
  61. m_list << new Fb2HeadItem(child, this);
  62. } else if (tag == "img") {
  63. m_list << new Fb2HeadItem(child, this);
  64. } else {
  65. addChildren(child);
  66. }
  67. child = child.nextSibling();
  68. }
  69. }
  70. Fb2HeadItem * Fb2HeadItem::item(const QModelIndex &index) const
  71. {
  72. int row = index.row();
  73. if (row < 0 || row >= m_list.size()) return NULL;
  74. return m_list[row];
  75. }
  76. Fb2HeadItem * Fb2HeadItem::item(int row) const
  77. {
  78. if (row < 0 || row >= m_list.size()) return NULL;
  79. return m_list[row];
  80. }
  81. QString Fb2HeadItem::text(int col) const
  82. {
  83. switch (col) {
  84. case 0: return QString("<%1> %2").arg(m_name).arg(hint());
  85. case 1: return value();
  86. }
  87. return QString();
  88. }
  89. QString Fb2HeadItem::hint() const
  90. {
  91. static HintHash hints;
  92. HintHash::const_iterator it = hints.find(m_name);
  93. if (it == hints.end()) return QString();
  94. return it.value();
  95. }
  96. QString Fb2HeadItem::value() const
  97. {
  98. switch (toKeyword(m_name)) {
  99. case Auth : {
  100. return sub("last-name") + " " + sub("first-name") + " " + sub("middle-name");
  101. } break;
  102. case Cover : {
  103. QString text;
  104. foreach (Fb2HeadItem * item, m_list) {
  105. if (item->m_name == "img") {
  106. if (!text.isEmpty()) text += ", ";
  107. text += item->value();
  108. }
  109. }
  110. return text;
  111. } break;
  112. case Image : {
  113. return m_element.attribute("alt");
  114. } break;
  115. case Seqn : {
  116. QString text = m_element.attribute("fb2:name");
  117. QString numb = m_element.attribute("fb2:number");
  118. if (numb.isEmpty() || numb == "0") return text;
  119. return text + ", " + tr("#") + numb;
  120. } break;
  121. default: ;
  122. }
  123. if (m_list.count()) return QString();
  124. return m_element.toPlainText().simplified();
  125. }
  126. QString Fb2HeadItem::sub(const QString &key) const
  127. {
  128. foreach (Fb2HeadItem * item, m_list) {
  129. if (item->m_name == key) return item->value();
  130. }
  131. return QString();
  132. }
  133. //---------------------------------------------------------------------------
  134. // Fb2HeadModel
  135. //---------------------------------------------------------------------------
  136. Fb2HeadModel::Fb2HeadModel(QWebView &view, QObject *parent)
  137. : QAbstractItemModel(parent)
  138. , m_view(view)
  139. , m_root(NULL)
  140. {
  141. QWebElement doc = view.page()->mainFrame()->documentElement();
  142. QWebElement head = doc.findFirst("div.description");
  143. if (head.isNull()) return;
  144. m_root = new Fb2HeadItem(head);
  145. }
  146. Fb2HeadModel::~Fb2HeadModel()
  147. {
  148. if (m_root) delete m_root;
  149. }
  150. void Fb2HeadModel::expand(QTreeView *view)
  151. {
  152. QModelIndex parent = QModelIndex();
  153. int count = rowCount(parent);
  154. for (int i = 0; i < count; i++) {
  155. QModelIndex child = index(i, 0, parent);
  156. Fb2HeadItem *node = item(child);
  157. if (node) view->expand(child);
  158. }
  159. }
  160. Fb2HeadItem * Fb2HeadModel::item(const QModelIndex &index) const
  161. {
  162. if (index.isValid()) {
  163. return static_cast<Fb2HeadItem*>(index.internalPointer());
  164. } else {
  165. return m_root;
  166. }
  167. }
  168. int Fb2HeadModel::columnCount(const QModelIndex &parent) const
  169. {
  170. Q_UNUSED(parent);
  171. return 2;
  172. }
  173. QModelIndex Fb2HeadModel::index(int row, int column, const QModelIndex &parent) const
  174. {
  175. if (!m_root || row < 0 || column < 0) return QModelIndex();
  176. if (Fb2HeadItem *owner = item(parent)) {
  177. if (Fb2HeadItem *child = owner->item(row)) {
  178. return createIndex(row, column, (void*)child);
  179. }
  180. }
  181. return QModelIndex();
  182. }
  183. QModelIndex Fb2HeadModel::parent(const QModelIndex &child) const
  184. {
  185. if (Fb2HeadItem * node = static_cast<Fb2HeadItem*>(child.internalPointer())) {
  186. if (Fb2HeadItem * parent = node->parent()) {
  187. if (Fb2HeadItem * owner = parent->parent()) {
  188. return createIndex(owner->index(parent), 0, (void*)parent);
  189. }
  190. }
  191. }
  192. return QModelIndex();
  193. }
  194. int Fb2HeadModel::rowCount(const QModelIndex &parent) const
  195. {
  196. if (parent.column() > 0) return 0;
  197. Fb2HeadItem *owner = item(parent);
  198. return owner ? owner->count() : 0;
  199. }
  200. QVariant Fb2HeadModel::data(const QModelIndex &index, int role) const
  201. {
  202. if (role != Qt::DisplayRole) return QVariant();
  203. Fb2HeadItem * i = item(index);
  204. return i ? i->text(index.column()) : QVariant();
  205. }
  206. QVariant Fb2HeadModel::headerData(int section, Qt::Orientation orientation, int role) const
  207. {
  208. if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
  209. switch (section) {
  210. case 0: return tr("Key");
  211. case 1: return tr("Value");
  212. }
  213. }
  214. return QVariant();
  215. }
  216. void Fb2HeadModel::select(const QModelIndex &index)
  217. {
  218. Fb2HeadItem *node = item(index);
  219. if (!node || node->id().isEmpty()) return;
  220. m_view.page()->mainFrame()->scrollToAnchor(node->id());
  221. }
  222. //---------------------------------------------------------------------------
  223. // Fb2TreeView
  224. //---------------------------------------------------------------------------
  225. Fb2HeadView::Fb2HeadView(Fb2WebView &view, QWidget *parent)
  226. : QTreeView(parent)
  227. , m_view(view)
  228. {
  229. connect(&m_view, SIGNAL(loadFinished(bool)), SLOT(updateTree()));
  230. }
  231. void Fb2HeadView::updateTree()
  232. {
  233. Fb2HeadModel * model = new Fb2HeadModel(m_view, this);
  234. setModel(model);
  235. model->expand(this);
  236. }