1
0

fb2main.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. #include <QtGui>
  2. #include <QtDebug>
  3. #include <QTreeView>
  4. #include <QWebInspector>
  5. #include <QWebFrame>
  6. #include "fb2main.hpp"
  7. #include "fb2code.hpp"
  8. #include "fb2read.hpp"
  9. #include "fb2tree.hpp"
  10. #include "fb2view.hpp"
  11. #include "fb2head.hpp"
  12. #define FB2DELETE(p) { if ( (p) != NULL ) { delete p; p = NULL; } }
  13. Fb2MainWindow::Fb2MainWindow()
  14. {
  15. init();
  16. setCurrentFile();
  17. viewText();
  18. textEdit->load(":blank.fb2");
  19. }
  20. Fb2MainWindow::Fb2MainWindow(const QString &filename, ViewMode mode)
  21. {
  22. init();
  23. setCurrentFile(filename);
  24. if (mode == FB2) {
  25. viewText();
  26. textEdit->load(filename);
  27. } else {
  28. viewCode();
  29. loadXML(filename);
  30. }
  31. }
  32. void Fb2MainWindow::init()
  33. {
  34. connect(qApp, SIGNAL(logMessage(QString)), SLOT(logMessage(QString)));
  35. setAttribute(Qt::WA_DeleteOnClose);
  36. setWindowIcon(QIcon(":icon.ico"));
  37. isUntitled = true;
  38. createActions();
  39. createStatusBar();
  40. textEdit = NULL;
  41. noteEdit = NULL;
  42. codeEdit = NULL;
  43. treeView = NULL;
  44. headTree = NULL;
  45. toolEdit = NULL;
  46. messageEdit = NULL;
  47. readSettings();
  48. setUnifiedTitleAndToolBarOnMac(true);
  49. }
  50. void Fb2MainWindow::logMessage(const QString &message)
  51. {
  52. if (!messageEdit) {
  53. messageEdit = new QTextEdit(this);
  54. connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
  55. QDockWidget * dock = new QDockWidget(tr("Message log"), this);
  56. dock->setAttribute(Qt::WA_DeleteOnClose);
  57. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  58. dock->setWidget(messageEdit);
  59. addDockWidget(Qt::BottomDockWidgetArea, dock);
  60. messageEdit->setMaximumHeight(80);
  61. }
  62. messageEdit->append(message);
  63. }
  64. void Fb2MainWindow::logShowed()
  65. {
  66. messageEdit->setMaximumHeight(QWIDGETSIZE_MAX);
  67. }
  68. void Fb2MainWindow::logDestroyed()
  69. {
  70. messageEdit = NULL;
  71. }
  72. void Fb2MainWindow::treeActivated(const QModelIndex &index)
  73. {
  74. if (!treeView) return;
  75. Fb2TreeModel *model = dynamic_cast<Fb2TreeModel*>(treeView->model());
  76. if (model) model->select(index);
  77. }
  78. void Fb2MainWindow::treeDestroyed()
  79. {
  80. treeView = NULL;
  81. dockTree = NULL;
  82. }
  83. bool Fb2MainWindow::loadXML(const QString &filename)
  84. {
  85. if (!filename.isEmpty()) {
  86. QFile file(filename);
  87. if (file.open(QFile::ReadOnly | QFile::Text)) {
  88. codeEdit->clear();
  89. return codeEdit->read(&file);
  90. }
  91. }
  92. return false;
  93. }
  94. void Fb2MainWindow::closeEvent(QCloseEvent *event)
  95. {
  96. if (maybeSave()) {
  97. writeSettings();
  98. event->accept();
  99. } else {
  100. event->ignore();
  101. }
  102. }
  103. void Fb2MainWindow::fileNew()
  104. {
  105. Fb2MainWindow *other = new Fb2MainWindow;
  106. other->move(x() + 40, y() + 40);
  107. other->show();
  108. }
  109. void Fb2MainWindow::fileOpen()
  110. {
  111. QString filename = QFileDialog::getOpenFileName(this, tr("Open file"), QString(), "Fiction book files (*.fb2)");
  112. if (filename.isEmpty()) return;
  113. Fb2MainWindow * existing = findFb2MainWindow(filename);
  114. if (existing) {
  115. existing->show();
  116. existing->raise();
  117. existing->activateWindow();
  118. return;
  119. }
  120. if (textEdit) {
  121. if (isUntitled && !isWindowModified()) {
  122. setCurrentFile(filename);
  123. textEdit->load(filename);
  124. } else {
  125. Fb2MainWindow * other = new Fb2MainWindow(filename, FB2);
  126. other->move(x() + 40, y() + 40);
  127. other->show();
  128. }
  129. } else if (codeEdit) {
  130. if (isUntitled && !isWindowModified()) {
  131. setCurrentFile(filename);
  132. loadXML(filename);
  133. } else {
  134. Fb2MainWindow * other = new Fb2MainWindow(filename, XML);
  135. other->move(x() + 40, y() + 40);
  136. other->show();
  137. }
  138. }
  139. }
  140. bool Fb2MainWindow::fileSave()
  141. {
  142. if (isUntitled) {
  143. return fileSaveAs();
  144. } else {
  145. return saveFile(curFile);
  146. }
  147. }
  148. bool Fb2MainWindow::fileSaveAs()
  149. {
  150. QString fileName = QFileDialog::getSaveFileName(this, tr("Save As..."), curFile);
  151. if (fileName.isEmpty()) return false;
  152. return saveFile(fileName);
  153. }
  154. void Fb2MainWindow::about()
  155. {
  156. QMessageBox::about(this, tr("About fb2edit"),
  157. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  158. }
  159. void Fb2MainWindow::documentWasModified()
  160. {
  161. if (isWindowModified()) return;
  162. QFileInfo info = windowFilePath();
  163. QString title = info.fileName();
  164. title += QString("[*]");
  165. title += QString(" - ") += qApp->applicationName();
  166. setWindowTitle(title);
  167. setWindowModified(true);
  168. if (codeEdit) disconnect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  169. if (textEdit) disconnect(textEdit->page(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
  170. }
  171. QIcon Fb2MainWindow::icon(const QString &name)
  172. {
  173. QIcon icon;
  174. icon.addFile(QString(":/24x24/%1.png").arg(name), QSize(24,24));
  175. icon.addFile(QString(":/16x24/%1.png").arg(name), QSize(16,16));
  176. return QIcon::fromTheme(name, icon);
  177. }
  178. void Fb2MainWindow::createActions()
  179. {
  180. QAction * act;
  181. QMenu * menu;
  182. QToolBar * tool;
  183. QList<QAction*> actions;
  184. menu = menuBar()->addMenu(tr("&File"));
  185. tool = addToolBar(tr("File"));
  186. tool->setMovable(false);
  187. act = new QAction(icon("document-new"), tr("&New"), this);
  188. act->setPriority(QAction::LowPriority);
  189. act->setShortcuts(QKeySequence::New);
  190. act->setStatusTip(tr("Create a new file"));
  191. connect(act, SIGNAL(triggered()), this, SLOT(fileNew()));
  192. menu->addAction(act);
  193. tool->addAction(act);
  194. act = new QAction(icon("document-open"), tr("&Open..."), this);
  195. act->setShortcuts(QKeySequence::Open);
  196. act->setStatusTip(tr("Open an existing file"));
  197. connect(act, SIGNAL(triggered()), this, SLOT(fileOpen()));
  198. menu->addAction(act);
  199. tool->addAction(act);
  200. act = new QAction(icon("document-save"), tr("&Save"), this);
  201. act->setShortcuts(QKeySequence::Save);
  202. act->setStatusTip(tr("Save the document to disk"));
  203. connect(act, SIGNAL(triggered()), this, SLOT(fileSave()));
  204. menu->addAction(act);
  205. tool->addAction(act);
  206. act = new QAction(icon("document-save-as"), tr("Save &As..."), this);
  207. act->setShortcuts(QKeySequence::SaveAs);
  208. act->setStatusTip(tr("Save the document under a new name"));
  209. connect(act, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
  210. menu->addAction(act);
  211. menu->addSeparator();
  212. act = new QAction(icon("window-close"), tr("&Close"), this);
  213. act->setShortcuts(QKeySequence::Close);
  214. act->setStatusTip(tr("Close this window"));
  215. connect(act, SIGNAL(triggered()), this, SLOT(close()));
  216. menu->addAction(act);
  217. act = new QAction(icon("application-exit"), tr("E&xit"), this);
  218. act->setShortcuts(QKeySequence::Quit);
  219. act->setStatusTip(tr("Exit the application"));
  220. connect(act, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  221. menu->addAction(act);
  222. menuEdit = menu = menuBar()->addMenu(tr("&Edit"));
  223. connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
  224. actionUndo = act = new QAction(icon("edit-undo"), tr("&Undo"), this);
  225. act->setPriority(QAction::LowPriority);
  226. act->setShortcut(QKeySequence::Undo);
  227. act->setEnabled(false);
  228. menu->addAction(act);
  229. actionRedo = act = new QAction(icon("edit-redo"), tr("&Redo"), this);
  230. act->setPriority(QAction::LowPriority);
  231. act->setShortcut(QKeySequence::Redo);
  232. act->setEnabled(false);
  233. menu->addAction(act);
  234. menu->addSeparator();
  235. actionCut = act = new QAction(icon("edit-cut"), tr("Cu&t"), this);
  236. act->setPriority(QAction::LowPriority);
  237. act->setShortcuts(QKeySequence::Cut);
  238. act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  239. act->setEnabled(false);
  240. menu->addAction(act);
  241. actionCopy = act = new QAction(icon("edit-copy"), tr("&Copy"), this);
  242. act->setPriority(QAction::LowPriority);
  243. act->setShortcuts(QKeySequence::Copy);
  244. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  245. act->setEnabled(false);
  246. menu->addAction(act);
  247. actionPaste = act = new QAction(icon("edit-paste"), tr("&Paste"), this);
  248. act->setPriority(QAction::LowPriority);
  249. act->setShortcuts(QKeySequence::Paste);
  250. act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  251. menu->addAction(act);
  252. clipboardDataChanged();
  253. menu->addSeparator();
  254. actionFind = act = new QAction(icon("edit-find"), tr("&Find..."), this);
  255. act->setShortcuts(QKeySequence::Find);
  256. menu->addAction(act);
  257. actionReplace = act = new QAction(icon("edit-find-replace"), tr("&Replace..."), this);
  258. menu->addAction(act);
  259. menu->addSeparator();
  260. actionInsert = act = new QAction(icon("list-add"), tr("&Append"), this);
  261. act->setPriority(QAction::LowPriority);
  262. act->setShortcuts(QKeySequence::New);
  263. menu->addAction(act);
  264. actionDelete = act = new QAction(icon("list-remove"), tr("&Delete"), this);
  265. act->setPriority(QAction::LowPriority);
  266. act->setShortcuts(QKeySequence::Delete);
  267. menu->addAction(act);
  268. menu->addSeparator();
  269. act = new QAction(icon("preferences-desktop"), tr("&Settings"), this);
  270. act->setShortcuts(QKeySequence::Preferences);
  271. act->setStatusTip(tr("Application settings"));
  272. connect(act, SIGNAL(triggered()), SLOT(openSettings()));
  273. menu->addAction(act);
  274. menu = menuBar()->addMenu(tr("&Insert", "Main menu"));
  275. actionImage = act = new QAction(icon("insert-image"), tr("&Image"), this);
  276. menu->addAction(act);
  277. act = new QAction(icon("insert-link"), tr("&Link"), this);
  278. menu->addAction(act);
  279. act = new QAction(icon("insert-object"), tr("&Object"), this);
  280. menu->addAction(act);
  281. act = new QAction(icon("insert-text"), tr("&Text"), this);
  282. menu->addAction(act);
  283. menuText = menu = menuBar()->addMenu(tr("Fo&rmat"));
  284. actionTextBold = act = new QAction(icon("format-text-bold"), tr("Bold"), this);
  285. act->setShortcuts(QKeySequence::Bold);
  286. act->setCheckable(true);
  287. menu->addAction(act);
  288. actionTextItalic = act = new QAction(icon("format-text-italic"), tr("Italic"), this);
  289. act->setShortcuts(QKeySequence::Italic);
  290. act->setCheckable(true);
  291. menu->addAction(act);
  292. actionTextStrike = act = new QAction(icon("format-text-strikethrough"), tr("Strikethrough"), this);
  293. act->setCheckable(true);
  294. menu->addAction(act);
  295. actionTextSup = act = new QAction(icon("format-text-superscript"), tr("Superscript"), this);
  296. act->setCheckable(true);
  297. menu->addAction(act);
  298. actionTextSub = act = new QAction(icon("format-text-subscript"), tr("Subscript"), this);
  299. act->setCheckable(true);
  300. menu->addAction(act);
  301. menuView = menu = menuBar()->addMenu(tr("&View"));
  302. tool->addSeparator();
  303. QActionGroup * viewGroup = new QActionGroup(this);
  304. act = new QAction(tr("&Text"), this);
  305. act->setCheckable(true);
  306. act->setChecked(true);
  307. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  308. viewGroup->addAction(act);
  309. menu->addAction(act);
  310. tool->addAction(act);
  311. act = new QAction(tr("&Head"), this);
  312. act->setCheckable(true);
  313. connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
  314. viewGroup->addAction(act);
  315. menu->addAction(act);
  316. tool->addAction(act);
  317. act = new QAction(tr("&XML"), this);
  318. act->setCheckable(true);
  319. connect(act, SIGNAL(triggered()), this, SLOT(viewCode()));
  320. viewGroup->addAction(act);
  321. menu->addAction(act);
  322. tool->addAction(act);
  323. menu->addSeparator();
  324. actionZoomIn = act = new QAction(icon("zoom-in"), tr("Zoom in"), this);
  325. act->setShortcuts(QKeySequence::ZoomIn);
  326. menu->addAction(act);
  327. actionZoomOut = act = new QAction(icon("zoom-out"), tr("Zoom out"), this);
  328. act->setShortcuts(QKeySequence::ZoomOut);
  329. menu->addAction(act);
  330. actionZoomOrig = act = new QAction(icon("zoom-original"), tr("Zoom original"), this);
  331. menu->addAction(act);
  332. menu->addSeparator();
  333. act = new QAction(tr("&Contents"), this);
  334. connect(act, SIGNAL(triggered()), this, SLOT(viewTree()));
  335. menu->addAction(act);
  336. act = new QAction(tr("&Web inspector"), this);
  337. connect(act, SIGNAL(triggered()), this, SLOT(showInspector()));
  338. menu->addAction(act);
  339. menuBar()->addSeparator();
  340. menu = menuBar()->addMenu(tr("&Help"));
  341. act = new QAction(icon("help-about"), tr("&About"), this);
  342. act->setStatusTip(tr("Show the application's About box"));
  343. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  344. menu->addAction(act);
  345. act = new QAction(tr("About &Qt"), this);
  346. act->setStatusTip(tr("Show the Qt library's About box"));
  347. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  348. menu->addAction(act);
  349. }
  350. void Fb2MainWindow::openSettings()
  351. {
  352. QMessageBox::about(this, tr("Settings"),
  353. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  354. }
  355. void Fb2MainWindow::createTree()
  356. {
  357. if (treeView) return;
  358. treeView = new QTreeView(this);
  359. treeView->setHeaderHidden(true);
  360. connect(treeView, SIGNAL(activated(QModelIndex)), SLOT(treeActivated(QModelIndex)));
  361. connect(treeView, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  362. dockTree = new QDockWidget(tr("Contents"), this);
  363. dockTree->setAttribute(Qt::WA_DeleteOnClose);
  364. dockTree->setFeatures(QDockWidget::AllDockWidgetFeatures);
  365. dockTree->setWidget(treeView);
  366. addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  367. treeView->setFocus();
  368. }
  369. void Fb2MainWindow::loadFinished(bool ok)
  370. {
  371. if (headTree) {
  372. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  373. headTree->setModel(model);
  374. model->expand(headTree);
  375. }
  376. if (treeView) {
  377. Fb2TreeModel *model = new Fb2TreeModel(*textEdit, treeView);
  378. treeView->setModel(model);
  379. model->expand(treeView);
  380. }
  381. }
  382. void Fb2MainWindow::selectionChanged()
  383. {
  384. actionCut->setEnabled(textEdit->CutEnabled());
  385. actionCopy->setEnabled(textEdit->CopyEnabled());
  386. actionTextBold->setChecked(textEdit->BoldChecked());
  387. actionTextItalic->setChecked(textEdit->ItalicChecked());
  388. actionTextStrike->setChecked(textEdit->StrikeChecked());
  389. actionTextSub->setChecked(textEdit->SubChecked());
  390. actionTextSup->setChecked(textEdit->SupChecked());
  391. QString script = "\
  392. (f = function(node){\
  393. var tag = node.tagName;\
  394. if (tag == 'BODY') return '';\
  395. if (tag == 'DIV') tag = node.getAttribute('CLASS');\
  396. return f(node.parentNode) + '/' + tag;\
  397. })(document.getSelection().baseNode.parentNode);";
  398. QString message = textEdit->page()->mainFrame()->evaluateJavaScript(script).toString();
  399. statusBar()->showMessage(message);
  400. }
  401. void Fb2MainWindow::undoChanged()
  402. {
  403. actionUndo->setEnabled(textEdit->UndoEnabled());
  404. }
  405. void Fb2MainWindow::redoChanged()
  406. {
  407. actionRedo->setEnabled(textEdit->RedoEnabled());
  408. }
  409. void Fb2MainWindow::createStatusBar()
  410. {
  411. statusBar()->showMessage(tr("Ready"));
  412. }
  413. void Fb2MainWindow::readSettings()
  414. {
  415. QSettings settings;
  416. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  417. QSize size = settings.value("size", QSize(400, 400)).toSize();
  418. move(pos);
  419. resize(size);
  420. }
  421. void Fb2MainWindow::writeSettings()
  422. {
  423. QSettings settings;
  424. settings.setValue("pos", pos());
  425. settings.setValue("size", size());
  426. }
  427. bool Fb2MainWindow::maybeSave()
  428. {
  429. if (textEdit && textEdit->isModified()) {
  430. QMessageBox::StandardButton ret;
  431. ret = QMessageBox::warning(this, qApp->applicationName(),
  432. tr("The document has been modified. Do you want to save your changes?"),
  433. QMessageBox::Save | QMessageBox::Discard
  434. | QMessageBox::Cancel);
  435. if (ret == QMessageBox::Save)
  436. return fileSave();
  437. else if (ret == QMessageBox::Cancel)
  438. return false;
  439. }
  440. return true;
  441. }
  442. bool Fb2MainWindow::saveFile(const QString &fileName)
  443. {
  444. QFile file(fileName);
  445. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  446. QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  447. return false;
  448. }
  449. if (textEdit) return textEdit->save(&file);
  450. return true;
  451. /*
  452. QTextStream out(&file);
  453. QApplication::setOverrideCursor(Qt::WaitCursor);
  454. out << textEdit->toPlainText();
  455. QApplication::restoreOverrideCursor();
  456. setCurrentFile(fileName);
  457. statusBar()->showMessage(tr("File saved"), 2000);
  458. */
  459. }
  460. void Fb2MainWindow::setCurrentFile(const QString &filename)
  461. {
  462. static int sequenceNumber = 1;
  463. QString title;
  464. isUntitled = filename.isEmpty();
  465. if (isUntitled) {
  466. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  467. title = curFile;
  468. } else {
  469. QFileInfo info = filename;
  470. curFile = info.canonicalFilePath();
  471. title = info.fileName();
  472. }
  473. title += QString(" - ") += qApp->applicationName();
  474. setWindowModified(false);
  475. setWindowFilePath(curFile);
  476. setWindowTitle(title);
  477. }
  478. Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
  479. {
  480. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  481. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  482. Fb2MainWindow *mainWin = qobject_cast<Fb2MainWindow *>(widget);
  483. if (mainWin && mainWin->curFile == canonicalFilePath)
  484. return mainWin;
  485. }
  486. return 0;
  487. }
  488. void Fb2MainWindow::zoomOrig()
  489. {
  490. if (codeEdit) codeEdit->zoomTo(1);
  491. }
  492. void Fb2MainWindow::checkScintillaUndo()
  493. {
  494. if (!codeEdit) return;
  495. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  496. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  497. }
  498. void Fb2MainWindow::viewCode()
  499. {
  500. if (codeEdit && centralWidget() == codeEdit) return;
  501. bool load = false;
  502. QByteArray xml;
  503. QList<int> folds;
  504. if (textEdit) {
  505. textEdit->save(&xml, &folds);
  506. load = true;
  507. }
  508. FB2DELETE(textEdit);
  509. FB2DELETE(dockTree);
  510. FB2DELETE(headTree);
  511. if (!codeEdit) {
  512. codeEdit = new Fb2Scintilla;
  513. }
  514. if (load) codeEdit->load(xml, folds);
  515. setCentralWidget(codeEdit);
  516. codeEdit->setFocus();
  517. FB2DELETE(toolEdit);
  518. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  519. tool->addSeparator();
  520. tool->addAction(actionUndo);
  521. tool->addAction(actionRedo);
  522. tool->addSeparator();
  523. tool->addAction(actionCut);
  524. tool->addAction(actionCopy);
  525. tool->addAction(actionPaste);
  526. tool->addSeparator();
  527. tool->addAction(actionZoomIn);
  528. tool->addAction(actionZoomOut);
  529. tool->addAction(actionZoomOrig);
  530. tool->setMovable(false);
  531. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  532. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  533. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  534. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  535. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  536. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  537. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  538. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  539. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  540. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  541. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  542. connect(actionZoomOrig, SIGNAL(triggered()), this, SLOT(zoomOrig()));
  543. }
  544. void Fb2MainWindow::viewText()
  545. {
  546. if (textEdit && centralWidget() == textEdit) return;
  547. QString xml;
  548. if (codeEdit) xml = codeEdit->text();
  549. FB2DELETE(codeEdit);
  550. FB2DELETE(headTree);
  551. if (!textEdit) {
  552. textEdit = new Fb2WebView(this);
  553. }
  554. setCentralWidget(textEdit);
  555. textEdit->setFocus();
  556. viewTree();
  557. connect(textEdit->page(), SIGNAL(contentsChanged()), SLOT(documentWasModified()));
  558. connect(textEdit->page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  559. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  560. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  561. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  562. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  563. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  564. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  565. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  566. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  567. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  568. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  569. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  570. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  571. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  572. connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));
  573. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  574. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  575. connect(actionZoomOrig, SIGNAL(triggered()), textEdit, SLOT(zoomOrig()));
  576. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  577. FB2DELETE(toolEdit);
  578. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  579. tool->setMovable(false);
  580. tool->addSeparator();
  581. QAction *act;
  582. act = textEdit->pageAction(QWebPage::Undo);
  583. act->setIcon(icon("edit-undo"));
  584. act->setText(tr("&Undo"));
  585. act->setPriority(QAction::LowPriority);
  586. act->setShortcut(QKeySequence::Undo);
  587. tool->addAction(act);
  588. act = textEdit->pageAction(QWebPage::Redo);
  589. act->setIcon(icon("edit-redo"));
  590. act->setText(tr("&Redo"));
  591. act->setPriority(QAction::LowPriority);
  592. act->setShortcut(QKeySequence::Redo);
  593. tool->addAction(act);
  594. tool->addSeparator();
  595. act = textEdit->pageAction(QWebPage::Cut);
  596. act->setIcon(icon("edit-cut"));
  597. act->setText(tr("Cu&t"));
  598. act->setPriority(QAction::LowPriority);
  599. act->setShortcuts(QKeySequence::Cut);
  600. act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  601. tool->addAction(act);
  602. act = textEdit->pageAction(QWebPage::Copy);
  603. act->setIcon(icon("edit-copy"));
  604. act->setText(tr("&Copy"));
  605. act->setPriority(QAction::LowPriority);
  606. act->setShortcuts(QKeySequence::Copy);
  607. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  608. tool->addAction(act);
  609. act = textEdit->pageAction(QWebPage::Paste);
  610. act->setIcon(icon("edit-paste"));
  611. act->setText(tr("&Paste"));
  612. act->setPriority(QAction::LowPriority);
  613. act->setShortcuts(QKeySequence::Paste);
  614. act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  615. tool->addAction(act);
  616. tool->addSeparator();
  617. act = textEdit->pageAction(QWebPage::ToggleBold);
  618. act->setIcon(icon("format-text-bold"));
  619. act->setText(tr("&Bold"));
  620. act->setShortcuts(QKeySequence::Bold);
  621. tool->addAction(act);
  622. act = textEdit->pageAction(QWebPage::ToggleItalic);
  623. act->setIcon(icon("format-text-italic"));
  624. act->setText(tr("&Italic"));
  625. act->setShortcuts(QKeySequence::Italic);
  626. tool->addAction(act);
  627. act = textEdit->pageAction(QWebPage::ToggleStrikethrough);
  628. act->setIcon(icon("format-text-strikethrough"));
  629. act->setText(tr("&Strikethrough"));
  630. tool->addAction(act);
  631. act = textEdit->pageAction(QWebPage::ToggleSuperscript);
  632. act->setIcon(icon("format-text-superscript"));
  633. act->setText(tr("Superscript"));
  634. tool->addAction(act);
  635. act = textEdit->pageAction(QWebPage::ToggleSubscript);
  636. act->setIcon(icon("format-text-subscript"));
  637. act->setText(tr("Subscript"));
  638. tool->addAction(act);
  639. tool->addSeparator();
  640. tool->addAction(actionZoomIn);
  641. tool->addAction(actionZoomOut);
  642. tool->addAction(actionZoomOrig);
  643. }
  644. void Fb2MainWindow::viewHead()
  645. {
  646. if (headTree && centralWidget() == headTree) return;
  647. QString xml;
  648. if (codeEdit) xml = codeEdit->text();
  649. FB2DELETE(dockTree);
  650. FB2DELETE(codeEdit);
  651. FB2DELETE(toolEdit);
  652. if (!headTree) {
  653. headTree = new QTreeView(this);
  654. headTree->header()->setDefaultSectionSize(200);
  655. }
  656. if (textEdit) {
  657. this->setFocus();
  658. textEdit->setParent(NULL);
  659. setCentralWidget(headTree);
  660. textEdit->setParent(this);
  661. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  662. headTree->setModel(model);
  663. model->expand(headTree);
  664. } else {
  665. textEdit = new Fb2WebView(this);
  666. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  667. setCentralWidget(headTree);
  668. }
  669. headTree->setFocus();
  670. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  671. if (textEdit) {
  672. actionUndo->disconnect();
  673. actionRedo->disconnect();
  674. actionCut->disconnect();
  675. actionCopy->disconnect();
  676. actionPaste->disconnect();
  677. actionTextBold->disconnect();
  678. actionTextItalic->disconnect();
  679. actionTextStrike->disconnect();
  680. actionTextSub->disconnect();
  681. actionTextSup->disconnect();
  682. }
  683. FB2DELETE(toolEdit);
  684. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  685. tool->addSeparator();
  686. tool->addAction(actionInsert);
  687. tool->addAction(actionDelete);
  688. tool->setMovable(false);
  689. }
  690. void Fb2MainWindow::viewTree()
  691. {
  692. if (centralWidget() != textEdit) return;
  693. if (treeView == NULL) createTree();
  694. loadFinished(true);
  695. }
  696. void Fb2MainWindow::clipboardDataChanged()
  697. {
  698. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  699. actionPaste->setEnabled(md->hasText());
  700. }
  701. }
  702. void Fb2MainWindow::showInspector()
  703. {
  704. if (!textEdit) return;
  705. QWebInspector * inspector = new QWebInspector();
  706. inspector->setPage(textEdit->page());
  707. inspector->show();
  708. }