fb2main.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. #include <QtGui>
  2. #include <QtDebug>
  3. #include <QTreeView>
  4. #include <QWebFrame>
  5. #include "fb2app.hpp"
  6. #include "fb2main.hpp"
  7. #include "fb2code.hpp"
  8. #include "fb2dlgs.hpp"
  9. #include "fb2dock.hpp"
  10. #include "fb2page.hpp"
  11. #include "fb2read.hpp"
  12. #include "fb2save.hpp"
  13. #include "fb2text.hpp"
  14. #include "fb2utils.h"
  15. //---------------------------------------------------------------------------
  16. // FbMainWindow
  17. //---------------------------------------------------------------------------
  18. FbMainWindow::FbMainWindow(const QString &filename, ViewMode mode)
  19. : QMainWindow()
  20. , noteEdit(0)
  21. , toolEdit(0)
  22. , inspector(0)
  23. , messageEdit(0)
  24. , isSwitched(false)
  25. , isUntitled(true)
  26. {
  27. connect(qApp, SIGNAL(logMessage(QString)), SLOT(logMessage(QString)));
  28. setUnifiedTitleAndToolBarOnMac(true);
  29. setAttribute(Qt::WA_DeleteOnClose);
  30. setWindowIcon(QIcon(":icon.ico"));
  31. mainDock = new FbMainDock(this);
  32. setCentralWidget(mainDock);
  33. createActions();
  34. createStatusBar();
  35. readSettings();
  36. mainDock->setMode(Fb::Text);
  37. setCurrentFile(filename);
  38. mainDock->load(filename);
  39. }
  40. /*
  41. FbTextPage * FbMainWindow::page()
  42. {
  43. return textFrame ? textFrame->view()->page() : 0;
  44. }
  45. */
  46. void FbMainWindow::logMessage(const QString &message)
  47. {
  48. if (!messageEdit) {
  49. messageEdit = new QTextEdit(this);
  50. connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
  51. QDockWidget * dock = new FbLogDock(tr("Message log"), this);
  52. dock->setAttribute(Qt::WA_DeleteOnClose);
  53. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  54. dock->setWidget(messageEdit);
  55. addDockWidget(Qt::BottomDockWidgetArea, dock);
  56. messageEdit->setMaximumHeight(80);
  57. }
  58. messageEdit->append(message);
  59. }
  60. void FbMainWindow::logShowed()
  61. {
  62. messageEdit->setMaximumHeight(QWIDGETSIZE_MAX);
  63. }
  64. void FbMainWindow::logDestroyed()
  65. {
  66. messageEdit = NULL;
  67. }
  68. void FbMainWindow::closeEvent(QCloseEvent *event)
  69. {
  70. if (maybeSave()) {
  71. writeSettings();
  72. event->accept();
  73. } else {
  74. event->ignore();
  75. }
  76. }
  77. void FbMainWindow::fileNew()
  78. {
  79. FbMainWindow *other = new FbMainWindow;
  80. other->move(x() + 40, y() + 40);
  81. other->show();
  82. }
  83. void FbMainWindow::fileOpen()
  84. {
  85. QString filename = QFileDialog::getOpenFileName(this, tr("Open file"), QString(), "Fiction book files (*.fb2)");
  86. if (filename.isEmpty()) return;
  87. FbMainWindow * existing = findFbMainWindow(filename);
  88. if (existing) {
  89. existing->show();
  90. existing->raise();
  91. existing->activateWindow();
  92. return;
  93. }
  94. if (isUntitled && !isWindowModified()) {
  95. mainDock->load(filename);
  96. } else {
  97. FbMainWindow * other = new FbMainWindow(filename, FB2);
  98. other->mainDock->load(filename);
  99. other->move(x() + 40, y() + 40);
  100. other->show();
  101. }
  102. }
  103. bool FbMainWindow::fileSave()
  104. {
  105. if (isUntitled) {
  106. return fileSaveAs();
  107. } else {
  108. return saveFile(curFile);
  109. }
  110. }
  111. bool FbMainWindow::fileSaveAs()
  112. {
  113. FbSaveDialog dlg(this, tr("Save As..."));
  114. dlg.selectFile(curFile);
  115. if (!dlg.exec()) return false;
  116. QString fileName = dlg.fileName();
  117. if (fileName.isEmpty()) return false;
  118. return saveFile(fileName, dlg.codec());
  119. }
  120. void FbMainWindow::about()
  121. {
  122. QString text = tr("The <b>fb2edit</b> is application for editing FB2-files.");
  123. text += QString("<br>") += FbApplication::lastCommit();
  124. QMessageBox::about(this, tr("About fb2edit"), text);
  125. }
  126. void FbMainWindow::documentWasModified()
  127. {
  128. // setModified(isSwitched || codeEdit->isModified());
  129. }
  130. void FbMainWindow::setModified(bool modified)
  131. {
  132. QFileInfo info = windowFilePath();
  133. QString title = info.fileName();
  134. if (modified) title += QString("[*]");
  135. title += appTitle();
  136. setWindowTitle(title);
  137. setWindowModified(modified);
  138. }
  139. void FbMainWindow::createActions()
  140. {
  141. QAction * act;
  142. QMenu * menu;
  143. QToolBar * tool;
  144. FbTextEdit *text = mainDock->text();
  145. FbHeadEdit *head = mainDock->head();
  146. FbCodeEdit *code = mainDock->code();
  147. menu = menuBar()->addMenu(tr("&File"));
  148. tool = addToolBar(tr("File"));
  149. tool->setMovable(false);
  150. act = new QAction(FbIcon("document-new"), tr("&New"), this);
  151. act->setPriority(QAction::LowPriority);
  152. act->setShortcuts(QKeySequence::New);
  153. act->setStatusTip(tr("Create a new file"));
  154. connect(act, SIGNAL(triggered()), this, SLOT(fileNew()));
  155. menu->addAction(act);
  156. tool->addAction(act);
  157. act = new QAction(FbIcon("document-open"), tr("&Open..."), this);
  158. act->setShortcuts(QKeySequence::Open);
  159. act->setStatusTip(tr("Open an existing file"));
  160. connect(act, SIGNAL(triggered()), this, SLOT(fileOpen()));
  161. menu->addAction(act);
  162. tool->addAction(act);
  163. act = new QAction(FbIcon("document-save"), tr("&Save"), this);
  164. act->setShortcuts(QKeySequence::Save);
  165. act->setStatusTip(tr("Save the document to disk"));
  166. connect(act, SIGNAL(triggered()), this, SLOT(fileSave()));
  167. menu->addAction(act);
  168. tool->addAction(act);
  169. act = new QAction(FbIcon("document-save-as"), tr("Save &As..."), this);
  170. act->setShortcuts(QKeySequence::SaveAs);
  171. act->setStatusTip(tr("Save the document under a new name"));
  172. connect(act, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
  173. menu->addAction(act);
  174. menu->addSeparator();
  175. act = new QAction(FbIcon("window-close"), tr("&Close"), this);
  176. act->setShortcuts(QKeySequence::Close);
  177. act->setStatusTip(tr("Close this window"));
  178. connect(act, SIGNAL(triggered()), this, SLOT(close()));
  179. menu->addAction(act);
  180. act = new QAction(FbIcon("application-exit"), tr("E&xit"), this);
  181. act->setShortcuts(QKeySequence::Quit);
  182. act->setStatusTip(tr("Exit the application"));
  183. connect(act, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  184. menu->addAction(act);
  185. menu = menuBar()->addMenu(tr("&Edit"));
  186. connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
  187. act = new FbTextAction(FbIcon("edit-undo"), tr("&Undo"), QWebPage::Undo, text);
  188. text->setAction(Fb::EditUndo, act);
  189. code->setAction(Fb::EditUndo, act);
  190. act->setPriority(QAction::LowPriority);
  191. act->setShortcut(QKeySequence::Undo);
  192. act->setEnabled(false);
  193. menu->addAction(act);
  194. act = new FbTextAction(FbIcon("edit-redo"), tr("&Redo"), QWebPage::Redo, text);
  195. text->setAction(Fb::EditRedo, act);
  196. code->setAction(Fb::EditRedo, act);
  197. act->setPriority(QAction::LowPriority);
  198. act->setShortcut(QKeySequence::Redo);
  199. act->setEnabled(false);
  200. menu->addAction(act);
  201. menu->addSeparator();
  202. act = new FbTextAction(FbIcon("edit-cut"), tr("Cu&t"), QWebPage::Cut, text);
  203. text->setAction(Fb::EditCut, act);
  204. code->setAction(Fb::EditCut, act);
  205. act->setShortcutContext(Qt::WidgetShortcut);
  206. act->setPriority(QAction::LowPriority);
  207. act->setShortcuts(QKeySequence::Cut);
  208. act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  209. act->setEnabled(false);
  210. menu->addAction(act);
  211. act = new FbTextAction(FbIcon("edit-copy"), tr("&Copy"), QWebPage::Copy, text);
  212. text->setAction(Fb::EditCopy, act);
  213. code->setAction(Fb::EditCopy, act);
  214. act->setShortcutContext(Qt::WidgetShortcut);
  215. act->setPriority(QAction::LowPriority);
  216. act->setShortcuts(QKeySequence::Copy);
  217. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  218. act->setEnabled(false);
  219. menu->addAction(act);
  220. act = new FbTextAction(FbIcon("edit-paste"), tr("&Paste"), QWebPage::Paste, text);
  221. text->setAction(Fb::EditPaste, act);
  222. code->setAction(Fb::EditPaste, act);
  223. act->setShortcutContext(Qt::WidgetShortcut);
  224. act->setPriority(QAction::LowPriority);
  225. act->setShortcuts(QKeySequence::Paste);
  226. act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  227. menu->addAction(act);
  228. act = new FbTextAction(tr("Paste (no style)"), QWebPage::PasteAndMatchStyle, text);
  229. text->setAction(Fb::PasteText, act);
  230. menu->addAction(act);
  231. menu->addSeparator();
  232. act = new QAction(FbIcon("edit-find"), tr("&Find..."), this);
  233. text->setAction(Fb::EditFind, act);
  234. code->setAction(Fb::EditFind, act);
  235. act->setShortcuts(QKeySequence::Find);
  236. menu->addAction(act);
  237. act = new QAction(FbIcon("edit-find-replace"), tr("&Replace..."), this);
  238. text->setAction(Fb::EditReplace, act);
  239. code->setAction(Fb::EditReplace, act);
  240. menu->addAction(act);
  241. menu->addSeparator();
  242. act = new QAction(FbIcon("preferences-desktop"), tr("&Settings"), this);
  243. act->setShortcuts(QKeySequence::Preferences);
  244. act->setStatusTip(tr("Application settings"));
  245. connect(act, SIGNAL(triggered()), SLOT(openSettings()));
  246. menu->addAction(act);
  247. menu = menuBar()->addMenu(tr("&Insert", "Main menu"));
  248. mainDock->addMenu(menu);
  249. act = new QAction(FbIcon("insert-image"), tr("&Image"), this);
  250. text->setAction(Fb::InsertImage, act);
  251. menu->addAction(act);
  252. act = new QAction(FbIcon("insert-text"), tr("&Footnote"), this);
  253. text->setAction(Fb::InsertNote, act);
  254. menu->addAction(act);
  255. act = new QAction(FbIcon("insert-link"), tr("&Hiperlink"), this);
  256. text->setAction(Fb::InsertLink, act);
  257. menu->addAction(act);
  258. menu->addSeparator();
  259. act = new QAction(tr("&Body"), this);
  260. text->setAction(Fb::InsertBody, act);
  261. menu->addAction(act);
  262. act = new QAction(FbIcon("insert-object"), tr("&Section"), this);
  263. text->setAction(Fb::InsertSection, act);
  264. menu->addAction(act);
  265. act = new QAction(tr("&Title"), this);
  266. text->setAction(Fb::InsertTitle, act);
  267. menu->addAction(act);
  268. act = new QAction(tr("&Epigraph"), this);
  269. text->setAction(Fb::InsertEpigraph, act);
  270. menu->addAction(act);
  271. act = new QAction(tr("&Annotation"), this);
  272. text->setAction(Fb::InsertAnnot, act);
  273. menu->addAction(act);
  274. act = new QAction(tr("&Subtitle"), this);
  275. text->setAction(Fb::InsertSubtitle, act);
  276. menu->addAction(act);
  277. act = new QAction(tr("&Cite"), this);
  278. text->setAction(Fb::InsertCite, act);
  279. menu->addAction(act);
  280. act = new QAction(tr("&Poem"), this);
  281. text->setAction(Fb::InsertPoem, act);
  282. menu->addAction(act);
  283. act = new QAction(tr("&Stanza"), this);
  284. text->setAction(Fb::InsertStanza, act);
  285. menu->addAction(act);
  286. act = new QAction(tr("&Author"), this);
  287. text->setAction(Fb::InsertAuthor, act);
  288. menu->addAction(act);
  289. act = new QAction(tr("&Date"), this);
  290. text->setAction(Fb::InsertDate, act);
  291. menu->addAction(act);
  292. menu->addSeparator();
  293. act = new QAction(tr("Simple text"), this);
  294. text->setAction(Fb::InsertText, act);
  295. menu->addAction(act);
  296. act = new QAction(tr("Paragraph"), this);
  297. text->setAction(Fb::InsertParag, act);
  298. menu->addAction(act);
  299. act = new QAction(tr("Line end"), this);
  300. text->setAction(Fb::InsertLine, act);
  301. menu->addAction(act);
  302. menu = menuBar()->addMenu(tr("Fo&rmat"));
  303. mainDock->addMenu(menu);
  304. act = new FbTextAction(FbIcon("edit-clear"), tr("Clear format"), QWebPage::RemoveFormat, text);
  305. text->setAction(Fb::ClearFormat, act);
  306. menu->addAction(act);
  307. menu->addSeparator();
  308. act = new FbTextAction(FbIcon("format-text-bold"), tr("&Bold"), QWebPage::ToggleBold, text);
  309. text->setAction(Fb::TextBold, act);
  310. act->setShortcuts(QKeySequence::Bold);
  311. act->setCheckable(true);
  312. menu->addAction(act);
  313. act = new FbTextAction(FbIcon("format-text-italic"), tr("&Italic"), QWebPage::ToggleItalic, text);
  314. text->setAction(Fb::TextItalic, act);
  315. act->setShortcuts(QKeySequence::Italic);
  316. act->setCheckable(true);
  317. menu->addAction(act);
  318. act = new FbTextAction(FbIcon("format-text-strikethrough"), tr("&Strikethrough"), QWebPage::ToggleStrikethrough, text);
  319. text->setAction(Fb::TextStrike, act);
  320. act->setCheckable(true);
  321. menu->addAction(act);
  322. act = new FbTextAction(FbIcon("format-text-superscript"), tr("Su&perscript"), QWebPage::ToggleSuperscript, text);
  323. text->setAction(Fb::TextSup, act);
  324. act->setCheckable(true);
  325. menu->addAction(act);
  326. act = new FbTextAction(FbIcon("format-text-subscript"), tr("Su&bscript"), QWebPage::ToggleSubscript, text);
  327. text->setAction(Fb::TextSub, act);
  328. act->setCheckable(true);
  329. menu->addAction(act);
  330. act = new QAction(FbIcon("utilities-terminal"), tr("&Code"), this);
  331. text->setAction(Fb::TextCode, act);
  332. act->setCheckable(true);
  333. menu->addAction(act);
  334. menu->addSeparator();
  335. act = new QAction(FbIcon("format-indent-more"), tr("Create section"), text);
  336. text->setAction(Fb::SectionAdd, act);
  337. menu->addAction(act);
  338. act = new QAction(FbIcon("format-indent-less"), tr("Remove section"), text);
  339. text->setAction(Fb::SectionDel, act);
  340. menu->addAction(act);
  341. act = new QAction(FbIcon("format-justify-center"), tr("Make title"), text);
  342. text->setAction(Fb::TextTitle, act);
  343. menu->addAction(act);
  344. menu = menuBar()->addMenu(tr("&View"));
  345. tool->addSeparator();
  346. QActionGroup * viewGroup = new QActionGroup(this);
  347. act = new FbModeAction(mainDock, Fb::Text, tr("&Text"));
  348. viewGroup->addAction(act);
  349. menu->addAction(act);
  350. tool->addAction(act);
  351. act->setChecked(true);
  352. act = new FbModeAction(mainDock, Fb::Head, tr("&Head"));
  353. viewGroup->addAction(act);
  354. menu->addAction(act);
  355. tool->addAction(act);
  356. act = new FbModeAction(mainDock, Fb::Code, tr("&XML"));
  357. viewGroup->addAction(act);
  358. menu->addAction(act);
  359. tool->addAction(act);
  360. #ifdef QT_DEBUG
  361. act = new FbModeAction(mainDock, Fb::Html, tr("&HTML"));
  362. viewGroup->addAction(act);
  363. menu->addAction(act);
  364. #endif // _DEBUG
  365. menu->addSeparator();
  366. act = new QAction(FbIcon("zoom-in"), tr("Zoom in"), this);
  367. text->setAction(Fb::ZoomIn, act);
  368. code->setAction(Fb::ZoomIn, act);
  369. act->setShortcuts(QKeySequence::ZoomIn);
  370. menu->addAction(act);
  371. act = new QAction(FbIcon("zoom-out"), tr("Zoom out"), this);
  372. text->setAction(Fb::ZoomOut, act);
  373. code->setAction(Fb::ZoomOut, act);
  374. act->setShortcuts(QKeySequence::ZoomOut);
  375. menu->addAction(act);
  376. act = new QAction(FbIcon("zoom-original"), tr("Zoom original"), this);
  377. text->setAction(Fb::ZoomReset, act);
  378. code->setAction(Fb::ZoomReset, act);
  379. menu->addAction(act);
  380. menu->addSeparator();
  381. act = new QAction(tr("&Contents"), this);
  382. text->setAction(Fb::ViewContents, act);
  383. act->setCheckable(true);
  384. menu->addAction(act);
  385. act = new QAction(tr("&Pictures"), this);
  386. text->setAction(Fb::ViewPictures, act);
  387. act->setCheckable(true);
  388. menu->addAction(act);
  389. act = new QAction(tr("&Web inspector"), this);
  390. text->setAction(Fb::ViewInspector, act);
  391. act->setCheckable(true);
  392. menu->addAction(act);
  393. menuBar()->addSeparator();
  394. menu = menuBar()->addMenu(tr("&Help"));
  395. act = new QAction(FbIcon("help-about"), tr("&About"), this);
  396. act->setStatusTip(tr("Show the application's About box"));
  397. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  398. menu->addAction(act);
  399. act = new QAction(tr("About &Qt"), this);
  400. act->setStatusTip(tr("Show the Qt library's About box"));
  401. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  402. menu->addAction(act);
  403. toolEdit = tool = addToolBar(tr("Edit"));
  404. tool->setMovable(false);
  405. tool->addSeparator();
  406. mainDock->setTool(tool);
  407. }
  408. void FbMainWindow::openSettings()
  409. {
  410. FbSetupDlg dlg(this);
  411. dlg.exec();
  412. }
  413. void FbMainWindow::createStatusBar()
  414. {
  415. statusBar()->showMessage(tr("Ready"));
  416. }
  417. void FbMainWindow::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 FbMainWindow::writeSettings()
  426. {
  427. QSettings settings;
  428. settings.setValue("pos", pos());
  429. settings.setValue("size", size());
  430. }
  431. bool FbMainWindow::maybeSave()
  432. {
  433. if (mainDock->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 FbMainWindow::saveFile(const QString &fileName, const QString &codec)
  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. bool ok = mainDock->save(&file);
  454. setCurrentFile(fileName);
  455. return ok;
  456. }
  457. void FbMainWindow::setCurrentFile(const QString &filename)
  458. {
  459. if (filename.isEmpty()) {
  460. static int sequenceNumber = 1;
  461. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  462. } else {
  463. QFileInfo info = filename;
  464. curFile = info.canonicalFilePath();
  465. }
  466. setWindowFilePath(curFile);
  467. setModified(false);
  468. }
  469. QString FbMainWindow::appTitle() const
  470. {
  471. return QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
  472. }
  473. FbMainWindow *FbMainWindow::findFbMainWindow(const QString &fileName)
  474. {
  475. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  476. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  477. FbMainWindow *mainWin = qobject_cast<FbMainWindow *>(widget);
  478. if (mainWin && mainWin->curFile == canonicalFilePath)
  479. return mainWin;
  480. }
  481. return 0;
  482. }
  483. /*
  484. void FbMainWindow::checkScintillaUndo()
  485. {
  486. if (!codeEdit) return;
  487. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  488. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  489. }
  490. void FbMainWindow::viewText(FbTextPage *page)
  491. {
  492. if (textFrame && centralWidget() == textFrame) return;
  493. if (textFrame) textFrame->hideInspector();
  494. if (codeEdit) {
  495. QString xml = codeEdit->text();
  496. page = new FbTextPage(this);
  497. if (!page->load(QString(), xml)) {
  498. delete page;
  499. return;
  500. }
  501. isSwitched = true;
  502. }
  503. FB2DELETE(codeEdit);
  504. FB2DELETE(headTree);
  505. if (textFrame) {
  506. createTextToolbar();
  507. } else {
  508. textFrame = new FbTextFrame(this, actionInspect);
  509. }
  510. setCentralWidget(textFrame);
  511. FbTextEdit *textEdit = textFrame->view();
  512. if (page) {
  513. page->setParent(textEdit);
  514. textEdit->setPage(page);
  515. }
  516. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(createTextToolbar()));
  517. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  518. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  519. actionContents->setEnabled(true);
  520. actionPictures->setEnabled(true);
  521. actionInspect->setEnabled(true);
  522. textEdit->setFocus();
  523. viewTree();
  524. }
  525. void FbMainWindow::viewHead()
  526. {
  527. if (headTree && centralWidget() == headTree) return;
  528. if (textFrame) textFrame->hideInspector();
  529. FbTextPage *page = 0;
  530. if (codeEdit) {
  531. QString xml = codeEdit->text();
  532. page = new FbTextPage(this);
  533. if (!page->load(QString(), xml)) {
  534. delete page;
  535. return;
  536. }
  537. isSwitched = true;
  538. }
  539. FB2DELETE(dockTree);
  540. FB2DELETE(dockImgs);
  541. FB2DELETE(codeEdit);
  542. FB2DELETE(toolEdit);
  543. if (!textFrame) {
  544. textFrame = new FbTextFrame(this, actionInspect);
  545. FbTextEdit *textEdit = textFrame->view();
  546. if (page) {
  547. page->setParent(textEdit);
  548. textEdit->setPage(page);
  549. }
  550. }
  551. if (!headTree) {
  552. headTree = new FbHeadEdit(this);
  553. headTree->setText(textFrame->view());
  554. connect(headTree, SIGNAL(status(QString)), this, SLOT(status(QString)));
  555. }
  556. this->setFocus();
  557. textFrame->setParent(NULL);
  558. setCentralWidget(headTree);
  559. textFrame->setParent(this);
  560. headTree->updateTree();
  561. headTree->setFocus();
  562. if (textFrame) {
  563. actionUndo->disconnect();
  564. actionRedo->disconnect();
  565. actionCut->disconnect();
  566. actionCopy->disconnect();
  567. actionPaste->disconnect();
  568. actionTextBold->disconnect();
  569. actionTextItalic->disconnect();
  570. actionTextStrike->disconnect();
  571. actionTextSub->disconnect();
  572. actionTextSup->disconnect();
  573. }
  574. FB2DELETE(toolEdit);
  575. toolEdit = addToolBar(tr("Edit"));
  576. headTree->initToolbar(*toolEdit);
  577. toolEdit->addSeparator();
  578. toolEdit->setMovable(false);
  579. actionContents->setEnabled(false);
  580. actionPictures->setEnabled(false);
  581. actionInspect->setEnabled(true);
  582. }
  583. void FbMainWindow::viewCode()
  584. {
  585. if (codeEdit && centralWidget() == codeEdit) return;
  586. bool load = false;
  587. QByteArray xml;
  588. if (textFrame) {
  589. textFrame->view()->save(&xml);
  590. isSwitched = true;
  591. load = true;
  592. }
  593. FB2DELETE(textFrame);
  594. FB2DELETE(dockTree);
  595. FB2DELETE(dockImgs);
  596. FB2DELETE(headTree);
  597. if (!codeEdit) {
  598. codeEdit = new FbCodeEdit;
  599. }
  600. if (load) codeEdit->load(xml);
  601. setCentralWidget(codeEdit);
  602. codeEdit->setFocus();
  603. FB2DELETE(toolEdit);
  604. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  605. tool->addSeparator();
  606. tool->addAction(actionUndo);
  607. tool->addAction(actionRedo);
  608. tool->addSeparator();
  609. tool->addAction(actionCut);
  610. tool->addAction(actionCopy);
  611. tool->addAction(actionPaste);
  612. tool->addSeparator();
  613. tool->addAction(actionZoomIn);
  614. tool->addAction(actionZoomOut);
  615. tool->addAction(actionZoomReset);
  616. tool->setMovable(false);
  617. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  618. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  619. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  620. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  621. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  622. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  623. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  624. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  625. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  626. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  627. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  628. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  629. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  630. actionContents->setEnabled(false);
  631. actionPictures->setEnabled(false);
  632. actionInspect->setEnabled(false);
  633. }
  634. void FbMainWindow::viewHtml()
  635. {
  636. if (codeEdit && centralWidget() == codeEdit) return;
  637. if (!textFrame) return;
  638. QString html = textFrame->view()->page()->mainFrame()->toHtml();
  639. isSwitched = true;
  640. FB2DELETE(textFrame);
  641. FB2DELETE(dockTree);
  642. FB2DELETE(dockImgs);
  643. FB2DELETE(headTree);
  644. if (!codeEdit) {
  645. codeEdit = new FbCodeEdit;
  646. }
  647. codeEdit->setPlainText(html);
  648. setCentralWidget(codeEdit);
  649. codeEdit->setFocus();
  650. FB2DELETE(toolEdit);
  651. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  652. tool->addSeparator();
  653. tool->addAction(actionUndo);
  654. tool->addAction(actionRedo);
  655. tool->addSeparator();
  656. tool->addAction(actionCut);
  657. tool->addAction(actionCopy);
  658. tool->addAction(actionPaste);
  659. tool->addSeparator();
  660. tool->addAction(actionZoomIn);
  661. tool->addAction(actionZoomOut);
  662. tool->addAction(actionZoomReset);
  663. tool->setMovable(false);
  664. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  665. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  666. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  667. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  668. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  669. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  670. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  671. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  672. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  673. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  674. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  675. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  676. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  677. actionContents->setEnabled(false);
  678. actionPictures->setEnabled(false);
  679. actionInspect->setEnabled(false);
  680. }
  681. void FbMainWindow::viewTree()
  682. {
  683. if (dockTree) dockTree->deleteLater(); else createTree();
  684. }
  685. void FbMainWindow::viewImgs()
  686. {
  687. if (dockImgs) dockImgs->deleteLater(); else createImgs();
  688. }
  689. */
  690. void FbMainWindow::status(const QString &text)
  691. {
  692. statusBar()->showMessage(text);
  693. }