fb2main.cpp 20 KB

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