fb2main.cpp 25 KB

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