fb2main.cpp 19 KB

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