1
0

fb2main.cpp 24 KB

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