fb2main.cpp 23 KB

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