fb2main.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. #include <QtGui>
  2. #include <QtDebug>
  3. #include <QTreeView>
  4. #include <QWebInspector>
  5. #include <QWebFrame>
  6. #include "fb2main.h"
  7. #include "fb2read.h"
  8. #include "fb2tree.h"
  9. #include "fb2view.h"
  10. #include "fb2head.h"
  11. #include <Qsci/qsciscintilla.h>
  12. #include <Qsci/qscilexerxml.h>
  13. #define FB2DELETE(p) { if ( (p) != NULL ) { delete p; p = NULL; } }
  14. Fb2MainWindow::Fb2MainWindow()
  15. {
  16. init();
  17. setCurrentFile();
  18. textEdit = new Fb2WebView(this);
  19. viewText();
  20. textEdit->setHtml("<body/>");
  21. }
  22. Fb2MainWindow::Fb2MainWindow(const QString &filename, ViewMode mode)
  23. {
  24. init();
  25. setCurrentFile(filename);
  26. if (mode == FB2) {
  27. viewText();
  28. textEdit->load(filename);
  29. } else {
  30. createQsci();
  31. loadXML(filename);
  32. }
  33. }
  34. void Fb2MainWindow::init()
  35. {
  36. connect(qApp, SIGNAL(logMessage(QString)), SLOT(logMessage(QString)));
  37. setAttribute(Qt::WA_DeleteOnClose);
  38. isUntitled = true;
  39. createActions();
  40. createStatusBar();
  41. textEdit = NULL;
  42. noteEdit = NULL;
  43. qsciEdit = NULL;
  44. treeView = NULL;
  45. headTree = NULL;
  46. toolEdit = NULL;
  47. messageEdit = NULL;
  48. readSettings();
  49. setUnifiedTitleAndToolBarOnMac(true);
  50. }
  51. void Fb2MainWindow::logMessage(const QString &message)
  52. {
  53. if (!messageEdit) {
  54. messageEdit = new QTextEdit(this);
  55. connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
  56. QDockWidget * dock = new QDockWidget(tr("Message log"), this);
  57. dock->setAttribute(Qt::WA_DeleteOnClose);
  58. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  59. dock->setWidget(messageEdit);
  60. addDockWidget(Qt::BottomDockWidgetArea, dock);
  61. messageEdit->setMaximumHeight(80);
  62. }
  63. messageEdit->append(message);
  64. }
  65. void Fb2MainWindow::logShowed()
  66. {
  67. messageEdit->setMaximumHeight(QWIDGETSIZE_MAX);
  68. }
  69. void Fb2MainWindow::logDestroyed()
  70. {
  71. messageEdit = NULL;
  72. }
  73. void Fb2MainWindow::treeActivated(const QModelIndex &index)
  74. {
  75. if (!treeView) return;
  76. Fb2TreeModel *model = dynamic_cast<Fb2TreeModel*>(treeView->model());
  77. if (model) model->select(index);
  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. qsciEdit->clear();
  90. return qsciEdit->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 (qsciEdit) {
  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. if (textEdit && textEdit->isModified()) title += QString("[*]");
  166. title += QString(" - ") += qApp->applicationName();
  167. setWindowTitle(title);
  168. setWindowModified(true);
  169. }
  170. QIcon Fb2MainWindow::icon(const QString &name)
  171. {
  172. QIcon icon;
  173. icon.addFile(QString(":/24/%1.png").arg(name), QSize(24,24));
  174. icon.addFile(QString(":/16/%1.png").arg(name), QSize(16,16));
  175. return QIcon::fromTheme(name, icon);
  176. }
  177. void Fb2MainWindow::createActions()
  178. {
  179. QAction * act;
  180. QMenu * menu;
  181. QToolBar * tool;
  182. QList<QAction*> actions;
  183. menu = menuBar()->addMenu(tr("&File"));
  184. tool = addToolBar(tr("File"));
  185. tool->setMovable(false);
  186. act = new QAction(icon("document-new"), tr("&New"), this);
  187. act->setPriority(QAction::LowPriority);
  188. act->setShortcuts(QKeySequence::New);
  189. act->setStatusTip(tr("Create a new file"));
  190. connect(act, SIGNAL(triggered()), this, SLOT(fileNew()));
  191. menu->addAction(act);
  192. tool->addAction(act);
  193. act = new QAction(icon("document-open"), tr("&Open..."), this);
  194. act->setShortcuts(QKeySequence::Open);
  195. act->setStatusTip(tr("Open an existing file"));
  196. connect(act, SIGNAL(triggered()), this, SLOT(fileOpen()));
  197. menu->addAction(act);
  198. tool->addAction(act);
  199. act = new QAction(icon("document-save"), tr("&Save"), this);
  200. act->setShortcuts(QKeySequence::Save);
  201. act->setStatusTip(tr("Save the document to disk"));
  202. connect(act, SIGNAL(triggered()), this, SLOT(fileSave()));
  203. menu->addAction(act);
  204. tool->addAction(act);
  205. act = new QAction(icon("document-save-as"), tr("Save &As..."), this);
  206. act->setShortcuts(QKeySequence::SaveAs);
  207. act->setStatusTip(tr("Save the document under a new name"));
  208. connect(act, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
  209. menu->addAction(act);
  210. menu->addSeparator();
  211. act = new QAction(icon("window-close"), tr("&Close"), this);
  212. act->setShortcuts(QKeySequence::Close);
  213. act->setStatusTip(tr("Close this window"));
  214. connect(act, SIGNAL(triggered()), this, SLOT(close()));
  215. menu->addAction(act);
  216. act = new QAction(icon("application-exit"), tr("E&xit"), this);
  217. act->setShortcuts(QKeySequence::Quit);
  218. act->setStatusTip(tr("Exit the application"));
  219. connect(act, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  220. menu->addAction(act);
  221. menuEdit = menu = menuBar()->addMenu(tr("&Edit"));
  222. connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
  223. actionUndo = act = new QAction(icon("edit-undo"), tr("&Undo"), this);
  224. act->setPriority(QAction::LowPriority);
  225. act->setShortcut(QKeySequence::Undo);
  226. act->setEnabled(false);
  227. menu->addAction(act);
  228. actionRedo = act = new QAction(icon("edit-redo"), tr("&Redo"), this);
  229. act->setPriority(QAction::LowPriority);
  230. act->setShortcut(QKeySequence::Redo);
  231. act->setEnabled(false);
  232. menu->addAction(act);
  233. menu->addSeparator();
  234. actionCut = act = new QAction(icon("edit-cut"), tr("Cu&t"), this);
  235. act->setPriority(QAction::LowPriority);
  236. act->setShortcuts(QKeySequence::Cut);
  237. act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  238. act->setEnabled(false);
  239. menu->addAction(act);
  240. actionCopy = act = new QAction(icon("edit-copy"), tr("&Copy"), this);
  241. act->setPriority(QAction::LowPriority);
  242. act->setShortcuts(QKeySequence::Copy);
  243. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  244. act->setEnabled(false);
  245. menu->addAction(act);
  246. actionPaste = act = new QAction(icon("edit-paste"), tr("&Paste"), this);
  247. act->setPriority(QAction::LowPriority);
  248. act->setShortcuts(QKeySequence::Paste);
  249. act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  250. menu->addAction(act);
  251. clipboardDataChanged();
  252. menu->addSeparator();
  253. actionInsert = act = new QAction(icon("list-add"), tr("Insert"), this);
  254. act->setPriority(QAction::LowPriority);
  255. act->setShortcuts(QKeySequence::New);
  256. menu->addAction(act);
  257. actionDelete = act = new QAction(icon("list-remove"), tr("Delete"), this);
  258. act->setPriority(QAction::LowPriority);
  259. act->setShortcuts(QKeySequence::Delete);
  260. menu->addAction(act);
  261. menuText = menu = menuBar()->addMenu(tr("Fo&rmat"));
  262. actionTextBold = act = new QAction(icon("format-text-bold"), tr("Bold"), this);
  263. act->setShortcuts(QKeySequence::Bold);
  264. act->setCheckable(true);
  265. menu->addAction(act);
  266. actionTextItalic = act = new QAction(icon("format-text-italic"), tr("Italic"), this);
  267. act->setShortcuts(QKeySequence::Italic);
  268. act->setCheckable(true);
  269. menu->addAction(act);
  270. actionTextStrike = act = new QAction(icon("format-text-strikethrough"), tr("Strikethrough"), this);
  271. act->setCheckable(true);
  272. menu->addAction(act);
  273. actionTextSup = act = new QAction(icon("format-text-superscript"), tr("Superscript"), this);
  274. act->setCheckable(true);
  275. menu->addAction(act);
  276. actionTextSub = act = new QAction(icon("format-text-subscript"), tr("Subscript"), this);
  277. act->setCheckable(true);
  278. menu->addAction(act);
  279. menuView = menu = menuBar()->addMenu(tr("&View"));
  280. tool->addSeparator();
  281. QActionGroup * viewGroup = new QActionGroup(this);
  282. act = new QAction(tr("&Text"), this);
  283. act->setCheckable(true);
  284. act->setChecked(true);
  285. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  286. viewGroup->addAction(act);
  287. menu->addAction(act);
  288. tool->addAction(act);
  289. act = new QAction(tr("&Head"), this);
  290. act->setCheckable(true);
  291. connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
  292. viewGroup->addAction(act);
  293. menu->addAction(act);
  294. tool->addAction(act);
  295. act = new QAction(tr("&XML"), this);
  296. act->setCheckable(true);
  297. connect(act, SIGNAL(triggered()), this, SLOT(viewQsci()));
  298. viewGroup->addAction(act);
  299. menu->addAction(act);
  300. tool->addAction(act);
  301. menu->addSeparator();
  302. actionZoomIn = act = new QAction(icon("zoom-in"), tr("Zoom in"), this);
  303. act->setShortcuts(QKeySequence::ZoomIn);
  304. menu->addAction(act);
  305. actionZoomOut = act = new QAction(icon("zoom-out"), tr("Zoom out"), this);
  306. act->setShortcuts(QKeySequence::ZoomOut);
  307. menu->addAction(act);
  308. actionZoomOrig = act = new QAction(icon("zoom-original"), tr("Zoom original"), this);
  309. menu->addAction(act);
  310. menu->addSeparator();
  311. act = new QAction(tr("&Contents"), this);
  312. connect(act, SIGNAL(triggered()), this, SLOT(viewTree()));
  313. menu->addAction(act);
  314. act = new QAction(tr("&Web inspector"), this);
  315. connect(act, SIGNAL(triggered()), this, SLOT(showInspector()));
  316. menu->addAction(act);
  317. menuBar()->addSeparator();
  318. menu = menuBar()->addMenu(tr("&Help"));
  319. act = new QAction(icon("help-about"), tr("&About"), this);
  320. act->setStatusTip(tr("Show the application's About box"));
  321. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  322. menu->addAction(act);
  323. act = new QAction(tr("About &Qt"), this);
  324. act->setStatusTip(tr("Show the Qt library's About box"));
  325. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  326. menu->addAction(act);
  327. }
  328. void Fb2MainWindow::createTree()
  329. {
  330. if (treeView) return;
  331. treeView = new QTreeView(this);
  332. treeView->setHeaderHidden(true);
  333. connect(treeView, SIGNAL(activated(QModelIndex)), SLOT(treeActivated(QModelIndex)));
  334. connect(treeView, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  335. dockTree = new QDockWidget(tr("Contents"), this);
  336. dockTree->setAttribute(Qt::WA_DeleteOnClose);
  337. dockTree->setFeatures(QDockWidget::AllDockWidgetFeatures);
  338. dockTree->setWidget(treeView);
  339. addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  340. treeView->setFocus();
  341. }
  342. void Fb2MainWindow::createHead()
  343. {
  344. if (headTree) return;
  345. headTree = new QTreeView(this);
  346. headTree->header()->setDefaultSectionSize(200);
  347. if (textEdit) {
  348. this->setFocus();
  349. textEdit->setParent(NULL);
  350. setCentralWidget(headTree);
  351. textEdit->setParent(this);
  352. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  353. headTree->setModel(model);
  354. model->expand(headTree);
  355. } else {
  356. setCentralWidget(headTree);
  357. }
  358. headTree->setFocus();
  359. }
  360. void Fb2MainWindow::loadFinished(bool ok)
  361. {
  362. if (!treeView) return ;
  363. Fb2TreeModel *model = new Fb2TreeModel(*textEdit, treeView);
  364. treeView->setModel(model);
  365. model->expand(treeView);
  366. }
  367. void Fb2MainWindow::selectionChanged()
  368. {
  369. actionCut->setEnabled(textEdit->CutEnabled());
  370. actionCopy->setEnabled(textEdit->CopyEnabled());
  371. actionTextBold->setChecked(textEdit->BoldChecked());
  372. actionTextItalic->setChecked(textEdit->ItalicChecked());
  373. actionTextStrike->setChecked(textEdit->StrikeChecked());
  374. actionTextSub->setChecked(textEdit->SubChecked());
  375. actionTextSup->setChecked(textEdit->SupChecked());
  376. // QString script = "document.getSelection().baseNode.parentNode.tagName";
  377. // qCritical() << textEdit->page()->mainFrame()->evaluateJavaScript(script).toString();
  378. }
  379. void Fb2MainWindow::undoChanged()
  380. {
  381. actionUndo->setEnabled(textEdit->UndoEnabled());
  382. }
  383. void Fb2MainWindow::redoChanged()
  384. {
  385. actionRedo->setEnabled(textEdit->RedoEnabled());
  386. }
  387. void Fb2MainWindow::createQsci()
  388. {
  389. // http://qtcoder.blogspot.com/2010/10/qscintills.html
  390. // http://www.riverbankcomputing.co.uk/static/Docs/QScintilla2/classQsciScintilla.html
  391. if (qsciEdit) return;
  392. qsciEdit = new QsciScintilla;
  393. qsciEdit->setUtf8(true);
  394. qsciEdit->setCaretLineVisible(true);
  395. qsciEdit->setCaretLineBackgroundColor(QColor("gainsboro"));
  396. qsciEdit->setWrapMode(QsciScintilla::WrapWord);
  397. qsciEdit->setEolMode(QsciScintilla::EolWindows);
  398. qsciEdit->setAutoIndent(true);
  399. qsciEdit->setIndentationGuides(true);
  400. qsciEdit->setAutoCompletionSource(QsciScintilla::AcsAll);
  401. qsciEdit->setAutoCompletionCaseSensitivity(true);
  402. qsciEdit->setAutoCompletionReplaceWord(true);
  403. qsciEdit->setAutoCompletionShowSingle(true);
  404. qsciEdit->setAutoCompletionThreshold(2);
  405. qsciEdit->setMarginsBackgroundColor(QColor("gainsboro"));
  406. qsciEdit->setMarginWidth(0, 0);
  407. qsciEdit->setMarginLineNumbers(1, true);
  408. qsciEdit->setMarginWidth(1, QString("10000"));
  409. qsciEdit->setFolding(QsciScintilla::BoxedFoldStyle, 2);
  410. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  411. qsciEdit->setMatchedBraceBackgroundColor(Qt::yellow);
  412. qsciEdit->setUnmatchedBraceForegroundColor(Qt::blue);
  413. QFont font("Courier", 10);
  414. font.setStyleHint(QFont::TypeWriter);
  415. QsciLexerXML * lexer = new QsciLexerXML;
  416. lexer->setFont(font, -1);
  417. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  418. qsciEdit->setLexer(lexer);
  419. setCentralWidget(qsciEdit);
  420. qsciEdit->setFocus();
  421. // connect(qsciEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  422. }
  423. void Fb2MainWindow::createStatusBar()
  424. {
  425. statusBar()->showMessage(tr("Ready"));
  426. }
  427. void Fb2MainWindow::readSettings()
  428. {
  429. QSettings settings;
  430. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  431. QSize size = settings.value("size", QSize(400, 400)).toSize();
  432. move(pos);
  433. resize(size);
  434. }
  435. void Fb2MainWindow::writeSettings()
  436. {
  437. QSettings settings;
  438. settings.setValue("pos", pos());
  439. settings.setValue("size", size());
  440. }
  441. bool Fb2MainWindow::maybeSave()
  442. {
  443. if (textEdit && textEdit->isModified()) {
  444. QMessageBox::StandardButton ret;
  445. ret = QMessageBox::warning(this, qApp->applicationName(),
  446. tr("The document has been modified. Do you want to save your changes?"),
  447. QMessageBox::Save | QMessageBox::Discard
  448. | QMessageBox::Cancel);
  449. if (ret == QMessageBox::Save)
  450. return fileSave();
  451. else if (ret == QMessageBox::Cancel)
  452. return false;
  453. }
  454. return true;
  455. }
  456. bool Fb2MainWindow::saveFile(const QString &fileName)
  457. {
  458. QFile file(fileName);
  459. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  460. QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  461. return false;
  462. }
  463. if (textEdit) return textEdit->save(&file);
  464. return true;
  465. /*
  466. QTextStream out(&file);
  467. QApplication::setOverrideCursor(Qt::WaitCursor);
  468. out << textEdit->toPlainText();
  469. QApplication::restoreOverrideCursor();
  470. setCurrentFile(fileName);
  471. statusBar()->showMessage(tr("File saved"), 2000);
  472. */
  473. }
  474. void Fb2MainWindow::setCurrentFile(const QString &filename)
  475. {
  476. static int sequenceNumber = 1;
  477. QString title;
  478. isUntitled = filename.isEmpty();
  479. if (isUntitled) {
  480. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  481. title = curFile;
  482. } else {
  483. QFileInfo info = filename;
  484. curFile = info.canonicalFilePath();
  485. title = info.fileName();
  486. }
  487. title += QString(" - ") += qApp->applicationName();
  488. setWindowModified(false);
  489. setWindowFilePath(curFile);
  490. setWindowTitle(title);
  491. }
  492. Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
  493. {
  494. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  495. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  496. Fb2MainWindow *mainWin = qobject_cast<Fb2MainWindow *>(widget);
  497. if (mainWin && mainWin->curFile == canonicalFilePath)
  498. return mainWin;
  499. }
  500. return 0;
  501. }
  502. void Fb2MainWindow::viewQsci()
  503. {
  504. if (centralWidget() == qsciEdit) return;
  505. QString xml;
  506. if (textEdit) textEdit->save(&xml);
  507. FB2DELETE(textEdit);
  508. FB2DELETE(dockTree);
  509. FB2DELETE(headTree);
  510. FB2DELETE(toolEdit);
  511. createQsci();
  512. qsciEdit->setText(xml);
  513. }
  514. void Fb2MainWindow::viewText()
  515. {
  516. if (centralWidget() == textEdit) return;
  517. FB2DELETE(qsciEdit);
  518. FB2DELETE(headTree);
  519. if (!textEdit) {
  520. textEdit = new Fb2WebView(this);
  521. }
  522. setCentralWidget(textEdit);
  523. textEdit->setFocus();
  524. viewTree();
  525. connect(textEdit->page(), SIGNAL(contentsChanged()), SLOT(documentWasModified()));
  526. connect(textEdit->page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  527. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  528. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  529. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  530. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  531. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  532. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  533. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  534. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  535. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  536. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  537. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  538. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  539. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  540. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  541. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  542. connect(actionZoomOrig, SIGNAL(triggered()), textEdit, SLOT(zoomOrig()));
  543. FB2DELETE(toolEdit);
  544. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  545. tool->setMovable(false);
  546. tool->addSeparator();
  547. QAction *act;
  548. act = textEdit->pageAction(QWebPage::Undo);
  549. act->setIcon(icon("edit-undo"));
  550. act->setText(tr("&Undo"));
  551. act->setPriority(QAction::LowPriority);
  552. act->setShortcut(QKeySequence::Undo);
  553. tool->addAction(act);
  554. act = textEdit->pageAction(QWebPage::Redo);
  555. act->setIcon(icon("edit-redo"));
  556. act->setText(tr("&Redo"));
  557. act->setPriority(QAction::LowPriority);
  558. act->setShortcut(QKeySequence::Redo);
  559. tool->addAction(act);
  560. tool->addSeparator();
  561. act = textEdit->pageAction(QWebPage::Cut);
  562. act->setIcon(icon("edit-cut"));
  563. act->setText(tr("Cu&t"));
  564. act->setPriority(QAction::LowPriority);
  565. act->setShortcuts(QKeySequence::Cut);
  566. act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  567. tool->addAction(act);
  568. act = textEdit->pageAction(QWebPage::Copy);
  569. act->setIcon(icon("edit-copy"));
  570. act->setText(tr("&Copy"));
  571. act->setPriority(QAction::LowPriority);
  572. act->setShortcuts(QKeySequence::Copy);
  573. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  574. tool->addAction(act);
  575. act = textEdit->pageAction(QWebPage::Paste);
  576. act->setIcon(icon("edit-paste"));
  577. act->setText(tr("&Paste"));
  578. act->setPriority(QAction::LowPriority);
  579. act->setShortcuts(QKeySequence::Paste);
  580. act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  581. tool->addAction(act);
  582. tool->addSeparator();
  583. act = textEdit->pageAction(QWebPage::ToggleBold);
  584. act->setIcon(icon("format-text-bold"));
  585. act->setText(tr("&Bold"));
  586. act->setShortcuts(QKeySequence::Bold);
  587. tool->addAction(act);
  588. act = textEdit->pageAction(QWebPage::ToggleItalic);
  589. act->setIcon(icon("format-text-italic"));
  590. act->setText(tr("&Italic"));
  591. act->setShortcuts(QKeySequence::Italic);
  592. tool->addAction(act);
  593. act = textEdit->pageAction(QWebPage::ToggleStrikethrough);
  594. act->setIcon(icon("format-text-strikethrough"));
  595. act->setText(tr("&Strikethrough"));
  596. tool->addAction(act);
  597. act = textEdit->pageAction(QWebPage::ToggleSuperscript);
  598. act->setIcon(icon("format-text-superscript"));
  599. act->setText(tr("Superscript"));
  600. tool->addAction(act);
  601. act = textEdit->pageAction(QWebPage::ToggleSubscript);
  602. act->setIcon(icon("format-text-subscript"));
  603. act->setText(tr("Subscript"));
  604. tool->addAction(act);
  605. tool->addSeparator();
  606. tool->addAction(actionZoomIn);
  607. tool->addAction(actionZoomOut);
  608. tool->addAction(actionZoomOrig);
  609. }
  610. void Fb2MainWindow::viewHead()
  611. {
  612. if (centralWidget() == headTree) return;
  613. FB2DELETE(dockTree);
  614. FB2DELETE(qsciEdit);
  615. FB2DELETE(toolEdit);
  616. createHead();
  617. if (textEdit) {
  618. actionUndo->disconnect();
  619. actionRedo->disconnect();
  620. actionCut->disconnect();
  621. actionCopy->disconnect();
  622. actionPaste->disconnect();
  623. actionTextBold->disconnect();
  624. actionTextItalic->disconnect();
  625. actionTextStrike->disconnect();
  626. actionTextSub->disconnect();
  627. actionTextSup->disconnect();
  628. }
  629. FB2DELETE(toolEdit);
  630. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  631. tool->addAction(actionInsert);
  632. tool->addAction(actionDelete);
  633. tool->setMovable(false);
  634. tool->addSeparator();
  635. }
  636. void Fb2MainWindow::viewTree()
  637. {
  638. if (centralWidget() != textEdit) return;
  639. if (treeView == NULL) createTree();
  640. loadFinished(true);
  641. }
  642. void Fb2MainWindow::clipboardDataChanged()
  643. {
  644. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  645. actionPaste->setEnabled(md->hasText());
  646. }
  647. }
  648. void Fb2MainWindow::showInspector()
  649. {
  650. if (!textEdit) return;
  651. QWebInspector * inspector = new QWebInspector();
  652. inspector->setPage(textEdit->page());
  653. inspector->show();
  654. }