fb2main.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 "fb2head.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. textEdit = new Fb2WebView(this);
  19. viewText();
  20. textEdit->load(":blank.fb2");
  21. }
  22. Fb2MainWindow::Fb2MainWindow(const QString &filename, ViewMode mode)
  23. {
  24. init();
  25. setCurrentFile(filename);
  26. if (mode == FB2) {
  27. textEdit = new Fb2WebView(this);
  28. viewText();
  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. codeEdit = NULL;
  45. treeView = NULL;
  46. headTree = NULL;
  47. toolEdit = NULL;
  48. messageEdit = NULL;
  49. readSettings();
  50. setUnifiedTitleAndToolBarOnMac(true);
  51. }
  52. void Fb2MainWindow::logMessage(const QString &message)
  53. {
  54. if (!messageEdit) {
  55. messageEdit = new QTextEdit(this);
  56. connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
  57. QDockWidget * dock = new QDockWidget(tr("Message log"), this);
  58. dock->setAttribute(Qt::WA_DeleteOnClose);
  59. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  60. dock->setWidget(messageEdit);
  61. addDockWidget(Qt::BottomDockWidgetArea, dock);
  62. messageEdit->setMaximumHeight(80);
  63. }
  64. messageEdit->append(message);
  65. }
  66. void Fb2MainWindow::logShowed()
  67. {
  68. messageEdit->setMaximumHeight(QWIDGETSIZE_MAX);
  69. }
  70. void Fb2MainWindow::logDestroyed()
  71. {
  72. messageEdit = NULL;
  73. }
  74. void Fb2MainWindow::treeActivated(const QModelIndex &index)
  75. {
  76. if (!treeView) return;
  77. Fb2TreeModel *model = dynamic_cast<Fb2TreeModel*>(treeView->model());
  78. if (model) model->select(index);
  79. }
  80. void Fb2MainWindow::treeDestroyed()
  81. {
  82. treeView = NULL;
  83. dockTree = NULL;
  84. }
  85. bool Fb2MainWindow::loadXML(const QString &filename)
  86. {
  87. if (!filename.isEmpty()) {
  88. QFile file(filename);
  89. if (file.open(QFile::ReadOnly | QFile::Text)) {
  90. codeEdit->clear();
  91. return codeEdit->read(&file);
  92. }
  93. }
  94. return false;
  95. }
  96. void Fb2MainWindow::closeEvent(QCloseEvent *event)
  97. {
  98. if (maybeSave()) {
  99. writeSettings();
  100. event->accept();
  101. } else {
  102. event->ignore();
  103. }
  104. }
  105. void Fb2MainWindow::fileNew()
  106. {
  107. Fb2MainWindow *other = new Fb2MainWindow;
  108. other->move(x() + 40, y() + 40);
  109. other->show();
  110. }
  111. void Fb2MainWindow::fileOpen()
  112. {
  113. QString filename = QFileDialog::getOpenFileName(this, tr("Open file"), QString(), "Fiction book files (*.fb2)");
  114. if (filename.isEmpty()) return;
  115. Fb2MainWindow * existing = findFb2MainWindow(filename);
  116. if (existing) {
  117. existing->show();
  118. existing->raise();
  119. existing->activateWindow();
  120. return;
  121. }
  122. if (textEdit) {
  123. if (isUntitled && !isWindowModified()) {
  124. setCurrentFile(filename);
  125. textEdit->load(filename);
  126. } else {
  127. Fb2MainWindow * other = new Fb2MainWindow(filename, FB2);
  128. other->move(x() + 40, y() + 40);
  129. other->show();
  130. }
  131. } else if (codeEdit) {
  132. if (isUntitled && !isWindowModified()) {
  133. setCurrentFile(filename);
  134. loadXML(filename);
  135. } else {
  136. Fb2MainWindow * other = new Fb2MainWindow(filename, XML);
  137. other->move(x() + 40, y() + 40);
  138. other->show();
  139. }
  140. }
  141. }
  142. bool Fb2MainWindow::fileSave()
  143. {
  144. if (isUntitled) {
  145. return fileSaveAs();
  146. } else {
  147. return saveFile(curFile);
  148. }
  149. }
  150. bool Fb2MainWindow::fileSaveAs()
  151. {
  152. QString fileName = QFileDialog::getSaveFileName(this, tr("Save As..."), curFile);
  153. if (fileName.isEmpty()) return false;
  154. return saveFile(fileName);
  155. }
  156. void Fb2MainWindow::about()
  157. {
  158. QMessageBox::about(this, tr("About fb2edit"),
  159. tr("The <b>fb2edit</b> is application for editing FB2-files."));
  160. }
  161. void Fb2MainWindow::documentWasModified()
  162. {
  163. if (isWindowModified()) return;
  164. QFileInfo info = windowFilePath();
  165. QString title = info.fileName();
  166. title += QString("[*]");
  167. title += QString(" - ") += qApp->applicationName();
  168. setWindowTitle(title);
  169. setWindowModified(true);
  170. if (codeEdit) disconnect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  171. if (textEdit) disconnect(textEdit->page(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
  172. }
  173. QIcon Fb2MainWindow::icon(const QString &name)
  174. {
  175. QIcon icon;
  176. icon.addFile(QString(":/24/%1.png").arg(name), QSize(24,24));
  177. icon.addFile(QString(":/16/%1.png").arg(name), QSize(16,16));
  178. return QIcon::fromTheme(name, icon);
  179. }
  180. void Fb2MainWindow::createActions()
  181. {
  182. QAction * act;
  183. QMenu * menu;
  184. QToolBar * tool;
  185. QList<QAction*> actions;
  186. menu = menuBar()->addMenu(tr("&File"));
  187. tool = addToolBar(tr("File"));
  188. tool->setMovable(false);
  189. act = new QAction(icon("document-new"), tr("&New"), this);
  190. act->setPriority(QAction::LowPriority);
  191. act->setShortcuts(QKeySequence::New);
  192. act->setStatusTip(tr("Create a new file"));
  193. connect(act, SIGNAL(triggered()), this, SLOT(fileNew()));
  194. menu->addAction(act);
  195. tool->addAction(act);
  196. act = new QAction(icon("document-open"), tr("&Open..."), this);
  197. act->setShortcuts(QKeySequence::Open);
  198. act->setStatusTip(tr("Open an existing file"));
  199. connect(act, SIGNAL(triggered()), this, SLOT(fileOpen()));
  200. menu->addAction(act);
  201. tool->addAction(act);
  202. act = new QAction(icon("document-save"), tr("&Save"), this);
  203. act->setShortcuts(QKeySequence::Save);
  204. act->setStatusTip(tr("Save the document to disk"));
  205. connect(act, SIGNAL(triggered()), this, SLOT(fileSave()));
  206. menu->addAction(act);
  207. tool->addAction(act);
  208. act = new QAction(icon("document-save-as"), tr("Save &As..."), this);
  209. act->setShortcuts(QKeySequence::SaveAs);
  210. act->setStatusTip(tr("Save the document under a new name"));
  211. connect(act, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
  212. menu->addAction(act);
  213. menu->addSeparator();
  214. act = new QAction(icon("window-close"), tr("&Close"), this);
  215. act->setShortcuts(QKeySequence::Close);
  216. act->setStatusTip(tr("Close this window"));
  217. connect(act, SIGNAL(triggered()), this, SLOT(close()));
  218. menu->addAction(act);
  219. act = new QAction(icon("application-exit"), tr("E&xit"), this);
  220. act->setShortcuts(QKeySequence::Quit);
  221. act->setStatusTip(tr("Exit the application"));
  222. connect(act, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  223. menu->addAction(act);
  224. menuEdit = menu = menuBar()->addMenu(tr("&Edit"));
  225. connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
  226. actionUndo = act = new QAction(icon("edit-undo"), tr("&Undo"), this);
  227. act->setPriority(QAction::LowPriority);
  228. act->setShortcut(QKeySequence::Undo);
  229. act->setEnabled(false);
  230. menu->addAction(act);
  231. actionRedo = act = new QAction(icon("edit-redo"), tr("&Redo"), this);
  232. act->setPriority(QAction::LowPriority);
  233. act->setShortcut(QKeySequence::Redo);
  234. act->setEnabled(false);
  235. menu->addAction(act);
  236. menu->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. 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. 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. clipboardDataChanged();
  255. menu->addSeparator();
  256. actionInsert = act = new QAction(icon("list-add"), tr("Insert"), this);
  257. act->setPriority(QAction::LowPriority);
  258. act->setShortcuts(QKeySequence::New);
  259. menu->addAction(act);
  260. actionDelete = act = new QAction(icon("list-remove"), tr("Delete"), this);
  261. act->setPriority(QAction::LowPriority);
  262. act->setShortcuts(QKeySequence::Delete);
  263. menu->addAction(act);
  264. menuText = menu = menuBar()->addMenu(tr("Fo&rmat"));
  265. actionTextBold = act = new QAction(icon("format-text-bold"), tr("Bold"), this);
  266. act->setShortcuts(QKeySequence::Bold);
  267. act->setCheckable(true);
  268. menu->addAction(act);
  269. actionTextItalic = act = new QAction(icon("format-text-italic"), tr("Italic"), this);
  270. act->setShortcuts(QKeySequence::Italic);
  271. act->setCheckable(true);
  272. menu->addAction(act);
  273. actionTextStrike = act = new QAction(icon("format-text-strikethrough"), tr("Strikethrough"), this);
  274. act->setCheckable(true);
  275. menu->addAction(act);
  276. actionTextSup = act = new QAction(icon("format-text-superscript"), tr("Superscript"), this);
  277. act->setCheckable(true);
  278. menu->addAction(act);
  279. actionTextSub = act = new QAction(icon("format-text-subscript"), tr("Subscript"), this);
  280. act->setCheckable(true);
  281. menu->addAction(act);
  282. menuView = menu = menuBar()->addMenu(tr("&View"));
  283. tool->addSeparator();
  284. QActionGroup * viewGroup = new QActionGroup(this);
  285. act = new QAction(tr("&Text"), this);
  286. act->setCheckable(true);
  287. act->setChecked(true);
  288. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  289. viewGroup->addAction(act);
  290. menu->addAction(act);
  291. tool->addAction(act);
  292. act = new QAction(tr("&Head"), this);
  293. act->setCheckable(true);
  294. connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
  295. viewGroup->addAction(act);
  296. menu->addAction(act);
  297. tool->addAction(act);
  298. act = new QAction(tr("&XML"), this);
  299. act->setCheckable(true);
  300. connect(act, SIGNAL(triggered()), this, SLOT(viewQsci()));
  301. viewGroup->addAction(act);
  302. menu->addAction(act);
  303. tool->addAction(act);
  304. menu->addSeparator();
  305. actionZoomIn = act = new QAction(icon("zoom-in"), tr("Zoom in"), this);
  306. act->setShortcuts(QKeySequence::ZoomIn);
  307. menu->addAction(act);
  308. actionZoomOut = act = new QAction(icon("zoom-out"), tr("Zoom out"), this);
  309. act->setShortcuts(QKeySequence::ZoomOut);
  310. menu->addAction(act);
  311. actionZoomOrig = act = new QAction(icon("zoom-original"), tr("Zoom original"), this);
  312. menu->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. treeView->setFocus();
  344. }
  345. void Fb2MainWindow::loadFinished(bool ok)
  346. {
  347. if (headTree) {
  348. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  349. headTree->setModel(model);
  350. model->expand(headTree);
  351. }
  352. if (treeView) {
  353. Fb2TreeModel *model = new Fb2TreeModel(*textEdit, treeView);
  354. treeView->setModel(model);
  355. model->expand(treeView);
  356. }
  357. }
  358. void Fb2MainWindow::selectionChanged()
  359. {
  360. actionCut->setEnabled(textEdit->CutEnabled());
  361. actionCopy->setEnabled(textEdit->CopyEnabled());
  362. actionTextBold->setChecked(textEdit->BoldChecked());
  363. actionTextItalic->setChecked(textEdit->ItalicChecked());
  364. actionTextStrike->setChecked(textEdit->StrikeChecked());
  365. actionTextSub->setChecked(textEdit->SubChecked());
  366. actionTextSup->setChecked(textEdit->SupChecked());
  367. QString script = "document.getSelection().baseNode.parentNode.tagName";
  368. QString message = textEdit->page()->mainFrame()->evaluateJavaScript(script).toString();
  369. statusBar()->showMessage(message);
  370. }
  371. void Fb2MainWindow::undoChanged()
  372. {
  373. actionUndo->setEnabled(textEdit->UndoEnabled());
  374. }
  375. void Fb2MainWindow::redoChanged()
  376. {
  377. actionRedo->setEnabled(textEdit->RedoEnabled());
  378. }
  379. void Fb2MainWindow::createQsci()
  380. {
  381. // http://qtcoder.blogspot.com/2010/10/qscintills.html
  382. // http://www.riverbankcomputing.co.uk/static/Docs/QScintilla2/classQsciScintilla.html
  383. if (codeEdit) return;
  384. codeEdit = new QsciScintilla;
  385. codeEdit->zoomTo(1);
  386. codeEdit->setUtf8(true);
  387. codeEdit->setCaretLineVisible(true);
  388. codeEdit->setCaretLineBackgroundColor(QColor("gainsboro"));
  389. codeEdit->setWrapMode(QsciScintilla::WrapWord);
  390. codeEdit->setEolMode(QsciScintilla::EolWindows);
  391. codeEdit->setAutoIndent(true);
  392. codeEdit->setIndentationGuides(true);
  393. codeEdit->setAutoCompletionSource(QsciScintilla::AcsAll);
  394. codeEdit->setAutoCompletionCaseSensitivity(true);
  395. codeEdit->setAutoCompletionReplaceWord(true);
  396. codeEdit->setAutoCompletionShowSingle(true);
  397. codeEdit->setAutoCompletionThreshold(2);
  398. codeEdit->setMarginsBackgroundColor(QColor("gainsboro"));
  399. codeEdit->setMarginLineNumbers(0, true);
  400. codeEdit->setFolding(QsciScintilla::BoxedFoldStyle, 1);
  401. codeEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  402. codeEdit->setMatchedBraceBackgroundColor(Qt::yellow);
  403. codeEdit->setUnmatchedBraceForegroundColor(Qt::blue);
  404. QFont font("Courier", 10);
  405. font.setStyleHint(QFont::TypeWriter);
  406. QsciLexerXML * lexer = new QsciLexerXML;
  407. lexer->setFont(font, -1);
  408. codeEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  409. codeEdit->setLexer(lexer);
  410. setCentralWidget(codeEdit);
  411. codeEdit->setFocus();
  412. }
  413. void Fb2MainWindow::createStatusBar()
  414. {
  415. statusBar()->showMessage(tr("Ready"));
  416. }
  417. void Fb2MainWindow::readSettings()
  418. {
  419. QSettings settings;
  420. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  421. QSize size = settings.value("size", QSize(400, 400)).toSize();
  422. move(pos);
  423. resize(size);
  424. }
  425. void Fb2MainWindow::writeSettings()
  426. {
  427. QSettings settings;
  428. settings.setValue("pos", pos());
  429. settings.setValue("size", size());
  430. }
  431. bool Fb2MainWindow::maybeSave()
  432. {
  433. if (textEdit && textEdit->isModified()) {
  434. QMessageBox::StandardButton ret;
  435. ret = QMessageBox::warning(this, qApp->applicationName(),
  436. tr("The document has been modified. Do you want to save your changes?"),
  437. QMessageBox::Save | QMessageBox::Discard
  438. | QMessageBox::Cancel);
  439. if (ret == QMessageBox::Save)
  440. return fileSave();
  441. else if (ret == QMessageBox::Cancel)
  442. return false;
  443. }
  444. return true;
  445. }
  446. bool Fb2MainWindow::saveFile(const QString &fileName)
  447. {
  448. QFile file(fileName);
  449. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  450. QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  451. return false;
  452. }
  453. if (textEdit) return textEdit->save(&file);
  454. return true;
  455. /*
  456. QTextStream out(&file);
  457. QApplication::setOverrideCursor(Qt::WaitCursor);
  458. out << textEdit->toPlainText();
  459. QApplication::restoreOverrideCursor();
  460. setCurrentFile(fileName);
  461. statusBar()->showMessage(tr("File saved"), 2000);
  462. */
  463. }
  464. void Fb2MainWindow::setCurrentFile(const QString &filename)
  465. {
  466. static int sequenceNumber = 1;
  467. QString title;
  468. isUntitled = filename.isEmpty();
  469. if (isUntitled) {
  470. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  471. title = curFile;
  472. } else {
  473. QFileInfo info = filename;
  474. curFile = info.canonicalFilePath();
  475. title = info.fileName();
  476. }
  477. title += QString(" - ") += qApp->applicationName();
  478. setWindowModified(false);
  479. setWindowFilePath(curFile);
  480. setWindowTitle(title);
  481. }
  482. Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
  483. {
  484. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  485. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  486. Fb2MainWindow *mainWin = qobject_cast<Fb2MainWindow *>(widget);
  487. if (mainWin && mainWin->curFile == canonicalFilePath)
  488. return mainWin;
  489. }
  490. return 0;
  491. }
  492. void Fb2MainWindow::zoomOrig()
  493. {
  494. if (codeEdit) codeEdit->zoomTo(1);
  495. }
  496. void Fb2MainWindow::checkScintillaUndo()
  497. {
  498. if (!codeEdit) return;
  499. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  500. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  501. }
  502. void Fb2MainWindow::viewQsci()
  503. {
  504. if (centralWidget() == codeEdit) return;
  505. QString xml;
  506. if (textEdit) textEdit->save(&xml);
  507. FB2DELETE(textEdit);
  508. FB2DELETE(dockTree);
  509. FB2DELETE(headTree);
  510. createQsci();
  511. FB2DELETE(toolEdit);
  512. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  513. tool->addSeparator();
  514. tool->addAction(actionUndo);
  515. tool->addAction(actionRedo);
  516. tool->addSeparator();
  517. tool->addAction(actionCut);
  518. tool->addAction(actionCopy);
  519. tool->addAction(actionPaste);
  520. tool->addSeparator();
  521. tool->addAction(actionZoomIn);
  522. tool->addAction(actionZoomOut);
  523. tool->addAction(actionZoomOrig);
  524. tool->setMovable(false);
  525. connect(codeEdit, SIGNAL(linesChanged()), this, SLOT(linesChanged()));
  526. codeEdit->setText(xml);
  527. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  528. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  529. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  530. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  531. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  532. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  533. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  534. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  535. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  536. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  537. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  538. connect(actionZoomOrig, SIGNAL(triggered()), this, SLOT(zoomOrig()));
  539. }
  540. void Fb2MainWindow::linesChanged()
  541. {
  542. QString width = QString().setNum(codeEdit->lines() * 10);
  543. codeEdit->setMarginWidth(0, width);
  544. }
  545. void Fb2MainWindow::viewText()
  546. {
  547. if (centralWidget() == textEdit) return;
  548. QString xml;
  549. if (codeEdit) xml = codeEdit->text();
  550. FB2DELETE(codeEdit);
  551. FB2DELETE(headTree);
  552. if (!textEdit) {
  553. textEdit = new Fb2WebView(this);
  554. }
  555. setCentralWidget(textEdit);
  556. textEdit->setFocus();
  557. viewTree();
  558. connect(textEdit->page(), SIGNAL(contentsChanged()), SLOT(documentWasModified()));
  559. connect(textEdit->page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  560. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  561. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  562. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  563. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  564. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  565. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  566. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  567. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  568. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  569. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  570. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  571. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  572. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  573. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  574. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  575. connect(actionZoomOrig, SIGNAL(triggered()), textEdit, SLOT(zoomOrig()));
  576. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  577. FB2DELETE(toolEdit);
  578. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  579. tool->setMovable(false);
  580. tool->addSeparator();
  581. QAction *act;
  582. act = textEdit->pageAction(QWebPage::Undo);
  583. act->setIcon(icon("edit-undo"));
  584. act->setText(tr("&Undo"));
  585. act->setPriority(QAction::LowPriority);
  586. act->setShortcut(QKeySequence::Undo);
  587. tool->addAction(act);
  588. act = textEdit->pageAction(QWebPage::Redo);
  589. act->setIcon(icon("edit-redo"));
  590. act->setText(tr("&Redo"));
  591. act->setPriority(QAction::LowPriority);
  592. act->setShortcut(QKeySequence::Redo);
  593. tool->addAction(act);
  594. tool->addSeparator();
  595. act = textEdit->pageAction(QWebPage::Cut);
  596. act->setIcon(icon("edit-cut"));
  597. act->setText(tr("Cu&t"));
  598. act->setPriority(QAction::LowPriority);
  599. act->setShortcuts(QKeySequence::Cut);
  600. act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  601. tool->addAction(act);
  602. act = textEdit->pageAction(QWebPage::Copy);
  603. act->setIcon(icon("edit-copy"));
  604. act->setText(tr("&Copy"));
  605. act->setPriority(QAction::LowPriority);
  606. act->setShortcuts(QKeySequence::Copy);
  607. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  608. tool->addAction(act);
  609. act = textEdit->pageAction(QWebPage::Paste);
  610. act->setIcon(icon("edit-paste"));
  611. act->setText(tr("&Paste"));
  612. act->setPriority(QAction::LowPriority);
  613. act->setShortcuts(QKeySequence::Paste);
  614. act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  615. tool->addAction(act);
  616. tool->addSeparator();
  617. act = textEdit->pageAction(QWebPage::ToggleBold);
  618. act->setIcon(icon("format-text-bold"));
  619. act->setText(tr("&Bold"));
  620. act->setShortcuts(QKeySequence::Bold);
  621. tool->addAction(act);
  622. act = textEdit->pageAction(QWebPage::ToggleItalic);
  623. act->setIcon(icon("format-text-italic"));
  624. act->setText(tr("&Italic"));
  625. act->setShortcuts(QKeySequence::Italic);
  626. tool->addAction(act);
  627. act = textEdit->pageAction(QWebPage::ToggleStrikethrough);
  628. act->setIcon(icon("format-text-strikethrough"));
  629. act->setText(tr("&Strikethrough"));
  630. tool->addAction(act);
  631. act = textEdit->pageAction(QWebPage::ToggleSuperscript);
  632. act->setIcon(icon("format-text-superscript"));
  633. act->setText(tr("Superscript"));
  634. tool->addAction(act);
  635. act = textEdit->pageAction(QWebPage::ToggleSubscript);
  636. act->setIcon(icon("format-text-subscript"));
  637. act->setText(tr("Subscript"));
  638. tool->addAction(act);
  639. tool->addSeparator();
  640. tool->addAction(actionZoomIn);
  641. tool->addAction(actionZoomOut);
  642. tool->addAction(actionZoomOrig);
  643. }
  644. void Fb2MainWindow::viewHead()
  645. {
  646. if (centralWidget() == headTree) return;
  647. QString xml;
  648. if (codeEdit) xml = codeEdit->text();
  649. FB2DELETE(dockTree);
  650. FB2DELETE(codeEdit);
  651. FB2DELETE(toolEdit);
  652. if (!headTree) {
  653. headTree = new QTreeView(this);
  654. headTree->header()->setDefaultSectionSize(200);
  655. }
  656. if (textEdit) {
  657. this->setFocus();
  658. textEdit->setParent(NULL);
  659. setCentralWidget(headTree);
  660. textEdit->setParent(this);
  661. Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
  662. headTree->setModel(model);
  663. model->expand(headTree);
  664. } else {
  665. textEdit = new Fb2WebView(this);
  666. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  667. setCentralWidget(headTree);
  668. }
  669. headTree->setFocus();
  670. if (!xml.isEmpty()) textEdit->load(curFile, xml);
  671. if (textEdit) {
  672. actionUndo->disconnect();
  673. actionRedo->disconnect();
  674. actionCut->disconnect();
  675. actionCopy->disconnect();
  676. actionPaste->disconnect();
  677. actionTextBold->disconnect();
  678. actionTextItalic->disconnect();
  679. actionTextStrike->disconnect();
  680. actionTextSub->disconnect();
  681. actionTextSup->disconnect();
  682. }
  683. FB2DELETE(toolEdit);
  684. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  685. tool->addSeparator();
  686. tool->addAction(actionInsert);
  687. tool->addAction(actionDelete);
  688. tool->setMovable(false);
  689. }
  690. void Fb2MainWindow::viewTree()
  691. {
  692. if (centralWidget() != textEdit) return;
  693. if (treeView == NULL) createTree();
  694. loadFinished(true);
  695. }
  696. void Fb2MainWindow::clipboardDataChanged()
  697. {
  698. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  699. actionPaste->setEnabled(md->hasText());
  700. }
  701. }
  702. void Fb2MainWindow::showInspector()
  703. {
  704. if (!textEdit) return;
  705. QWebInspector * inspector = new QWebInspector();
  706. inspector->setPage(textEdit->page());
  707. inspector->show();
  708. }