fb2main.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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 "fb2tree.hpp"
  10. #include "fb2view.hpp"
  11. #include "fb2head.hpp"
  12. #include "fb2utils.h"
  13. Fb2MainWindow::Fb2MainWindow()
  14. {
  15. init();
  16. setCurrentFile();
  17. viewText();
  18. textFrame->view.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. textFrame->view.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. 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 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::treeDestroyed()
  73. {
  74. dockTree = NULL;
  75. }
  76. bool Fb2MainWindow::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 Fb2MainWindow::closeEvent(QCloseEvent *event)
  88. {
  89. if (maybeSave()) {
  90. writeSettings();
  91. event->accept();
  92. } else {
  93. event->ignore();
  94. }
  95. }
  96. void Fb2MainWindow::fileNew()
  97. {
  98. Fb2MainWindow *other = new Fb2MainWindow;
  99. other->move(x() + 40, y() + 40);
  100. other->show();
  101. }
  102. void Fb2MainWindow::fileOpen()
  103. {
  104. QString filename = QFileDialog::getOpenFileName(this, tr("Open file"), QString(), "Fiction book files (*.fb2)");
  105. if (filename.isEmpty()) return;
  106. Fb2MainWindow * existing = findFb2MainWindow(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. Fb2MainWindow * other = new Fb2MainWindow(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. Fb2MainWindow * other = new Fb2MainWindow(filename, XML);
  128. other->move(x() + 40, y() + 40);
  129. other->show();
  130. }
  131. }
  132. }
  133. bool Fb2MainWindow::fileSave()
  134. {
  135. if (isUntitled) {
  136. return fileSaveAs();
  137. } else {
  138. return saveFile(curFile);
  139. }
  140. }
  141. bool Fb2MainWindow::fileSaveAs()
  142. {
  143. Fb2SaveDialog 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 Fb2MainWindow::about()
  151. {
  152. QMessageBox::about(this, tr("About fb2edit"),
  153. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  154. }
  155. void Fb2MainWindow::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 Fb2MainWindow::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 Fb2MainWindow::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(FB2::icon("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(FB2::icon("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(FB2::icon("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(FB2::icon("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(FB2::icon("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(FB2::icon("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(FB2::icon("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(FB2::icon("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(FB2::icon("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(FB2::icon("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(FB2::icon("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(FB2::icon("edit-find"), tr("&Find..."), this);
  255. act->setShortcuts(QKeySequence::Find);
  256. menu->addAction(act);
  257. actionReplace = act = new QAction(FB2::icon("edit-find-replace"), tr("&Replace..."), this);
  258. menu->addAction(act);
  259. menu->addSeparator();
  260. act = new QAction(FB2::icon("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(FB2::icon("insert-image"), tr("&Image"), this);
  267. menu->addAction(act);
  268. actionNote = act = new QAction(FB2::icon("insert-text"), tr("&Footnote"), this);
  269. menu->addAction(act);
  270. actionLink = act = new QAction(FB2::icon("insert-link"), tr("&Hiperlink"), this);
  271. menu->addAction(act);
  272. menu->addSeparator();
  273. actionSection = act = new QAction(FB2::icon("insert-object"), tr("&Section"), this);
  274. menu->addAction(act);
  275. actionTitle = act = new QAction(tr("&Title"), this);
  276. menu->addAction(act);
  277. actionSubtitle = act = new QAction(tr("&Subtitle"), this);
  278. menu->addAction(act);
  279. actionAuthor = act = new QAction(tr("&Author"), this);
  280. menu->addAction(act);
  281. actionDescr = act = new QAction(tr("&Annotation"), this);
  282. menu->addAction(act);
  283. actionPoem = act = new QAction(tr("&Poem"), this);
  284. menu->addAction(act);
  285. actionStanza = act = new QAction(tr("&Stanza"), this);
  286. menu->addAction(act);
  287. actionBody = act = new QAction(tr("&Body"), this);
  288. menu->addAction(act);
  289. menuText = menu = menuBar()->addMenu(tr("Fo&rmat"));
  290. actionTextBold = act = new QAction(FB2::icon("format-text-bold"), tr("&Bold"), this);
  291. act->setShortcuts(QKeySequence::Bold);
  292. act->setCheckable(true);
  293. menu->addAction(act);
  294. actionTextItalic = act = new QAction(FB2::icon("format-text-italic"), tr("&Italic"), this);
  295. act->setShortcuts(QKeySequence::Italic);
  296. act->setCheckable(true);
  297. menu->addAction(act);
  298. actionTextStrike = act = new QAction(FB2::icon("format-text-strikethrough"), tr("&Strikethrough"), this);
  299. act->setCheckable(true);
  300. menu->addAction(act);
  301. actionTextSup = act = new QAction(FB2::icon("format-text-superscript"), tr("Su&perscript"), this);
  302. act->setCheckable(true);
  303. menu->addAction(act);
  304. actionTextSub = act = new QAction(FB2::icon("format-text-subscript"), tr("Su&bscript"), this);
  305. act->setCheckable(true);
  306. menu->addAction(act);
  307. menuView = menu = menuBar()->addMenu(tr("&View"));
  308. tool->addSeparator();
  309. QActionGroup * viewGroup = new QActionGroup(this);
  310. act = new QAction(tr("&Text"), this);
  311. act->setCheckable(true);
  312. act->setChecked(true);
  313. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  314. viewGroup->addAction(act);
  315. menu->addAction(act);
  316. tool->addAction(act);
  317. act = new QAction(tr("&Head"), this);
  318. act->setCheckable(true);
  319. connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
  320. viewGroup->addAction(act);
  321. menu->addAction(act);
  322. tool->addAction(act);
  323. act = new QAction(tr("&XML"), this);
  324. act->setCheckable(true);
  325. connect(act, SIGNAL(triggered()), this, SLOT(viewCode()));
  326. viewGroup->addAction(act);
  327. menu->addAction(act);
  328. tool->addAction(act);
  329. menu->addSeparator();
  330. actionZoomIn = act = new QAction(FB2::icon("zoom-in"), tr("Zoom in"), this);
  331. act->setShortcuts(QKeySequence::ZoomIn);
  332. menu->addAction(act);
  333. actionZoomOut = act = new QAction(FB2::icon("zoom-out"), tr("Zoom out"), this);
  334. act->setShortcuts(QKeySequence::ZoomOut);
  335. menu->addAction(act);
  336. actionZoomReset = act = new QAction(FB2::icon("zoom-original"), tr("Zoom original"), this);
  337. menu->addAction(act);
  338. menu->addSeparator();
  339. act = new QAction(tr("&Contents"), this);
  340. connect(act, SIGNAL(triggered()), this, SLOT(viewTree()));
  341. menu->addAction(act);
  342. actionInspect = act = new QAction(tr("&Web inspector"), this);
  343. menu->addAction(act);
  344. menuBar()->addSeparator();
  345. menu = menuBar()->addMenu(tr("&Help"));
  346. act = new QAction(FB2::icon("help-about"), tr("&About"), this);
  347. act->setStatusTip(tr("Show the application's About box"));
  348. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  349. menu->addAction(act);
  350. act = new QAction(tr("About &Qt"), this);
  351. act->setStatusTip(tr("Show the Qt library's About box"));
  352. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  353. menu->addAction(act);
  354. }
  355. void Fb2MainWindow::openSettings()
  356. {
  357. QMessageBox::about(this, tr("Settings"),
  358. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  359. }
  360. void Fb2MainWindow::createTree()
  361. {
  362. if (textFrame && centralWidget() == textFrame) {
  363. dockTree = new QDockWidget(tr("Contents"), this);
  364. dockTree->setAttribute(Qt::WA_DeleteOnClose);
  365. dockTree->setFeatures(QDockWidget::AllDockWidgetFeatures);
  366. dockTree->setWidget(new Fb2TreeWidget(textFrame->view, this));
  367. connect(dockTree, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  368. addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  369. }
  370. }
  371. void Fb2MainWindow::selectionChanged()
  372. {
  373. actionCut->setEnabled(textFrame->view.CutEnabled());
  374. actionCopy->setEnabled(textFrame->view.CopyEnabled());
  375. actionTextBold->setChecked(textFrame->view.BoldChecked());
  376. actionTextItalic->setChecked(textFrame->view.ItalicChecked());
  377. actionTextStrike->setChecked(textFrame->view.StrikeChecked());
  378. actionTextSub->setChecked(textFrame->view.SubChecked());
  379. actionTextSup->setChecked(textFrame->view.SupChecked());
  380. statusBar()->showMessage(textFrame->view.status());
  381. }
  382. void Fb2MainWindow::canUndoChanged(bool canUndo)
  383. {
  384. actionUndo->setEnabled(canUndo);
  385. }
  386. void Fb2MainWindow::canRedoChanged(bool canRedo)
  387. {
  388. actionRedo->setEnabled(canRedo);
  389. }
  390. void Fb2MainWindow::undoChanged()
  391. {
  392. actionUndo->setEnabled(textFrame->view.UndoEnabled());
  393. }
  394. void Fb2MainWindow::redoChanged()
  395. {
  396. actionRedo->setEnabled(textFrame->view.RedoEnabled());
  397. }
  398. void Fb2MainWindow::createStatusBar()
  399. {
  400. statusBar()->showMessage(tr("Ready"));
  401. }
  402. void Fb2MainWindow::readSettings()
  403. {
  404. QSettings settings;
  405. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  406. QSize size = settings.value("size", QSize(400, 400)).toSize();
  407. move(pos);
  408. resize(size);
  409. }
  410. void Fb2MainWindow::writeSettings()
  411. {
  412. QSettings settings;
  413. settings.setValue("pos", pos());
  414. settings.setValue("size", size());
  415. }
  416. bool Fb2MainWindow::maybeSave()
  417. {
  418. if (textFrame && textFrame->view.isModified()) {
  419. QMessageBox::StandardButton ret;
  420. ret = QMessageBox::warning(this, qApp->applicationName(),
  421. tr("The document has been modified. Do you want to save your changes?"),
  422. QMessageBox::Save | QMessageBox::Discard
  423. | QMessageBox::Cancel);
  424. if (ret == QMessageBox::Save)
  425. return fileSave();
  426. else if (ret == QMessageBox::Cancel)
  427. return false;
  428. }
  429. return true;
  430. }
  431. bool Fb2MainWindow::saveFile(const QString &fileName, const QString &codec)
  432. {
  433. QFile file(fileName);
  434. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  435. QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  436. return false;
  437. }
  438. if (textFrame) {
  439. textFrame->view.save(&file, codec);
  440. setCurrentFile(fileName);
  441. }
  442. return true;
  443. /*
  444. QTextStream out(&file);
  445. QApplication::setOverrideCursor(Qt::WaitCursor);
  446. out << textFrame->view.toPlainText();
  447. QApplication::restoreOverrideCursor();
  448. setCurrentFile(fileName);
  449. statusBar()->showMessage(tr("File saved"), 2000);
  450. */
  451. }
  452. void Fb2MainWindow::setCurrentFile(const QString &filename)
  453. {
  454. static int sequenceNumber = 1;
  455. QString title;
  456. isUntitled = filename.isEmpty();
  457. if (isUntitled) {
  458. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  459. title = curFile;
  460. } else {
  461. QFileInfo info = filename;
  462. curFile = info.canonicalFilePath();
  463. title = info.fileName();
  464. }
  465. title += appTitle();
  466. setWindowModified(false);
  467. setWindowFilePath(curFile);
  468. setWindowTitle(title);
  469. }
  470. QString Fb2MainWindow::appTitle() const
  471. {
  472. return QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
  473. }
  474. Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
  475. {
  476. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  477. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  478. Fb2MainWindow *mainWin = qobject_cast<Fb2MainWindow *>(widget);
  479. if (mainWin && mainWin->curFile == canonicalFilePath)
  480. return mainWin;
  481. }
  482. return 0;
  483. }
  484. void Fb2MainWindow::checkScintillaUndo()
  485. {
  486. if (!codeEdit) return;
  487. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  488. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  489. }
  490. void Fb2MainWindow::viewCode()
  491. {
  492. if (codeEdit && centralWidget() == codeEdit) return;
  493. bool load = false;
  494. QByteArray xml;
  495. QList<int> folds;
  496. if (textFrame) {
  497. textFrame->view.save(&xml);
  498. load = true;
  499. }
  500. FB2DELETE(textFrame);
  501. FB2DELETE(dockTree);
  502. FB2DELETE(headTree);
  503. if (!codeEdit) {
  504. codeEdit = new Fb2CodeEdit;
  505. }
  506. if (load) codeEdit->load(xml, folds);
  507. setCentralWidget(codeEdit);
  508. codeEdit->setFocus();
  509. FB2DELETE(toolEdit);
  510. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  511. tool->addSeparator();
  512. tool->addAction(actionUndo);
  513. tool->addAction(actionRedo);
  514. tool->addSeparator();
  515. tool->addAction(actionCut);
  516. tool->addAction(actionCopy);
  517. tool->addAction(actionPaste);
  518. tool->addSeparator();
  519. tool->addAction(actionZoomIn);
  520. tool->addAction(actionZoomOut);
  521. tool->addAction(actionZoomReset);
  522. tool->setMovable(false);
  523. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  524. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  525. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  526. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  527. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  528. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  529. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  530. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  531. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  532. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  533. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  534. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  535. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  536. }
  537. void Fb2MainWindow::viewText()
  538. {
  539. if (textFrame && centralWidget() == textFrame) return;
  540. QString xml;
  541. if (codeEdit) xml = codeEdit->text();
  542. FB2DELETE(codeEdit);
  543. FB2DELETE(headTree);
  544. if (!textFrame) {
  545. textFrame = new Fb2TextFrame(this);
  546. }
  547. setCentralWidget(textFrame);
  548. textFrame->view.setFocus();
  549. viewTree();
  550. connect(textFrame->view.page()->undoStack(), SIGNAL(cleanChanged(bool)), SLOT(cleanChanged(bool)));
  551. connect(textFrame->view.page()->undoStack(), SIGNAL(canUndoChanged(bool)), SLOT(canUndoChanged(bool)));
  552. connect(textFrame->view.page()->undoStack(), SIGNAL(canRedoChanged(bool)), SLOT(canRedoChanged(bool)));
  553. connect(textFrame->view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  554. connect(textFrame->view.pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  555. connect(textFrame->view.pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  556. connect(actionUndo, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::Undo), SIGNAL(triggered()));
  557. connect(actionRedo, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::Redo), SIGNAL(triggered()));
  558. connect(actionCut, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::Cut), SIGNAL(triggered()));
  559. connect(actionCopy, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::Copy), SIGNAL(triggered()));
  560. connect(actionPaste, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::Paste), SIGNAL(triggered()));
  561. connect(actionTextBold, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  562. connect(actionTextItalic, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  563. connect(actionTextStrike, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  564. connect(actionTextSub, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  565. connect(actionTextSup, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  566. QWebView * textEdit = &(textFrame->view);
  567. connect(actionFind, SIGNAL(triggered()), textEdit, SLOT(find()));
  568. connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));
  569. connect(actionNote, SIGNAL(triggered()), textEdit, SLOT(insertNote()));
  570. connect(actionLink, SIGNAL(triggered()), textEdit, SLOT(insertLink()));
  571. connect(actionTitle, SIGNAL(triggered()), textEdit, SLOT(insertTitle()));
  572. connect(actionSubtitle, SIGNAL(triggered()), textEdit, SLOT(insertSubtitle()));
  573. connect(actionBody, SIGNAL(triggered()), textEdit->page(), SLOT(insertBody()));
  574. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  575. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  576. connect(actionZoomReset, SIGNAL(triggered()), textEdit, SLOT(zoomReset()));
  577. connect(actionInspect, SIGNAL(triggered()), textFrame, SLOT(showInspector()));
  578. if (!xml.isEmpty()) textFrame->view.load(curFile, xml);
  579. FB2DELETE(toolEdit);
  580. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  581. tool->setMovable(false);
  582. tool->addSeparator();
  583. FB2::addTools(tool, textEdit);
  584. tool->addSeparator();
  585. tool->addAction(actionImage);
  586. tool->addAction(actionNote);
  587. tool->addAction(actionLink);
  588. tool->addAction(actionSection);
  589. tool->addSeparator();
  590. tool->addAction(actionZoomIn);
  591. tool->addAction(actionZoomOut);
  592. tool->addAction(actionZoomReset);
  593. }
  594. void Fb2MainWindow::viewHead()
  595. {
  596. if (headTree && centralWidget() == headTree) return;
  597. if (textFrame) textFrame->hideInspector();
  598. QString xml;
  599. if (codeEdit) xml = codeEdit->text();
  600. FB2DELETE(dockTree);
  601. FB2DELETE(codeEdit);
  602. FB2DELETE(toolEdit);
  603. if (!textFrame) {
  604. textFrame = new Fb2TextFrame(this);
  605. }
  606. if (!headTree) {
  607. headTree = new Fb2HeadView(textFrame->view, this);
  608. connect(headTree, SIGNAL(status(QString)), this, SLOT(status(QString)));
  609. }
  610. this->setFocus();
  611. textFrame->setParent(NULL);
  612. setCentralWidget(headTree);
  613. textFrame->setParent(this);
  614. headTree->updateTree();
  615. headTree->setFocus();
  616. if (!xml.isEmpty()) textFrame->view.load(curFile, xml);
  617. if (textFrame) {
  618. actionUndo->disconnect();
  619. actionRedo->disconnect();
  620. actionCut->disconnect();
  621. actionCopy->disconnect();
  622. actionPaste->disconnect();
  623. actionTextBold->disconnect();
  624. actionTextItalic->disconnect();
  625. actionTextStrike->disconnect();
  626. actionTextSub->disconnect();
  627. actionTextSup->disconnect();
  628. }
  629. FB2DELETE(toolEdit);
  630. toolEdit = addToolBar(tr("Edit"));
  631. headTree->initToolbar(*toolEdit);
  632. toolEdit->addSeparator();
  633. toolEdit->setMovable(false);
  634. }
  635. void Fb2MainWindow::viewTree()
  636. {
  637. if (dockTree) dockTree->deleteLater(); else createTree();
  638. }
  639. void Fb2MainWindow::clipboardDataChanged()
  640. {
  641. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  642. actionPaste->setEnabled(md->hasText());
  643. }
  644. }
  645. void Fb2MainWindow::status(const QString &text)
  646. {
  647. statusBar()->showMessage(text);
  648. }