fb2main.cpp 24 KB

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