fb2main.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. textEdit = new Fb2WebView(this);
  28. viewText();
  29. textEdit->load(filename);
  30. } else {
  31. createQsci();
  32. loadXML(filename);
  33. }
  34. }
  35. void Fb2MainWindow::init()
  36. {
  37. connect(qApp, SIGNAL(logMessage(QString)), SLOT(logMessage(QString)));
  38. setAttribute(Qt::WA_DeleteOnClose);
  39. isUntitled = true;
  40. createActions();
  41. createStatusBar();
  42. textEdit = NULL;
  43. noteEdit = NULL;
  44. codeEdit = NULL;
  45. treeView = NULL;
  46. headTree = NULL;
  47. toolEdit = NULL;
  48. messageEdit = NULL;
  49. readSettings();
  50. setUnifiedTitleAndToolBarOnMac(true);
  51. }
  52. void Fb2MainWindow::logMessage(const QString &message)
  53. {
  54. if (!messageEdit) {
  55. messageEdit = new QTextEdit(this);
  56. connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
  57. QDockWidget * dock = new QDockWidget(tr("Message log"), this);
  58. dock->setAttribute(Qt::WA_DeleteOnClose);
  59. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  60. dock->setWidget(messageEdit);
  61. addDockWidget(Qt::BottomDockWidgetArea, dock);
  62. messageEdit->setMaximumHeight(80);
  63. }
  64. messageEdit->append(message);
  65. }
  66. void Fb2MainWindow::logShowed()
  67. {
  68. messageEdit->setMaximumHeight(QWIDGETSIZE_MAX);
  69. }
  70. void Fb2MainWindow::logDestroyed()
  71. {
  72. messageEdit = NULL;
  73. }
  74. void Fb2MainWindow::treeActivated(const QModelIndex &index)
  75. {
  76. if (!treeView) return;
  77. Fb2TreeModel *model = dynamic_cast<Fb2TreeModel*>(treeView->model());
  78. if (model) model->select(index);
  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(":/24/%1.png").arg(name), QSize(24,24));
  177. icon.addFile(QString(":/16/%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. actionInsert = act = new QAction(icon("list-add"), tr("Insert"), this);
  257. act->setPriority(QAction::LowPriority);
  258. act->setShortcuts(QKeySequence::New);
  259. menu->addAction(act);
  260. actionDelete = act = new QAction(icon("list-remove"), tr("Delete"), this);
  261. act->setPriority(QAction::LowPriority);
  262. act->setShortcuts(QKeySequence::Delete);
  263. menu->addAction(act);
  264. menuText = menu = menuBar()->addMenu(tr("Fo&rmat"));
  265. actionTextBold = act = new QAction(icon("format-text-bold"), tr("Bold"), this);
  266. act->setShortcuts(QKeySequence::Bold);
  267. act->setCheckable(true);
  268. menu->addAction(act);
  269. actionTextItalic = act = new QAction(icon("format-text-italic"), tr("Italic"), this);
  270. act->setShortcuts(QKeySequence::Italic);
  271. act->setCheckable(true);
  272. menu->addAction(act);
  273. actionTextStrike = act = new QAction(icon("format-text-strikethrough"), tr("Strikethrough"), this);
  274. act->setCheckable(true);
  275. menu->addAction(act);
  276. actionTextSup = act = new QAction(icon("format-text-superscript"), tr("Superscript"), this);
  277. act->setCheckable(true);
  278. menu->addAction(act);
  279. actionTextSub = act = new QAction(icon("format-text-subscript"), tr("Subscript"), this);
  280. act->setCheckable(true);
  281. menu->addAction(act);
  282. menuView = menu = menuBar()->addMenu(tr("&View"));
  283. tool->addSeparator();
  284. QActionGroup * viewGroup = new QActionGroup(this);
  285. act = new QAction(tr("&Text"), this);
  286. act->setCheckable(true);
  287. act->setChecked(true);
  288. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  289. viewGroup->addAction(act);
  290. menu->addAction(act);
  291. tool->addAction(act);
  292. act = new QAction(tr("&Head"), this);
  293. act->setCheckable(true);
  294. connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
  295. viewGroup->addAction(act);
  296. menu->addAction(act);
  297. tool->addAction(act);
  298. act = new QAction(tr("&XML"), this);
  299. act->setCheckable(true);
  300. connect(act, SIGNAL(triggered()), this, SLOT(viewQsci()));
  301. viewGroup->addAction(act);
  302. menu->addAction(act);
  303. tool->addAction(act);
  304. menu->addSeparator();
  305. actionZoomIn = act = new QAction(icon("zoom-in"), tr("Zoom in"), this);
  306. act->setShortcuts(QKeySequence::ZoomIn);
  307. menu->addAction(act);
  308. actionZoomOut = act = new QAction(icon("zoom-out"), tr("Zoom out"), this);
  309. act->setShortcuts(QKeySequence::ZoomOut);
  310. menu->addAction(act);
  311. actionZoomOrig = act = new QAction(icon("zoom-original"), tr("Zoom original"), this);
  312. menu->addAction(act);
  313. menu->addSeparator();
  314. act = new QAction(tr("&Contents"), this);
  315. connect(act, SIGNAL(triggered()), this, SLOT(viewTree()));
  316. menu->addAction(act);
  317. act = new QAction(tr("&Web inspector"), this);
  318. connect(act, SIGNAL(triggered()), this, SLOT(showInspector()));
  319. menu->addAction(act);
  320. menuBar()->addSeparator();
  321. menu = menuBar()->addMenu(tr("&Help"));
  322. act = new QAction(icon("help-about"), tr("&About"), this);
  323. act->setStatusTip(tr("Show the application's About box"));
  324. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  325. menu->addAction(act);
  326. act = new QAction(tr("About &Qt"), this);
  327. act->setStatusTip(tr("Show the Qt library's About box"));
  328. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  329. menu->addAction(act);
  330. }
  331. void Fb2MainWindow::createTree()
  332. {
  333. if (treeView) return;
  334. treeView = new QTreeView(this);
  335. treeView->setHeaderHidden(true);
  336. connect(treeView, SIGNAL(activated(QModelIndex)), SLOT(treeActivated(QModelIndex)));
  337. connect(treeView, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  338. dockTree = new QDockWidget(tr("Contents"), this);
  339. dockTree->setAttribute(Qt::WA_DeleteOnClose);
  340. dockTree->setFeatures(QDockWidget::AllDockWidgetFeatures);
  341. dockTree->setWidget(treeView);
  342. addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  343. treeView->setFocus();
  344. }
  345. void Fb2MainWindow::loadFinished(bool ok)
  346. {
  347. if (headTree) {
  348. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  349. headTree->setModel(model);
  350. model->expand(headTree);
  351. }
  352. if (treeView) {
  353. Fb2TreeModel *model = new Fb2TreeModel(*textEdit, treeView);
  354. treeView->setModel(model);
  355. model->expand(treeView);
  356. }
  357. }
  358. void Fb2MainWindow::selectionChanged()
  359. {
  360. actionCut->setEnabled(textEdit->CutEnabled());
  361. actionCopy->setEnabled(textEdit->CopyEnabled());
  362. actionTextBold->setChecked(textEdit->BoldChecked());
  363. actionTextItalic->setChecked(textEdit->ItalicChecked());
  364. actionTextStrike->setChecked(textEdit->StrikeChecked());
  365. actionTextSub->setChecked(textEdit->SubChecked());
  366. actionTextSup->setChecked(textEdit->SupChecked());
  367. // QString script = "document.getSelection().baseNode.parentNode.tagName";
  368. // qCritical() << textEdit->page()->mainFrame()->evaluateJavaScript(script).toString();
  369. }
  370. void Fb2MainWindow::undoChanged()
  371. {
  372. actionUndo->setEnabled(textEdit->UndoEnabled());
  373. }
  374. void Fb2MainWindow::redoChanged()
  375. {
  376. actionRedo->setEnabled(textEdit->RedoEnabled());
  377. }
  378. void Fb2MainWindow::createQsci()
  379. {
  380. // http://qtcoder.blogspot.com/2010/10/qscintills.html
  381. // http://www.riverbankcomputing.co.uk/static/Docs/QScintilla2/classQsciScintilla.html
  382. if (codeEdit) return;
  383. codeEdit = new QsciScintilla;
  384. codeEdit->zoomTo(1);
  385. codeEdit->setUtf8(true);
  386. codeEdit->setCaretLineVisible(true);
  387. codeEdit->setCaretLineBackgroundColor(QColor("gainsboro"));
  388. codeEdit->setWrapMode(QsciScintilla::WrapWord);
  389. codeEdit->setEolMode(QsciScintilla::EolWindows);
  390. codeEdit->setAutoIndent(true);
  391. codeEdit->setIndentationGuides(true);
  392. codeEdit->setAutoCompletionSource(QsciScintilla::AcsAll);
  393. codeEdit->setAutoCompletionCaseSensitivity(true);
  394. codeEdit->setAutoCompletionReplaceWord(true);
  395. codeEdit->setAutoCompletionShowSingle(true);
  396. codeEdit->setAutoCompletionThreshold(2);
  397. codeEdit->setMarginsBackgroundColor(QColor("gainsboro"));
  398. codeEdit->setMarginWidth(0, 0);
  399. codeEdit->setMarginLineNumbers(1, true);
  400. codeEdit->setMarginWidth(1, QString("10000"));
  401. codeEdit->setFolding(QsciScintilla::BoxedFoldStyle, 2);
  402. codeEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  403. codeEdit->setMatchedBraceBackgroundColor(Qt::yellow);
  404. codeEdit->setUnmatchedBraceForegroundColor(Qt::blue);
  405. QFont font("Courier", 10);
  406. font.setStyleHint(QFont::TypeWriter);
  407. QsciLexerXML * lexer = new QsciLexerXML;
  408. lexer->setFont(font, -1);
  409. codeEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  410. codeEdit->setLexer(lexer);
  411. setCentralWidget(codeEdit);
  412. codeEdit->setFocus();
  413. }
  414. void Fb2MainWindow::createStatusBar()
  415. {
  416. statusBar()->showMessage(tr("Ready"));
  417. }
  418. void Fb2MainWindow::readSettings()
  419. {
  420. QSettings settings;
  421. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  422. QSize size = settings.value("size", QSize(400, 400)).toSize();
  423. move(pos);
  424. resize(size);
  425. }
  426. void Fb2MainWindow::writeSettings()
  427. {
  428. QSettings settings;
  429. settings.setValue("pos", pos());
  430. settings.setValue("size", size());
  431. }
  432. bool Fb2MainWindow::maybeSave()
  433. {
  434. if (textEdit && textEdit->isModified()) {
  435. QMessageBox::StandardButton ret;
  436. ret = QMessageBox::warning(this, qApp->applicationName(),
  437. tr("The document has been modified. Do you want to save your changes?"),
  438. QMessageBox::Save | QMessageBox::Discard
  439. | QMessageBox::Cancel);
  440. if (ret == QMessageBox::Save)
  441. return fileSave();
  442. else if (ret == QMessageBox::Cancel)
  443. return false;
  444. }
  445. return true;
  446. }
  447. bool Fb2MainWindow::saveFile(const QString &fileName)
  448. {
  449. QFile file(fileName);
  450. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  451. QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  452. return false;
  453. }
  454. if (textEdit) return textEdit->save(&file);
  455. return true;
  456. /*
  457. QTextStream out(&file);
  458. QApplication::setOverrideCursor(Qt::WaitCursor);
  459. out << textEdit->toPlainText();
  460. QApplication::restoreOverrideCursor();
  461. setCurrentFile(fileName);
  462. statusBar()->showMessage(tr("File saved"), 2000);
  463. */
  464. }
  465. void Fb2MainWindow::setCurrentFile(const QString &filename)
  466. {
  467. static int sequenceNumber = 1;
  468. QString title;
  469. isUntitled = filename.isEmpty();
  470. if (isUntitled) {
  471. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  472. title = curFile;
  473. } else {
  474. QFileInfo info = filename;
  475. curFile = info.canonicalFilePath();
  476. title = info.fileName();
  477. }
  478. title += QString(" - ") += qApp->applicationName();
  479. setWindowModified(false);
  480. setWindowFilePath(curFile);
  481. setWindowTitle(title);
  482. }
  483. Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
  484. {
  485. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  486. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  487. Fb2MainWindow *mainWin = qobject_cast<Fb2MainWindow *>(widget);
  488. if (mainWin && mainWin->curFile == canonicalFilePath)
  489. return mainWin;
  490. }
  491. return 0;
  492. }
  493. void Fb2MainWindow::zoomOrig()
  494. {
  495. if (codeEdit) codeEdit->zoomTo(1);
  496. }
  497. void Fb2MainWindow::checkScintillaUndo()
  498. {
  499. if (!codeEdit) return;
  500. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  501. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  502. }
  503. void Fb2MainWindow::viewQsci()
  504. {
  505. if (centralWidget() == codeEdit) return;
  506. QString xml;
  507. if (textEdit) textEdit->save(&xml);
  508. FB2DELETE(textEdit);
  509. FB2DELETE(dockTree);
  510. FB2DELETE(headTree);
  511. createQsci();
  512. codeEdit->setText(xml);
  513. FB2DELETE(toolEdit);
  514. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  515. tool->addSeparator();
  516. tool->addAction(actionUndo);
  517. tool->addAction(actionRedo);
  518. tool->addSeparator();
  519. tool->addAction(actionCut);
  520. tool->addAction(actionCopy);
  521. tool->addAction(actionPaste);
  522. tool->addSeparator();
  523. tool->addAction(actionZoomIn);
  524. tool->addAction(actionZoomOut);
  525. tool->addAction(actionZoomOrig);
  526. tool->setMovable(false);
  527. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  528. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  529. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  530. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  531. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  532. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  533. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  534. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  535. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  536. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  537. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  538. connect(actionZoomOrig, SIGNAL(triggered()), this, SLOT(zoomOrig()));
  539. }
  540. void Fb2MainWindow::viewText()
  541. {
  542. if (centralWidget() == textEdit) return;
  543. QString xml;
  544. if (codeEdit) xml = codeEdit->text();
  545. FB2DELETE(codeEdit);
  546. FB2DELETE(headTree);
  547. if (!textEdit) {
  548. textEdit = new Fb2WebView(this);
  549. }
  550. setCentralWidget(textEdit);
  551. textEdit->setFocus();
  552. viewTree();
  553. connect(textEdit->page(), SIGNAL(contentsChanged()), SLOT(documentWasModified()));
  554. connect(textEdit->page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  555. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  556. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  557. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  558. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  559. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  560. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  561. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  562. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  563. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  564. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  565. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  566. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  567. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  568. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  569. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  570. connect(actionZoomOrig, SIGNAL(triggered()), textEdit, SLOT(zoomOrig()));
  571. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  572. FB2DELETE(toolEdit);
  573. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  574. tool->setMovable(false);
  575. tool->addSeparator();
  576. QAction *act;
  577. act = textEdit->pageAction(QWebPage::Undo);
  578. act->setIcon(icon("edit-undo"));
  579. act->setText(tr("&Undo"));
  580. act->setPriority(QAction::LowPriority);
  581. act->setShortcut(QKeySequence::Undo);
  582. tool->addAction(act);
  583. act = textEdit->pageAction(QWebPage::Redo);
  584. act->setIcon(icon("edit-redo"));
  585. act->setText(tr("&Redo"));
  586. act->setPriority(QAction::LowPriority);
  587. act->setShortcut(QKeySequence::Redo);
  588. tool->addAction(act);
  589. tool->addSeparator();
  590. act = textEdit->pageAction(QWebPage::Cut);
  591. act->setIcon(icon("edit-cut"));
  592. act->setText(tr("Cu&t"));
  593. act->setPriority(QAction::LowPriority);
  594. act->setShortcuts(QKeySequence::Cut);
  595. act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  596. tool->addAction(act);
  597. act = textEdit->pageAction(QWebPage::Copy);
  598. act->setIcon(icon("edit-copy"));
  599. act->setText(tr("&Copy"));
  600. act->setPriority(QAction::LowPriority);
  601. act->setShortcuts(QKeySequence::Copy);
  602. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  603. tool->addAction(act);
  604. act = textEdit->pageAction(QWebPage::Paste);
  605. act->setIcon(icon("edit-paste"));
  606. act->setText(tr("&Paste"));
  607. act->setPriority(QAction::LowPriority);
  608. act->setShortcuts(QKeySequence::Paste);
  609. act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  610. tool->addAction(act);
  611. tool->addSeparator();
  612. act = textEdit->pageAction(QWebPage::ToggleBold);
  613. act->setIcon(icon("format-text-bold"));
  614. act->setText(tr("&Bold"));
  615. act->setShortcuts(QKeySequence::Bold);
  616. tool->addAction(act);
  617. act = textEdit->pageAction(QWebPage::ToggleItalic);
  618. act->setIcon(icon("format-text-italic"));
  619. act->setText(tr("&Italic"));
  620. act->setShortcuts(QKeySequence::Italic);
  621. tool->addAction(act);
  622. act = textEdit->pageAction(QWebPage::ToggleStrikethrough);
  623. act->setIcon(icon("format-text-strikethrough"));
  624. act->setText(tr("&Strikethrough"));
  625. tool->addAction(act);
  626. act = textEdit->pageAction(QWebPage::ToggleSuperscript);
  627. act->setIcon(icon("format-text-superscript"));
  628. act->setText(tr("Superscript"));
  629. tool->addAction(act);
  630. act = textEdit->pageAction(QWebPage::ToggleSubscript);
  631. act->setIcon(icon("format-text-subscript"));
  632. act->setText(tr("Subscript"));
  633. tool->addAction(act);
  634. tool->addSeparator();
  635. tool->addAction(actionZoomIn);
  636. tool->addAction(actionZoomOut);
  637. tool->addAction(actionZoomOrig);
  638. }
  639. void Fb2MainWindow::viewHead()
  640. {
  641. if (centralWidget() == headTree) return;
  642. QString xml;
  643. if (codeEdit) xml = codeEdit->text();
  644. FB2DELETE(dockTree);
  645. FB2DELETE(codeEdit);
  646. FB2DELETE(toolEdit);
  647. if (!headTree) {
  648. headTree = new QTreeView(this);
  649. headTree->header()->setDefaultSectionSize(200);
  650. }
  651. if (textEdit) {
  652. this->setFocus();
  653. textEdit->setParent(NULL);
  654. setCentralWidget(headTree);
  655. textEdit->setParent(this);
  656. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  657. headTree->setModel(model);
  658. model->expand(headTree);
  659. } else {
  660. textEdit = new Fb2WebView(this);
  661. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  662. setCentralWidget(headTree);
  663. }
  664. headTree->setFocus();
  665. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  666. if (textEdit) {
  667. actionUndo->disconnect();
  668. actionRedo->disconnect();
  669. actionCut->disconnect();
  670. actionCopy->disconnect();
  671. actionPaste->disconnect();
  672. actionTextBold->disconnect();
  673. actionTextItalic->disconnect();
  674. actionTextStrike->disconnect();
  675. actionTextSub->disconnect();
  676. actionTextSup->disconnect();
  677. }
  678. FB2DELETE(toolEdit);
  679. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  680. tool->addSeparator();
  681. tool->addAction(actionInsert);
  682. tool->addAction(actionDelete);
  683. tool->setMovable(false);
  684. }
  685. void Fb2MainWindow::viewTree()
  686. {
  687. if (centralWidget() != textEdit) return;
  688. if (treeView == NULL) createTree();
  689. loadFinished(true);
  690. }
  691. void Fb2MainWindow::clipboardDataChanged()
  692. {
  693. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  694. actionPaste->setEnabled(md->hasText());
  695. }
  696. }
  697. void Fb2MainWindow::showInspector()
  698. {
  699. if (!textEdit) return;
  700. QWebInspector * inspector = new QWebInspector();
  701. inspector->setPage(textEdit->page());
  702. inspector->show();
  703. }