fb2main.cpp 32 KB

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