fb2main.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 QAction(FbIcon("edit-undo"), tr("&Undo"), this);
  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 QAction(FbIcon("edit-redo"), tr("&Redo"), this);
  195. text->setAction(Fb::EditRedo, act);
  196. code->setAction(Fb::EditUndo, 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 QAction(FbIcon("edit-cut"), tr("Cu&t"), this);
  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 QAction(FbIcon("edit-copy"), tr("&Copy"), this);
  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 QAction(FbIcon("edit-paste"), tr("&Paste"), this);
  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 QAction(tr("Paste (no style)"), this);
  229. text->setAction(Fb::PasteText, act);
  230. menu->addAction(act);
  231. clipboardDataChanged();
  232. menu->addSeparator();
  233. act = new QAction(FbIcon("edit-find"), tr("&Find..."), this);
  234. text->setAction(Fb::TextFind, act);
  235. code->setAction(Fb::TextFind, act);
  236. act->setShortcuts(QKeySequence::Find);
  237. menu->addAction(act);
  238. act = new QAction(FbIcon("edit-find-replace"), tr("&Replace..."), this);
  239. text->setAction(Fb::TextReplace, act);
  240. code->setAction(Fb::TextReplace, act);
  241. menu->addAction(act);
  242. menu->addSeparator();
  243. act = new QAction(FbIcon("preferences-desktop"), tr("&Settings"), this);
  244. act->setShortcuts(QKeySequence::Preferences);
  245. act->setStatusTip(tr("Application settings"));
  246. connect(act, SIGNAL(triggered()), SLOT(openSettings()));
  247. menu->addAction(act);
  248. menu = menuBar()->addMenu(tr("&Insert", "Main 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. act = new FbTextAction(FbIcon("edit-clear"), tr("Clear format"), QWebPage::RemoveFormat, text);
  304. text->setAction(Fb::ClearFormat, act);
  305. menu->addAction(act);
  306. menu->addSeparator();
  307. act = new FbTextAction(FbIcon("format-text-bold"), tr("&Bold"), QWebPage::ToggleBold, text);
  308. text->setAction(Fb::TextBold, act);
  309. act->setShortcuts(QKeySequence::Bold);
  310. act->setCheckable(true);
  311. menu->addAction(act);
  312. act = new FbTextAction(FbIcon("format-text-italic"), tr("&Italic"), QWebPage::ToggleItalic, text);
  313. text->setAction(Fb::TextItalic, act);
  314. act->setShortcuts(QKeySequence::Italic);
  315. act->setCheckable(true);
  316. menu->addAction(act);
  317. act = new FbTextAction(FbIcon("format-text-strikethrough"), tr("&Strikethrough"), QWebPage::ToggleStrikethrough, text);
  318. text->setAction(Fb::TextStrike, act);
  319. act->setCheckable(true);
  320. menu->addAction(act);
  321. act = new FbTextAction(FbIcon("format-text-superscript"), tr("Su&perscript"), QWebPage::ToggleSuperscript, text);
  322. text->setAction(Fb::TextSup, act);
  323. act->setCheckable(true);
  324. menu->addAction(act);
  325. act = new FbTextAction(FbIcon("format-text-subscript"), tr("Su&bscript"), QWebPage::ToggleSubscript, text);
  326. text->setAction(Fb::TextSub, act);
  327. act->setCheckable(true);
  328. menu->addAction(act);
  329. act = new QAction(FbIcon("utilities-terminal"), tr("&Code"), this);
  330. text->setAction(Fb::TextCode, act);
  331. act->setCheckable(true);
  332. menu->addAction(act);
  333. menu->addSeparator();
  334. act = new FbTextAction(FbIcon("format-indent-more"), tr("Create section"), QWebPage::ToggleSubscript, text);
  335. text->setAction(Fb::SectionAdd, act);
  336. menu->addAction(act);
  337. act = new FbTextAction(FbIcon("format-indent-less"), tr("Remove section"), QWebPage::ToggleSubscript, text);
  338. text->setAction(Fb::SectionDel, act);
  339. menu->addAction(act);
  340. act = new FbTextAction(FbIcon("format-justify-center"), tr("Make title"), QWebPage::ToggleSubscript, text);
  341. text->setAction(Fb::TextTitle, act);
  342. menu->addAction(act);
  343. menu = menuBar()->addMenu(tr("&View"));
  344. tool->addSeparator();
  345. QActionGroup * viewGroup = new QActionGroup(this);
  346. act = new FbModeAction(mainDock, Fb::Text, tr("&Text"));
  347. viewGroup->addAction(act);
  348. menu->addAction(act);
  349. tool->addAction(act);
  350. act->setChecked(true);
  351. act = new FbModeAction(mainDock, Fb::Head, tr("&Head"));
  352. viewGroup->addAction(act);
  353. menu->addAction(act);
  354. tool->addAction(act);
  355. act = new FbModeAction(mainDock, Fb::Code, tr("&XML"));
  356. viewGroup->addAction(act);
  357. menu->addAction(act);
  358. tool->addAction(act);
  359. #ifdef QT_DEBUG
  360. act = new FbModeAction(mainDock, Fb::Html, tr("&HTML"));
  361. viewGroup->addAction(act);
  362. menu->addAction(act);
  363. #endif // _DEBUG
  364. menu->addSeparator();
  365. act = new QAction(FbIcon("zoom-in"), tr("Zoom in"), this);
  366. text->setAction(Fb::ZoomIn, act);
  367. code->setAction(Fb::ZoomIn, act);
  368. act->setShortcuts(QKeySequence::ZoomIn);
  369. menu->addAction(act);
  370. act = new QAction(FbIcon("zoom-out"), tr("Zoom out"), this);
  371. text->setAction(Fb::ZoomOut, act);
  372. code->setAction(Fb::ZoomOut, act);
  373. act->setShortcuts(QKeySequence::ZoomOut);
  374. menu->addAction(act);
  375. act = new QAction(FbIcon("zoom-original"), tr("Zoom original"), this);
  376. text->setAction(Fb::ZoomReset, act);
  377. code->setAction(Fb::ZoomReset, act);
  378. menu->addAction(act);
  379. menu->addSeparator();
  380. act = new QAction(tr("&Contents"), this);
  381. text->setAction(Fb::ViewContents, act);
  382. act->setCheckable(true);
  383. menu->addAction(act);
  384. act = new QAction(tr("&Pictures"), this);
  385. text->setAction(Fb::ViewPictures, act);
  386. act->setCheckable(true);
  387. menu->addAction(act);
  388. act = new QAction(tr("&Web inspector"), this);
  389. text->setAction(Fb::ViewInspect, act);
  390. act->setCheckable(true);
  391. menu->addAction(act);
  392. menuBar()->addSeparator();
  393. menu = menuBar()->addMenu(tr("&Help"));
  394. act = new QAction(FbIcon("help-about"), tr("&About"), this);
  395. act->setStatusTip(tr("Show the application's About box"));
  396. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  397. menu->addAction(act);
  398. act = new QAction(tr("About &Qt"), this);
  399. act->setStatusTip(tr("Show the Qt library's About box"));
  400. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  401. menu->addAction(act);
  402. toolEdit = tool = addToolBar(tr("Edit"));
  403. tool->setMovable(false);
  404. tool->addSeparator();
  405. mainDock->setTool(tool);
  406. }
  407. void FbMainWindow::openSettings()
  408. {
  409. FbSetupDlg dlg(this);
  410. dlg.exec();
  411. }
  412. void FbMainWindow::createStatusBar()
  413. {
  414. statusBar()->showMessage(tr("Ready"));
  415. }
  416. void FbMainWindow::readSettings()
  417. {
  418. QSettings settings;
  419. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  420. QSize size = settings.value("size", QSize(400, 400)).toSize();
  421. move(pos);
  422. resize(size);
  423. }
  424. void FbMainWindow::writeSettings()
  425. {
  426. QSettings settings;
  427. settings.setValue("pos", pos());
  428. settings.setValue("size", size());
  429. }
  430. bool FbMainWindow::maybeSave()
  431. {
  432. if (mainDock->isModified()) {
  433. QMessageBox::StandardButton ret;
  434. ret = QMessageBox::warning(this, qApp->applicationName(),
  435. tr("The document has been modified. Do you want to save your changes?"),
  436. QMessageBox::Save | QMessageBox::Discard
  437. | QMessageBox::Cancel);
  438. if (ret == QMessageBox::Save)
  439. return fileSave();
  440. else if (ret == QMessageBox::Cancel)
  441. return false;
  442. }
  443. return true;
  444. }
  445. bool FbMainWindow::saveFile(const QString &fileName, const QString &codec)
  446. {
  447. QFile file(fileName);
  448. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  449. QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
  450. return false;
  451. }
  452. bool ok = mainDock->save(&file);
  453. setCurrentFile(fileName);
  454. return ok;
  455. }
  456. void FbMainWindow::setCurrentFile(const QString &filename)
  457. {
  458. if (filename.isEmpty()) {
  459. static int sequenceNumber = 1;
  460. curFile = QString("book%1.fb2").arg(sequenceNumber++);
  461. } else {
  462. QFileInfo info = filename;
  463. curFile = info.canonicalFilePath();
  464. }
  465. setWindowFilePath(curFile);
  466. setModified(false);
  467. }
  468. QString FbMainWindow::appTitle() const
  469. {
  470. return QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
  471. }
  472. FbMainWindow *FbMainWindow::findFbMainWindow(const QString &fileName)
  473. {
  474. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  475. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  476. FbMainWindow *mainWin = qobject_cast<FbMainWindow *>(widget);
  477. if (mainWin && mainWin->curFile == canonicalFilePath)
  478. return mainWin;
  479. }
  480. return 0;
  481. }
  482. /*
  483. void FbMainWindow::checkScintillaUndo()
  484. {
  485. if (!codeEdit) return;
  486. actionUndo->setEnabled(codeEdit->isUndoAvailable());
  487. actionRedo->setEnabled(codeEdit->isRedoAvailable());
  488. }
  489. void FbMainWindow::viewText(FbTextPage *page)
  490. {
  491. if (textFrame && centralWidget() == textFrame) return;
  492. if (textFrame) textFrame->hideInspector();
  493. if (codeEdit) {
  494. QString xml = codeEdit->text();
  495. page = new FbTextPage(this);
  496. if (!page->load(QString(), xml)) {
  497. delete page;
  498. return;
  499. }
  500. isSwitched = true;
  501. }
  502. FB2DELETE(codeEdit);
  503. FB2DELETE(headTree);
  504. if (textFrame) {
  505. createTextToolbar();
  506. } else {
  507. textFrame = new FbTextFrame(this, actionInspect);
  508. }
  509. setCentralWidget(textFrame);
  510. FbTextEdit *textEdit = textFrame->view();
  511. if (page) {
  512. page->setParent(textEdit);
  513. textEdit->setPage(page);
  514. }
  515. connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(createTextToolbar()));
  516. connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
  517. connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
  518. actionContents->setEnabled(true);
  519. actionPictures->setEnabled(true);
  520. actionInspect->setEnabled(true);
  521. textEdit->setFocus();
  522. viewTree();
  523. }
  524. void FbMainWindow::viewHead()
  525. {
  526. if (headTree && centralWidget() == headTree) return;
  527. if (textFrame) textFrame->hideInspector();
  528. FbTextPage *page = 0;
  529. if (codeEdit) {
  530. QString xml = codeEdit->text();
  531. page = new FbTextPage(this);
  532. if (!page->load(QString(), xml)) {
  533. delete page;
  534. return;
  535. }
  536. isSwitched = true;
  537. }
  538. FB2DELETE(dockTree);
  539. FB2DELETE(dockImgs);
  540. FB2DELETE(codeEdit);
  541. FB2DELETE(toolEdit);
  542. if (!textFrame) {
  543. textFrame = new FbTextFrame(this, actionInspect);
  544. FbTextEdit *textEdit = textFrame->view();
  545. if (page) {
  546. page->setParent(textEdit);
  547. textEdit->setPage(page);
  548. }
  549. }
  550. if (!headTree) {
  551. headTree = new FbHeadEdit(this);
  552. headTree->setText(textFrame->view());
  553. connect(headTree, SIGNAL(status(QString)), this, SLOT(status(QString)));
  554. }
  555. this->setFocus();
  556. textFrame->setParent(NULL);
  557. setCentralWidget(headTree);
  558. textFrame->setParent(this);
  559. headTree->updateTree();
  560. headTree->setFocus();
  561. if (textFrame) {
  562. actionUndo->disconnect();
  563. actionRedo->disconnect();
  564. actionCut->disconnect();
  565. actionCopy->disconnect();
  566. actionPaste->disconnect();
  567. actionTextBold->disconnect();
  568. actionTextItalic->disconnect();
  569. actionTextStrike->disconnect();
  570. actionTextSub->disconnect();
  571. actionTextSup->disconnect();
  572. }
  573. FB2DELETE(toolEdit);
  574. toolEdit = addToolBar(tr("Edit"));
  575. headTree->initToolbar(*toolEdit);
  576. toolEdit->addSeparator();
  577. toolEdit->setMovable(false);
  578. actionContents->setEnabled(false);
  579. actionPictures->setEnabled(false);
  580. actionInspect->setEnabled(true);
  581. }
  582. void FbMainWindow::viewCode()
  583. {
  584. if (codeEdit && centralWidget() == codeEdit) return;
  585. bool load = false;
  586. QByteArray xml;
  587. if (textFrame) {
  588. textFrame->view()->save(&xml);
  589. isSwitched = true;
  590. load = true;
  591. }
  592. FB2DELETE(textFrame);
  593. FB2DELETE(dockTree);
  594. FB2DELETE(dockImgs);
  595. FB2DELETE(headTree);
  596. if (!codeEdit) {
  597. codeEdit = new FbCodeEdit;
  598. }
  599. if (load) codeEdit->load(xml);
  600. setCentralWidget(codeEdit);
  601. codeEdit->setFocus();
  602. FB2DELETE(toolEdit);
  603. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  604. tool->addSeparator();
  605. tool->addAction(actionUndo);
  606. tool->addAction(actionRedo);
  607. tool->addSeparator();
  608. tool->addAction(actionCut);
  609. tool->addAction(actionCopy);
  610. tool->addAction(actionPaste);
  611. tool->addSeparator();
  612. tool->addAction(actionZoomIn);
  613. tool->addAction(actionZoomOut);
  614. tool->addAction(actionZoomReset);
  615. tool->setMovable(false);
  616. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  617. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  618. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  619. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  620. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  621. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  622. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  623. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  624. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  625. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  626. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  627. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  628. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  629. actionContents->setEnabled(false);
  630. actionPictures->setEnabled(false);
  631. actionInspect->setEnabled(false);
  632. }
  633. void FbMainWindow::viewHtml()
  634. {
  635. if (codeEdit && centralWidget() == codeEdit) return;
  636. if (!textFrame) return;
  637. QString html = textFrame->view()->page()->mainFrame()->toHtml();
  638. isSwitched = true;
  639. FB2DELETE(textFrame);
  640. FB2DELETE(dockTree);
  641. FB2DELETE(dockImgs);
  642. FB2DELETE(headTree);
  643. if (!codeEdit) {
  644. codeEdit = new FbCodeEdit;
  645. }
  646. codeEdit->setPlainText(html);
  647. setCentralWidget(codeEdit);
  648. codeEdit->setFocus();
  649. FB2DELETE(toolEdit);
  650. QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
  651. tool->addSeparator();
  652. tool->addAction(actionUndo);
  653. tool->addAction(actionRedo);
  654. tool->addSeparator();
  655. tool->addAction(actionCut);
  656. tool->addAction(actionCopy);
  657. tool->addAction(actionPaste);
  658. tool->addSeparator();
  659. tool->addAction(actionZoomIn);
  660. tool->addAction(actionZoomOut);
  661. tool->addAction(actionZoomReset);
  662. tool->setMovable(false);
  663. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  664. connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
  665. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  666. connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
  667. connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
  668. connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
  669. connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
  670. connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
  671. connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
  672. connect(actionFind, SIGNAL(triggered()), codeEdit, SLOT(find()));
  673. connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
  674. connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
  675. connect(actionZoomReset, SIGNAL(triggered()), codeEdit, SLOT(zoomReset()));
  676. actionContents->setEnabled(false);
  677. actionPictures->setEnabled(false);
  678. actionInspect->setEnabled(false);
  679. }
  680. void FbMainWindow::viewTree()
  681. {
  682. if (dockTree) dockTree->deleteLater(); else createTree();
  683. }
  684. void FbMainWindow::viewImgs()
  685. {
  686. if (dockImgs) dockImgs->deleteLater(); else createImgs();
  687. }
  688. */
  689. void FbMainWindow::clipboardDataChanged()
  690. {
  691. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  692. // actionPaste->setEnabled(md->hasText());
  693. // actionPasteText->setEnabled(md->hasText());
  694. }
  695. }
  696. void FbMainWindow::status(const QString &text)
  697. {
  698. statusBar()->showMessage(text);
  699. }