fb2main.cpp 23 KB

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