fb2main.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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("[*]") += appTitle();
  170. setWindowTitle(title);
  171. setWindowModified(true);
  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. menu->addSeparator();
  277. actionSection = act = new QAction(FB2::icon("insert-object"), tr("&Section"), this);
  278. menu->addAction(act);
  279. actionTtile = act = new QAction(tr("&Title"), this);
  280. menu->addAction(act);
  281. actionAuthor = act = new QAction(tr("&Author"), this);
  282. menu->addAction(act);
  283. actionDescr = act = new QAction(tr("&Annotation"), this);
  284. menu->addAction(act);
  285. actionPoem = act = new QAction(tr("&Poem"), this);
  286. menu->addAction(act);
  287. actionStanza = act = new QAction(tr("&Stanza"), this);
  288. menu->addAction(act);
  289. actionBody = act = new QAction(tr("&Body"), this);
  290. menu->addAction(act);
  291. menuText = menu = menuBar()->addMenu(tr("Fo&rmat"));
  292. actionTextBold = act = new QAction(FB2::icon("format-text-bold"), tr("&Bold"), this);
  293. act->setShortcuts(QKeySequence::Bold);
  294. act->setCheckable(true);
  295. menu->addAction(act);
  296. actionTextItalic = act = new QAction(FB2::icon("format-text-italic"), tr("&Italic"), this);
  297. act->setShortcuts(QKeySequence::Italic);
  298. act->setCheckable(true);
  299. menu->addAction(act);
  300. actionTextStrike = act = new QAction(FB2::icon("format-text-strikethrough"), tr("&Strikethrough"), this);
  301. act->setCheckable(true);
  302. menu->addAction(act);
  303. actionTextSup = act = new QAction(FB2::icon("format-text-superscript"), tr("Su&perscript"), this);
  304. act->setCheckable(true);
  305. menu->addAction(act);
  306. actionTextSub = act = new QAction(FB2::icon("format-text-subscript"), tr("Su&bscript"), this);
  307. act->setCheckable(true);
  308. menu->addAction(act);
  309. menuView = menu = menuBar()->addMenu(tr("&View"));
  310. tool->addSeparator();
  311. QActionGroup * viewGroup = new QActionGroup(this);
  312. act = new QAction(tr("&Text"), this);
  313. act->setCheckable(true);
  314. act->setChecked(true);
  315. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  316. viewGroup->addAction(act);
  317. menu->addAction(act);
  318. tool->addAction(act);
  319. act = new QAction(tr("&Head"), this);
  320. act->setCheckable(true);
  321. connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
  322. viewGroup->addAction(act);
  323. menu->addAction(act);
  324. tool->addAction(act);
  325. act = new QAction(tr("&XML"), this);
  326. act->setCheckable(true);
  327. connect(act, SIGNAL(triggered()), this, SLOT(viewCode()));
  328. viewGroup->addAction(act);
  329. menu->addAction(act);
  330. tool->addAction(act);
  331. menu->addSeparator();
  332. actionZoomIn = act = new QAction(FB2::icon("zoom-in"), tr("Zoom in"), this);
  333. act->setShortcuts(QKeySequence::ZoomIn);
  334. menu->addAction(act);
  335. actionZoomOut = act = new QAction(FB2::icon("zoom-out"), tr("Zoom out"), this);
  336. act->setShortcuts(QKeySequence::ZoomOut);
  337. menu->addAction(act);
  338. actionZoomReset = act = new QAction(FB2::icon("zoom-original"), tr("Zoom original"), this);
  339. menu->addAction(act);
  340. menu->addSeparator();
  341. act = new QAction(tr("&Contents"), this);
  342. connect(act, SIGNAL(triggered()), this, SLOT(viewTree()));
  343. menu->addAction(act);
  344. actionInspect = act = new QAction(tr("&Web inspector"), this);
  345. menu->addAction(act);
  346. menuBar()->addSeparator();
  347. menu = menuBar()->addMenu(tr("&Help"));
  348. act = new QAction(FB2::icon("help-about"), tr("&About"), this);
  349. act->setStatusTip(tr("Show the application's About box"));
  350. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  351. menu->addAction(act);
  352. act = new QAction(tr("About &Qt"), this);
  353. act->setStatusTip(tr("Show the Qt library's About box"));
  354. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  355. menu->addAction(act);
  356. }
  357. void Fb2MainWindow::openSettings()
  358. {
  359. QMessageBox::about(this, tr("Settings"),
  360. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  361. }
  362. void Fb2MainWindow::createTree()
  363. {
  364. if (treeView) return;
  365. treeView = new Fb2TreeView(this);
  366. treeView->setHeaderHidden(true);
  367. connect(textEdit, SIGNAL(updateTree()), SLOT(updateTree()));
  368. connect(textEdit, SIGNAL(selectTree()), treeView, SLOT(select()));
  369. connect(treeView, SIGNAL(activated(QModelIndex)), SLOT(treeActivated(QModelIndex)));
  370. connect(treeView, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  371. dockTree = new QDockWidget(tr("Contents"), this);
  372. dockTree->setAttribute(Qt::WA_DeleteOnClose);
  373. dockTree->setFeatures(QDockWidget::AllDockWidgetFeatures);
  374. dockTree->setWidget(treeView);
  375. addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  376. treeView->setFocus();
  377. }
  378. void Fb2MainWindow::loadFinished(bool ok)
  379. {
  380. Q_UNUSED(ok);
  381. updateTree();
  382. if (headTree) {
  383. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  384. headTree->setModel(model);
  385. model->expand(headTree);
  386. }
  387. }
  388. void Fb2MainWindow::updateTree()
  389. {
  390. if (treeView) {
  391. Fb2TreeModel *model = new Fb2TreeModel(*textEdit, treeView);
  392. treeView->setModel(model);
  393. model->expand(treeView);
  394. }
  395. }
  396. void Fb2MainWindow::selectionChanged()
  397. {
  398. actionCut->setEnabled(textEdit->CutEnabled());
  399. actionCopy->setEnabled(textEdit->CopyEnabled());
  400. actionTextBold->setChecked(textEdit->BoldChecked());
  401. actionTextItalic->setChecked(textEdit->ItalicChecked());
  402. actionTextStrike->setChecked(textEdit->StrikeChecked());
  403. actionTextSub->setChecked(textEdit->SubChecked());
  404. actionTextSup->setChecked(textEdit->SupChecked());
  405. statusBar()->showMessage(textEdit->status());
  406. }
  407. void Fb2MainWindow::undoChanged()
  408. {
  409. actionUndo->setEnabled(textEdit->UndoEnabled());
  410. }
  411. void Fb2MainWindow::redoChanged()
  412. {
  413. actionRedo->setEnabled(textEdit->RedoEnabled());
  414. }
  415. void Fb2MainWindow::createStatusBar()
  416. {
  417. statusBar()->showMessage(tr("Ready"));
  418. }
  419. void Fb2MainWindow::readSettings()
  420. {
  421. QSettings settings;
  422. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  423. QSize size = settings.value("size", QSize(400, 400)).toSize();
  424. move(pos);
  425. resize(size);
  426. }
  427. void Fb2MainWindow::writeSettings()
  428. {
  429. QSettings settings;
  430. settings.setValue("pos", pos());
  431. settings.setValue("size", size());
  432. }
  433. bool Fb2MainWindow::maybeSave()
  434. {
  435. if (textEdit && textEdit->isModified()) {
  436. QMessageBox::StandardButton ret;
  437. ret = QMessageBox::warning(this, qApp->applicationName(),
  438. tr("The document has been modified. Do you want to save your changes?"),
  439. QMessageBox::Save | QMessageBox::Discard
  440. | QMessageBox::Cancel);
  441. if (ret == QMessageBox::Save)
  442. return fileSave();
  443. else if (ret == QMessageBox::Cancel)
  444. return false;
  445. }
  446. return true;
  447. }
  448. bool Fb2MainWindow::saveFile(const QString &fileName, const QString &codec)
  449. {
  450. QFile file(fileName);
  451. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  452. QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  453. return false;
  454. }
  455. if (textEdit) {
  456. textEdit->save(&file, codec);
  457. setCurrentFile(fileName);
  458. }
  459. return true;
  460. /*
  461. QTextStream out(&file);
  462. QApplication::setOverrideCursor(Qt::WaitCursor);
  463. out << textEdit->toPlainText();
  464. QApplication::restoreOverrideCursor();
  465. setCurrentFile(fileName);
  466. statusBar()->showMessage(tr("File saved"), 2000);
  467. */
  468. }
  469. void Fb2MainWindow::setCurrentFile(const QString &filename)
  470. {
  471. static int sequenceNumber = 1;
  472. QString title;
  473. isUntitled = filename.isEmpty();
  474. if (isUntitled) {
  475. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  476. title = curFile;
  477. } else {
  478. QFileInfo info = filename;
  479. curFile = info.canonicalFilePath();
  480. title = info.fileName();
  481. }
  482. title += appTitle();
  483. setWindowModified(false);
  484. setWindowFilePath(curFile);
  485. setWindowTitle(title);
  486. }
  487. QString Fb2MainWindow::appTitle() const
  488. {
  489. return QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
  490. }
  491. Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
  492. {
  493. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  494. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  495. Fb2MainWindow *mainWin = qobject_cast<Fb2MainWindow *>(widget);
  496. if (mainWin && mainWin->curFile == canonicalFilePath)
  497. return mainWin;
  498. }
  499. return 0;
  500. }
  501. void Fb2MainWindow::checkScintillaUndo()
  502. {
  503. if (!codeEdit) return;
  504. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  505. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  506. }
  507. void Fb2MainWindow::viewCode()
  508. {
  509. if (codeEdit && centralWidget() == codeEdit) return;
  510. bool load = false;
  511. QByteArray xml;
  512. QList<int> folds;
  513. if (textEdit) {
  514. textEdit->save(&xml);
  515. load = true;
  516. }
  517. FB2DELETE(textEdit);
  518. FB2DELETE(dockTree);
  519. FB2DELETE(headTree);
  520. if (!codeEdit) {
  521. codeEdit = new Fb2CodeEdit;
  522. }
  523. if (load) codeEdit->load(xml, folds);
  524. setCentralWidget(codeEdit);
  525. codeEdit->setFocus();
  526. FB2DELETE(toolEdit);
  527. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  528. tool->addSeparator();
  529. tool->addAction(actionUndo);
  530. tool->addAction(actionRedo);
  531. tool->addSeparator();
  532. tool->addAction(actionCut);
  533. tool->addAction(actionCopy);
  534. tool->addAction(actionPaste);
  535. tool->addSeparator();
  536. tool->addAction(actionZoomIn);
  537. tool->addAction(actionZoomOut);
  538. tool->addAction(actionZoomReset);
  539. tool->setMovable(false);
  540. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  541. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  542. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  543. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  544. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  545. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  546. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  547. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  548. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  549. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  550. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  551. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  552. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  553. }
  554. void Fb2MainWindow::viewText()
  555. {
  556. if (textEdit && centralWidget() == textEdit) return;
  557. QString xml;
  558. if (codeEdit) xml = codeEdit->text();
  559. FB2DELETE(codeEdit);
  560. FB2DELETE(headTree);
  561. if (!textEdit) {
  562. textEdit = new Fb2WebView(this);
  563. }
  564. setCentralWidget(textEdit);
  565. textEdit->setFocus();
  566. viewTree();
  567. connect(textEdit->page(), SIGNAL(contentsChanged()), SLOT(documentWasModified()));
  568. connect(textEdit->page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  569. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  570. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  571. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  572. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  573. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  574. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  575. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  576. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  577. connect(actionFind, SIGNAL(triggered()), textEdit, SLOT(find()));
  578. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  579. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  580. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  581. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  582. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  583. connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));
  584. connect(actionNote, SIGNAL(triggered()), textEdit, SLOT(insertNote()));
  585. connect(actionLink, SIGNAL(triggered()), textEdit, SLOT(insertLink()));
  586. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  587. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  588. connect(actionZoomReset, SIGNAL(triggered()), textEdit, SLOT(zoomReset()));
  589. connect(actionInspect, SIGNAL(triggered()), textEdit, SLOT(showInspector()));
  590. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  591. FB2DELETE(toolEdit);
  592. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  593. tool->setMovable(false);
  594. tool->addSeparator();
  595. FB2::addTools(tool, textEdit);
  596. tool->addSeparator();
  597. tool->addAction(actionImage);
  598. tool->addAction(actionNote);
  599. tool->addAction(actionLink);
  600. tool->addAction(actionSection);
  601. tool->addSeparator();
  602. tool->addAction(actionZoomIn);
  603. tool->addAction(actionZoomOut);
  604. tool->addAction(actionZoomReset);
  605. }
  606. void Fb2MainWindow::viewHead()
  607. {
  608. if (headTree && centralWidget() == headTree) return;
  609. QString xml;
  610. if (codeEdit) xml = codeEdit->text();
  611. FB2DELETE(dockTree);
  612. FB2DELETE(codeEdit);
  613. FB2DELETE(toolEdit);
  614. if (!headTree) {
  615. headTree = new QTreeView(this);
  616. headTree->header()->setDefaultSectionSize(200);
  617. }
  618. if (textEdit) {
  619. this->setFocus();
  620. textEdit->setParent(NULL);
  621. setCentralWidget(headTree);
  622. textEdit->setParent(this);
  623. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  624. headTree->setModel(model);
  625. model->expand(headTree);
  626. } else {
  627. textEdit = new Fb2WebView(this);
  628. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  629. setCentralWidget(headTree);
  630. }
  631. headTree->setFocus();
  632. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  633. if (textEdit) {
  634. actionUndo->disconnect();
  635. actionRedo->disconnect();
  636. actionCut->disconnect();
  637. actionCopy->disconnect();
  638. actionPaste->disconnect();
  639. actionTextBold->disconnect();
  640. actionTextItalic->disconnect();
  641. actionTextStrike->disconnect();
  642. actionTextSub->disconnect();
  643. actionTextSup->disconnect();
  644. }
  645. FB2DELETE(toolEdit);
  646. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  647. tool->addSeparator();
  648. tool->addAction(actionInsert);
  649. tool->addAction(actionDelete);
  650. tool->setMovable(false);
  651. }
  652. void Fb2MainWindow::viewTree()
  653. {
  654. if (centralWidget() != textEdit) return;
  655. if (treeView == NULL) createTree();
  656. loadFinished(true);
  657. }
  658. void Fb2MainWindow::clipboardDataChanged()
  659. {
  660. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  661. actionPaste->setEnabled(md->hasText());
  662. }
  663. }