fb2main.cpp 26 KB

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