fb2main.cpp 26 KB

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