fb2main.cpp 28 KB

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