1
0

fb2main.cpp 25 KB

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