fb2main.cpp 24 KB

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