fb2main.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 "fb2tree.hpp"
  9. #include "fb2view.hpp"
  10. #include "fb2head.hpp"
  11. #include "fb2utils.h"
  12. Fb2MainWindow::Fb2MainWindow()
  13. {
  14. init();
  15. setCurrentFile();
  16. viewText();
  17. textEdit->load(":blank.fb2");
  18. }
  19. Fb2MainWindow::Fb2MainWindow(const QString &filename, ViewMode mode)
  20. {
  21. init();
  22. setCurrentFile(filename);
  23. if (mode == FB2) {
  24. viewText();
  25. textEdit->load(filename);
  26. } else {
  27. viewCode();
  28. loadXML(filename);
  29. }
  30. }
  31. void Fb2MainWindow::init()
  32. {
  33. connect(qApp, SIGNAL(logMessage(QString)), SLOT(logMessage(QString)));
  34. setAttribute(Qt::WA_DeleteOnClose);
  35. setWindowIcon(QIcon(":icon.ico"));
  36. isUntitled = true;
  37. createActions();
  38. createStatusBar();
  39. textEdit = NULL;
  40. noteEdit = NULL;
  41. codeEdit = NULL;
  42. treeView = NULL;
  43. headTree = NULL;
  44. toolEdit = NULL;
  45. messageEdit = NULL;
  46. readSettings();
  47. setUnifiedTitleAndToolBarOnMac(true);
  48. }
  49. void Fb2MainWindow::logMessage(const QString &message)
  50. {
  51. if (!messageEdit) {
  52. messageEdit = new QTextEdit(this);
  53. connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
  54. QDockWidget * dock = new QDockWidget(tr("Message log"), this);
  55. dock->setAttribute(Qt::WA_DeleteOnClose);
  56. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  57. dock->setWidget(messageEdit);
  58. addDockWidget(Qt::BottomDockWidgetArea, dock);
  59. messageEdit->setMaximumHeight(80);
  60. }
  61. messageEdit->append(message);
  62. }
  63. void Fb2MainWindow::logShowed()
  64. {
  65. messageEdit->setMaximumHeight(QWIDGETSIZE_MAX);
  66. }
  67. void Fb2MainWindow::logDestroyed()
  68. {
  69. messageEdit = NULL;
  70. }
  71. void Fb2MainWindow::treeActivated(const QModelIndex &index)
  72. {
  73. if (!treeView) return;
  74. Fb2TreeModel *model = dynamic_cast<Fb2TreeModel*>(treeView->model());
  75. if (!model) return;
  76. model->select(index);
  77. selectionChanged();
  78. }
  79. void Fb2MainWindow::treeDestroyed()
  80. {
  81. treeView = NULL;
  82. dockTree = NULL;
  83. }
  84. bool Fb2MainWindow::loadXML(const QString &filename)
  85. {
  86. if (!filename.isEmpty()) {
  87. QFile file(filename);
  88. if (file.open(QFile::ReadOnly | QFile::Text)) {
  89. codeEdit->clear();
  90. return codeEdit->read(&file);
  91. }
  92. }
  93. return false;
  94. }
  95. void Fb2MainWindow::closeEvent(QCloseEvent *event)
  96. {
  97. if (maybeSave()) {
  98. writeSettings();
  99. event->accept();
  100. } else {
  101. event->ignore();
  102. }
  103. }
  104. void Fb2MainWindow::fileNew()
  105. {
  106. Fb2MainWindow *other = new Fb2MainWindow;
  107. other->move(x() + 40, y() + 40);
  108. other->show();
  109. }
  110. void Fb2MainWindow::fileOpen()
  111. {
  112. QString filename = QFileDialog::getOpenFileName(this, tr("Open file"), QString(), "Fiction book files (*.fb2)");
  113. if (filename.isEmpty()) return;
  114. Fb2MainWindow * existing = findFb2MainWindow(filename);
  115. if (existing) {
  116. existing->show();
  117. existing->raise();
  118. existing->activateWindow();
  119. return;
  120. }
  121. if (textEdit) {
  122. if (isUntitled && !isWindowModified()) {
  123. setCurrentFile(filename);
  124. textEdit->load(filename);
  125. } else {
  126. Fb2MainWindow * other = new Fb2MainWindow(filename, FB2);
  127. other->move(x() + 40, y() + 40);
  128. other->show();
  129. }
  130. } else if (codeEdit) {
  131. if (isUntitled && !isWindowModified()) {
  132. setCurrentFile(filename);
  133. loadXML(filename);
  134. } else {
  135. Fb2MainWindow * other = new Fb2MainWindow(filename, XML);
  136. other->move(x() + 40, y() + 40);
  137. other->show();
  138. }
  139. }
  140. }
  141. bool Fb2MainWindow::fileSave()
  142. {
  143. if (isUntitled) {
  144. return fileSaveAs();
  145. } else {
  146. return saveFile(curFile);
  147. }
  148. }
  149. bool Fb2MainWindow::fileSaveAs()
  150. {
  151. QString fileName = QFileDialog::getSaveFileName(this, tr("Save As..."), curFile);
  152. if (fileName.isEmpty()) return false;
  153. return saveFile(fileName);
  154. }
  155. void Fb2MainWindow::about()
  156. {
  157. QMessageBox::about(this, tr("About fb2edit"),
  158. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  159. }
  160. void Fb2MainWindow::documentWasModified()
  161. {
  162. if (isWindowModified()) return;
  163. QFileInfo info = windowFilePath();
  164. QString title = info.fileName();
  165. title += QString("[*]");
  166. title += QString(" - ") += qApp->applicationName();
  167. setWindowTitle(title);
  168. setWindowModified(true);
  169. if (codeEdit) disconnect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  170. if (textEdit) disconnect(textEdit->page(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
  171. }
  172. void Fb2MainWindow::createActions()
  173. {
  174. QAction * act;
  175. QMenu * menu;
  176. QToolBar * tool;
  177. QList<QAction*> actions;
  178. menu = menuBar()->addMenu(tr("&File"));
  179. tool = addToolBar(tr("File"));
  180. tool->setMovable(false);
  181. act = new QAction(FB2::icon("document-new"), tr("&New"), this);
  182. act->setPriority(QAction::LowPriority);
  183. act->setShortcuts(QKeySequence::New);
  184. act->setStatusTip(tr("Create a new file"));
  185. connect(act, SIGNAL(triggered()), this, SLOT(fileNew()));
  186. menu->addAction(act);
  187. tool->addAction(act);
  188. act = new QAction(FB2::icon("document-open"), tr("&Open..."), this);
  189. act->setShortcuts(QKeySequence::Open);
  190. act->setStatusTip(tr("Open an existing file"));
  191. connect(act, SIGNAL(triggered()), this, SLOT(fileOpen()));
  192. menu->addAction(act);
  193. tool->addAction(act);
  194. act = new QAction(FB2::icon("document-save"), tr("&Save"), this);
  195. act->setShortcuts(QKeySequence::Save);
  196. act->setStatusTip(tr("Save the document to disk"));
  197. connect(act, SIGNAL(triggered()), this, SLOT(fileSave()));
  198. menu->addAction(act);
  199. tool->addAction(act);
  200. act = new QAction(FB2::icon("document-save-as"), tr("Save &As..."), this);
  201. act->setShortcuts(QKeySequence::SaveAs);
  202. act->setStatusTip(tr("Save the document under a new name"));
  203. connect(act, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
  204. menu->addAction(act);
  205. menu->addSeparator();
  206. act = new QAction(FB2::icon("window-close"), tr("&Close"), this);
  207. act->setShortcuts(QKeySequence::Close);
  208. act->setStatusTip(tr("Close this window"));
  209. connect(act, SIGNAL(triggered()), this, SLOT(close()));
  210. menu->addAction(act);
  211. act = new QAction(FB2::icon("application-exit"), tr("E&xit"), this);
  212. act->setShortcuts(QKeySequence::Quit);
  213. act->setStatusTip(tr("Exit the application"));
  214. connect(act, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  215. menu->addAction(act);
  216. menuEdit = menu = menuBar()->addMenu(tr("&Edit"));
  217. connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
  218. actionUndo = act = new QAction(FB2::icon("edit-undo"), tr("&Undo"), this);
  219. act->setPriority(QAction::LowPriority);
  220. act->setShortcut(QKeySequence::Undo);
  221. act->setEnabled(false);
  222. menu->addAction(act);
  223. actionRedo = act = new QAction(FB2::icon("edit-redo"), tr("&Redo"), this);
  224. act->setPriority(QAction::LowPriority);
  225. act->setShortcut(QKeySequence::Redo);
  226. act->setEnabled(false);
  227. menu->addAction(act);
  228. menu->addSeparator();
  229. actionCut = act = new QAction(FB2::icon("edit-cut"), tr("Cu&t"), this);
  230. act->setPriority(QAction::LowPriority);
  231. act->setShortcuts(QKeySequence::Cut);
  232. act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  233. act->setEnabled(false);
  234. menu->addAction(act);
  235. actionCopy = act = new QAction(FB2::icon("edit-copy"), tr("&Copy"), this);
  236. act->setPriority(QAction::LowPriority);
  237. act->setShortcuts(QKeySequence::Copy);
  238. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  239. act->setEnabled(false);
  240. menu->addAction(act);
  241. actionPaste = act = new QAction(FB2::icon("edit-paste"), tr("&Paste"), this);
  242. act->setPriority(QAction::LowPriority);
  243. act->setShortcuts(QKeySequence::Paste);
  244. act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  245. menu->addAction(act);
  246. clipboardDataChanged();
  247. menu->addSeparator();
  248. actionFind = act = new QAction(FB2::icon("edit-find"), tr("&Find..."), this);
  249. act->setShortcuts(QKeySequence::Find);
  250. menu->addAction(act);
  251. actionReplace = act = new QAction(FB2::icon("edit-find-replace"), tr("&Replace..."), this);
  252. menu->addAction(act);
  253. menu->addSeparator();
  254. actionInsert = act = new QAction(FB2::icon("list-add"), tr("&Append"), this);
  255. act->setPriority(QAction::LowPriority);
  256. act->setShortcuts(QKeySequence::New);
  257. menu->addAction(act);
  258. actionDelete = act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
  259. act->setPriority(QAction::LowPriority);
  260. act->setShortcuts(QKeySequence::Delete);
  261. menu->addAction(act);
  262. menu->addSeparator();
  263. act = new QAction(FB2::icon("preferences-desktop"), tr("&Settings"), this);
  264. act->setShortcuts(QKeySequence::Preferences);
  265. act->setStatusTip(tr("Application settings"));
  266. connect(act, SIGNAL(triggered()), SLOT(openSettings()));
  267. menu->addAction(act);
  268. menu = menuBar()->addMenu(tr("&Insert", "Main menu"));
  269. actionImage = act = new QAction(FB2::icon("insert-image"), tr("&Image"), this);
  270. menu->addAction(act);
  271. actionNote = act = new QAction(FB2::icon("insert-text"), tr("&Footnote"), this);
  272. menu->addAction(act);
  273. actionLink = act = new QAction(FB2::icon("insert-link"), tr("&Hiperlink"), this);
  274. menu->addAction(act);
  275. actionBody = act = new QAction(FB2::icon("insert-object"), tr("&Body"), this);
  276. menu->addAction(act);
  277. menuText = menu = menuBar()->addMenu(tr("Fo&rmat"));
  278. actionTextBold = act = new QAction(FB2::icon("format-text-bold"), tr("&Bold"), this);
  279. act->setShortcuts(QKeySequence::Bold);
  280. act->setCheckable(true);
  281. menu->addAction(act);
  282. actionTextItalic = act = new QAction(FB2::icon("format-text-italic"), tr("&Italic"), this);
  283. act->setShortcuts(QKeySequence::Italic);
  284. act->setCheckable(true);
  285. menu->addAction(act);
  286. actionTextStrike = act = new QAction(FB2::icon("format-text-strikethrough"), tr("&Strikethrough"), this);
  287. act->setCheckable(true);
  288. menu->addAction(act);
  289. actionTextSup = act = new QAction(FB2::icon("format-text-superscript"), tr("Su&perscript"), this);
  290. act->setCheckable(true);
  291. menu->addAction(act);
  292. actionTextSub = act = new QAction(FB2::icon("format-text-subscript"), tr("Su&bscript"), this);
  293. act->setCheckable(true);
  294. menu->addAction(act);
  295. menuView = menu = menuBar()->addMenu(tr("&View"));
  296. tool->addSeparator();
  297. QActionGroup * viewGroup = new QActionGroup(this);
  298. act = new QAction(tr("&Text"), this);
  299. act->setCheckable(true);
  300. act->setChecked(true);
  301. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  302. viewGroup->addAction(act);
  303. menu->addAction(act);
  304. tool->addAction(act);
  305. act = new QAction(tr("&Head"), this);
  306. act->setCheckable(true);
  307. connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
  308. viewGroup->addAction(act);
  309. menu->addAction(act);
  310. tool->addAction(act);
  311. act = new QAction(tr("&XML"), this);
  312. act->setCheckable(true);
  313. connect(act, SIGNAL(triggered()), this, SLOT(viewCode()));
  314. viewGroup->addAction(act);
  315. menu->addAction(act);
  316. tool->addAction(act);
  317. menu->addSeparator();
  318. actionZoomIn = act = new QAction(FB2::icon("zoom-in"), tr("Zoom in"), this);
  319. act->setShortcuts(QKeySequence::ZoomIn);
  320. menu->addAction(act);
  321. actionZoomOut = act = new QAction(FB2::icon("zoom-out"), tr("Zoom out"), this);
  322. act->setShortcuts(QKeySequence::ZoomOut);
  323. menu->addAction(act);
  324. actionZoomOrig = act = new QAction(FB2::icon("zoom-original"), tr("Zoom original"), this);
  325. menu->addAction(act);
  326. menu->addSeparator();
  327. act = new QAction(tr("&Contents"), this);
  328. connect(act, SIGNAL(triggered()), this, SLOT(viewTree()));
  329. menu->addAction(act);
  330. actionInspect = act = new QAction(tr("&Web inspector"), this);
  331. menu->addAction(act);
  332. menuBar()->addSeparator();
  333. menu = menuBar()->addMenu(tr("&Help"));
  334. act = new QAction(FB2::icon("help-about"), tr("&About"), this);
  335. act->setStatusTip(tr("Show the application's About box"));
  336. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  337. menu->addAction(act);
  338. act = new QAction(tr("About &Qt"), this);
  339. act->setStatusTip(tr("Show the Qt library's About box"));
  340. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  341. menu->addAction(act);
  342. }
  343. void Fb2MainWindow::openSettings()
  344. {
  345. QMessageBox::about(this, tr("Settings"),
  346. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  347. }
  348. void Fb2MainWindow::createTree()
  349. {
  350. if (treeView) return;
  351. treeView = new QTreeView(this);
  352. treeView->setHeaderHidden(true);
  353. connect(treeView, SIGNAL(activated(QModelIndex)), SLOT(treeActivated(QModelIndex)));
  354. connect(treeView, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  355. dockTree = new QDockWidget(tr("Contents"), this);
  356. dockTree->setAttribute(Qt::WA_DeleteOnClose);
  357. dockTree->setFeatures(QDockWidget::AllDockWidgetFeatures);
  358. dockTree->setWidget(treeView);
  359. addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  360. treeView->setFocus();
  361. }
  362. void Fb2MainWindow::loadFinished(bool ok)
  363. {
  364. if (headTree) {
  365. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  366. headTree->setModel(model);
  367. model->expand(headTree);
  368. }
  369. if (treeView) {
  370. Fb2TreeModel *model = new Fb2TreeModel(*textEdit, treeView);
  371. treeView->setModel(model);
  372. model->expand(treeView);
  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)
  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) return textEdit->save(&file);
  435. return true;
  436. /*
  437. QTextStream out(&file);
  438. QApplication::setOverrideCursor(Qt::WaitCursor);
  439. out << textEdit->toPlainText();
  440. QApplication::restoreOverrideCursor();
  441. setCurrentFile(fileName);
  442. statusBar()->showMessage(tr("File saved"), 2000);
  443. */
  444. }
  445. void Fb2MainWindow::setCurrentFile(const QString &filename)
  446. {
  447. static int sequenceNumber = 1;
  448. QString title;
  449. isUntitled = filename.isEmpty();
  450. if (isUntitled) {
  451. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  452. title = curFile;
  453. } else {
  454. QFileInfo info = filename;
  455. curFile = info.canonicalFilePath();
  456. title = info.fileName();
  457. }
  458. title += QString(" - ") += qApp->applicationName();
  459. setWindowModified(false);
  460. setWindowFilePath(curFile);
  461. setWindowTitle(title);
  462. }
  463. Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
  464. {
  465. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  466. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  467. Fb2MainWindow *mainWin = qobject_cast<Fb2MainWindow *>(widget);
  468. if (mainWin && mainWin->curFile == canonicalFilePath)
  469. return mainWin;
  470. }
  471. return 0;
  472. }
  473. void Fb2MainWindow::zoomOrig()
  474. {
  475. if (codeEdit) codeEdit->zoomTo(1);
  476. }
  477. void Fb2MainWindow::checkScintillaUndo()
  478. {
  479. if (!codeEdit) return;
  480. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  481. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  482. }
  483. void Fb2MainWindow::viewCode()
  484. {
  485. if (codeEdit && centralWidget() == codeEdit) return;
  486. bool load = false;
  487. QByteArray xml;
  488. QList<int> folds;
  489. if (textEdit) {
  490. textEdit->save(&xml, &folds);
  491. load = true;
  492. }
  493. FB2DELETE(textEdit);
  494. FB2DELETE(dockTree);
  495. FB2DELETE(headTree);
  496. if (!codeEdit) {
  497. codeEdit = new Fb2CodeEdit;
  498. }
  499. if (load) codeEdit->load(xml, folds);
  500. setCentralWidget(codeEdit);
  501. codeEdit->setFocus();
  502. FB2DELETE(toolEdit);
  503. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  504. tool->addSeparator();
  505. tool->addAction(actionUndo);
  506. tool->addAction(actionRedo);
  507. tool->addSeparator();
  508. tool->addAction(actionCut);
  509. tool->addAction(actionCopy);
  510. tool->addAction(actionPaste);
  511. tool->addSeparator();
  512. tool->addAction(actionZoomIn);
  513. tool->addAction(actionZoomOut);
  514. tool->addAction(actionZoomOrig);
  515. tool->setMovable(false);
  516. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  517. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  518. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  519. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  520. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  521. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  522. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  523. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  524. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  525. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  526. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  527. connect(actionZoomOrig, SIGNAL(triggered()), this, SLOT(zoomOrig()));
  528. }
  529. void Fb2MainWindow::viewText()
  530. {
  531. if (textEdit && centralWidget() == textEdit) return;
  532. QString xml;
  533. if (codeEdit) xml = codeEdit->text();
  534. FB2DELETE(codeEdit);
  535. FB2DELETE(headTree);
  536. if (!textEdit) {
  537. textEdit = new Fb2WebView(this);
  538. }
  539. setCentralWidget(textEdit);
  540. textEdit->setFocus();
  541. viewTree();
  542. connect(textEdit->page(), SIGNAL(contentsChanged()), SLOT(documentWasModified()));
  543. connect(textEdit->page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  544. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  545. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  546. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  547. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  548. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  549. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  550. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  551. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  552. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  553. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  554. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  555. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  556. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  557. connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));
  558. connect(actionNote, SIGNAL(triggered()), textEdit, SLOT(insertNote()));
  559. connect(actionLink, SIGNAL(triggered()), textEdit, SLOT(insertLink()));
  560. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  561. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  562. connect(actionZoomOrig, SIGNAL(triggered()), textEdit, SLOT(zoomOrig()));
  563. connect(actionInspect, SIGNAL(triggered()), textEdit, SLOT(showInspector()));
  564. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  565. FB2DELETE(toolEdit);
  566. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  567. tool->setMovable(false);
  568. tool->addSeparator();
  569. FB2::addTools(tool, textEdit);
  570. tool->addSeparator();
  571. tool->addAction(actionImage);
  572. tool->addAction(actionNote);
  573. tool->addAction(actionLink);
  574. tool->addAction(actionBody);
  575. tool->addSeparator();
  576. tool->addAction(actionZoomIn);
  577. tool->addAction(actionZoomOut);
  578. tool->addAction(actionZoomOrig);
  579. }
  580. void Fb2MainWindow::viewHead()
  581. {
  582. if (headTree && centralWidget() == headTree) return;
  583. QString xml;
  584. if (codeEdit) xml = codeEdit->text();
  585. FB2DELETE(dockTree);
  586. FB2DELETE(codeEdit);
  587. FB2DELETE(toolEdit);
  588. if (!headTree) {
  589. headTree = new QTreeView(this);
  590. headTree->header()->setDefaultSectionSize(200);
  591. }
  592. if (textEdit) {
  593. this->setFocus();
  594. textEdit->setParent(NULL);
  595. setCentralWidget(headTree);
  596. textEdit->setParent(this);
  597. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  598. headTree->setModel(model);
  599. model->expand(headTree);
  600. } else {
  601. textEdit = new Fb2WebView(this);
  602. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  603. setCentralWidget(headTree);
  604. }
  605. headTree->setFocus();
  606. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  607. if (textEdit) {
  608. actionUndo->disconnect();
  609. actionRedo->disconnect();
  610. actionCut->disconnect();
  611. actionCopy->disconnect();
  612. actionPaste->disconnect();
  613. actionTextBold->disconnect();
  614. actionTextItalic->disconnect();
  615. actionTextStrike->disconnect();
  616. actionTextSub->disconnect();
  617. actionTextSup->disconnect();
  618. }
  619. FB2DELETE(toolEdit);
  620. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  621. tool->addSeparator();
  622. tool->addAction(actionInsert);
  623. tool->addAction(actionDelete);
  624. tool->setMovable(false);
  625. }
  626. void Fb2MainWindow::viewTree()
  627. {
  628. if (centralWidget() != textEdit) return;
  629. if (treeView == NULL) createTree();
  630. loadFinished(true);
  631. }
  632. void Fb2MainWindow::clipboardDataChanged()
  633. {
  634. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  635. actionPaste->setEnabled(md->hasText());
  636. }
  637. }