fb2main.cpp 33 KB

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