fb2head.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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) continue;
  168. view->expand(child);
  169. int count = rowCount(child);
  170. for (int j = 0; j < count; j++) {
  171. QModelIndex ch = index(j, 0, child);
  172. Fb2HeadItem *node = item(ch);
  173. if (node) view->expand(ch);
  174. }
  175. }
  176. }
  177. Fb2HeadItem * Fb2HeadModel::item(const QModelIndex &index) const
  178. {
  179. if (index.isValid()) {
  180. return static_cast<Fb2HeadItem*>(index.internalPointer());
  181. } else {
  182. return 0;
  183. }
  184. }
  185. int Fb2HeadModel::columnCount(const QModelIndex &parent) const
  186. {
  187. Q_UNUSED(parent);
  188. return 2;
  189. }
  190. QModelIndex Fb2HeadModel::index(int row, int column, const QModelIndex &parent) const
  191. {
  192. if (!m_root || row < 0 || column < 0) return QModelIndex();
  193. if (!parent.isValid() && m_root) {
  194. return createIndex(row, column, (void*)m_root);
  195. }
  196. if (Fb2HeadItem *owner = item(parent)) {
  197. if (Fb2HeadItem *child = owner->item(row)) {
  198. return createIndex(row, column, (void*)child);
  199. }
  200. }
  201. return QModelIndex();
  202. }
  203. QModelIndex Fb2HeadModel::parent(const QModelIndex &child) const
  204. {
  205. if (Fb2HeadItem * node = static_cast<Fb2HeadItem*>(child.internalPointer())) {
  206. if (Fb2HeadItem * parent = node->parent()) {
  207. if (Fb2HeadItem * owner = parent->parent()) {
  208. return createIndex(owner->index(parent), 0, (void*)parent);
  209. } else {
  210. return createIndex(0, 0, (void*)parent);
  211. }
  212. }
  213. }
  214. return QModelIndex();
  215. }
  216. int Fb2HeadModel::rowCount(const QModelIndex &parent) const
  217. {
  218. if (parent.column() > 0) return 0;
  219. if (!parent.isValid()) return m_root ? 1 : 0;
  220. Fb2HeadItem *owner = item(parent);
  221. return owner ? owner->count() : 0;
  222. }
  223. QVariant Fb2HeadModel::data(const QModelIndex &index, int role) const
  224. {
  225. if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
  226. Fb2HeadItem * i = item(index);
  227. return i ? i->text(index.column()) : QVariant();
  228. }
  229. QVariant Fb2HeadModel::headerData(int section, Qt::Orientation orientation, int role) const
  230. {
  231. if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
  232. switch (section) {
  233. case 0: return tr("Key");
  234. case 1: return tr("Value");
  235. }
  236. }
  237. return QVariant();
  238. }
  239. void Fb2HeadModel::select(const QModelIndex &index)
  240. {
  241. Fb2HeadItem *node = item(index);
  242. if (!node || node->id().isEmpty()) return;
  243. m_view.page()->mainFrame()->scrollToAnchor(node->id());
  244. }
  245. void Fb2HeadItem::setText(const QString &text)
  246. {
  247. m_text = text;
  248. }
  249. bool Fb2HeadModel::setData(const QModelIndex &index, const QVariant &value, int role)
  250. {
  251. if (role != Qt::EditRole) return false;
  252. Fb2HeadItem * i = item(index);
  253. if (!i) return false;
  254. i->setText(value.toString());
  255. emit dataChanged(index, index);
  256. return true;
  257. }
  258. Qt::ItemFlags Fb2HeadModel::flags(const QModelIndex &index) const
  259. {
  260. if (!index.isValid()) return 0;
  261. Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  262. if (index.column() == 1) flags |= Qt::ItemIsEditable;
  263. return flags;
  264. }
  265. //---------------------------------------------------------------------------
  266. // Fb2TreeView
  267. //---------------------------------------------------------------------------
  268. Fb2HeadView::Fb2HeadView(Fb2WebView &view, QWidget *parent)
  269. : QTreeView(parent)
  270. , m_view(view)
  271. {
  272. QAction * act;
  273. actionInsert = act = new QAction(FB2::icon("list-add"), tr("&Append"), this);
  274. act->setPriority(QAction::LowPriority);
  275. act->setShortcuts(QKeySequence::New);
  276. actionModify = act = new QAction(FB2::icon("list-add"), tr("&Modify"), this);
  277. act->setPriority(QAction::LowPriority);
  278. actionDelete = act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
  279. act->setPriority(QAction::LowPriority);
  280. act->setShortcuts(QKeySequence::Delete);
  281. //setItemDelegate(new QItemDelegate(this));
  282. setSelectionBehavior(QAbstractItemView::SelectItems);
  283. setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
  284. connect(&m_view, SIGNAL(loadFinished(bool)), SLOT(updateTree()));
  285. connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
  286. header()->setDefaultSectionSize(200);
  287. connect(actionModify, SIGNAL(triggered()), SLOT(editCurrent()));
  288. }
  289. void Fb2HeadView::initToolbar(QToolBar &toolbar)
  290. {
  291. toolbar.addSeparator();
  292. toolbar.addAction(actionInsert);
  293. toolbar.addAction(actionDelete);
  294. }
  295. void Fb2HeadView::updateTree()
  296. {
  297. Fb2HeadModel * model = new Fb2HeadModel(m_view, this);
  298. setModel(model);
  299. model->expand(this);
  300. }
  301. void Fb2HeadView::editCurrent()
  302. {
  303. QModelIndex current = currentIndex();
  304. if (!current.isValid()) return;
  305. Fb2HeadModel * m = static_cast<Fb2HeadModel*>(model());
  306. QModelIndex index = m->index(current.row(), 1, m->parent(current));
  307. setCurrentIndex(index);
  308. edit(index);
  309. }
  310. void Fb2HeadView::activated(const QModelIndex &index)
  311. {
  312. if (index.isValid() && index.column() == 1) edit(index);
  313. }