fb2head.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. #include "fb2head.hpp"
  2. #include <QtDebug>
  3. #include <QAction>
  4. #include <QComboBox>
  5. #include <QDialogButtonBox>
  6. #include <QFormLayout>
  7. #include <QGridLayout>
  8. #include <QHeaderView>
  9. #include <QLabel>
  10. #include <QLineEdit>
  11. #include <QToolBar>
  12. #include <QWebFrame>
  13. #include <QWebPage>
  14. #include <QWebView>
  15. #include <QItemDelegate>
  16. #include <QTreeView>
  17. #include "fb2text.hpp"
  18. #include "fb2utils.h"
  19. //---------------------------------------------------------------------------
  20. // FbScheme::Fb
  21. //---------------------------------------------------------------------------
  22. FbScheme::FbDom::FbDom()
  23. {
  24. QFile file(":/fb2/FictionBook2.1.xsd");
  25. if (file.open(QIODevice::ReadOnly)) setContent(&file);
  26. }
  27. //---------------------------------------------------------------------------
  28. // FbScheme
  29. //---------------------------------------------------------------------------
  30. const QDomDocument & FbScheme::fb2()
  31. {
  32. static const FbDom doc;
  33. return doc;
  34. }
  35. FB2_BEGIN_KEYHASH(FbScheme)
  36. FB2_KEY( XsElement , "xs:element" );
  37. FB2_KEY( XsChoice , "xs:choice" );
  38. FB2_KEY( XsComplexType , "xs:complexType" );
  39. FB2_KEY( XsSequence , "xs:sequence" );
  40. FB2_END_KEYHASH
  41. void FbScheme::items(QStringList &list) const
  42. {
  43. FbScheme child = typeScheme().firstChildElement();
  44. while (!child.isNull()) {
  45. switch (toKeyword(child.tagName())) {
  46. case XsElement: {
  47. QString name = child.attribute("name");
  48. if (!list.contains(name)) list << name;
  49. } break;
  50. case XsChoice:
  51. case XsComplexType:
  52. case XsSequence: {
  53. child.items(list);
  54. } break;
  55. default: ;
  56. }
  57. child = child.nextSiblingElement();
  58. }
  59. }
  60. bool FbScheme::canEdit() const
  61. {
  62. if (type() == "sequenceType") return true;
  63. FbScheme scheme = typeScheme();
  64. FbScheme child = scheme.firstChildElement();
  65. while (!child.isNull()) {
  66. switch (toKeyword(child.tagName())) {
  67. case XsElement:
  68. return false;
  69. case XsChoice:
  70. case XsComplexType:
  71. case XsSequence:
  72. if (!child.canEdit()) return false;
  73. default: ;
  74. }
  75. child = child.nextSiblingElement();
  76. }
  77. return true;
  78. }
  79. FbScheme FbScheme::typeScheme() const
  80. {
  81. QString typeName = type();
  82. if (typeName.isEmpty()) return *this;
  83. FbScheme child = fb2().firstChildElement("xs:schema").firstChildElement();
  84. while (!child.isNull()) {
  85. if (child.tagName() == "xs:complexType") {
  86. if (child.attribute("name") == typeName) return child.element(typeName);
  87. }
  88. child = child.nextSiblingElement();
  89. }
  90. return FbScheme();
  91. }
  92. QString FbScheme::info() const
  93. {
  94. QDomElement element = *this;
  95. if (element.isNull()) return QString();
  96. element = element.firstChildElement("xs:annotation");
  97. if (element.isNull()) return QString();
  98. element = element.firstChildElement("xs:documentation");
  99. if (element.isNull()) return QString();
  100. return element.text();
  101. }
  102. QString FbScheme::type() const
  103. {
  104. if (isNull()) return QString();
  105. QString result = attribute("type");
  106. if (!result.isEmpty()) return result;
  107. FbScheme child = firstChildElement("xs:complexType").firstChildElement();
  108. QString tag;
  109. while (!child.isNull()) {
  110. tag = child.tagName();
  111. if (tag == "xs:complexContent" || tag == "xs:simpleContent") {
  112. return child.firstChildElement("xs:extension").attribute("base");
  113. }
  114. child = child.nextSiblingElement();
  115. }
  116. return QString();
  117. }
  118. FbScheme FbScheme::item(const QString &name) const
  119. {
  120. FbScheme child = firstChildElement();
  121. while (!child.isNull()) {
  122. switch (toKeyword(child.tagName())) {
  123. case XsElement: {
  124. if (child.attribute("name") == name) return child;
  125. } break;
  126. case XsChoice:
  127. case XsComplexType:
  128. case XsSequence: {
  129. FbScheme result = child.item(name);
  130. if (!result.isNull()) return result;
  131. } break;
  132. default: ;
  133. }
  134. child = child.nextSiblingElement();
  135. }
  136. return FbScheme();
  137. }
  138. FbScheme FbScheme::element(const QString &name) const
  139. {
  140. FbScheme parent = *this;
  141. if (parent.isNull()) {
  142. parent = fb2().documentElement();
  143. parent = parent.element("FictionBook");
  144. }
  145. FbScheme child = parent.item(name);
  146. if (!child.isNull()) return child;
  147. QString type = this->type();
  148. if (type.isEmpty()) return *this;
  149. child = fb2().firstChildElement("xs:schema").firstChildElement();
  150. while (!child.isNull()) {
  151. if (child.tagName() == "xs:complexType") {
  152. if (child.attribute("name") == type) return child.element(name);
  153. }
  154. child = child.nextSiblingElement();
  155. }
  156. return FbScheme();
  157. }
  158. //---------------------------------------------------------------------------
  159. // FbHeadItem
  160. //---------------------------------------------------------------------------
  161. FbHeadItem::HintHash::HintHash()
  162. {
  163. insert( "title-info" , tr( "Book" ));
  164. insert( "document-info" , tr( "File" ));
  165. insert( "publish-info" , tr( "Publish" ));
  166. insert( "custom-info" , tr( "Add-ons" ));
  167. insert( "genre" , tr( "Genre" ));
  168. insert( "author" , tr( "Author" ));
  169. insert( "book-title" , tr( "Title" ));
  170. insert( "annotation" , tr( "Annotation" ));
  171. insert( "coverpage" , tr( "Cover" ));
  172. insert( "program-used" , tr( "Editor" ));
  173. insert( "date" , tr( "Date" ));
  174. insert( "lang" , tr( "Language" ));
  175. insert( "translator" , tr( "Translator" ));
  176. insert( "sequence" , tr( "Sequence" ));
  177. insert( "first-name" , tr( "First name" ));
  178. insert( "middle-name" , tr( "Middle name" ));
  179. insert( "last-name" , tr( "Last name" ));
  180. insert( "history" , tr( "History" ));
  181. }
  182. FB2_BEGIN_KEYHASH(FbHeadItem)
  183. FB2_KEY( Genr , "genre" );
  184. FB2_KEY( Auth , "author" );
  185. FB2_KEY( Date , "date" );
  186. FB2_KEY( Cover , "coverpage" );
  187. FB2_KEY( Image , "image" );
  188. FB2_KEY( Seqn , "sequence" );
  189. FB2_END_KEYHASH
  190. FbHeadItem::FbHeadItem(QWebElement &element, FbHeadItem *parent)
  191. : QObject(parent)
  192. , m_element(element)
  193. , m_parent(parent)
  194. {
  195. m_name = element.tagName().toLower();
  196. if (m_name.left(3) == "fb:") {
  197. m_name = m_name.mid(3);
  198. if (m_name == "annotation") return;
  199. if (m_name == "history") return;
  200. } else if (m_name == "img") {
  201. m_name = "image";
  202. }
  203. addChildren(element);
  204. }
  205. FbHeadItem::~FbHeadItem()
  206. {
  207. for (FbHeadItem * item: m_list) {
  208. delete item;
  209. }
  210. }
  211. FbHeadItem * FbHeadItem::append(const QString name)
  212. {
  213. m_element.appendInside(QString("<fb:%1></fb:%1>").arg(name));
  214. QWebElement element = m_element.lastChild();
  215. if (name == "annotation" || name == "history") {
  216. element.appendInside("<p><br></p>");
  217. }
  218. FbHeadItem * child = new FbHeadItem(element, this);
  219. m_list << child;
  220. return child;
  221. }
  222. void FbHeadItem::addChildren(QWebElement &parent)
  223. {
  224. QWebElement child = parent.firstChild();
  225. while (!child.isNull()) {
  226. QString tag = child.tagName().toLower();
  227. if (tag.left(3) == "fb:") {
  228. m_list << new FbHeadItem(child, this);
  229. } else if (tag == "img") {
  230. m_list << new FbHeadItem(child, this);
  231. } else {
  232. addChildren(child);
  233. }
  234. child = child.nextSibling();
  235. }
  236. }
  237. FbHeadItem * FbHeadItem::item(const QModelIndex &index) const
  238. {
  239. int row = index.row();
  240. if (row < 0 || row >= m_list.size()) return NULL;
  241. return m_list[row];
  242. }
  243. FbHeadItem * FbHeadItem::item(int row) const
  244. {
  245. if (row < 0 || row >= m_list.size()) return NULL;
  246. return m_list[row];
  247. }
  248. QString FbHeadItem::data(int col) const
  249. {
  250. switch (col) {
  251. case 0: return QString("<%1> %2").arg(m_name).arg(hint());
  252. case 1: return value();
  253. case 2: return extra();
  254. case 3: return scheme().info();
  255. case 4: return scheme().type();
  256. case 5: return scheme().canEdit() ? "Yes" : "No";
  257. case 6: return scheme().attribute("minOccurs");
  258. case 7: return scheme().attribute("maxOccurs");
  259. }
  260. return QString();
  261. }
  262. void FbHeadItem::setData(const QString &text, int col)
  263. {
  264. switch (col) {
  265. case 1: return setValue(text);
  266. case 2: return setExtra(text);
  267. }
  268. }
  269. QString FbHeadItem::hint() const
  270. {
  271. static HintHash hints;
  272. HintHash::const_iterator it = hints.find(m_name);
  273. if (it == hints.end()) return QString();
  274. return it.value();
  275. }
  276. QString FbHeadItem::value() const
  277. {
  278. switch (toKeyword(m_name)) {
  279. case Auth: {
  280. QString result = sub("last-name");
  281. result += " " + sub("first-name");
  282. result += " " + sub("middle-name");
  283. return result.simplified();
  284. } break;
  285. case Cover: {
  286. QString text;
  287. for (FbHeadItem * item: m_list) {
  288. if (item->m_name == "image") {
  289. if (!text.isEmpty()) text += ", ";
  290. text += item->value();
  291. }
  292. }
  293. return text;
  294. } break;
  295. case Image: {
  296. return m_element.attribute("src");
  297. } break;
  298. case Seqn: {
  299. return m_element.attribute("name");
  300. } break;
  301. default: ;
  302. }
  303. if (m_list.count()) return QString();
  304. return m_element.toPlainText().simplified();
  305. }
  306. QString FbHeadItem::extra() const
  307. {
  308. switch (toKeyword(m_name)) {
  309. case Genr: return m_element.attribute("match");
  310. case Seqn: return m_element.attribute("number");
  311. case Date: return m_element.attribute("value");
  312. default: return QString();
  313. }
  314. }
  315. void FbHeadItem::setValue(const QString &text)
  316. {
  317. switch (toKeyword(m_name)) {
  318. case Seqn: m_element.setAttribute("name", text); break;
  319. default: m_element.setPlainText(text);
  320. }
  321. }
  322. void FbHeadItem::setExtra(const QString &text)
  323. {
  324. switch (toKeyword(m_name)) {
  325. case Date: m_element.setAttribute("value", text); break;
  326. case Genr: m_element.setAttribute("match", text); break;
  327. case Seqn: m_element.setAttribute("number", text); break;
  328. default: ;
  329. }
  330. }
  331. bool FbHeadItem::canEditExtra() const
  332. {
  333. switch (toKeyword(m_name)) {
  334. case Date:
  335. case Genr:
  336. case Seqn:
  337. return true;
  338. default:
  339. return false;
  340. }
  341. }
  342. QString FbHeadItem::sub(const QString &key) const
  343. {
  344. for (FbHeadItem * item: m_list) {
  345. if (item->m_name == key) return item->value();
  346. }
  347. return QString();
  348. }
  349. FbScheme FbHeadItem::scheme() const
  350. {
  351. FbScheme parent = m_parent ? m_parent->scheme() : FbScheme();
  352. return parent.element(m_name);
  353. }
  354. void FbHeadItem::remove(int row)
  355. {
  356. if (row < 0 || row >= count()) return;
  357. m_list[row]->m_element.removeFromDocument();
  358. m_list.removeAt(row);
  359. }
  360. //---------------------------------------------------------------------------
  361. // FbHeadModel
  362. //---------------------------------------------------------------------------
  363. FbHeadModel::FbHeadModel(QWebView &view, QObject *parent)
  364. : QAbstractItemModel(parent)
  365. , m_view(view)
  366. , m_root(NULL)
  367. {
  368. QWebElement doc = view.page()->mainFrame()->documentElement();
  369. QWebElement head = doc.findFirst("fb\\:description");
  370. if (head.isNull()) return;
  371. m_root = new FbHeadItem(head);
  372. }
  373. FbHeadModel::~FbHeadModel()
  374. {
  375. if (m_root) delete m_root;
  376. }
  377. void FbHeadModel::expand(QTreeView *view)
  378. {
  379. QModelIndex parent = QModelIndex();
  380. int count = rowCount(parent);
  381. for (int i = 0; i < count; ++i) {
  382. QModelIndex child = index(i, 0, parent);
  383. FbHeadItem *node = item(child);
  384. if (!node) continue;
  385. view->expand(child);
  386. int count = rowCount(child);
  387. for (int j = 0; j < count; ++j) {
  388. QModelIndex ch = index(j, 0, child);
  389. FbHeadItem *node = item(ch);
  390. if (node) view->expand(ch);
  391. }
  392. }
  393. }
  394. FbHeadItem * FbHeadModel::item(const QModelIndex &index) const
  395. {
  396. if (index.isValid()) {
  397. return static_cast<FbHeadItem*>(index.internalPointer());
  398. } else {
  399. return 0;
  400. }
  401. }
  402. int FbHeadModel::columnCount(const QModelIndex &parent) const
  403. {
  404. Q_UNUSED(parent);
  405. #ifdef QT_DEBUG
  406. return 8;
  407. #else
  408. return 3;
  409. #endif
  410. }
  411. QModelIndex FbHeadModel::index(int row, int column, const QModelIndex &parent) const
  412. {
  413. if (!m_root || row < 0 || column < 0) return QModelIndex();
  414. if (!parent.isValid() && m_root) {
  415. return createIndex(row, column, (void*)m_root);
  416. }
  417. if (FbHeadItem *owner = item(parent)) {
  418. if (FbHeadItem *child = owner->item(row)) {
  419. return createIndex(row, column, (void*)child);
  420. }
  421. }
  422. return QModelIndex();
  423. }
  424. QModelIndex FbHeadModel::parent(const QModelIndex &child) const
  425. {
  426. if (FbHeadItem * node = static_cast<FbHeadItem*>(child.internalPointer())) {
  427. if (FbHeadItem * parent = node->parent()) {
  428. if (FbHeadItem * owner = parent->parent()) {
  429. return createIndex(owner->index(parent), 0, (void*)parent);
  430. } else {
  431. return createIndex(0, 0, (void*)parent);
  432. }
  433. }
  434. }
  435. return QModelIndex();
  436. }
  437. int FbHeadModel::rowCount(const QModelIndex &parent) const
  438. {
  439. if (parent.column() > 0) return 0;
  440. if (!parent.isValid()) return m_root ? 1 : 0;
  441. FbHeadItem *owner = item(parent);
  442. return owner ? owner->count() : 0;
  443. }
  444. QVariant FbHeadModel::data(const QModelIndex &index, int role) const
  445. {
  446. if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
  447. FbHeadItem * i = item(index);
  448. return i ? i->data(index.column()) : QVariant();
  449. }
  450. QVariant FbHeadModel::headerData(int section, Qt::Orientation orientation, int role) const
  451. {
  452. if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
  453. switch (section) {
  454. case 0: return tr("Key");
  455. case 1: return tr("Value");
  456. case 2: return tr("Extra");
  457. #ifdef QT_DEBUG
  458. case 3: return tr("Info");
  459. case 4: return tr("Type");
  460. case 5: return tr("Can edit");
  461. case 6: return tr("Min");
  462. case 7: return tr("Max");
  463. #endif
  464. }
  465. }
  466. return QVariant();
  467. }
  468. bool FbHeadModel::setData(const QModelIndex &index, const QVariant &value, int role)
  469. {
  470. if (role != Qt::EditRole) return false;
  471. if (FbHeadItem * i = item(index)) {
  472. i->setData(value.toString(), index.column());
  473. emit dataChanged(index, index);
  474. return true;
  475. }
  476. return true;
  477. }
  478. Qt::ItemFlags FbHeadModel::flags(const QModelIndex &index) const
  479. {
  480. if (!index.isValid()) return Qt::NoItemFlags;
  481. Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  482. if (canEdit(index)) flags |= Qt::ItemIsEditable;
  483. return flags;
  484. }
  485. bool FbHeadModel::canEdit(const QModelIndex &index) const
  486. {
  487. switch (index.column()) {
  488. case 1: if (FbHeadItem * i = item(index)) return i->canEdit(); break;
  489. case 2: if (FbHeadItem * i = item(index)) return i->canEditExtra(); break;
  490. }
  491. return false;
  492. }
  493. //---------------------------------------------------------------------------
  494. // FbTreeView
  495. //---------------------------------------------------------------------------
  496. FbHeadEdit::FbHeadEdit(QWidget *parent, FbTextEdit *text)
  497. : QTreeView(parent)
  498. , m_text(text)
  499. {
  500. QAction * act;
  501. setContextMenuPolicy(Qt::ActionsContextMenu);
  502. setStyleSheet("show-decoration-selected: 1;");
  503. actionInsert = act = new QAction(FbIcon("list-add"), tr("&Append"), this);
  504. act->setShortcutContext(Qt::WidgetShortcut);
  505. act->setShortcut(Qt::Key_Insert);
  506. act->setPriority(QAction::LowPriority);
  507. connect(act, SIGNAL(triggered()), SLOT(appendNode()));
  508. addAction(act);
  509. actionModify = act = new QAction(FbIcon("list-add"), tr("&Modify"), this);
  510. act->setPriority(QAction::LowPriority);
  511. actionDelete = act = new QAction(FbIcon("list-remove"), tr("&Delete"), this);
  512. act->setShortcutContext(Qt::WidgetShortcut);
  513. act->setShortcut(Qt::Key_Delete);
  514. act->setPriority(QAction::LowPriority);
  515. connect(act, SIGNAL(triggered()), SLOT(removeNode()));
  516. addAction(act);
  517. //setItemDelegate(new QItemDelegate(this));
  518. setRootIsDecorated(false);
  519. setSelectionBehavior(QAbstractItemView::SelectItems);
  520. setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
  521. connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
  522. connect(this, SIGNAL(collapsed(QModelIndex)), SLOT(collapsed(QModelIndex)));
  523. header()->setDefaultSectionSize(200);
  524. }
  525. void FbHeadEdit::setAction(Fb::Actions index, QAction *action)
  526. {
  527. m_actions[index] = action;
  528. }
  529. void FbHeadEdit::connectActions(QToolBar *tool)
  530. {
  531. tool->clear();
  532. tool->addSeparator();
  533. tool->addAction(actionInsert);
  534. tool->addAction(actionDelete);
  535. }
  536. void FbHeadEdit::disconnectActions()
  537. {
  538. m_actions.disconnect();
  539. }
  540. FbHeadModel * FbHeadEdit::model() const
  541. {
  542. return qobject_cast<FbHeadModel*>(QTreeView::model());
  543. }
  544. void FbHeadEdit::updateTree()
  545. {
  546. FbHeadModel * model = new FbHeadModel(*m_text, this);
  547. setModel(model);
  548. model->expand(this);
  549. }
  550. void FbHeadEdit::editCurrent(const QModelIndex &index)
  551. {
  552. FbHeadModel * m = qobject_cast<FbHeadModel*>(model());
  553. if (!m) return;
  554. FbHeadItem * item = m->item(index);
  555. if (!item) return;
  556. FbNodeEditDlg dlg(this, item->scheme(), item->element());
  557. if (dlg.exec()) {
  558. //TODO
  559. }
  560. }
  561. void FbHeadEdit::activated(const QModelIndex &index)
  562. {
  563. if (model()->canEdit(index)) edit(index);
  564. showStatus(index);
  565. }
  566. void FbHeadEdit::currentChanged(const QModelIndex &current, const QModelIndex &previous)
  567. {
  568. QTreeView::currentChanged(current, previous);
  569. showStatus(current);
  570. }
  571. void FbHeadEdit::showStatus(const QModelIndex &current)
  572. {
  573. if (!model()) return;
  574. if (!current.isValid()) return;
  575. QModelIndex parent = model()->parent(current);
  576. QModelIndex index = model()->index(current.row(), 3, parent);
  577. emit status(model()->data(index).toString());
  578. }
  579. void FbHeadEdit::collapsed(const QModelIndex &index)
  580. {
  581. if (model() && !model()->parent(index).isValid()) {
  582. expand(index);
  583. }
  584. }
  585. void FbHeadEdit::appendNode()
  586. {
  587. FbHeadModel * m = qobject_cast<FbHeadModel*>(model());
  588. if (!m) return;
  589. QModelIndex current = currentIndex();
  590. FbHeadItem * item = m->item(current);
  591. if (!item) return;
  592. QString name = item->name().toLower();
  593. if (name == "annotation" || name == "history") {
  594. current = m->parent(current);
  595. item = m->item(current);
  596. }
  597. QStringList list;
  598. item->scheme().items(list);
  599. if (list.count() == 0) {
  600. current = m->parent(current);
  601. item = m->item(current);
  602. if (!item) return;
  603. item->scheme().items(list);
  604. }
  605. FbNodeDlg dlg(this, item->scheme(), list);
  606. if (dlg.exec()) {
  607. QModelIndex child = m->append(current, dlg.value());
  608. if (child.isValid()) {
  609. expand(current);
  610. setCurrentIndex(child);
  611. scrollTo(child);
  612. }
  613. }
  614. }
  615. void FbHeadEdit::removeNode()
  616. {
  617. FbHeadModel * m = qobject_cast<FbHeadModel*>(model());
  618. if (m) m->remove(currentIndex());
  619. }
  620. QModelIndex FbHeadModel::append(const QModelIndex &parent, const QString &name)
  621. {
  622. FbHeadItem * owner = item(parent);
  623. if (!owner) return QModelIndex();
  624. int row = owner->count();
  625. beginInsertRows(parent, row, row);
  626. FbHeadItem * item = owner->append(name);
  627. endInsertRows();
  628. return createIndex(row, 0, (void*)item);
  629. }
  630. void FbHeadModel::remove(const QModelIndex &index)
  631. {
  632. int r = index.row();
  633. QModelIndex p = parent(index);
  634. beginRemoveRows(p, r, r + 1);
  635. FbHeadItem * i = item(p);
  636. if (i) i->remove(r);
  637. endRemoveRows();
  638. }
  639. //---------------------------------------------------------------------------
  640. // FbNodeDlg
  641. //---------------------------------------------------------------------------
  642. FbNodeDlg::FbNodeDlg(QWidget *parent, const FbScheme &scheme, const QStringList &list)
  643. : QDialog(parent)
  644. , m_scheme(scheme)
  645. {
  646. setWindowTitle(tr("Insert tag"));
  647. QGridLayout * layout = new QGridLayout(this);
  648. QLabel * label = new QLabel(this);
  649. label->setText(tr("Tag name:"));
  650. layout->addWidget(label, 0, 0, 1, 1);
  651. m_combo = new QComboBox(this);
  652. m_combo->setEditable(true);
  653. m_combo->addItems(list);
  654. layout->addWidget(m_combo, 0, 1, 1, 1);
  655. m_text = new QLabel(this);
  656. m_text->setStyleSheet(QString::fromUtf8("border-style:outset;border-width:1px;border-radius:10px;padding:6px;"));
  657. m_text->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop);
  658. m_text->setMinimumSize(QSize(300, 100));
  659. m_text->setWordWrap(true);
  660. layout->addWidget(m_text, 1, 0, 1, 2);
  661. QDialogButtonBox * buttons = new QDialogButtonBox(this);
  662. buttons->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
  663. buttons->setOrientation(Qt::Horizontal);
  664. layout->addWidget(buttons, 2, 0, 1, 2);
  665. layout->setColumnStretch(1, 1);
  666. connect(m_combo, SIGNAL(editTextChanged(QString)), SLOT(comboChanged(QString)));
  667. connect(buttons, SIGNAL(accepted()), SLOT(accept()));
  668. connect(buttons, SIGNAL(rejected()), SLOT(reject()));
  669. if (list.count()) {
  670. m_combo->setCurrentIndex(0);
  671. comboChanged(list.first());
  672. }
  673. }
  674. void FbNodeDlg::comboChanged(const QString &text)
  675. {
  676. m_text->setText(m_scheme.element(text).info());
  677. }
  678. QString FbNodeDlg::value() const
  679. {
  680. return m_combo->currentText();
  681. }
  682. //---------------------------------------------------------------------------
  683. // FbNodeEditDlg
  684. //---------------------------------------------------------------------------
  685. FbNodeEditDlg::FbNodeEditDlg(QWidget *parent, const FbScheme &scheme, const QWebElement &element)
  686. : QDialog(parent)
  687. , m_scheme(scheme)
  688. , m_element(element)
  689. {
  690. setWindowTitle(tr("Modify tag"));
  691. QGridLayout * layout = new QGridLayout(this);
  692. QLabel *label = new QLabel(this);
  693. label->setText(tr("Value:"));
  694. layout->addWidget(label, 0, 0, 1, 1);
  695. QLineEdit *edit = new QLineEdit(this);
  696. layout->addWidget(edit, 0, 1, 1, 1);
  697. QDialogButtonBox * buttons = new QDialogButtonBox(this);
  698. buttons->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
  699. buttons->setOrientation(Qt::Horizontal);
  700. layout->addWidget(buttons, 2, 0, 1, 2);
  701. layout->setColumnStretch(1, 1);
  702. connect(buttons, SIGNAL(accepted()), SLOT(accept()));
  703. connect(buttons, SIGNAL(rejected()), SLOT(reject()));
  704. }
  705. //---------------------------------------------------------------------------
  706. // FbAuthorDlg
  707. //---------------------------------------------------------------------------
  708. FbAuthorDlg::FbAuthorDlg(QWidget *parent)
  709. : QDialog(parent)
  710. {
  711. setWindowTitle(tr("Author"));
  712. QFormLayout * layout = new QFormLayout(this);
  713. add(layout, "last-name", tr("Last name"));
  714. add(layout, "first-name", tr("First name"));
  715. add(layout, "moddle-name", tr("Middle name"));
  716. add(layout, "nickname", tr("Nic name"));
  717. add(layout, "home-page", tr("Home page"));
  718. add(layout, "email", tr("E-mail"));
  719. QDialogButtonBox * buttonBox = new QDialogButtonBox(this);
  720. buttonBox->setOrientation(Qt::Horizontal);
  721. buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
  722. layout->addRow(buttonBox);
  723. QObject::connect(buttonBox, SIGNAL(accepted()), SLOT(accept()));
  724. QObject::connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));
  725. }
  726. void FbAuthorDlg::add(QFormLayout *layout, const QString &key, const QString &text)
  727. {
  728. QLabel * label = new QLabel(this);
  729. label->setText(text);
  730. QLineEdit * field = new QLineEdit(this);
  731. field->setMinimumWidth(200);
  732. m_fields[key] = field;
  733. layout->addRow(label, field);
  734. }