fb2main.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. menu->addSeparator();
  273. actionSection = act = new QAction(FbIcon("insert-object"), tr("&Section"), this);
  274. menu->addAction(act);
  275. actionTitle = act = new QAction(tr("&Title"), this);
  276. menu->addAction(act);
  277. actionEpigraph = act = new QAction(tr("&Epigraph"), this);
  278. menu->addAction(act);
  279. actionDescr = act = new QAction(tr("&Annotation"), this);
  280. menu->addAction(act);
  281. actionSubtitle = act = new QAction(tr("&Subtitle"), this);
  282. menu->addAction(act);
  283. actionAuthor = act = new QAction(tr("&Author"), this);
  284. menu->addAction(act);
  285. actionPoem = act = new QAction(tr("&Poem"), this);
  286. menu->addAction(act);
  287. actionStanza = act = new QAction(tr("&Stanza"), this);
  288. menu->addAction(act);
  289. actionBody = act = new QAction(tr("&Body"), this);
  290. menu->addAction(act);
  291. menuText = menu = menuBar()->addMenu(tr("Fo&rmat"));
  292. actionTextBold = act = new QAction(FbIcon("format-text-bold"), tr("&Bold"), this);
  293. act->setShortcuts(QKeySequence::Bold);
  294. act->setCheckable(true);
  295. menu->addAction(act);
  296. actionTextItalic = act = new QAction(FbIcon("format-text-italic"), tr("&Italic"), this);
  297. act->setShortcuts(QKeySequence::Italic);
  298. act->setCheckable(true);
  299. menu->addAction(act);
  300. actionTextStrike = act = new QAction(FbIcon("format-text-strikethrough"), tr("&Strikethrough"), this);
  301. act->setCheckable(true);
  302. menu->addAction(act);
  303. actionTextSup = act = new QAction(FbIcon("format-text-superscript"), tr("Su&perscript"), this);
  304. act->setCheckable(true);
  305. menu->addAction(act);
  306. actionTextSub = act = new QAction(FbIcon("format-text-subscript"), tr("Su&bscript"), this);
  307. act->setCheckable(true);
  308. menu->addAction(act);
  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. textFrame->view.save(&file, codec);
  442. setCurrentFile(fileName);
  443. }
  444. return true;
  445. /*
  446. QTextStream out(&file);
  447. QApplication::setOverrideCursor(Qt::WaitCursor);
  448. out << textFrame->view.toPlainText();
  449. QApplication::restoreOverrideCursor();
  450. setCurrentFile(fileName);
  451. statusBar()->showMessage(tr("File saved"), 2000);
  452. */
  453. }
  454. void FbMainWindow::setCurrentFile(const QString &filename)
  455. {
  456. static int sequenceNumber = 1;
  457. QString title;
  458. isUntitled = filename.isEmpty();
  459. if (isUntitled) {
  460. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  461. title = curFile;
  462. } else {
  463. QFileInfo info = filename;
  464. curFile = info.canonicalFilePath();
  465. title = info.fileName();
  466. }
  467. title += appTitle();
  468. setWindowModified(false);
  469. setWindowFilePath(curFile);
  470. setWindowTitle(title);
  471. }
  472. QString FbMainWindow::appTitle() const
  473. {
  474. return QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
  475. }
  476. FbMainWindow *FbMainWindow::findFbMainWindow(const QString &fileName)
  477. {
  478. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  479. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  480. FbMainWindow *mainWin = qobject_cast<FbMainWindow *>(widget);
  481. if (mainWin && mainWin->curFile == canonicalFilePath)
  482. return mainWin;
  483. }
  484. return 0;
  485. }
  486. void FbMainWindow::checkScintillaUndo()
  487. {
  488. if (!codeEdit) return;
  489. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  490. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  491. }
  492. void FbMainWindow::viewCode()
  493. {
  494. if (codeEdit && centralWidget() == codeEdit) return;
  495. bool load = false;
  496. QByteArray xml;
  497. QList<int> folds;
  498. if (textFrame) {
  499. textFrame->view.save(&xml);
  500. load = true;
  501. }
  502. FB2DELETE(textFrame);
  503. FB2DELETE(dockTree);
  504. FB2DELETE(headTree);
  505. if (!codeEdit) {
  506. codeEdit = new FbCodeEdit;
  507. }
  508. if (load) codeEdit->load(xml, folds);
  509. setCentralWidget(codeEdit);
  510. codeEdit->setFocus();
  511. FB2DELETE(toolEdit);
  512. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  513. tool->addSeparator();
  514. tool->addAction(actionUndo);
  515. tool->addAction(actionRedo);
  516. tool->addSeparator();
  517. tool->addAction(actionCut);
  518. tool->addAction(actionCopy);
  519. tool->addAction(actionPaste);
  520. tool->addSeparator();
  521. tool->addAction(actionZoomIn);
  522. tool->addAction(actionZoomOut);
  523. tool->addAction(actionZoomReset);
  524. tool->setMovable(false);
  525. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  526. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  527. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  528. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  529. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  530. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  531. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  532. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  533. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  534. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  535. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  536. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  537. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  538. }
  539. void FbMainWindow::viewText()
  540. {
  541. if (textFrame && centralWidget() == textFrame) return;
  542. QString xml;
  543. if (codeEdit) xml = codeEdit->text();
  544. FB2DELETE(codeEdit);
  545. FB2DELETE(headTree);
  546. if (!textFrame) {
  547. textFrame = new FbTextFrame(this);
  548. }
  549. setCentralWidget(textFrame);
  550. textFrame->view.setFocus();
  551. viewTree();
  552. connect(textFrame->view.page()->undoStack(), SIGNAL(cleanChanged(bool)), SLOT(cleanChanged(bool)));
  553. connect(textFrame->view.page()->undoStack(), SIGNAL(canUndoChanged(bool)), SLOT(canUndoChanged(bool)));
  554. connect(textFrame->view.page()->undoStack(), SIGNAL(canRedoChanged(bool)), SLOT(canRedoChanged(bool)));
  555. connect(textFrame->view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  556. connect(textFrame->view.pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  557. connect(textFrame->view.pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  558. connect(actionUndo, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::Undo), SIGNAL(triggered()));
  559. connect(actionRedo, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::Redo), SIGNAL(triggered()));
  560. connect(actionCut, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::Cut), SIGNAL(triggered()));
  561. connect(actionCopy, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::Copy), SIGNAL(triggered()));
  562. connect(actionPaste, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::Paste), SIGNAL(triggered()));
  563. connect(actionTextBold, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  564. connect(actionTextItalic, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  565. connect(actionTextStrike, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  566. connect(actionTextSub, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  567. connect(actionTextSup, SIGNAL(triggered()), textFrame->view.pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  568. FbTextEdit * textEdit = &(textFrame->view);
  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. FbTextPage * textPage = textEdit->page();
  574. connect(actionTitle, SIGNAL(triggered()), textPage, SLOT(insertTitle()));
  575. connect(actionSubtitle, SIGNAL(triggered()), textPage, SLOT(insertSubtitle()));
  576. connect(actionSection, SIGNAL(triggered()), textPage, SLOT(insertSection()));
  577. connect(actionBody, SIGNAL(triggered()), textPage, SLOT(insertBody()));
  578. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  579. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  580. connect(actionZoomReset, SIGNAL(triggered()), textEdit, SLOT(zoomReset()));
  581. connect(actionInspect, SIGNAL(triggered()), textFrame, SLOT(showInspector()));
  582. if (!xml.isEmpty()) textFrame->view.load(curFile, xml);
  583. FB2DELETE(toolEdit);
  584. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  585. tool->setMovable(false);
  586. tool->addSeparator();
  587. textEdit->addTools(tool);
  588. tool->addSeparator();
  589. tool->addAction(actionImage);
  590. tool->addAction(actionNote);
  591. tool->addAction(actionLink);
  592. tool->addAction(actionSection);
  593. tool->addSeparator();
  594. tool->addAction(actionZoomIn);
  595. tool->addAction(actionZoomOut);
  596. tool->addAction(actionZoomReset);
  597. }
  598. void FbMainWindow::viewHead()
  599. {
  600. if (headTree && centralWidget() == headTree) return;
  601. if (textFrame) textFrame->hideInspector();
  602. QString xml;
  603. if (codeEdit) xml = codeEdit->text();
  604. FB2DELETE(dockTree);
  605. FB2DELETE(codeEdit);
  606. FB2DELETE(toolEdit);
  607. if (!textFrame) {
  608. textFrame = new FbTextFrame(this);
  609. }
  610. if (!headTree) {
  611. headTree = new FbHeadView(textFrame->view, this);
  612. connect(headTree, SIGNAL(status(QString)), this, SLOT(status(QString)));
  613. }
  614. this->setFocus();
  615. textFrame->setParent(NULL);
  616. setCentralWidget(headTree);
  617. textFrame->setParent(this);
  618. headTree->updateTree();
  619. headTree->setFocus();
  620. if (!xml.isEmpty()) textFrame->view.load(curFile, xml);
  621. if (textFrame) {
  622. actionUndo->disconnect();
  623. actionRedo->disconnect();
  624. actionCut->disconnect();
  625. actionCopy->disconnect();
  626. actionPaste->disconnect();
  627. actionTextBold->disconnect();
  628. actionTextItalic->disconnect();
  629. actionTextStrike->disconnect();
  630. actionTextSub->disconnect();
  631. actionTextSup->disconnect();
  632. }
  633. FB2DELETE(toolEdit);
  634. toolEdit = addToolBar(tr("Edit"));
  635. headTree->initToolbar(*toolEdit);
  636. toolEdit->addSeparator();
  637. toolEdit->setMovable(false);
  638. }
  639. void FbMainWindow::viewTree()
  640. {
  641. if (dockTree) dockTree->deleteLater(); else createTree();
  642. }
  643. void FbMainWindow::clipboardDataChanged()
  644. {
  645. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  646. actionPaste->setEnabled(md->hasText());
  647. }
  648. }
  649. void FbMainWindow::status(const QString &text)
  650. {
  651. statusBar()->showMessage(text);
  652. }