fb2main.cpp 25 KB

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