fb2main.cpp 19 KB

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