fb2main.cpp 26 KB

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