fb2main.cpp 26 KB

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