fb2main.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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::loadFinished(bool ok)
  367. {
  368. Q_UNUSED(ok);
  369. if (headTree) {
  370. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  371. headTree->setModel(model);
  372. model->expand(headTree);
  373. }
  374. }
  375. void Fb2MainWindow::selectionChanged()
  376. {
  377. actionCut->setEnabled(textEdit->CutEnabled());
  378. actionCopy->setEnabled(textEdit->CopyEnabled());
  379. actionTextBold->setChecked(textEdit->BoldChecked());
  380. actionTextItalic->setChecked(textEdit->ItalicChecked());
  381. actionTextStrike->setChecked(textEdit->StrikeChecked());
  382. actionTextSub->setChecked(textEdit->SubChecked());
  383. actionTextSup->setChecked(textEdit->SupChecked());
  384. statusBar()->showMessage(textEdit->status());
  385. }
  386. void Fb2MainWindow::undoChanged()
  387. {
  388. actionUndo->setEnabled(textEdit->UndoEnabled());
  389. }
  390. void Fb2MainWindow::redoChanged()
  391. {
  392. actionRedo->setEnabled(textEdit->RedoEnabled());
  393. }
  394. void Fb2MainWindow::createStatusBar()
  395. {
  396. statusBar()->showMessage(tr("Ready"));
  397. }
  398. void Fb2MainWindow::readSettings()
  399. {
  400. QSettings settings;
  401. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  402. QSize size = settings.value("size", QSize(400, 400)).toSize();
  403. move(pos);
  404. resize(size);
  405. }
  406. void Fb2MainWindow::writeSettings()
  407. {
  408. QSettings settings;
  409. settings.setValue("pos", pos());
  410. settings.setValue("size", size());
  411. }
  412. bool Fb2MainWindow::maybeSave()
  413. {
  414. if (textEdit && textEdit->isModified()) {
  415. QMessageBox::StandardButton ret;
  416. ret = QMessageBox::warning(this, qApp->applicationName(),
  417. tr("The document has been modified. Do you want to save your changes?"),
  418. QMessageBox::Save | QMessageBox::Discard
  419. | QMessageBox::Cancel);
  420. if (ret == QMessageBox::Save)
  421. return fileSave();
  422. else if (ret == QMessageBox::Cancel)
  423. return false;
  424. }
  425. return true;
  426. }
  427. bool Fb2MainWindow::saveFile(const QString &fileName, const QString &codec)
  428. {
  429. QFile file(fileName);
  430. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  431. QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  432. return false;
  433. }
  434. if (textEdit) {
  435. textEdit->save(&file, codec);
  436. setCurrentFile(fileName);
  437. }
  438. return true;
  439. /*
  440. QTextStream out(&file);
  441. QApplication::setOverrideCursor(Qt::WaitCursor);
  442. out << textEdit->toPlainText();
  443. QApplication::restoreOverrideCursor();
  444. setCurrentFile(fileName);
  445. statusBar()->showMessage(tr("File saved"), 2000);
  446. */
  447. }
  448. void Fb2MainWindow::setCurrentFile(const QString &filename)
  449. {
  450. static int sequenceNumber = 1;
  451. QString title;
  452. isUntitled = filename.isEmpty();
  453. if (isUntitled) {
  454. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  455. title = curFile;
  456. } else {
  457. QFileInfo info = filename;
  458. curFile = info.canonicalFilePath();
  459. title = info.fileName();
  460. }
  461. title += appTitle();
  462. setWindowModified(false);
  463. setWindowFilePath(curFile);
  464. setWindowTitle(title);
  465. }
  466. QString Fb2MainWindow::appTitle() const
  467. {
  468. return QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
  469. }
  470. Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
  471. {
  472. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  473. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  474. Fb2MainWindow *mainWin = qobject_cast<Fb2MainWindow *>(widget);
  475. if (mainWin && mainWin->curFile == canonicalFilePath)
  476. return mainWin;
  477. }
  478. return 0;
  479. }
  480. void Fb2MainWindow::checkScintillaUndo()
  481. {
  482. if (!codeEdit) return;
  483. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  484. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  485. }
  486. void Fb2MainWindow::viewCode()
  487. {
  488. if (codeEdit && centralWidget() == codeEdit) return;
  489. bool load = false;
  490. QByteArray xml;
  491. QList<int> folds;
  492. if (textEdit) {
  493. textEdit->save(&xml);
  494. load = true;
  495. }
  496. FB2DELETE(textEdit);
  497. FB2DELETE(dockTree);
  498. FB2DELETE(headTree);
  499. if (!codeEdit) {
  500. codeEdit = new Fb2CodeEdit;
  501. }
  502. if (load) codeEdit->load(xml, folds);
  503. setCentralWidget(codeEdit);
  504. codeEdit->setFocus();
  505. FB2DELETE(toolEdit);
  506. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  507. tool->addSeparator();
  508. tool->addAction(actionUndo);
  509. tool->addAction(actionRedo);
  510. tool->addSeparator();
  511. tool->addAction(actionCut);
  512. tool->addAction(actionCopy);
  513. tool->addAction(actionPaste);
  514. tool->addSeparator();
  515. tool->addAction(actionZoomIn);
  516. tool->addAction(actionZoomOut);
  517. tool->addAction(actionZoomReset);
  518. tool->setMovable(false);
  519. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  520. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  521. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  522. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  523. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  524. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  525. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  526. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  527. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  528. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  529. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  530. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  531. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  532. }
  533. void Fb2MainWindow::viewText()
  534. {
  535. if (textEdit && centralWidget() == textEdit) return;
  536. QString xml;
  537. if (codeEdit) xml = codeEdit->text();
  538. FB2DELETE(codeEdit);
  539. FB2DELETE(headTree);
  540. if (!textEdit) {
  541. textEdit = new Fb2WebView(this);
  542. }
  543. setCentralWidget(textEdit);
  544. textEdit->setFocus();
  545. viewTree();
  546. connect(textEdit->page(), SIGNAL(contentsChanged()), SLOT(documentWasModified()));
  547. connect(textEdit->page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  548. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  549. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  550. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  551. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  552. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  553. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  554. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  555. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  556. connect(actionFind, SIGNAL(triggered()), textEdit, SLOT(find()));
  557. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  558. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  559. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  560. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  561. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  562. connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));
  563. connect(actionNote, SIGNAL(triggered()), textEdit, SLOT(insertNote()));
  564. connect(actionLink, SIGNAL(triggered()), textEdit, SLOT(insertLink()));
  565. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  566. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  567. connect(actionZoomReset, SIGNAL(triggered()), textEdit, SLOT(zoomReset()));
  568. connect(actionInspect, SIGNAL(triggered()), textEdit, SLOT(showInspector()));
  569. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  570. FB2DELETE(toolEdit);
  571. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  572. tool->setMovable(false);
  573. tool->addSeparator();
  574. FB2::addTools(tool, textEdit);
  575. tool->addSeparator();
  576. tool->addAction(actionImage);
  577. tool->addAction(actionNote);
  578. tool->addAction(actionLink);
  579. tool->addAction(actionSection);
  580. tool->addSeparator();
  581. tool->addAction(actionZoomIn);
  582. tool->addAction(actionZoomOut);
  583. tool->addAction(actionZoomReset);
  584. }
  585. void Fb2MainWindow::viewHead()
  586. {
  587. if (headTree && centralWidget() == headTree) return;
  588. QString xml;
  589. if (codeEdit) xml = codeEdit->text();
  590. FB2DELETE(dockTree);
  591. FB2DELETE(codeEdit);
  592. FB2DELETE(toolEdit);
  593. if (!headTree) {
  594. headTree = new QTreeView(this);
  595. headTree->header()->setDefaultSectionSize(200);
  596. }
  597. if (textEdit) {
  598. this->setFocus();
  599. textEdit->setParent(NULL);
  600. setCentralWidget(headTree);
  601. textEdit->setParent(this);
  602. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  603. headTree->setModel(model);
  604. model->expand(headTree);
  605. } else {
  606. textEdit = new Fb2WebView(this);
  607. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  608. setCentralWidget(headTree);
  609. }
  610. headTree->setFocus();
  611. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  612. if (textEdit) {
  613. actionUndo->disconnect();
  614. actionRedo->disconnect();
  615. actionCut->disconnect();
  616. actionCopy->disconnect();
  617. actionPaste->disconnect();
  618. actionTextBold->disconnect();
  619. actionTextItalic->disconnect();
  620. actionTextStrike->disconnect();
  621. actionTextSub->disconnect();
  622. actionTextSup->disconnect();
  623. }
  624. FB2DELETE(toolEdit);
  625. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  626. tool->addSeparator();
  627. tool->addAction(actionInsert);
  628. tool->addAction(actionDelete);
  629. tool->setMovable(false);
  630. }
  631. void Fb2MainWindow::viewTree()
  632. {
  633. if (centralWidget() != textEdit) return;
  634. if (treeView == NULL) createTree();
  635. loadFinished(true);
  636. }
  637. void Fb2MainWindow::clipboardDataChanged()
  638. {
  639. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  640. actionPaste->setEnabled(md->hasText());
  641. }
  642. }