fb2main.cpp 19 KB

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