fb2main.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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 (mainDock->mode() != FbMainDock::Text) return;
  405. dockTree = new FbDockWidget(tr("Contents"), this);
  406. dockTree->setWidget(new FbTreeWidget(mainDock->text(), 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. void FbMainWindow::createImgs()
  412. {
  413. if (mainDock->mode() != FbMainDock::Text) return;
  414. dockImgs = new FbDockWidget(tr("Pictures"), this);
  415. dockImgs->setWidget(new FbListWidget(mainDock->text(), this));
  416. connect(dockImgs, SIGNAL(visibilityChanged(bool)), actionPictures, SLOT(setChecked(bool)));
  417. connect(dockImgs, SIGNAL(destroyed()), SLOT(imgsDestroyed()));
  418. addDockWidget(Qt::RightDockWidgetArea, dockImgs);
  419. }
  420. void FbMainWindow::selectionChanged()
  421. {
  422. FbTextEdit *view = mainDock->text();
  423. actionCut->setEnabled(view->actionEnabled(QWebPage::Cut));
  424. actionCopy->setEnabled(view->actionEnabled(QWebPage::Copy));
  425. statusBar()->showMessage(view->page()->status());
  426. }
  427. void FbMainWindow::canUndoChanged(bool canUndo)
  428. {
  429. actionUndo->setEnabled(canUndo);
  430. }
  431. void FbMainWindow::canRedoChanged(bool canRedo)
  432. {
  433. actionRedo->setEnabled(canRedo);
  434. }
  435. void FbMainWindow::undoChanged()
  436. {
  437. actionUndo->setEnabled(mainDock->text()->actionEnabled(QWebPage::Undo));
  438. }
  439. void FbMainWindow::redoChanged()
  440. {
  441. actionRedo->setEnabled(mainDock->text()->actionEnabled(QWebPage::Redo));
  442. }
  443. void FbMainWindow::createStatusBar()
  444. {
  445. statusBar()->showMessage(tr("Ready"));
  446. }
  447. void FbMainWindow::readSettings()
  448. {
  449. QSettings settings;
  450. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  451. QSize size = settings.value("size", QSize(400, 400)).toSize();
  452. move(pos);
  453. resize(size);
  454. }
  455. void FbMainWindow::writeSettings()
  456. {
  457. QSettings settings;
  458. settings.setValue("pos", pos());
  459. settings.setValue("size", size());
  460. }
  461. bool FbMainWindow::maybeSave()
  462. {
  463. /*
  464. if (textFrame && textFrame->view()->isModified()) {
  465. QMessageBox::StandardButton ret;
  466. ret = QMessageBox::warning(this, qApp->applicationName(),
  467. tr("The document has been modified. Do you want to save your changes?"),
  468. QMessageBox::Save | QMessageBox::Discard
  469. | QMessageBox::Cancel);
  470. if (ret == QMessageBox::Save)
  471. return fileSave();
  472. else if (ret == QMessageBox::Cancel)
  473. return false;
  474. }
  475. return true;
  476. */ return false;
  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. bool ok = mainDock->save(&file);
  486. setCurrentFile(fileName);
  487. return ok;
  488. }
  489. void FbMainWindow::setCurrentFile(const QString &filename)
  490. {
  491. if (filename.isEmpty()) {
  492. static int sequenceNumber = 1;
  493. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  494. } else {
  495. QFileInfo info = filename;
  496. curFile = info.canonicalFilePath();
  497. }
  498. setWindowFilePath(curFile);
  499. setModified(false);
  500. }
  501. QString FbMainWindow::appTitle() const
  502. {
  503. return QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
  504. }
  505. FbMainWindow *FbMainWindow::findFbMainWindow(const QString &fileName)
  506. {
  507. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  508. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  509. FbMainWindow *mainWin = qobject_cast<FbMainWindow *>(widget);
  510. if (mainWin && mainWin->curFile == canonicalFilePath)
  511. return mainWin;
  512. }
  513. return 0;
  514. }
  515. void FbMainWindow::checkScintillaUndo()
  516. {
  517. /*
  518. if (!codeEdit) return;
  519. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  520. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  521. */
  522. }
  523. void FbMainWindow::createTextToolbar()
  524. {
  525. FbTextEdit * textEdit = mainDock->text();
  526. FbTextPage * textPage = textEdit->page();
  527. connect(textPage->undoStack(), SIGNAL(cleanChanged(bool)), SLOT(cleanChanged(bool)));
  528. connect(textPage->undoStack(), SIGNAL(canUndoChanged(bool)), SLOT(canUndoChanged(bool)));
  529. connect(textPage->undoStack(), SIGNAL(canRedoChanged(bool)), SLOT(canRedoChanged(bool)));
  530. connect(textPage, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  531. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  532. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  533. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  534. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  535. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  536. connect(actionPasteText, SIGNAL(triggered()), textEdit->pageAction(QWebPage::PasteAndMatchStyle), SIGNAL(triggered()));
  537. connect(actionClearFormat, SIGNAL(triggered()), textEdit->pageAction(QWebPage::RemoveFormat), SIGNAL(triggered()));
  538. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  539. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  540. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  541. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  542. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  543. connect(textEdit->pageAction(QWebPage::RemoveFormat), SIGNAL(changed()), actionClearFormat, SLOT(updateEnabled()));
  544. connect(textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(changed()), actionTextBold, SLOT(updateChecked()));
  545. connect(textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(changed()), actionTextItalic, SLOT(updateChecked()));
  546. connect(textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(changed()), actionTextStrike, SLOT(updateChecked()));
  547. connect(textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(changed()), actionTextSub, SLOT(updateChecked()));
  548. connect(textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(changed()), actionTextSup, SLOT(updateChecked()));
  549. connect(actionFind, SIGNAL(triggered()), textEdit, SLOT(find()));
  550. connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));
  551. connect(actionNote, SIGNAL(triggered()), textEdit, SLOT(insertNote()));
  552. connect(actionLink, SIGNAL(triggered()), textEdit, SLOT(insertLink()));
  553. connect(actionTitle, SIGNAL(triggered()), textPage, SLOT(insertTitle()));
  554. connect(actionAnnot, SIGNAL(triggered()), textPage, SLOT(insertAnnot()));
  555. connect(actionAuthor, SIGNAL(triggered()), textPage, SLOT(insertAuthor()));
  556. connect(actionEpigraph, SIGNAL(triggered()), textPage, SLOT(insertEpigraph()));
  557. connect(actionSubtitle, SIGNAL(triggered()), textPage, SLOT(insertSubtitle()));
  558. connect(actionSection, SIGNAL(triggered()), textPage, SLOT(insertSection()));
  559. connect(actionStanza, SIGNAL(triggered()), textPage, SLOT(insertStanza()));
  560. connect(actionPoem, SIGNAL(triggered()), textPage, SLOT(insertPoem()));
  561. connect(actionDate, SIGNAL(triggered()), textPage, SLOT(insertDate()));
  562. connect(actionBody, SIGNAL(triggered()), textPage, SLOT(insertBody()));
  563. connect(actionSimpleText, SIGNAL(triggered()), textPage, SLOT(insertText()));
  564. connect(actionParaSeparator, SIGNAL(triggered()), textEdit->pageAction(QWebPage::InsertParagraphSeparator), SIGNAL(triggered()));
  565. connect(actionLineSeparator, SIGNAL(triggered()), textEdit->pageAction(QWebPage::InsertLineSeparator), SIGNAL(triggered()));
  566. connect(actionSectionAdd, SIGNAL(triggered()), textPage, SLOT(createSection()));
  567. connect(actionSectionDel, SIGNAL(triggered()), textPage, SLOT(deleteSection()));
  568. connect(actionTextTitle, SIGNAL(triggered()), textPage, SLOT(createTitle()));
  569. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  570. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  571. connect(actionZoomReset, SIGNAL(triggered()), textEdit, SLOT(zoomReset()));
  572. FB2DELETE(toolEdit);
  573. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  574. tool->setMovable(false);
  575. tool->addSeparator();
  576. textEdit->addTools(tool);
  577. tool->addSeparator();
  578. tool->addAction(actionSectionAdd);
  579. tool->addAction(actionSectionDel);
  580. tool->addAction(actionTextTitle);
  581. tool->addSeparator();
  582. tool->addAction(actionImage);
  583. tool->addAction(actionNote);
  584. tool->addAction(actionLink);
  585. tool->addAction(actionSection);
  586. }
  587. void FbMainWindow::viewText(FbTextPage *page)
  588. {
  589. if (textFrame && centralWidget() == textFrame) return;
  590. if (textFrame) textFrame->hideInspector();
  591. if (codeEdit) {
  592. QString xml = codeEdit->text();
  593. page = new FbTextPage(this);
  594. if (!page->load(QString(), xml)) {
  595. delete page;
  596. return;
  597. }
  598. isSwitched = true;
  599. }
  600. FB2DELETE(codeEdit);
  601. FB2DELETE(headTree);
  602. if (textFrame) {
  603. createTextToolbar();
  604. } else {
  605. textFrame = new FbTextFrame(this, actionInspect);
  606. }
  607. setCentralWidget(textFrame);
  608. FbTextEdit *textEdit = textFrame->view();
  609. if (page) {
  610. page->setParent(textEdit);
  611. textEdit->setPage(page);
  612. }
  613. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(createTextToolbar()));
  614. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  615. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  616. actionContents->setEnabled(true);
  617. actionPictures->setEnabled(true);
  618. actionInspect->setEnabled(true);
  619. textEdit->setFocus();
  620. viewTree();
  621. }
  622. void FbMainWindow::viewHead()
  623. {
  624. if (headTree && centralWidget() == headTree) return;
  625. if (textFrame) textFrame->hideInspector();
  626. FbTextPage *page = 0;
  627. if (codeEdit) {
  628. QString xml = codeEdit->text();
  629. page = new FbTextPage(this);
  630. if (!page->load(QString(), xml)) {
  631. delete page;
  632. return;
  633. }
  634. isSwitched = true;
  635. }
  636. FB2DELETE(dockTree);
  637. FB2DELETE(dockImgs);
  638. FB2DELETE(codeEdit);
  639. FB2DELETE(toolEdit);
  640. if (!textFrame) {
  641. textFrame = new FbTextFrame(this, actionInspect);
  642. FbTextEdit *textEdit = textFrame->view();
  643. if (page) {
  644. page->setParent(textEdit);
  645. textEdit->setPage(page);
  646. }
  647. }
  648. if (!headTree) {
  649. headTree = new FbHeadEdit(this);
  650. headTree->setText(textFrame->view());
  651. connect(headTree, SIGNAL(status(QString)), this, SLOT(status(QString)));
  652. }
  653. this->setFocus();
  654. textFrame->setParent(NULL);
  655. setCentralWidget(headTree);
  656. textFrame->setParent(this);
  657. headTree->updateTree();
  658. headTree->setFocus();
  659. if (textFrame) {
  660. actionUndo->disconnect();
  661. actionRedo->disconnect();
  662. actionCut->disconnect();
  663. actionCopy->disconnect();
  664. actionPaste->disconnect();
  665. actionTextBold->disconnect();
  666. actionTextItalic->disconnect();
  667. actionTextStrike->disconnect();
  668. actionTextSub->disconnect();
  669. actionTextSup->disconnect();
  670. }
  671. FB2DELETE(toolEdit);
  672. toolEdit = addToolBar(tr("Edit"));
  673. headTree->initToolbar(*toolEdit);
  674. toolEdit->addSeparator();
  675. toolEdit->setMovable(false);
  676. actionContents->setEnabled(false);
  677. actionPictures->setEnabled(false);
  678. actionInspect->setEnabled(true);
  679. }
  680. void FbMainWindow::viewCode()
  681. {
  682. if (codeEdit && centralWidget() == codeEdit) return;
  683. bool load = false;
  684. QByteArray xml;
  685. if (textFrame) {
  686. textFrame->view()->save(&xml);
  687. isSwitched = true;
  688. load = true;
  689. }
  690. FB2DELETE(textFrame);
  691. FB2DELETE(dockTree);
  692. FB2DELETE(dockImgs);
  693. FB2DELETE(headTree);
  694. if (!codeEdit) {
  695. codeEdit = new FbCodeEdit;
  696. }
  697. if (load) codeEdit->load(xml);
  698. setCentralWidget(codeEdit);
  699. codeEdit->setFocus();
  700. FB2DELETE(toolEdit);
  701. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  702. tool->addSeparator();
  703. tool->addAction(actionUndo);
  704. tool->addAction(actionRedo);
  705. tool->addSeparator();
  706. tool->addAction(actionCut);
  707. tool->addAction(actionCopy);
  708. tool->addAction(actionPaste);
  709. tool->addSeparator();
  710. tool->addAction(actionZoomIn);
  711. tool->addAction(actionZoomOut);
  712. tool->addAction(actionZoomReset);
  713. tool->setMovable(false);
  714. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  715. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  716. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  717. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  718. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  719. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  720. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  721. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  722. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  723. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  724. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  725. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  726. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  727. actionContents->setEnabled(false);
  728. actionPictures->setEnabled(false);
  729. actionInspect->setEnabled(false);
  730. }
  731. void FbMainWindow::viewHtml()
  732. {
  733. if (codeEdit && centralWidget() == codeEdit) return;
  734. if (!textFrame) return;
  735. QString html = textFrame->view()->page()->mainFrame()->toHtml();
  736. isSwitched = true;
  737. FB2DELETE(textFrame);
  738. FB2DELETE(dockTree);
  739. FB2DELETE(dockImgs);
  740. FB2DELETE(headTree);
  741. if (!codeEdit) {
  742. codeEdit = new FbCodeEdit;
  743. }
  744. codeEdit->setPlainText(html);
  745. setCentralWidget(codeEdit);
  746. codeEdit->setFocus();
  747. FB2DELETE(toolEdit);
  748. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  749. tool->addSeparator();
  750. tool->addAction(actionUndo);
  751. tool->addAction(actionRedo);
  752. tool->addSeparator();
  753. tool->addAction(actionCut);
  754. tool->addAction(actionCopy);
  755. tool->addAction(actionPaste);
  756. tool->addSeparator();
  757. tool->addAction(actionZoomIn);
  758. tool->addAction(actionZoomOut);
  759. tool->addAction(actionZoomReset);
  760. tool->setMovable(false);
  761. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  762. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  763. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  764. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  765. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  766. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  767. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  768. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  769. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  770. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  771. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  772. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  773. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  774. actionContents->setEnabled(false);
  775. actionPictures->setEnabled(false);
  776. actionInspect->setEnabled(false);
  777. }
  778. void FbMainWindow::viewTree()
  779. {
  780. if (dockTree) dockTree->deleteLater(); else createTree();
  781. }
  782. void FbMainWindow::viewImgs()
  783. {
  784. if (dockImgs) dockImgs->deleteLater(); else createImgs();
  785. }
  786. void FbMainWindow::clipboardDataChanged()
  787. {
  788. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  789. actionPaste->setEnabled(md->hasText());
  790. actionPasteText->setEnabled(md->hasText());
  791. }
  792. }
  793. void FbMainWindow::status(const QString &text)
  794. {
  795. statusBar()->showMessage(text);
  796. }