fb2main.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. actionTextCode = act = new QAction(tr("&Code"), this);
  303. act->setCheckable(true);
  304. menu->addAction(act);
  305. act->setEnabled(false);
  306. menuView = menu = menuBar()->addMenu(tr("&View"));
  307. tool->addSeparator();
  308. QActionGroup * viewGroup = new QActionGroup(this);
  309. act = new QAction(tr("&Text"), this);
  310. act->setCheckable(true);
  311. act->setChecked(true);
  312. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  313. viewGroup->addAction(act);
  314. menu->addAction(act);
  315. tool->addAction(act);
  316. act = new QAction(tr("&Head"), this);
  317. act->setCheckable(true);
  318. connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
  319. viewGroup->addAction(act);
  320. menu->addAction(act);
  321. tool->addAction(act);
  322. act = new QAction(tr("&XML"), this);
  323. act->setCheckable(true);
  324. connect(act, SIGNAL(triggered()), this, SLOT(viewCode()));
  325. viewGroup->addAction(act);
  326. menu->addAction(act);
  327. tool->addAction(act);
  328. menu->addSeparator();
  329. actionZoomIn = act = new QAction(FbIcon("zoom-in"), tr("Zoom in"), this);
  330. act->setShortcuts(QKeySequence::ZoomIn);
  331. menu->addAction(act);
  332. actionZoomOut = act = new QAction(FbIcon("zoom-out"), tr("Zoom out"), this);
  333. act->setShortcuts(QKeySequence::ZoomOut);
  334. menu->addAction(act);
  335. actionZoomReset = act = new QAction(FbIcon("zoom-original"), tr("Zoom original"), this);
  336. menu->addAction(act);
  337. menu->addSeparator();
  338. act = new QAction(tr("&Contents"), this);
  339. connect(act, SIGNAL(triggered()), this, SLOT(viewTree()));
  340. menu->addAction(act);
  341. actionInspect = act = new QAction(tr("&Web inspector"), this);
  342. menu->addAction(act);
  343. menuBar()->addSeparator();
  344. menu = menuBar()->addMenu(tr("&Help"));
  345. act = new QAction(FbIcon("help-about"), tr("&About"), this);
  346. act->setStatusTip(tr("Show the application's About box"));
  347. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  348. menu->addAction(act);
  349. act = new QAction(tr("About &Qt"), this);
  350. act->setStatusTip(tr("Show the Qt library's About box"));
  351. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  352. menu->addAction(act);
  353. }
  354. void FbMainWindow::openSettings()
  355. {
  356. QMessageBox::about(this, tr("Settings"),
  357. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  358. }
  359. void FbMainWindow::createTree()
  360. {
  361. if (textFrame && centralWidget() == textFrame) {
  362. dockTree = new QDockWidget(tr("Contents"), this);
  363. dockTree->setAttribute(Qt::WA_DeleteOnClose);
  364. dockTree->setFeatures(QDockWidget::AllDockWidgetFeatures);
  365. dockTree->setWidget(new FbTreeWidget(textFrame->view, this));
  366. connect(dockTree, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  367. addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  368. }
  369. }
  370. void FbMainWindow::selectionChanged()
  371. {
  372. actionCut->setEnabled(textFrame->view.CutEnabled());
  373. actionCopy->setEnabled(textFrame->view.CopyEnabled());
  374. actionTextBold->setChecked(textFrame->view.BoldChecked());
  375. actionTextItalic->setChecked(textFrame->view.ItalicChecked());
  376. actionTextStrike->setChecked(textFrame->view.StrikeChecked());
  377. actionTextSub->setChecked(textFrame->view.SubChecked());
  378. actionTextSup->setChecked(textFrame->view.SupChecked());
  379. statusBar()->showMessage(textFrame->view.page()->status());
  380. }
  381. void FbMainWindow::canUndoChanged(bool canUndo)
  382. {
  383. actionUndo->setEnabled(canUndo);
  384. }
  385. void FbMainWindow::canRedoChanged(bool canRedo)
  386. {
  387. actionRedo->setEnabled(canRedo);
  388. }
  389. void FbMainWindow::undoChanged()
  390. {
  391. actionUndo->setEnabled(textFrame->view.UndoEnabled());
  392. }
  393. void FbMainWindow::redoChanged()
  394. {
  395. actionRedo->setEnabled(textFrame->view.RedoEnabled());
  396. }
  397. void FbMainWindow::createStatusBar()
  398. {
  399. statusBar()->showMessage(tr("Ready"));
  400. }
  401. void FbMainWindow::readSettings()
  402. {
  403. QSettings settings;
  404. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  405. QSize size = settings.value("size", QSize(400, 400)).toSize();
  406. move(pos);
  407. resize(size);
  408. }
  409. void FbMainWindow::writeSettings()
  410. {
  411. QSettings settings;
  412. settings.setValue("pos", pos());
  413. settings.setValue("size", size());
  414. }
  415. bool FbMainWindow::maybeSave()
  416. {
  417. if (textFrame && textFrame->view.isModified()) {
  418. QMessageBox::StandardButton ret;
  419. ret = QMessageBox::warning(this, qApp->applicationName(),
  420. tr("The document has been modified. Do you want to save your changes?"),
  421. QMessageBox::Save | QMessageBox::Discard
  422. | QMessageBox::Cancel);
  423. if (ret == QMessageBox::Save)
  424. return fileSave();
  425. else if (ret == QMessageBox::Cancel)
  426. return false;
  427. }
  428. return true;
  429. }
  430. bool FbMainWindow::saveFile(const QString &fileName, const QString &codec)
  431. {
  432. QFile file(fileName);
  433. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  434. QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  435. return false;
  436. }
  437. if (textFrame) {
  438. isSwitched = false;
  439. textFrame->view.save(&file, codec);
  440. setCurrentFile(fileName);
  441. return true;
  442. }
  443. if (codeEdit) {
  444. QTextStream out(&file);
  445. out << codeEdit->toPlainText();
  446. setCurrentFile(fileName);
  447. return true;
  448. }
  449. return false;
  450. }
  451. void FbMainWindow::setCurrentFile(const QString &filename)
  452. {
  453. if (filename.isEmpty()) {
  454. static int sequenceNumber = 1;
  455. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  456. } else {
  457. QFileInfo info = filename;
  458. curFile = info.canonicalFilePath();
  459. }
  460. setWindowFilePath(curFile);
  461. setModified(false);
  462. }
  463. QString FbMainWindow::appTitle() const
  464. {
  465. return QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
  466. }
  467. FbMainWindow *FbMainWindow::findFbMainWindow(const QString &fileName)
  468. {
  469. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  470. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  471. FbMainWindow *mainWin = qobject_cast<FbMainWindow *>(widget);
  472. if (mainWin && mainWin->curFile == canonicalFilePath)
  473. return mainWin;
  474. }
  475. return 0;
  476. }
  477. void FbMainWindow::checkScintillaUndo()
  478. {
  479. if (!codeEdit) return;
  480. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  481. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  482. }
  483. void FbMainWindow::viewCode()
  484. {
  485. if (codeEdit && centralWidget() == codeEdit) return;
  486. bool load = false;
  487. QByteArray xml;
  488. if (textFrame) {
  489. textFrame->view.save(&xml);
  490. isSwitched = true;
  491. load = true;
  492. }
  493. FB2DELETE(textFrame);
  494. FB2DELETE(dockTree);
  495. FB2DELETE(headTree);
  496. if (!codeEdit) {
  497. codeEdit = new FbCodeEdit;
  498. }
  499. if (load) codeEdit->load(xml);
  500. setCentralWidget(codeEdit);
  501. codeEdit->setFocus();
  502. FB2DELETE(toolEdit);
  503. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  504. tool->addSeparator();
  505. tool->addAction(actionUndo);
  506. tool->addAction(actionRedo);
  507. tool->addSeparator();
  508. tool->addAction(actionCut);
  509. tool->addAction(actionCopy);
  510. tool->addAction(actionPaste);
  511. tool->addSeparator();
  512. tool->addAction(actionZoomIn);
  513. tool->addAction(actionZoomOut);
  514. tool->addAction(actionZoomReset);
  515. tool->setMovable(false);
  516. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  517. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  518. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  519. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  520. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  521. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  522. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  523. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  524. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  525. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  526. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  527. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  528. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  529. }
  530. void FbMainWindow::viewText()
  531. {
  532. if (textFrame && centralWidget() == textFrame) return;
  533. bool load = false;
  534. QString xml;
  535. if (codeEdit) {
  536. xml = codeEdit->text();
  537. isSwitched = true;
  538. load = true;
  539. }
  540. FB2DELETE(codeEdit);
  541. FB2DELETE(headTree);
  542. if (!textFrame) {
  543. textFrame = new FbTextFrame(this);
  544. }
  545. setCentralWidget(textFrame);
  546. textFrame->view.setFocus();
  547. viewTree();
  548. FbTextEdit * textEdit = &textFrame->view;
  549. FbTextPage * textPage = textEdit->page();
  550. connect(textPage->undoStack(), SIGNAL(cleanChanged(bool)), SLOT(cleanChanged(bool)));
  551. connect(textPage->undoStack(), SIGNAL(canUndoChanged(bool)), SLOT(canUndoChanged(bool)));
  552. connect(textPage->undoStack(), SIGNAL(canRedoChanged(bool)), SLOT(canRedoChanged(bool)));
  553. connect(textPage, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  554. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  555. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  556. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  557. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  558. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  559. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  560. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  561. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  562. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  563. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  564. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  565. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  566. connect(actionFind, SIGNAL(triggered()), textEdit, SLOT(find()));
  567. connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));
  568. connect(actionNote, SIGNAL(triggered()), textEdit, SLOT(insertNote()));
  569. connect(actionLink, SIGNAL(triggered()), textEdit, SLOT(insertLink()));
  570. connect(actionTitle, SIGNAL(triggered()), textPage, SLOT(insertTitle()));
  571. connect(actionAnnot, SIGNAL(triggered()), textPage, SLOT(insertAnnot()));
  572. connect(actionAuthor, SIGNAL(triggered()), textPage, SLOT(insertAuthor()));
  573. connect(actionEpigraph, SIGNAL(triggered()), textPage, SLOT(insertEpigraph()));
  574. connect(actionSubtitle, SIGNAL(triggered()), textPage, SLOT(insertSubtitle()));
  575. connect(actionSection, SIGNAL(triggered()), textPage, SLOT(insertSection()));
  576. connect(actionStanza, SIGNAL(triggered()), textPage, SLOT(insertStanza()));
  577. connect(actionPoem, SIGNAL(triggered()), textPage, SLOT(insertPoem()));
  578. connect(actionDate, SIGNAL(triggered()), textPage, SLOT(insertDate()));
  579. connect(actionBody, SIGNAL(triggered()), textPage, SLOT(insertBody()));
  580. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  581. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  582. connect(actionZoomReset, SIGNAL(triggered()), textEdit, SLOT(zoomReset()));
  583. connect(actionInspect, SIGNAL(triggered()), textFrame, SLOT(showInspector()));
  584. if (load) textFrame->view.load(curFile, xml);
  585. FB2DELETE(toolEdit);
  586. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  587. tool->setMovable(false);
  588. tool->addSeparator();
  589. textEdit->addTools(tool);
  590. tool->addSeparator();
  591. tool->addAction(actionImage);
  592. tool->addAction(actionNote);
  593. tool->addAction(actionLink);
  594. tool->addAction(actionSection);
  595. tool->addSeparator();
  596. tool->addAction(actionZoomIn);
  597. tool->addAction(actionZoomOut);
  598. tool->addAction(actionZoomReset);
  599. }
  600. void FbMainWindow::viewHead()
  601. {
  602. if (headTree && centralWidget() == headTree) return;
  603. if (textFrame) textFrame->hideInspector();
  604. QString xml;
  605. if (codeEdit) xml = codeEdit->text();
  606. FB2DELETE(dockTree);
  607. FB2DELETE(codeEdit);
  608. FB2DELETE(toolEdit);
  609. if (!textFrame) {
  610. textFrame = new FbTextFrame(this);
  611. }
  612. if (!headTree) {
  613. headTree = new FbHeadView(textFrame->view, this);
  614. connect(headTree, SIGNAL(status(QString)), this, SLOT(status(QString)));
  615. }
  616. this->setFocus();
  617. textFrame->setParent(NULL);
  618. setCentralWidget(headTree);
  619. textFrame->setParent(this);
  620. headTree->updateTree();
  621. headTree->setFocus();
  622. if (!xml.isEmpty()) textFrame->view.load(curFile, xml);
  623. if (textFrame) {
  624. actionUndo->disconnect();
  625. actionRedo->disconnect();
  626. actionCut->disconnect();
  627. actionCopy->disconnect();
  628. actionPaste->disconnect();
  629. actionTextBold->disconnect();
  630. actionTextItalic->disconnect();
  631. actionTextStrike->disconnect();
  632. actionTextSub->disconnect();
  633. actionTextSup->disconnect();
  634. }
  635. FB2DELETE(toolEdit);
  636. toolEdit = addToolBar(tr("Edit"));
  637. headTree->initToolbar(*toolEdit);
  638. toolEdit->addSeparator();
  639. toolEdit->setMovable(false);
  640. }
  641. void FbMainWindow::viewTree()
  642. {
  643. if (dockTree) dockTree->deleteLater(); else createTree();
  644. }
  645. void FbMainWindow::clipboardDataChanged()
  646. {
  647. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  648. actionPaste->setEnabled(md->hasText());
  649. }
  650. }
  651. void FbMainWindow::status(const QString &text)
  652. {
  653. statusBar()->showMessage(text);
  654. }