fb2main.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. #include <QtGui>
  2. #include <QtDebug>
  3. #include <QTreeView>
  4. #include <QWebFrame>
  5. #include "fb2app.hpp"
  6. #include "fb2main.hpp"
  7. #include "fb2code.hpp"
  8. #include "fb2dlgs.hpp"
  9. #include "fb2dock.hpp"
  10. #include "fb2read.hpp"
  11. #include "fb2save.hpp"
  12. #include "fb2text.hpp"
  13. #include "fb2tree.hpp"
  14. #include "fb2head.hpp"
  15. #include "fb2utils.h"
  16. //---------------------------------------------------------------------------
  17. // FbDockWidget
  18. //---------------------------------------------------------------------------
  19. FbDockWidget::FbDockWidget(const QString &title, QWidget *parent, Qt::WindowFlags flags)
  20. : QDockWidget(title, parent, flags)
  21. {
  22. setFeatures(QDockWidget::AllDockWidgetFeatures);
  23. setAttribute(Qt::WA_DeleteOnClose);
  24. }
  25. //---------------------------------------------------------------------------
  26. // FbTextAction
  27. //---------------------------------------------------------------------------
  28. QAction * FbTextAction::action(QWebPage::WebAction action)
  29. {
  30. FbMainWindow * main = qobject_cast<FbMainWindow*>(parent());
  31. if (!main) return 0;
  32. FbTextPage * page = main->page();
  33. if (!page) return 0;
  34. return page->action(action);
  35. }
  36. //---------------------------------------------------------------------------
  37. // FbMainWindow
  38. //---------------------------------------------------------------------------
  39. FbMainWindow::FbMainWindow(const QString &filename, ViewMode mode)
  40. : QMainWindow()
  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. mainDock = new FbMainDock(this);
  55. setCentralWidget(mainDock);
  56. createActions();
  57. createStatusBar();
  58. readSettings();
  59. setCurrentFile(filename);
  60. mainDock->load(filename);
  61. }
  62. /*
  63. FbTextPage * FbMainWindow::page()
  64. {
  65. return textFrame ? textFrame->view()->page() : 0;
  66. }
  67. */
  68. void FbMainWindow::logMessage(const QString &message)
  69. {
  70. if (!messageEdit) {
  71. messageEdit = new QTextEdit(this);
  72. connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
  73. QDockWidget * dock = new FbLogDock(tr("Message log"), this);
  74. dock->setAttribute(Qt::WA_DeleteOnClose);
  75. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  76. dock->setWidget(messageEdit);
  77. addDockWidget(Qt::BottomDockWidgetArea, dock);
  78. messageEdit->setMaximumHeight(80);
  79. }
  80. messageEdit->append(message);
  81. }
  82. void FbMainWindow::logShowed()
  83. {
  84. messageEdit->setMaximumHeight(QWIDGETSIZE_MAX);
  85. }
  86. void FbMainWindow::logDestroyed()
  87. {
  88. messageEdit = NULL;
  89. }
  90. void FbMainWindow::treeDestroyed()
  91. {
  92. actionContents->setChecked(false);
  93. dockTree = NULL;
  94. }
  95. void FbMainWindow::imgsDestroyed()
  96. {
  97. actionPictures->setChecked(false);
  98. dockImgs = NULL;
  99. }
  100. void FbMainWindow::closeEvent(QCloseEvent *event)
  101. {
  102. if (maybeSave()) {
  103. writeSettings();
  104. event->accept();
  105. } else {
  106. event->ignore();
  107. }
  108. }
  109. void FbMainWindow::fileNew()
  110. {
  111. FbMainWindow *other = new FbMainWindow;
  112. other->move(x() + 40, y() + 40);
  113. other->show();
  114. }
  115. void FbMainWindow::fileOpen()
  116. {
  117. QString filename = QFileDialog::getOpenFileName(this, tr("Open file"), QString(), "Fiction book files (*.fb2)");
  118. if (filename.isEmpty()) return;
  119. FbMainWindow * existing = findFbMainWindow(filename);
  120. if (existing) {
  121. existing->show();
  122. existing->raise();
  123. existing->activateWindow();
  124. return;
  125. }
  126. if (isUntitled && !isWindowModified()) {
  127. mainDock->load(filename);
  128. } else {
  129. FbMainWindow * other = new FbMainWindow(filename, FB2);
  130. other->mainDock->load(filename);
  131. other->move(x() + 40, y() + 40);
  132. other->show();
  133. }
  134. }
  135. bool FbMainWindow::fileSave()
  136. {
  137. if (isUntitled) {
  138. return fileSaveAs();
  139. } else {
  140. return saveFile(curFile);
  141. }
  142. }
  143. bool FbMainWindow::fileSaveAs()
  144. {
  145. FbSaveDialog dlg(this, tr("Save As..."));
  146. dlg.selectFile(curFile);
  147. if (!dlg.exec()) return false;
  148. QString fileName = dlg.fileName();
  149. if (fileName.isEmpty()) return false;
  150. return saveFile(fileName, dlg.codec());
  151. }
  152. void FbMainWindow::about()
  153. {
  154. QString text = tr("The <b>fb2edit</b> is application for editing FB2-files.");
  155. text += QString("<br>") += FbApplication::lastCommit();
  156. QMessageBox::about(this, tr("About fb2edit"), text);
  157. }
  158. void FbMainWindow::documentWasModified()
  159. {
  160. setModified(isSwitched || codeEdit->isModified());
  161. }
  162. void FbMainWindow::cleanChanged(bool clean)
  163. {
  164. setModified(isSwitched || !clean);
  165. }
  166. void FbMainWindow::setModified(bool modified)
  167. {
  168. QFileInfo info = windowFilePath();
  169. QString title = info.fileName();
  170. if (modified) title += QString("[*]");
  171. title += appTitle();
  172. setWindowTitle(title);
  173. setWindowModified(modified);
  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. actionPasteText = act = new QAction(tr("Paste (no style)"), this);
  253. menu->addAction(act);
  254. clipboardDataChanged();
  255. menu->addSeparator();
  256. actionFind = act = new QAction(FbIcon("edit-find"), tr("&Find..."), this);
  257. act->setShortcuts(QKeySequence::Find);
  258. menu->addAction(act);
  259. actionReplace = act = new QAction(FbIcon("edit-find-replace"), tr("&Replace..."), this);
  260. menu->addAction(act);
  261. menu->addSeparator();
  262. act = new QAction(FbIcon("preferences-desktop"), tr("&Settings"), this);
  263. act->setShortcuts(QKeySequence::Preferences);
  264. act->setStatusTip(tr("Application settings"));
  265. connect(act, SIGNAL(triggered()), SLOT(openSettings()));
  266. menu->addAction(act);
  267. menu = menuBar()->addMenu(tr("&Insert", "Main menu"));
  268. actionImage = act = new QAction(FbIcon("insert-image"), tr("&Image"), this);
  269. menu->addAction(act);
  270. actionNote = act = new QAction(FbIcon("insert-text"), tr("&Footnote"), this);
  271. menu->addAction(act);
  272. actionLink = act = new QAction(FbIcon("insert-link"), tr("&Hiperlink"), this);
  273. menu->addAction(act);
  274. menu->addSeparator();
  275. actionBody = act = new QAction(tr("&Body"), this);
  276. menu->addAction(act);
  277. actionSection = act = new QAction(FbIcon("insert-object"), tr("&Section"), this);
  278. menu->addAction(act);
  279. actionTitle = act = new QAction(tr("&Title"), this);
  280. menu->addAction(act);
  281. actionEpigraph = act = new QAction(tr("&Epigraph"), this);
  282. menu->addAction(act);
  283. actionAnnot = act = new QAction(tr("&Annotation"), this);
  284. menu->addAction(act);
  285. actionSubtitle = act = new QAction(tr("&Subtitle"), this);
  286. menu->addAction(act);
  287. actionAuthor = act = new QAction(tr("&Cite"), this);
  288. menu->addAction(act);
  289. actionPoem = act = new QAction(tr("&Poem"), this);
  290. menu->addAction(act);
  291. actionStanza = act = new QAction(tr("&Stanza"), this);
  292. menu->addAction(act);
  293. actionAuthor = act = new QAction(tr("&Author"), this);
  294. menu->addAction(act);
  295. actionDate = act = new QAction(tr("&Date"), this);
  296. menu->addAction(act);
  297. menu->addSeparator();
  298. actionSimpleText = act = new QAction(tr("Simple text"), this);
  299. menu->addAction(act);
  300. actionParaSeparator = act = new QAction(tr("Paragraph"), this);
  301. menu->addAction(act);
  302. actionLineSeparator = act = new QAction(tr("Line end"), this);
  303. menu->addAction(act);
  304. menuText = menu = menuBar()->addMenu(tr("Fo&rmat"));
  305. actionClearFormat = act = new FbTextAction(FbIcon("edit-clear"), tr("Clear format"), QWebPage::RemoveFormat, this);
  306. menu->addAction(act);
  307. menu->addSeparator();
  308. actionTextBold = act = new FbTextAction(FbIcon("format-text-bold"), tr("&Bold"), QWebPage::ToggleBold, this);
  309. act->setShortcuts(QKeySequence::Bold);
  310. act->setCheckable(true);
  311. menu->addAction(act);
  312. actionTextItalic = act = new FbTextAction(FbIcon("format-text-italic"), tr("&Italic"), QWebPage::ToggleItalic, this);
  313. act->setShortcuts(QKeySequence::Italic);
  314. act->setCheckable(true);
  315. menu->addAction(act);
  316. actionTextStrike = act = new FbTextAction(FbIcon("format-text-strikethrough"), tr("&Strikethrough"), QWebPage::ToggleStrikethrough, this);
  317. act->setCheckable(true);
  318. menu->addAction(act);
  319. actionTextSup = act = new FbTextAction(FbIcon("format-text-superscript"), tr("Su&perscript"), QWebPage::ToggleSuperscript, this);
  320. act->setCheckable(true);
  321. menu->addAction(act);
  322. actionTextSub = act = new FbTextAction(FbIcon("format-text-subscript"), tr("Su&bscript"), QWebPage::ToggleSubscript, this);
  323. act->setCheckable(true);
  324. menu->addAction(act);
  325. actionTextCode = act = new QAction(FbIcon("utilities-terminal"), tr("&Code"), this);
  326. act->setCheckable(true);
  327. menu->addAction(act);
  328. menu->addSeparator();
  329. actionSectionAdd = act = new FbTextAction(FbIcon("format-indent-more"), tr("Create section"), QWebPage::ToggleSubscript, this);
  330. menu->addAction(act);
  331. actionSectionDel = act = new FbTextAction(FbIcon("format-indent-less"), tr("Remove section"), QWebPage::ToggleSubscript, this);
  332. menu->addAction(act);
  333. actionTextTitle = act = new FbTextAction(FbIcon("format-justify-center"), tr("Make title"), QWebPage::ToggleSubscript, this);
  334. menu->addAction(act);
  335. menuView = menu = menuBar()->addMenu(tr("&View"));
  336. tool->addSeparator();
  337. QActionGroup * viewGroup = new QActionGroup(this);
  338. act = new QAction(tr("&Text"), this);
  339. act->setCheckable(true);
  340. act->setChecked(true);
  341. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  342. viewGroup->addAction(act);
  343. menu->addAction(act);
  344. tool->addAction(act);
  345. act = new QAction(tr("&Head"), this);
  346. act->setCheckable(true);
  347. connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
  348. viewGroup->addAction(act);
  349. menu->addAction(act);
  350. tool->addAction(act);
  351. act = new QAction(tr("&XML"), this);
  352. act->setCheckable(true);
  353. connect(act, SIGNAL(triggered()), this, SLOT(viewCode()));
  354. viewGroup->addAction(act);
  355. menu->addAction(act);
  356. tool->addAction(act);
  357. #ifdef QT_DEBUG
  358. act = new QAction(tr("&HTML"), this);
  359. act->setCheckable(true);
  360. connect(act, SIGNAL(triggered()), this, SLOT(viewHtml()));
  361. viewGroup->addAction(act);
  362. menu->addAction(act);
  363. #endif // _DEBUG
  364. menu->addSeparator();
  365. actionZoomIn = act = new QAction(FbIcon("zoom-in"), tr("Zoom in"), this);
  366. act->setShortcuts(QKeySequence::ZoomIn);
  367. menu->addAction(act);
  368. actionZoomOut = act = new QAction(FbIcon("zoom-out"), tr("Zoom out"), this);
  369. act->setShortcuts(QKeySequence::ZoomOut);
  370. menu->addAction(act);
  371. actionZoomReset = act = new QAction(FbIcon("zoom-original"), tr("Zoom original"), this);
  372. menu->addAction(act);
  373. menu->addSeparator();
  374. actionContents = act = new QAction(tr("&Contents"), this);
  375. act->setCheckable(true);
  376. connect(act, SIGNAL(triggered()), this, SLOT(viewTree()));
  377. menu->addAction(act);
  378. actionPictures = act = new QAction(tr("&Pictures"), this);
  379. act->setCheckable(true);
  380. connect(act, SIGNAL(triggered()), this, SLOT(viewImgs()));
  381. menu->addAction(act);
  382. actionInspect = act = new QAction(tr("&Web inspector"), this);
  383. act->setCheckable(true);
  384. connect(this, SIGNAL(showInspectorChecked(bool)), actionInspect, SLOT(setChecked(bool)));
  385. menu->addAction(act);
  386. menuBar()->addSeparator();
  387. menu = menuBar()->addMenu(tr("&Help"));
  388. act = new QAction(FbIcon("help-about"), tr("&About"), this);
  389. act->setStatusTip(tr("Show the application's About box"));
  390. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  391. menu->addAction(act);
  392. act = new QAction(tr("About &Qt"), this);
  393. act->setStatusTip(tr("Show the Qt library's About box"));
  394. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  395. menu->addAction(act);
  396. }
  397. void FbMainWindow::openSettings()
  398. {
  399. FbSetupDlg dlg(this);
  400. dlg.exec();
  401. }
  402. void FbMainWindow::createTree()
  403. {
  404. if (textFrame && centralWidget() == textFrame) {
  405. dockTree = new FbDockWidget(tr("Contents"), this);
  406. dockTree->setWidget(new FbTreeWidget(textFrame->view(), this));
  407. connect(dockTree, SIGNAL(visibilityChanged(bool)), actionContents, SLOT(setChecked(bool)));
  408. connect(dockTree, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  409. addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  410. }
  411. }
  412. void FbMainWindow::createImgs()
  413. {
  414. if (textFrame && centralWidget() == textFrame) {
  415. dockImgs = new FbDockWidget(tr("Pictures"), this);
  416. dockImgs->setWidget(new FbListWidget(textFrame->view(), this));
  417. connect(dockImgs, SIGNAL(visibilityChanged(bool)), actionPictures, SLOT(setChecked(bool)));
  418. connect(dockImgs, SIGNAL(destroyed()), SLOT(imgsDestroyed()));
  419. addDockWidget(Qt::RightDockWidgetArea, dockImgs);
  420. }
  421. }
  422. void FbMainWindow::selectionChanged()
  423. {
  424. FbTextEdit *view = textFrame->view();
  425. actionCut->setEnabled(view->actionEnabled(QWebPage::Cut));
  426. actionCopy->setEnabled(view->actionEnabled(QWebPage::Copy));
  427. statusBar()->showMessage(view->page()->status());
  428. }
  429. void FbMainWindow::canUndoChanged(bool canUndo)
  430. {
  431. actionUndo->setEnabled(canUndo);
  432. }
  433. void FbMainWindow::canRedoChanged(bool canRedo)
  434. {
  435. actionRedo->setEnabled(canRedo);
  436. }
  437. void FbMainWindow::undoChanged()
  438. {
  439. actionUndo->setEnabled(textFrame->view()->actionEnabled(QWebPage::Undo));
  440. }
  441. void FbMainWindow::redoChanged()
  442. {
  443. actionRedo->setEnabled(textFrame->view()->actionEnabled(QWebPage::Redo));
  444. }
  445. void FbMainWindow::createStatusBar()
  446. {
  447. statusBar()->showMessage(tr("Ready"));
  448. }
  449. void FbMainWindow::readSettings()
  450. {
  451. QSettings settings;
  452. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  453. QSize size = settings.value("size", QSize(400, 400)).toSize();
  454. move(pos);
  455. resize(size);
  456. }
  457. void FbMainWindow::writeSettings()
  458. {
  459. QSettings settings;
  460. settings.setValue("pos", pos());
  461. settings.setValue("size", size());
  462. }
  463. bool FbMainWindow::maybeSave()
  464. {
  465. if (textFrame && textFrame->view()->isModified()) {
  466. QMessageBox::StandardButton ret;
  467. ret = QMessageBox::warning(this, qApp->applicationName(),
  468. tr("The document has been modified. Do you want to save your changes?"),
  469. QMessageBox::Save | QMessageBox::Discard
  470. | QMessageBox::Cancel);
  471. if (ret == QMessageBox::Save)
  472. return fileSave();
  473. else if (ret == QMessageBox::Cancel)
  474. return false;
  475. }
  476. return true;
  477. }
  478. bool FbMainWindow::saveFile(const QString &fileName, const QString &codec)
  479. {
  480. QFile file(fileName);
  481. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  482. QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  483. return false;
  484. }
  485. if (textFrame) {
  486. isSwitched = false;
  487. textFrame->view()->save(&file, codec);
  488. setCurrentFile(fileName);
  489. return true;
  490. }
  491. if (codeEdit) {
  492. QTextStream out(&file);
  493. out << codeEdit->toPlainText();
  494. setCurrentFile(fileName);
  495. return true;
  496. }
  497. return false;
  498. }
  499. void FbMainWindow::setCurrentFile(const QString &filename)
  500. {
  501. if (filename.isEmpty()) {
  502. static int sequenceNumber = 1;
  503. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  504. } else {
  505. QFileInfo info = filename;
  506. curFile = info.canonicalFilePath();
  507. }
  508. setWindowFilePath(curFile);
  509. setModified(false);
  510. }
  511. QString FbMainWindow::appTitle() const
  512. {
  513. return QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
  514. }
  515. FbMainWindow *FbMainWindow::findFbMainWindow(const QString &fileName)
  516. {
  517. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  518. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  519. FbMainWindow *mainWin = qobject_cast<FbMainWindow *>(widget);
  520. if (mainWin && mainWin->curFile == canonicalFilePath)
  521. return mainWin;
  522. }
  523. return 0;
  524. }
  525. void FbMainWindow::checkScintillaUndo()
  526. {
  527. if (!codeEdit) return;
  528. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  529. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  530. }
  531. void FbMainWindow::createTextToolbar()
  532. {
  533. FbTextEdit * textEdit = textFrame->view();
  534. FbTextPage * textPage = textEdit->page();
  535. connect(textPage->undoStack(), SIGNAL(cleanChanged(bool)), SLOT(cleanChanged(bool)));
  536. connect(textPage->undoStack(), SIGNAL(canUndoChanged(bool)), SLOT(canUndoChanged(bool)));
  537. connect(textPage->undoStack(), SIGNAL(canRedoChanged(bool)), SLOT(canRedoChanged(bool)));
  538. connect(textPage, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  539. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  540. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  541. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  542. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  543. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  544. connect(actionPasteText, SIGNAL(triggered()), textEdit->pageAction(QWebPage::PasteAndMatchStyle), SIGNAL(triggered()));
  545. connect(actionClearFormat, SIGNAL(triggered()), textEdit->pageAction(QWebPage::RemoveFormat), SIGNAL(triggered()));
  546. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  547. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  548. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  549. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  550. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  551. connect(textEdit->pageAction(QWebPage::RemoveFormat), SIGNAL(changed()), actionClearFormat, SLOT(updateEnabled()));
  552. connect(textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(changed()), actionTextBold, SLOT(updateChecked()));
  553. connect(textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(changed()), actionTextItalic, SLOT(updateChecked()));
  554. connect(textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(changed()), actionTextStrike, SLOT(updateChecked()));
  555. connect(textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(changed()), actionTextSub, SLOT(updateChecked()));
  556. connect(textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(changed()), actionTextSup, SLOT(updateChecked()));
  557. connect(actionFind, SIGNAL(triggered()), textEdit, SLOT(find()));
  558. connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));
  559. connect(actionNote, SIGNAL(triggered()), textEdit, SLOT(insertNote()));
  560. connect(actionLink, SIGNAL(triggered()), textEdit, SLOT(insertLink()));
  561. connect(actionTitle, SIGNAL(triggered()), textPage, SLOT(insertTitle()));
  562. connect(actionAnnot, SIGNAL(triggered()), textPage, SLOT(insertAnnot()));
  563. connect(actionAuthor, SIGNAL(triggered()), textPage, SLOT(insertAuthor()));
  564. connect(actionEpigraph, SIGNAL(triggered()), textPage, SLOT(insertEpigraph()));
  565. connect(actionSubtitle, SIGNAL(triggered()), textPage, SLOT(insertSubtitle()));
  566. connect(actionSection, SIGNAL(triggered()), textPage, SLOT(insertSection()));
  567. connect(actionStanza, SIGNAL(triggered()), textPage, SLOT(insertStanza()));
  568. connect(actionPoem, SIGNAL(triggered()), textPage, SLOT(insertPoem()));
  569. connect(actionDate, SIGNAL(triggered()), textPage, SLOT(insertDate()));
  570. connect(actionBody, SIGNAL(triggered()), textPage, SLOT(insertBody()));
  571. connect(actionSimpleText, SIGNAL(triggered()), textPage, SLOT(insertText()));
  572. connect(actionParaSeparator, SIGNAL(triggered()), textEdit->pageAction(QWebPage::InsertParagraphSeparator), SIGNAL(triggered()));
  573. connect(actionLineSeparator, SIGNAL(triggered()), textEdit->pageAction(QWebPage::InsertLineSeparator), SIGNAL(triggered()));
  574. connect(actionSectionAdd, SIGNAL(triggered()), textPage, SLOT(createSection()));
  575. connect(actionSectionDel, SIGNAL(triggered()), textPage, SLOT(deleteSection()));
  576. connect(actionTextTitle, SIGNAL(triggered()), textPage, SLOT(createTitle()));
  577. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  578. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  579. connect(actionZoomReset, SIGNAL(triggered()), textEdit, SLOT(zoomReset()));
  580. FB2DELETE(toolEdit);
  581. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  582. tool->setMovable(false);
  583. tool->addSeparator();
  584. textEdit->addTools(tool);
  585. tool->addSeparator();
  586. tool->addAction(actionSectionAdd);
  587. tool->addAction(actionSectionDel);
  588. tool->addAction(actionTextTitle);
  589. tool->addSeparator();
  590. tool->addAction(actionImage);
  591. tool->addAction(actionNote);
  592. tool->addAction(actionLink);
  593. tool->addAction(actionSection);
  594. }
  595. void FbMainWindow::viewText(FbTextPage *page)
  596. {
  597. if (textFrame && centralWidget() == textFrame) return;
  598. if (textFrame) textFrame->hideInspector();
  599. if (codeEdit) {
  600. QString xml = codeEdit->text();
  601. page = new FbTextPage(this);
  602. if (!page->load(QString(), xml)) {
  603. delete page;
  604. return;
  605. }
  606. isSwitched = true;
  607. }
  608. FB2DELETE(codeEdit);
  609. FB2DELETE(headTree);
  610. if (textFrame) {
  611. createTextToolbar();
  612. } else {
  613. textFrame = new FbTextFrame(this, actionInspect);
  614. }
  615. setCentralWidget(textFrame);
  616. FbTextEdit *textEdit = textFrame->view();
  617. if (page) {
  618. page->setParent(textEdit);
  619. textEdit->setPage(page);
  620. }
  621. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(createTextToolbar()));
  622. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  623. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  624. actionContents->setEnabled(true);
  625. actionPictures->setEnabled(true);
  626. actionInspect->setEnabled(true);
  627. textEdit->setFocus();
  628. viewTree();
  629. }
  630. void FbMainWindow::viewHead()
  631. {
  632. if (headTree && centralWidget() == headTree) return;
  633. if (textFrame) textFrame->hideInspector();
  634. FbTextPage *page = 0;
  635. if (codeEdit) {
  636. QString xml = codeEdit->text();
  637. page = new FbTextPage(this);
  638. if (!page->load(QString(), xml)) {
  639. delete page;
  640. return;
  641. }
  642. isSwitched = true;
  643. }
  644. FB2DELETE(dockTree);
  645. FB2DELETE(dockImgs);
  646. FB2DELETE(codeEdit);
  647. FB2DELETE(toolEdit);
  648. if (!textFrame) {
  649. textFrame = new FbTextFrame(this, actionInspect);
  650. FbTextEdit *textEdit = textFrame->view();
  651. if (page) {
  652. page->setParent(textEdit);
  653. textEdit->setPage(page);
  654. }
  655. }
  656. if (!headTree) {
  657. headTree = new FbHeadEdit(this);
  658. headTree->setText(textFrame->view());
  659. connect(headTree, SIGNAL(status(QString)), this, SLOT(status(QString)));
  660. }
  661. this->setFocus();
  662. textFrame->setParent(NULL);
  663. setCentralWidget(headTree);
  664. textFrame->setParent(this);
  665. headTree->updateTree();
  666. headTree->setFocus();
  667. if (textFrame) {
  668. actionUndo->disconnect();
  669. actionRedo->disconnect();
  670. actionCut->disconnect();
  671. actionCopy->disconnect();
  672. actionPaste->disconnect();
  673. actionTextBold->disconnect();
  674. actionTextItalic->disconnect();
  675. actionTextStrike->disconnect();
  676. actionTextSub->disconnect();
  677. actionTextSup->disconnect();
  678. }
  679. FB2DELETE(toolEdit);
  680. toolEdit = addToolBar(tr("Edit"));
  681. headTree->initToolbar(*toolEdit);
  682. toolEdit->addSeparator();
  683. toolEdit->setMovable(false);
  684. actionContents->setEnabled(false);
  685. actionPictures->setEnabled(false);
  686. actionInspect->setEnabled(true);
  687. }
  688. void FbMainWindow::viewCode()
  689. {
  690. if (codeEdit && centralWidget() == codeEdit) return;
  691. bool load = false;
  692. QByteArray xml;
  693. if (textFrame) {
  694. textFrame->view()->save(&xml);
  695. isSwitched = true;
  696. load = true;
  697. }
  698. FB2DELETE(textFrame);
  699. FB2DELETE(dockTree);
  700. FB2DELETE(dockImgs);
  701. FB2DELETE(headTree);
  702. if (!codeEdit) {
  703. codeEdit = new FbCodeEdit;
  704. }
  705. if (load) codeEdit->load(xml);
  706. setCentralWidget(codeEdit);
  707. codeEdit->setFocus();
  708. FB2DELETE(toolEdit);
  709. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  710. tool->addSeparator();
  711. tool->addAction(actionUndo);
  712. tool->addAction(actionRedo);
  713. tool->addSeparator();
  714. tool->addAction(actionCut);
  715. tool->addAction(actionCopy);
  716. tool->addAction(actionPaste);
  717. tool->addSeparator();
  718. tool->addAction(actionZoomIn);
  719. tool->addAction(actionZoomOut);
  720. tool->addAction(actionZoomReset);
  721. tool->setMovable(false);
  722. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  723. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  724. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  725. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  726. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  727. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  728. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  729. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  730. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  731. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  732. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  733. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  734. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  735. actionContents->setEnabled(false);
  736. actionPictures->setEnabled(false);
  737. actionInspect->setEnabled(false);
  738. }
  739. void FbMainWindow::viewHtml()
  740. {
  741. if (codeEdit && centralWidget() == codeEdit) return;
  742. if (!textFrame) return;
  743. QString html = textFrame->view()->page()->mainFrame()->toHtml();
  744. isSwitched = true;
  745. FB2DELETE(textFrame);
  746. FB2DELETE(dockTree);
  747. FB2DELETE(dockImgs);
  748. FB2DELETE(headTree);
  749. if (!codeEdit) {
  750. codeEdit = new FbCodeEdit;
  751. }
  752. codeEdit->setPlainText(html);
  753. setCentralWidget(codeEdit);
  754. codeEdit->setFocus();
  755. FB2DELETE(toolEdit);
  756. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  757. tool->addSeparator();
  758. tool->addAction(actionUndo);
  759. tool->addAction(actionRedo);
  760. tool->addSeparator();
  761. tool->addAction(actionCut);
  762. tool->addAction(actionCopy);
  763. tool->addAction(actionPaste);
  764. tool->addSeparator();
  765. tool->addAction(actionZoomIn);
  766. tool->addAction(actionZoomOut);
  767. tool->addAction(actionZoomReset);
  768. tool->setMovable(false);
  769. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  770. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  771. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  772. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  773. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  774. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  775. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  776. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  777. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  778. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  779. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  780. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  781. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  782. actionContents->setEnabled(false);
  783. actionPictures->setEnabled(false);
  784. actionInspect->setEnabled(false);
  785. }
  786. void FbMainWindow::viewTree()
  787. {
  788. if (dockTree) dockTree->deleteLater(); else createTree();
  789. }
  790. void FbMainWindow::viewImgs()
  791. {
  792. if (dockImgs) dockImgs->deleteLater(); else createImgs();
  793. }
  794. void FbMainWindow::clipboardDataChanged()
  795. {
  796. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  797. actionPaste->setEnabled(md->hasText());
  798. actionPasteText->setEnabled(md->hasText());
  799. }
  800. }
  801. void FbMainWindow::status(const QString &text)
  802. {
  803. statusBar()->showMessage(text);
  804. }