fb2main.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 "fb2main.h"
  11. #include <Qsci/qsciscintilla.h>
  12. #include <Qsci/qscilexerxml.h>
  13. Fb2MainWindow::Fb2MainWindow()
  14. {
  15. init();
  16. setCurrentFile();
  17. createText();
  18. createTree();
  19. }
  20. Fb2MainWindow::Fb2MainWindow(const QString &filename, ViewMode mode)
  21. {
  22. init();
  23. setCurrentFile(filename);
  24. if (mode == FB2) {
  25. createText();
  26. createTree();
  27. textEdit->load(filename);
  28. } else {
  29. createQsci();
  30. loadXML(filename);
  31. }
  32. }
  33. void Fb2MainWindow::init()
  34. {
  35. connect(qApp, SIGNAL(logMessage(QString)), SLOT(logMessage(QString)));
  36. setAttribute(Qt::WA_DeleteOnClose);
  37. isUntitled = true;
  38. createActions();
  39. createStatusBar();
  40. textEdit = NULL;
  41. noteEdit = NULL;
  42. qsciEdit = NULL;
  43. treeView = NULL;
  44. messageEdit = NULL;
  45. readSettings();
  46. setUnifiedTitleAndToolBarOnMac(true);
  47. }
  48. void Fb2MainWindow::logMessage(const QString &message)
  49. {
  50. if (!messageEdit) {
  51. messageEdit = new QTextEdit(this);
  52. connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
  53. QDockWidget * dock = new QDockWidget(tr("Message log"), this);
  54. dock->setAttribute(Qt::WA_DeleteOnClose);
  55. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  56. dock->setWidget(messageEdit);
  57. addDockWidget(Qt::BottomDockWidgetArea, dock);
  58. messageEdit->setMaximumHeight(80);
  59. }
  60. messageEdit->append(message);
  61. }
  62. void Fb2MainWindow::logShowed()
  63. {
  64. messageEdit->setMaximumHeight(QWIDGETSIZE_MAX);
  65. }
  66. void Fb2MainWindow::logDestroyed()
  67. {
  68. messageEdit = NULL;
  69. }
  70. void Fb2MainWindow::treeActivated(const QModelIndex &index)
  71. {
  72. if (!treeView) return;
  73. Fb2TreeModel *model = dynamic_cast<Fb2TreeModel*>(treeView->model());
  74. if (model) model->select(index);
  75. }
  76. void Fb2MainWindow::treeDestroyed()
  77. {
  78. treeView = NULL;
  79. dockTree = NULL;
  80. }
  81. bool Fb2MainWindow::loadXML(const QString &filename)
  82. {
  83. if (!filename.isEmpty()) {
  84. QFile file(filename);
  85. if (file.open(QFile::ReadOnly | QFile::Text)) {
  86. qsciEdit->clear();
  87. return qsciEdit->read(&file);
  88. }
  89. }
  90. return false;
  91. }
  92. void Fb2MainWindow::closeEvent(QCloseEvent *event)
  93. {
  94. if (maybeSave()) {
  95. writeSettings();
  96. event->accept();
  97. } else {
  98. event->ignore();
  99. }
  100. }
  101. void Fb2MainWindow::fileNew()
  102. {
  103. Fb2MainWindow *other = new Fb2MainWindow;
  104. other->move(x() + 40, y() + 40);
  105. other->show();
  106. }
  107. void Fb2MainWindow::fileOpen()
  108. {
  109. QString filename = QFileDialog::getOpenFileName(this);
  110. if (filename.isEmpty()) return;
  111. Fb2MainWindow * existing = findFb2MainWindow(filename);
  112. if (existing) {
  113. existing->show();
  114. existing->raise();
  115. existing->activateWindow();
  116. return;
  117. }
  118. if (textEdit) {
  119. if (isUntitled && !isWindowModified()) {
  120. setCurrentFile(filename);
  121. textEdit->load(filename);
  122. } else {
  123. Fb2MainWindow * other = new Fb2MainWindow(filename, FB2);
  124. other->move(x() + 40, y() + 40);
  125. other->show();
  126. }
  127. } else if (qsciEdit) {
  128. if (isUntitled && !isWindowModified()) {
  129. setCurrentFile(filename);
  130. loadXML(filename);
  131. } else {
  132. Fb2MainWindow * other = new Fb2MainWindow(filename, XML);
  133. other->move(x() + 40, y() + 40);
  134. other->show();
  135. }
  136. }
  137. }
  138. bool Fb2MainWindow::fileSave()
  139. {
  140. if (isUntitled) {
  141. return fileSaveAs();
  142. } else {
  143. return saveFile(curFile);
  144. }
  145. }
  146. bool Fb2MainWindow::fileSaveAs()
  147. {
  148. QString fileName = QFileDialog::getSaveFileName(this, tr("Save As..."), curFile);
  149. if (fileName.isEmpty()) return false;
  150. return saveFile(fileName);
  151. }
  152. void Fb2MainWindow::about()
  153. {
  154. QMessageBox::about(this, tr("About fb2edit"),
  155. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  156. }
  157. void Fb2MainWindow::documentWasModified()
  158. {
  159. if (isWindowModified()) return;
  160. QFileInfo info = windowFilePath();
  161. QString title = info.fileName();
  162. if (textEdit && textEdit->isModified()) title += QString("[*]");
  163. title += QString(" - ") += qApp->applicationName();
  164. setWindowTitle(title);
  165. setWindowModified(true);
  166. }
  167. QIcon Fb2MainWindow::icon(const QString &name)
  168. {
  169. QIcon icon;
  170. icon.addFile(QString(":/24/%1.png").arg(name), QSize(24,24));
  171. icon.addFile(QString(":/16/%1.png").arg(name), QSize(16,16));
  172. return QIcon::fromTheme(name, icon);
  173. }
  174. void Fb2MainWindow::createActions()
  175. {
  176. QAction * act;
  177. QMenu * menu;
  178. QToolBar * tool;
  179. QList<QAction*> actions;
  180. menu = menuBar()->addMenu(tr("&File"));
  181. tool = addToolBar(tr("File"));
  182. act = new QAction(icon("document-new"), tr("&New"), this);
  183. act->setPriority(QAction::LowPriority);
  184. act->setShortcuts(QKeySequence::New);
  185. act->setStatusTip(tr("Create a new file"));
  186. connect(act, SIGNAL(triggered()), this, SLOT(fileNew()));
  187. menu->addAction(act);
  188. tool->addAction(act);
  189. act = new QAction(icon("document-open"), tr("&Open..."), this);
  190. act->setShortcuts(QKeySequence::Open);
  191. act->setStatusTip(tr("Open an existing file"));
  192. connect(act, SIGNAL(triggered()), this, SLOT(fileOpen()));
  193. menu->addAction(act);
  194. tool->addAction(act);
  195. act = new QAction(icon("document-save"), tr("&Save"), this);
  196. act->setShortcuts(QKeySequence::Save);
  197. act->setStatusTip(tr("Save the document to disk"));
  198. connect(act, SIGNAL(triggered()), this, SLOT(fileSave()));
  199. menu->addAction(act);
  200. tool->addAction(act);
  201. act = new QAction(icon("document-save-as"), tr("Save &As..."), this);
  202. act->setShortcuts(QKeySequence::SaveAs);
  203. act->setStatusTip(tr("Save the document under a new name"));
  204. connect(act, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
  205. menu->addAction(act);
  206. menu->addSeparator();
  207. act = new QAction(icon("window-close"), tr("&Close"), this);
  208. act->setShortcuts(QKeySequence::Close);
  209. act->setStatusTip(tr("Close this window"));
  210. connect(act, SIGNAL(triggered()), this, SLOT(close()));
  211. menu->addAction(act);
  212. act = new QAction(icon("application-exit"), tr("E&xit"), this);
  213. act->setShortcuts(QKeySequence::Quit);
  214. act->setStatusTip(tr("Exit the application"));
  215. connect(act, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  216. menu->addAction(act);
  217. menu = menuBar()->addMenu(tr("&Edit"));
  218. tool = addToolBar(tr("Edit"));
  219. connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
  220. actionUndo = act = new QAction(icon("edit-undo"), tr("&Undo"), this);
  221. act->setPriority(QAction::LowPriority);
  222. act->setShortcut(QKeySequence::Undo);
  223. act->setEnabled(false);
  224. menu->addAction(act);
  225. tool->addAction(act);
  226. actionRedo = act = new QAction(icon("edit-redo"), tr("&Redo"), this);
  227. act->setPriority(QAction::LowPriority);
  228. act->setShortcut(QKeySequence::Redo);
  229. act->setEnabled(false);
  230. menu->addAction(act);
  231. tool->addAction(act);
  232. menu->addSeparator();
  233. tool->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. tool->addAction(act);
  241. actionCopy = act = new QAction(icon("edit-copy"), tr("&Copy"), this);
  242. act->setPriority(QAction::LowPriority);
  243. act->setShortcuts(QKeySequence::Copy);
  244. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  245. act->setEnabled(false);
  246. menu->addAction(act);
  247. tool->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. tool->addAction(act);
  254. clipboardDataChanged();
  255. menu = menuBar()->addMenu(tr("Fo&rmat"));
  256. tool = addToolBar(tr("Format"));
  257. actionTextBold = act = new QAction(icon("format-text-bold"), tr("Bold"), this);
  258. act->setShortcuts(QKeySequence::Bold);
  259. act->setCheckable(true);
  260. menu->addAction(act);
  261. tool->addAction(act);
  262. actionTextItalic = act = new QAction(icon("format-text-italic"), tr("Italic"), this);
  263. act->setShortcuts(QKeySequence::Italic);
  264. act->setCheckable(true);
  265. menu->addAction(act);
  266. tool->addAction(act);
  267. actionTextStrike = act = new QAction(icon("format-text-strikethrough"), tr("Strikethrough"), this);
  268. act->setCheckable(true);
  269. menu->addAction(act);
  270. tool->addAction(act);
  271. actionTextSup = act = new QAction(icon("format-text-superscript"), tr("Superscript"), this);
  272. act->setCheckable(true);
  273. menu->addAction(act);
  274. tool->addAction(act);
  275. actionTextSub = act = new QAction(icon("format-text-subscript"), tr("Subscript"), this);
  276. act->setCheckable(true);
  277. menu->addAction(act);
  278. tool->addAction(act);
  279. menu = menuBar()->addMenu(tr("&View"));
  280. QAction * actText = act = new QAction(tr("&Text"), this);
  281. act->setCheckable(true);
  282. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  283. QAction * actQsci = act = new QAction(tr("&XML"), this);
  284. act->setCheckable(true);
  285. connect(act, SIGNAL(triggered()), this, SLOT(viewQsci()));
  286. QActionGroup * viewGroup = new QActionGroup(this);
  287. viewGroup->addAction(actText);
  288. viewGroup->addAction(actQsci);
  289. actText->setChecked(true);
  290. menu->addAction(actText);
  291. menu->addAction(actQsci);
  292. menu->addSeparator();
  293. tool = addToolBar(tr("Zoom"));
  294. actionZoomIn = act = new QAction(icon("zoom-in"), tr("Zoom in"), this);
  295. act->setShortcuts(QKeySequence::ZoomIn);
  296. menu->addAction(act);
  297. tool->addAction(act);
  298. actionZoomOut = act = new QAction(icon("zoom-out"), tr("Zoom out"), this);
  299. act->setShortcuts(QKeySequence::ZoomOut);
  300. menu->addAction(act);
  301. tool->addAction(act);
  302. actionZoomOrig = act = new QAction(icon("zoom-original"), tr("Zoom original"), this);
  303. menu->addAction(act);
  304. tool->addAction(act);
  305. menu->addSeparator();
  306. act = new QAction(tr("Web inspector"), this);
  307. connect(act, SIGNAL(triggered()), this, SLOT(showInspector()));
  308. menu->addAction(act);
  309. menuBar()->addSeparator();
  310. menu = menuBar()->addMenu(tr("&Help"));
  311. act = new QAction(icon("help-about"), tr("&About"), this);
  312. act->setStatusTip(tr("Show the application's About box"));
  313. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  314. menu->addAction(act);
  315. act = new QAction(tr("About &Qt"), this);
  316. act->setStatusTip(tr("Show the Qt library's About box"));
  317. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  318. menu->addAction(act);
  319. }
  320. void Fb2MainWindow::createTree()
  321. {
  322. if (treeView) return;
  323. treeView = new QTreeView(this);
  324. treeView->setHeaderHidden(true);
  325. connect(treeView, SIGNAL(activated(QModelIndex)), SLOT(treeActivated(QModelIndex)));
  326. connect(treeView, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  327. dockTree = new QDockWidget(tr("Contents"), this);
  328. dockTree->setAttribute(Qt::WA_DeleteOnClose);
  329. dockTree->setFeatures(QDockWidget::AllDockWidgetFeatures);
  330. dockTree->setWidget(treeView);
  331. addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  332. }
  333. void Fb2MainWindow::createText()
  334. {
  335. if (textEdit) return;
  336. textEdit = new Fb2WebView(this);
  337. setCentralWidget(textEdit);
  338. textEdit->setFocus();
  339. connect(textEdit->page(), SIGNAL(contentsChanged()), SLOT(documentWasModified()));
  340. connect(textEdit, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  341. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  342. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  343. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  344. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  345. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  346. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  347. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  348. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  349. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  350. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  351. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  352. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  353. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  354. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  355. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  356. connect(actionZoomOrig, SIGNAL(triggered()), textEdit, SLOT(zoomOrig()));
  357. }
  358. void Fb2MainWindow::loadFinished(bool ok)
  359. {
  360. if (!treeView) return ;
  361. Fb2TreeModel *model = new Fb2TreeModel(*textEdit);
  362. treeView->setModel(model);
  363. model->expand(treeView);
  364. }
  365. void Fb2MainWindow::selectionChanged()
  366. {
  367. actionCut->setEnabled(textEdit->CutEnabled());
  368. actionCopy->setEnabled(textEdit->CopyEnabled());
  369. actionTextBold->setChecked(textEdit->BoldChecked());
  370. actionTextItalic->setChecked(textEdit->ItalicChecked());
  371. actionTextStrike->setChecked(textEdit->StrikeChecked());
  372. actionTextSub->setChecked(textEdit->SubChecked());
  373. actionTextSup->setChecked(textEdit->SupChecked());
  374. // QString script = "document.getSelection().baseNode.parentNode.tagName";
  375. // qCritical() << textEdit->page()->mainFrame()->evaluateJavaScript(script).toString();
  376. }
  377. void Fb2MainWindow::undoChanged()
  378. {
  379. actionUndo->setEnabled(textEdit->UndoEnabled());
  380. }
  381. void Fb2MainWindow::redoChanged()
  382. {
  383. actionRedo->setEnabled(textEdit->RedoEnabled());
  384. }
  385. void Fb2MainWindow::createQsci()
  386. {
  387. // http://qtcoder.blogspot.com/2010/10/qscintills.html
  388. // http://www.riverbankcomputing.co.uk/static/Docs/QScintilla2/classQsciScintilla.html
  389. if (qsciEdit) return;
  390. qsciEdit = new QsciScintilla;
  391. qsciEdit->setUtf8(true);
  392. qsciEdit->setCaretLineVisible(true);
  393. qsciEdit->setCaretLineBackgroundColor(QColor("gainsboro"));
  394. qsciEdit->setWrapMode(QsciScintilla::WrapWord);
  395. qsciEdit->setEolMode(QsciScintilla::EolWindows);
  396. qsciEdit->setAutoIndent(true);
  397. qsciEdit->setIndentationGuides(true);
  398. qsciEdit->setAutoCompletionSource(QsciScintilla::AcsAll);
  399. qsciEdit->setAutoCompletionCaseSensitivity(true);
  400. qsciEdit->setAutoCompletionReplaceWord(true);
  401. qsciEdit->setAutoCompletionShowSingle(true);
  402. qsciEdit->setAutoCompletionThreshold(2);
  403. qsciEdit->setMarginsBackgroundColor(QColor("gainsboro"));
  404. qsciEdit->setMarginWidth(0, 0);
  405. qsciEdit->setMarginLineNumbers(1, true);
  406. qsciEdit->setMarginWidth(1, QString("10000"));
  407. qsciEdit->setFolding(QsciScintilla::BoxedFoldStyle, 2);
  408. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  409. qsciEdit->setMatchedBraceBackgroundColor(Qt::yellow);
  410. qsciEdit->setUnmatchedBraceForegroundColor(Qt::blue);
  411. QFont font("Courier", 10);
  412. font.setStyleHint(QFont::TypeWriter);
  413. QsciLexerXML * lexer = new QsciLexerXML;
  414. lexer->setFont(font, -1);
  415. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  416. qsciEdit->setLexer(lexer);
  417. setCentralWidget(qsciEdit);
  418. qsciEdit->setFocus();
  419. // connect(qsciEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  420. actionUndo->setEnabled(false);
  421. actionRedo->setEnabled(false);
  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, tr("SDI"),
  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. return false;
  459. QFile file(fileName);
  460. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  461. QMessageBox::warning(this, tr("SDI"), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  462. return false;
  463. }
  464. QTextStream out(&file);
  465. QApplication::setOverrideCursor(Qt::WaitCursor);
  466. // out << textEdit->toPlainText();
  467. QApplication::restoreOverrideCursor();
  468. setCurrentFile(fileName);
  469. statusBar()->showMessage(tr("File saved"), 2000);
  470. return true;
  471. }
  472. void Fb2MainWindow::setCurrentFile(const QString &filename)
  473. {
  474. static int sequenceNumber = 1;
  475. QString title;
  476. isUntitled = filename.isEmpty();
  477. if (isUntitled) {
  478. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  479. title = curFile;
  480. } else {
  481. QFileInfo info = filename;
  482. curFile = info.canonicalFilePath();
  483. title = info.fileName();
  484. }
  485. title += QString(" - ") += qApp->applicationName();
  486. setWindowModified(false);
  487. setWindowFilePath(curFile);
  488. setWindowTitle(title);
  489. }
  490. Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
  491. {
  492. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  493. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  494. Fb2MainWindow *mainWin = qobject_cast<Fb2MainWindow *>(widget);
  495. if (mainWin && mainWin->curFile == canonicalFilePath)
  496. return mainWin;
  497. }
  498. return 0;
  499. }
  500. void Fb2MainWindow::viewQsci()
  501. {
  502. if (centralWidget() == qsciEdit) return;
  503. QString html;
  504. if (textEdit) {
  505. html = textEdit->toXml();
  506. delete textEdit;
  507. textEdit = NULL;
  508. }
  509. if (dockTree) {
  510. delete dockTree;
  511. dockTree = NULL;
  512. }
  513. createQsci();
  514. qsciEdit->setText(html);
  515. }
  516. void Fb2MainWindow::viewText()
  517. {
  518. if (centralWidget() == textEdit) return;
  519. if (qsciEdit) { delete qsciEdit; qsciEdit = NULL; }
  520. createText();
  521. }
  522. void Fb2MainWindow::clipboardDataChanged()
  523. {
  524. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  525. actionPaste->setEnabled(md->hasText());
  526. }
  527. }
  528. void Fb2MainWindow::showInspector()
  529. {
  530. if (!textEdit) return;
  531. QWebInspector *inspector = new QWebInspector();
  532. inspector->setPage(textEdit->page());
  533. inspector->show();
  534. }