fb2head.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. }
  94. return QString();
  95. }
  96. QString Fb2HeadItem::hint() const
  97. {
  98. static HintHash hints;
  99. HintHash::const_iterator it = hints.find(m_name);
  100. if (it == hints.end()) return QString();
  101. return it.value();
  102. }
  103. QString Fb2HeadItem::value() const
  104. {
  105. switch (toKeyword(m_name)) {
  106. case Auth : {
  107. QString result = sub("last-name");
  108. result += " " + sub("first-name");
  109. result += " " + sub("middle-name");
  110. return result.simplified();
  111. } break;
  112. case Cover : {
  113. QString text;
  114. foreach (Fb2HeadItem * item, m_list) {
  115. if (item->m_name == "img") {
  116. if (!text.isEmpty()) text += ", ";
  117. text += item->value();
  118. }
  119. }
  120. return text;
  121. } break;
  122. case Image : {
  123. return m_element.attribute("src");
  124. } break;
  125. case Seqn : {
  126. QString text = m_element.attribute("fb2.name");
  127. QString numb = m_element.attribute("fb2.number");
  128. if (numb.isEmpty() || numb == "0") return text;
  129. return text + ", " + tr("#") + numb;
  130. } break;
  131. default: ;
  132. }
  133. if (m_list.count()) return QString();
  134. return m_element.toPlainText().simplified();
  135. }
  136. QString Fb2HeadItem::sub(const QString &key) const
  137. {
  138. foreach (Fb2HeadItem * item, m_list) {
  139. if (item->m_name == key) return item->value();
  140. }
  141. return QString();
  142. }
  143. //---------------------------------------------------------------------------
  144. // Fb2HeadModel
  145. //---------------------------------------------------------------------------
  146. Fb2HeadModel::Fb2HeadModel(QWebView &view, QObject *parent)
  147. : QAbstractItemModel(parent)
  148. , m_view(view)
  149. , m_root(NULL)
  150. {
  151. QWebElement doc = view.page()->mainFrame()->documentElement();
  152. QWebElement head = doc.findFirst("div.description");
  153. if (head.isNull()) return;
  154. m_root = new Fb2HeadItem(head);
  155. }
  156. Fb2HeadModel::~Fb2HeadModel()
  157. {
  158. if (m_root) delete m_root;
  159. }
  160. void Fb2HeadModel::expand(QTreeView *view)
  161. {
  162. QModelIndex parent = QModelIndex();
  163. int count = rowCount(parent);
  164. for (int i = 0; i < count; i++) {
  165. QModelIndex child = index(i, 0, parent);
  166. Fb2HeadItem *node = item(child);
  167. if (node) view->expand(child);
  168. }
  169. }
  170. Fb2HeadItem * Fb2HeadModel::item(const QModelIndex &index) const
  171. {
  172. if (index.isValid()) {
  173. return static_cast<Fb2HeadItem*>(index.internalPointer());
  174. } else {
  175. return m_root;
  176. }
  177. }
  178. int Fb2HeadModel::columnCount(const QModelIndex &parent) const
  179. {
  180. Q_UNUSED(parent);
  181. return 2;
  182. }
  183. QModelIndex Fb2HeadModel::index(int row, int column, const QModelIndex &parent) const
  184. {
  185. if (!m_root || row < 0 || column < 0) return QModelIndex();
  186. if (Fb2HeadItem *owner = item(parent)) {
  187. if (Fb2HeadItem *child = owner->item(row)) {
  188. return createIndex(row, column, (void*)child);
  189. }
  190. }
  191. return QModelIndex();
  192. }
  193. QModelIndex Fb2HeadModel::parent(const QModelIndex &child) const
  194. {
  195. if (Fb2HeadItem * node = static_cast<Fb2HeadItem*>(child.internalPointer())) {
  196. if (Fb2HeadItem * parent = node->parent()) {
  197. if (Fb2HeadItem * owner = parent->parent()) {
  198. return createIndex(owner->index(parent), 0, (void*)parent);
  199. }
  200. }
  201. }
  202. return QModelIndex();
  203. }
  204. int Fb2HeadModel::rowCount(const QModelIndex &parent) const
  205. {
  206. if (parent.column() > 0) return 0;
  207. Fb2HeadItem *owner = item(parent);
  208. return owner ? owner->count() : 0;
  209. }
  210. QVariant Fb2HeadModel::data(const QModelIndex &index, int role) const
  211. {
  212. if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
  213. Fb2HeadItem * i = item(index);
  214. return i ? i->text(index.column()) : QVariant();
  215. }
  216. QVariant Fb2HeadModel::headerData(int section, Qt::Orientation orientation, int role) const
  217. {
  218. if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
  219. switch (section) {
  220. case 0: return tr("Key");
  221. case 1: return tr("Value");
  222. }
  223. }
  224. return QVariant();
  225. }
  226. void Fb2HeadModel::select(const QModelIndex &index)
  227. {
  228. Fb2HeadItem *node = item(index);
  229. if (!node || node->id().isEmpty()) return;
  230. m_view.page()->mainFrame()->scrollToAnchor(node->id());
  231. }
  232. void Fb2HeadItem::setText(const QString &text)
  233. {
  234. m_text = text;
  235. }
  236. bool Fb2HeadModel::setData(const QModelIndex &index, const QVariant &value, int role)
  237. {
  238. if (role != Qt::EditRole) return false;
  239. Fb2HeadItem * i = item(index);
  240. if (!i) return false;
  241. i->setText(value.toString());
  242. emit dataChanged(index, index);
  243. return true;
  244. }
  245. Qt::ItemFlags Fb2HeadModel::flags(const QModelIndex &index) const
  246. {
  247. if (!index.isValid()) return 0;
  248. Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  249. if (index.column() == 1) flags |= Qt::ItemIsEditable;
  250. return flags;
  251. }
  252. //---------------------------------------------------------------------------
  253. // Fb2TreeView
  254. //---------------------------------------------------------------------------
  255. Fb2HeadView::Fb2HeadView(Fb2WebView &view, QWidget *parent)
  256. : QTreeView(parent)
  257. , m_view(view)
  258. {
  259. QAction * act;
  260. actionInsert = act = new QAction(FB2::icon("list-add"), tr("&Append"), this);
  261. act->setPriority(QAction::LowPriority);
  262. act->setShortcuts(QKeySequence::New);
  263. actionModify = act = new QAction(FB2::icon("list-add"), tr("&Modify"), this);
  264. act->setPriority(QAction::LowPriority);
  265. actionDelete = act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
  266. act->setPriority(QAction::LowPriority);
  267. act->setShortcuts(QKeySequence::Delete);
  268. //setItemDelegate(new QItemDelegate(this));
  269. setSelectionBehavior(QAbstractItemView::SelectItems);
  270. setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
  271. connect(&m_view, SIGNAL(loadFinished(bool)), SLOT(updateTree()));
  272. connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
  273. header()->setDefaultSectionSize(200);
  274. connect(actionModify, SIGNAL(triggered()), SLOT(editCurrent()));
  275. }
  276. void Fb2HeadView::initToolbar(QToolBar &toolbar)
  277. {
  278. toolbar.addSeparator();
  279. toolbar.addAction(actionInsert);
  280. toolbar.addAction(actionDelete);
  281. }
  282. void Fb2HeadView::updateTree()
  283. {
  284. Fb2HeadModel * model = new Fb2HeadModel(m_view, this);
  285. setModel(model);
  286. model->expand(this);
  287. }
  288. void Fb2HeadView::editCurrent()
  289. {
  290. QModelIndex current = currentIndex();
  291. if (!current.isValid()) return;
  292. Fb2HeadModel * m = static_cast<Fb2HeadModel*>(model());
  293. QModelIndex index = m->index(current.row(), 1, m->parent(current));
  294. setCurrentIndex(index);
  295. edit(index);
  296. }
  297. void Fb2HeadView::activated(const QModelIndex &index)
  298. {
  299. if (index.isValid() && index.column() == 1) edit(index);
  300. }