fb2main.cpp 29 KB

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