fb2main.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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 "fb2tree.hpp"
  10. #include "fb2view.hpp"
  11. #include "fb2head.hpp"
  12. #include "fb2utils.h"
  13. Fb2MainWindow::Fb2MainWindow()
  14. {
  15. init();
  16. setCurrentFile();
  17. viewText();
  18. textEdit->load(":blank.fb2");
  19. }
  20. Fb2MainWindow::Fb2MainWindow(const QString &filename, ViewMode mode)
  21. {
  22. init();
  23. setCurrentFile(filename);
  24. if (mode == FB2) {
  25. viewText();
  26. textEdit->load(filename);
  27. } else {
  28. viewCode();
  29. loadXML(filename);
  30. }
  31. }
  32. void Fb2MainWindow::init()
  33. {
  34. connect(qApp, SIGNAL(logMessage(QString)), SLOT(logMessage(QString)));
  35. setAttribute(Qt::WA_DeleteOnClose);
  36. setWindowIcon(QIcon(":icon.ico"));
  37. isUntitled = true;
  38. createActions();
  39. createStatusBar();
  40. textEdit = NULL;
  41. noteEdit = NULL;
  42. codeEdit = NULL;
  43. headTree = NULL;
  44. toolEdit = NULL;
  45. dockTree = NULL;
  46. messageEdit = NULL;
  47. readSettings();
  48. setUnifiedTitleAndToolBarOnMac(true);
  49. }
  50. void Fb2MainWindow::logMessage(const QString &message)
  51. {
  52. if (!messageEdit) {
  53. messageEdit = new QTextEdit(this);
  54. connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
  55. QDockWidget * dock = new QDockWidget(tr("Message log"), this);
  56. dock->setAttribute(Qt::WA_DeleteOnClose);
  57. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  58. dock->setWidget(messageEdit);
  59. addDockWidget(Qt::BottomDockWidgetArea, dock);
  60. messageEdit->setMaximumHeight(80);
  61. }
  62. messageEdit->append(message);
  63. }
  64. void Fb2MainWindow::logShowed()
  65. {
  66. messageEdit->setMaximumHeight(QWIDGETSIZE_MAX);
  67. }
  68. void Fb2MainWindow::logDestroyed()
  69. {
  70. messageEdit = NULL;
  71. }
  72. void Fb2MainWindow::treeDestroyed()
  73. {
  74. dockTree = NULL;
  75. }
  76. bool Fb2MainWindow::loadXML(const QString &filename)
  77. {
  78. if (!filename.isEmpty()) {
  79. QFile file(filename);
  80. if (file.open(QFile::ReadOnly | QFile::Text)) {
  81. codeEdit->clear();
  82. return codeEdit->read(&file);
  83. }
  84. }
  85. return false;
  86. }
  87. void Fb2MainWindow::closeEvent(QCloseEvent *event)
  88. {
  89. if (maybeSave()) {
  90. writeSettings();
  91. event->accept();
  92. } else {
  93. event->ignore();
  94. }
  95. }
  96. void Fb2MainWindow::fileNew()
  97. {
  98. Fb2MainWindow *other = new Fb2MainWindow;
  99. other->move(x() + 40, y() + 40);
  100. other->show();
  101. }
  102. void Fb2MainWindow::fileOpen()
  103. {
  104. QString filename = QFileDialog::getOpenFileName(this, tr("Open file"), QString(), "Fiction book files (*.fb2)");
  105. if (filename.isEmpty()) return;
  106. Fb2MainWindow * existing = findFb2MainWindow(filename);
  107. if (existing) {
  108. existing->show();
  109. existing->raise();
  110. existing->activateWindow();
  111. return;
  112. }
  113. if (textEdit) {
  114. if (isUntitled && !isWindowModified()) {
  115. setCurrentFile(filename);
  116. textEdit->load(filename);
  117. } else {
  118. Fb2MainWindow * other = new Fb2MainWindow(filename, FB2);
  119. other->move(x() + 40, y() + 40);
  120. other->show();
  121. }
  122. } else if (codeEdit) {
  123. if (isUntitled && !isWindowModified()) {
  124. setCurrentFile(filename);
  125. loadXML(filename);
  126. } else {
  127. Fb2MainWindow * other = new Fb2MainWindow(filename, XML);
  128. other->move(x() + 40, y() + 40);
  129. other->show();
  130. }
  131. }
  132. }
  133. bool Fb2MainWindow::fileSave()
  134. {
  135. if (isUntitled) {
  136. return fileSaveAs();
  137. } else {
  138. return saveFile(curFile);
  139. }
  140. }
  141. bool Fb2MainWindow::fileSaveAs()
  142. {
  143. Fb2SaveDialog dlg(this, tr("Save As..."));
  144. dlg.selectFile(curFile);
  145. if (!dlg.exec()) return false;
  146. QString fileName = dlg.fileName();
  147. if (fileName.isEmpty()) return false;
  148. return saveFile(fileName, dlg.codec());
  149. }
  150. void Fb2MainWindow::about()
  151. {
  152. QMessageBox::about(this, tr("About fb2edit"),
  153. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  154. }
  155. void Fb2MainWindow::documentWasModified()
  156. {
  157. if (isWindowModified()) return;
  158. QFileInfo info = windowFilePath();
  159. QString title = info.fileName();
  160. title += QString("[*]") += appTitle();
  161. setWindowTitle(title);
  162. setWindowModified(true);
  163. }
  164. void Fb2MainWindow::createActions()
  165. {
  166. QAction * act;
  167. QMenu * menu;
  168. QToolBar * tool;
  169. QList<QAction*> actions;
  170. menu = menuBar()->addMenu(tr("&File"));
  171. tool = addToolBar(tr("File"));
  172. tool->setMovable(false);
  173. act = new QAction(FB2::icon("document-new"), tr("&New"), this);
  174. act->setPriority(QAction::LowPriority);
  175. act->setShortcuts(QKeySequence::New);
  176. act->setStatusTip(tr("Create a new file"));
  177. connect(act, SIGNAL(triggered()), this, SLOT(fileNew()));
  178. menu->addAction(act);
  179. tool->addAction(act);
  180. act = new QAction(FB2::icon("document-open"), tr("&Open..."), this);
  181. act->setShortcuts(QKeySequence::Open);
  182. act->setStatusTip(tr("Open an existing file"));
  183. connect(act, SIGNAL(triggered()), this, SLOT(fileOpen()));
  184. menu->addAction(act);
  185. tool->addAction(act);
  186. act = new QAction(FB2::icon("document-save"), tr("&Save"), this);
  187. act->setShortcuts(QKeySequence::Save);
  188. act->setStatusTip(tr("Save the document to disk"));
  189. connect(act, SIGNAL(triggered()), this, SLOT(fileSave()));
  190. menu->addAction(act);
  191. tool->addAction(act);
  192. act = new QAction(FB2::icon("document-save-as"), tr("Save &As..."), this);
  193. act->setShortcuts(QKeySequence::SaveAs);
  194. act->setStatusTip(tr("Save the document under a new name"));
  195. connect(act, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
  196. menu->addAction(act);
  197. menu->addSeparator();
  198. act = new QAction(FB2::icon("window-close"), tr("&Close"), this);
  199. act->setShortcuts(QKeySequence::Close);
  200. act->setStatusTip(tr("Close this window"));
  201. connect(act, SIGNAL(triggered()), this, SLOT(close()));
  202. menu->addAction(act);
  203. act = new QAction(FB2::icon("application-exit"), tr("E&xit"), this);
  204. act->setShortcuts(QKeySequence::Quit);
  205. act->setStatusTip(tr("Exit the application"));
  206. connect(act, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  207. menu->addAction(act);
  208. menuEdit = menu = menuBar()->addMenu(tr("&Edit"));
  209. connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
  210. actionUndo = act = new QAction(FB2::icon("edit-undo"), tr("&Undo"), this);
  211. act->setPriority(QAction::LowPriority);
  212. act->setShortcut(QKeySequence::Undo);
  213. act->setEnabled(false);
  214. menu->addAction(act);
  215. actionRedo = act = new QAction(FB2::icon("edit-redo"), tr("&Redo"), this);
  216. act->setPriority(QAction::LowPriority);
  217. act->setShortcut(QKeySequence::Redo);
  218. act->setEnabled(false);
  219. menu->addAction(act);
  220. menu->addSeparator();
  221. actionCut = act = new QAction(FB2::icon("edit-cut"), tr("Cu&t"), this);
  222. act->setPriority(QAction::LowPriority);
  223. act->setShortcuts(QKeySequence::Cut);
  224. act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  225. act->setEnabled(false);
  226. menu->addAction(act);
  227. actionCopy = act = new QAction(FB2::icon("edit-copy"), tr("&Copy"), this);
  228. act->setPriority(QAction::LowPriority);
  229. act->setShortcuts(QKeySequence::Copy);
  230. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  231. act->setEnabled(false);
  232. menu->addAction(act);
  233. actionPaste = act = new QAction(FB2::icon("edit-paste"), tr("&Paste"), this);
  234. act->setPriority(QAction::LowPriority);
  235. act->setShortcuts(QKeySequence::Paste);
  236. act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  237. menu->addAction(act);
  238. clipboardDataChanged();
  239. menu->addSeparator();
  240. actionFind = act = new QAction(FB2::icon("edit-find"), tr("&Find..."), this);
  241. act->setShortcuts(QKeySequence::Find);
  242. menu->addAction(act);
  243. actionReplace = act = new QAction(FB2::icon("edit-find-replace"), tr("&Replace..."), this);
  244. menu->addAction(act);
  245. menu->addSeparator();
  246. act = new QAction(FB2::icon("preferences-desktop"), tr("&Settings"), this);
  247. act->setShortcuts(QKeySequence::Preferences);
  248. act->setStatusTip(tr("Application settings"));
  249. connect(act, SIGNAL(triggered()), SLOT(openSettings()));
  250. menu->addAction(act);
  251. menu = menuBar()->addMenu(tr("&Insert", "Main menu"));
  252. actionImage = act = new QAction(FB2::icon("insert-image"), tr("&Image"), this);
  253. menu->addAction(act);
  254. actionNote = act = new QAction(FB2::icon("insert-text"), tr("&Footnote"), this);
  255. menu->addAction(act);
  256. actionLink = act = new QAction(FB2::icon("insert-link"), tr("&Hiperlink"), this);
  257. menu->addAction(act);
  258. menu->addSeparator();
  259. actionSection = act = new QAction(FB2::icon("insert-object"), tr("&Section"), this);
  260. menu->addAction(act);
  261. actionTitle = act = new QAction(tr("&Title"), this);
  262. menu->addAction(act);
  263. actionSubtitle = act = new QAction(tr("&Subtitle"), this);
  264. menu->addAction(act);
  265. actionAuthor = act = new QAction(tr("&Author"), this);
  266. menu->addAction(act);
  267. actionDescr = act = new QAction(tr("&Annotation"), this);
  268. menu->addAction(act);
  269. actionPoem = act = new QAction(tr("&Poem"), this);
  270. menu->addAction(act);
  271. actionStanza = act = new QAction(tr("&Stanza"), this);
  272. menu->addAction(act);
  273. actionBody = act = new QAction(tr("&Body"), this);
  274. menu->addAction(act);
  275. menuText = menu = menuBar()->addMenu(tr("Fo&rmat"));
  276. actionTextBold = act = new QAction(FB2::icon("format-text-bold"), tr("&Bold"), this);
  277. act->setShortcuts(QKeySequence::Bold);
  278. act->setCheckable(true);
  279. menu->addAction(act);
  280. actionTextItalic = act = new QAction(FB2::icon("format-text-italic"), tr("&Italic"), this);
  281. act->setShortcuts(QKeySequence::Italic);
  282. act->setCheckable(true);
  283. menu->addAction(act);
  284. actionTextStrike = act = new QAction(FB2::icon("format-text-strikethrough"), tr("&Strikethrough"), this);
  285. act->setCheckable(true);
  286. menu->addAction(act);
  287. actionTextSup = act = new QAction(FB2::icon("format-text-superscript"), tr("Su&perscript"), this);
  288. act->setCheckable(true);
  289. menu->addAction(act);
  290. actionTextSub = act = new QAction(FB2::icon("format-text-subscript"), tr("Su&bscript"), this);
  291. act->setCheckable(true);
  292. menu->addAction(act);
  293. menuView = menu = menuBar()->addMenu(tr("&View"));
  294. tool->addSeparator();
  295. QActionGroup * viewGroup = new QActionGroup(this);
  296. act = new QAction(tr("&Text"), this);
  297. act->setCheckable(true);
  298. act->setChecked(true);
  299. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  300. viewGroup->addAction(act);
  301. menu->addAction(act);
  302. tool->addAction(act);
  303. act = new QAction(tr("&Head"), this);
  304. act->setCheckable(true);
  305. connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
  306. viewGroup->addAction(act);
  307. menu->addAction(act);
  308. tool->addAction(act);
  309. act = new QAction(tr("&XML"), this);
  310. act->setCheckable(true);
  311. connect(act, SIGNAL(triggered()), this, SLOT(viewCode()));
  312. viewGroup->addAction(act);
  313. menu->addAction(act);
  314. tool->addAction(act);
  315. menu->addSeparator();
  316. actionZoomIn = act = new QAction(FB2::icon("zoom-in"), tr("Zoom in"), this);
  317. act->setShortcuts(QKeySequence::ZoomIn);
  318. menu->addAction(act);
  319. actionZoomOut = act = new QAction(FB2::icon("zoom-out"), tr("Zoom out"), this);
  320. act->setShortcuts(QKeySequence::ZoomOut);
  321. menu->addAction(act);
  322. actionZoomReset = act = new QAction(FB2::icon("zoom-original"), tr("Zoom original"), this);
  323. menu->addAction(act);
  324. menu->addSeparator();
  325. act = new QAction(tr("&Contents"), this);
  326. connect(act, SIGNAL(triggered()), this, SLOT(viewTree()));
  327. menu->addAction(act);
  328. actionInspect = act = new QAction(tr("&Web inspector"), this);
  329. menu->addAction(act);
  330. menuBar()->addSeparator();
  331. menu = menuBar()->addMenu(tr("&Help"));
  332. act = new QAction(FB2::icon("help-about"), tr("&About"), this);
  333. act->setStatusTip(tr("Show the application's About box"));
  334. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  335. menu->addAction(act);
  336. act = new QAction(tr("About &Qt"), this);
  337. act->setStatusTip(tr("Show the Qt library's About box"));
  338. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  339. menu->addAction(act);
  340. }
  341. void Fb2MainWindow::openSettings()
  342. {
  343. QMessageBox::about(this, tr("Settings"),
  344. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  345. }
  346. void Fb2MainWindow::createTree()
  347. {
  348. if (textEdit && centralWidget() == textEdit) {
  349. dockTree = new QDockWidget(tr("Contents"), this);
  350. dockTree->setAttribute(Qt::WA_DeleteOnClose);
  351. dockTree->setFeatures(QDockWidget::AllDockWidgetFeatures);
  352. dockTree->setWidget(new Fb2TreeWidget(*textEdit, this));
  353. connect(dockTree, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  354. addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  355. }
  356. }
  357. void Fb2MainWindow::selectionChanged()
  358. {
  359. actionCut->setEnabled(textEdit->CutEnabled());
  360. actionCopy->setEnabled(textEdit->CopyEnabled());
  361. actionTextBold->setChecked(textEdit->BoldChecked());
  362. actionTextItalic->setChecked(textEdit->ItalicChecked());
  363. actionTextStrike->setChecked(textEdit->StrikeChecked());
  364. actionTextSub->setChecked(textEdit->SubChecked());
  365. actionTextSup->setChecked(textEdit->SupChecked());
  366. statusBar()->showMessage(textEdit->status());
  367. }
  368. void Fb2MainWindow::undoChanged()
  369. {
  370. actionUndo->setEnabled(textEdit->UndoEnabled());
  371. }
  372. void Fb2MainWindow::redoChanged()
  373. {
  374. actionRedo->setEnabled(textEdit->RedoEnabled());
  375. }
  376. void Fb2MainWindow::createStatusBar()
  377. {
  378. statusBar()->showMessage(tr("Ready"));
  379. }
  380. void Fb2MainWindow::readSettings()
  381. {
  382. QSettings settings;
  383. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  384. QSize size = settings.value("size", QSize(400, 400)).toSize();
  385. move(pos);
  386. resize(size);
  387. }
  388. void Fb2MainWindow::writeSettings()
  389. {
  390. QSettings settings;
  391. settings.setValue("pos", pos());
  392. settings.setValue("size", size());
  393. }
  394. bool Fb2MainWindow::maybeSave()
  395. {
  396. if (textEdit && textEdit->isModified()) {
  397. QMessageBox::StandardButton ret;
  398. ret = QMessageBox::warning(this, qApp->applicationName(),
  399. tr("The document has been modified. Do you want to save your changes?"),
  400. QMessageBox::Save | QMessageBox::Discard
  401. | QMessageBox::Cancel);
  402. if (ret == QMessageBox::Save)
  403. return fileSave();
  404. else if (ret == QMessageBox::Cancel)
  405. return false;
  406. }
  407. return true;
  408. }
  409. bool Fb2MainWindow::saveFile(const QString &fileName, const QString &codec)
  410. {
  411. QFile file(fileName);
  412. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  413. QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  414. return false;
  415. }
  416. if (textEdit) {
  417. textEdit->save(&file, codec);
  418. setCurrentFile(fileName);
  419. }
  420. return true;
  421. /*
  422. QTextStream out(&file);
  423. QApplication::setOverrideCursor(Qt::WaitCursor);
  424. out << textEdit->toPlainText();
  425. QApplication::restoreOverrideCursor();
  426. setCurrentFile(fileName);
  427. statusBar()->showMessage(tr("File saved"), 2000);
  428. */
  429. }
  430. void Fb2MainWindow::setCurrentFile(const QString &filename)
  431. {
  432. static int sequenceNumber = 1;
  433. QString title;
  434. isUntitled = filename.isEmpty();
  435. if (isUntitled) {
  436. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  437. title = curFile;
  438. } else {
  439. QFileInfo info = filename;
  440. curFile = info.canonicalFilePath();
  441. title = info.fileName();
  442. }
  443. title += appTitle();
  444. setWindowModified(false);
  445. setWindowFilePath(curFile);
  446. setWindowTitle(title);
  447. }
  448. QString Fb2MainWindow::appTitle() const
  449. {
  450. return QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
  451. }
  452. Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
  453. {
  454. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  455. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  456. Fb2MainWindow *mainWin = qobject_cast<Fb2MainWindow *>(widget);
  457. if (mainWin && mainWin->curFile == canonicalFilePath)
  458. return mainWin;
  459. }
  460. return 0;
  461. }
  462. void Fb2MainWindow::checkScintillaUndo()
  463. {
  464. if (!codeEdit) return;
  465. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  466. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  467. }
  468. void Fb2MainWindow::viewCode()
  469. {
  470. if (codeEdit && centralWidget() == codeEdit) return;
  471. bool load = false;
  472. QByteArray xml;
  473. QList<int> folds;
  474. if (textEdit) {
  475. textEdit->save(&xml);
  476. load = true;
  477. }
  478. FB2DELETE(textEdit);
  479. FB2DELETE(dockTree);
  480. FB2DELETE(headTree);
  481. if (!codeEdit) {
  482. codeEdit = new Fb2CodeEdit;
  483. }
  484. if (load) codeEdit->load(xml, folds);
  485. setCentralWidget(codeEdit);
  486. codeEdit->setFocus();
  487. FB2DELETE(toolEdit);
  488. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  489. tool->addSeparator();
  490. tool->addAction(actionUndo);
  491. tool->addAction(actionRedo);
  492. tool->addSeparator();
  493. tool->addAction(actionCut);
  494. tool->addAction(actionCopy);
  495. tool->addAction(actionPaste);
  496. tool->addSeparator();
  497. tool->addAction(actionZoomIn);
  498. tool->addAction(actionZoomOut);
  499. tool->addAction(actionZoomReset);
  500. tool->setMovable(false);
  501. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  502. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  503. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  504. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  505. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  506. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  507. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  508. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  509. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  510. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  511. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  512. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  513. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  514. }
  515. void Fb2MainWindow::viewText()
  516. {
  517. if (textEdit && centralWidget() == textEdit) return;
  518. QString xml;
  519. if (codeEdit) xml = codeEdit->text();
  520. FB2DELETE(codeEdit);
  521. FB2DELETE(headTree);
  522. if (!textEdit) {
  523. textEdit = new Fb2WebView(this);
  524. }
  525. setCentralWidget(textEdit);
  526. textEdit->setFocus();
  527. viewTree();
  528. connect(textEdit->page(), SIGNAL(contentsChanged()), SLOT(documentWasModified()));
  529. connect(textEdit->page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  530. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  531. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  532. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  533. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  534. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  535. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  536. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  537. connect(actionFind, SIGNAL(triggered()), textEdit, SLOT(find()));
  538. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  539. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  540. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  541. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  542. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  543. connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));
  544. connect(actionNote, SIGNAL(triggered()), textEdit, SLOT(insertNote()));
  545. connect(actionLink, SIGNAL(triggered()), textEdit, SLOT(insertLink()));
  546. connect(actionTitle, SIGNAL(triggered()), textEdit, SLOT(insertTitle()));
  547. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  548. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  549. connect(actionZoomReset, SIGNAL(triggered()), textEdit, SLOT(zoomReset()));
  550. connect(actionInspect, SIGNAL(triggered()), textEdit, SLOT(showInspector()));
  551. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  552. FB2DELETE(toolEdit);
  553. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  554. tool->setMovable(false);
  555. tool->addSeparator();
  556. FB2::addTools(tool, textEdit);
  557. tool->addSeparator();
  558. tool->addAction(actionImage);
  559. tool->addAction(actionNote);
  560. tool->addAction(actionLink);
  561. tool->addAction(actionSection);
  562. tool->addSeparator();
  563. tool->addAction(actionZoomIn);
  564. tool->addAction(actionZoomOut);
  565. tool->addAction(actionZoomReset);
  566. }
  567. void Fb2MainWindow::viewHead()
  568. {
  569. if (headTree && centralWidget() == headTree) return;
  570. QString xml;
  571. if (codeEdit) xml = codeEdit->text();
  572. FB2DELETE(dockTree);
  573. FB2DELETE(codeEdit);
  574. FB2DELETE(toolEdit);
  575. if (!textEdit) {
  576. textEdit = new Fb2WebView(this);
  577. }
  578. if (!headTree) {
  579. headTree = new Fb2HeadView(*textEdit, this);
  580. connect(headTree, SIGNAL(status(QString)), this, SLOT(status(QString)));
  581. }
  582. this->setFocus();
  583. textEdit->setParent(NULL);
  584. setCentralWidget(headTree);
  585. textEdit->setParent(this);
  586. headTree->updateTree();
  587. headTree->setFocus();
  588. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  589. if (textEdit) {
  590. actionUndo->disconnect();
  591. actionRedo->disconnect();
  592. actionCut->disconnect();
  593. actionCopy->disconnect();
  594. actionPaste->disconnect();
  595. actionTextBold->disconnect();
  596. actionTextItalic->disconnect();
  597. actionTextStrike->disconnect();
  598. actionTextSub->disconnect();
  599. actionTextSup->disconnect();
  600. }
  601. FB2DELETE(toolEdit);
  602. toolEdit = addToolBar(tr("Edit"));
  603. headTree->initToolbar(*toolEdit);
  604. toolEdit->addSeparator();
  605. toolEdit->setMovable(false);
  606. }
  607. void Fb2MainWindow::viewTree()
  608. {
  609. if (dockTree) dockTree->deleteLater(); else createTree();
  610. }
  611. void Fb2MainWindow::clipboardDataChanged()
  612. {
  613. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  614. actionPaste->setEnabled(md->hasText());
  615. }
  616. }
  617. void Fb2MainWindow::status(const QString &text)
  618. {
  619. statusBar()->showMessage(text);
  620. }