fb2main.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. #include <QtGui>
  2. #include <QtDebug>
  3. #include <QTreeView>
  4. #include <QWebView>
  5. #include <QWebFrame>
  6. #include "fb2main.h"
  7. #include "fb2read.h"
  8. #include "fb2tree.h"
  9. #include "fb2view.h"
  10. #include "fb2main.h"
  11. #include <Qsci/qsciscintilla.h>
  12. #include <Qsci/qscilexerxml.h>
  13. MainWindow::MainWindow()
  14. {
  15. init();
  16. createText();
  17. setCurrentFile("");
  18. }
  19. MainWindow::MainWindow(const QString &filename)
  20. {
  21. init();
  22. createQsci();
  23. loadXML(filename);
  24. setCurrentFile(filename);
  25. }
  26. MainWindow::MainWindow(const QString &filename, Fb2MainDocument * document)
  27. {
  28. init();
  29. createText();
  30. thread = new Fb2ReadThread(this, filename);
  31. connect(thread, SIGNAL(sendDocument()), SLOT(sendDocument()));
  32. thread->start();
  33. }
  34. void MainWindow::init()
  35. {
  36. thread = NULL;
  37. connect(qApp, SIGNAL(logMessage(QString)), SLOT(logMessage(QString)));
  38. setAttribute(Qt::WA_DeleteOnClose);
  39. isUntitled = true;
  40. createActions();
  41. createStatusBar();
  42. textEdit = NULL;
  43. noteEdit = NULL;
  44. qsciEdit = NULL;
  45. treeView = NULL;
  46. messageEdit = NULL;
  47. readSettings();
  48. setUnifiedTitleAndToolBarOnMac(true);
  49. }
  50. void MainWindow::logMessage(const QString &message)
  51. {
  52. if (!messageEdit) {
  53. messageEdit = new QTextEdit(this);
  54. connect(messageEdit, SIGNAL(destroyed()), SLOT(logDestroyed()));
  55. QDockWidget * dock = new QDockWidget(tr("Message log"), this);
  56. dock->setAttribute(Qt::WA_DeleteOnClose);
  57. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  58. dock->setWidget(messageEdit);
  59. addDockWidget(Qt::BottomDockWidgetArea, dock);
  60. messageEdit->setMaximumHeight(80);
  61. }
  62. messageEdit->append(message);
  63. }
  64. void MainWindow::logShowed()
  65. {
  66. messageEdit->setMaximumHeight(QWIDGETSIZE_MAX);
  67. }
  68. void MainWindow::logDestroyed()
  69. {
  70. messageEdit = NULL;
  71. }
  72. static QTextFrame * findFrame(QTextFrame *root, const QModelIndex &index)
  73. {
  74. if (!index.isValid()) return root;
  75. if (QTextFrame *frame = findFrame(root, index.parent())) {
  76. QTextFrame *child = static_cast<QTextFrame*>(index.internalPointer());
  77. int i = frame->childFrames().indexOf(child);
  78. if (i >= 0) return child;
  79. }
  80. return NULL;
  81. }
  82. void MainWindow::treeActivated(const QModelIndex &index)
  83. {
  84. }
  85. void MainWindow::treeDestroyed()
  86. {
  87. treeView = NULL;
  88. }
  89. bool MainWindow::loadXML(const QString &filename)
  90. {
  91. if (!filename.isEmpty()) {
  92. QFile file(filename);
  93. if (file.open(QFile::ReadOnly | QFile::Text)) {
  94. qsciEdit->clear();
  95. return qsciEdit->read(&file);
  96. }
  97. }
  98. return false;
  99. }
  100. void MainWindow::closeEvent(QCloseEvent *event)
  101. {
  102. if (maybeSave()) {
  103. writeSettings();
  104. event->accept();
  105. } else {
  106. event->ignore();
  107. }
  108. }
  109. void MainWindow::fileNew()
  110. {
  111. MainWindow *other = new MainWindow;
  112. other->move(x() + 40, y() + 40);
  113. other->show();
  114. }
  115. void MainWindow::fileOpen()
  116. {
  117. QString filename = QFileDialog::getOpenFileName(this);
  118. if (filename.isEmpty()) return;
  119. MainWindow * existing = findMainWindow(filename);
  120. if (existing) {
  121. existing->show();
  122. existing->raise();
  123. existing->activateWindow();
  124. return;
  125. }
  126. if (textEdit) {
  127. if (isUntitled && !isWindowModified()) {
  128. thread = new Fb2ReadThread(this, filename);
  129. connect(thread, SIGNAL(sendDocument()), SLOT(sendDocument()));
  130. thread->start();
  131. } else {
  132. MainWindow * other = new MainWindow(filename, NULL);
  133. other->move(x() + 40, y() + 40);
  134. other->show();
  135. }
  136. } else if (qsciEdit) {
  137. if (isUntitled && !isWindowModified()) {
  138. loadXML(filename);
  139. setCurrentFile(filename);
  140. } else {
  141. MainWindow * other = new MainWindow(filename);
  142. other->move(x() + 40, y() + 40);
  143. other->show();
  144. }
  145. }
  146. }
  147. void MainWindow::sendDocument()
  148. {
  149. setCurrentFile(thread->file(), thread->html());
  150. return;
  151. treeView = new QTreeView(this);
  152. // treeView->setModel(new Fb2TreeModel(*textEdit));
  153. treeView->setHeaderHidden(true);
  154. connect(treeView, SIGNAL(activated(QModelIndex)), SLOT(treeActivated(QModelIndex)));
  155. connect(treeView, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  156. QDockWidget * dock = new QDockWidget(tr("Contents"), this);
  157. dock->setAttribute(Qt::WA_DeleteOnClose);
  158. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  159. dock->setWidget(treeView);
  160. addDockWidget(Qt::LeftDockWidgetArea, dock);
  161. }
  162. bool MainWindow::fileSave()
  163. {
  164. if (isUntitled) {
  165. return fileSaveAs();
  166. } else {
  167. return saveFile(curFile);
  168. }
  169. }
  170. bool MainWindow::fileSaveAs()
  171. {
  172. QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), curFile);
  173. if (fileName.isEmpty()) return false;
  174. return saveFile(fileName);
  175. }
  176. void MainWindow::about()
  177. {
  178. QMessageBox::about(this, tr("About SDI"),
  179. tr("The <b>SDI</b> example demonstrates how to write single "
  180. "document interface applications using Qt."));
  181. }
  182. void MainWindow::documentWasModified()
  183. {
  184. QFileInfo info = windowFilePath();
  185. QString title = info.fileName();
  186. title += QString("[*]") += QString(" - ") += qApp->applicationName();
  187. setWindowTitle(title);
  188. setWindowModified(true);
  189. }
  190. QIcon MainWindow::icon(const QString &name)
  191. {
  192. QIcon icon;
  193. icon.addFile(QString(":/images/24x24/%1.png").arg(name), QSize(24,24));
  194. icon.addFile(QString(":/images/16x16/%1.png").arg(name), QSize(16,16));
  195. return QIcon::fromTheme(name, icon);
  196. }
  197. void MainWindow::createActions()
  198. {
  199. QAction * act;
  200. QMenu * menu;
  201. QToolBar * tool;
  202. QList<QAction*> actions;
  203. menu = menuBar()->addMenu(tr("&File"));
  204. tool = addToolBar(tr("File"));
  205. act = new QAction(icon("document-new"), tr("&New"), this);
  206. act->setPriority(QAction::LowPriority);
  207. act->setShortcuts(QKeySequence::New);
  208. act->setStatusTip(tr("Create a new file"));
  209. connect(act, SIGNAL(triggered()), this, SLOT(fileNew()));
  210. menu->addAction(act);
  211. tool->addAction(act);
  212. act = new QAction(icon("document-open"), tr("&Open..."), this);
  213. act->setShortcuts(QKeySequence::Open);
  214. act->setStatusTip(tr("Open an existing file"));
  215. connect(act, SIGNAL(triggered()), this, SLOT(fileOpen()));
  216. menu->addAction(act);
  217. tool->addAction(act);
  218. act = new QAction(icon("document-save"), tr("&Save"), this);
  219. act->setShortcuts(QKeySequence::Save);
  220. act->setStatusTip(tr("Save the document to disk"));
  221. connect(act, SIGNAL(triggered()), this, SLOT(fileSave()));
  222. menu->addAction(act);
  223. tool->addAction(act);
  224. act = new QAction(icon("document-save-as"), tr("Save &As..."), this);
  225. act->setShortcuts(QKeySequence::SaveAs);
  226. act->setStatusTip(tr("Save the document under a new name"));
  227. connect(act, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
  228. menu->addAction(act);
  229. menu->addSeparator();
  230. act = new QAction(icon("window-close"), tr("&Close"), this);
  231. act->setShortcuts(QKeySequence::Close);
  232. act->setStatusTip(tr("Close this window"));
  233. connect(act, SIGNAL(triggered()), this, SLOT(close()));
  234. menu->addAction(act);
  235. act = new QAction(icon("application-exit"), tr("E&xit"), this);
  236. act->setShortcuts(QKeySequence::Quit);
  237. act->setStatusTip(tr("Exit the application"));
  238. connect(act, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  239. menu->addAction(act);
  240. menu = menuBar()->addMenu(tr("&Edit"));
  241. tool = addToolBar(tr("Edit"));
  242. connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
  243. actionUndo = act = new QAction(icon("edit-undo"), tr("&Undo"), this);
  244. act->setPriority(QAction::LowPriority);
  245. act->setShortcut(QKeySequence::Undo);
  246. menu->addAction(act);
  247. tool->addAction(act);
  248. actionRedo = act = new QAction(icon("edit-redo"), tr("&Redo"), this);
  249. act->setPriority(QAction::LowPriority);
  250. act->setShortcut(QKeySequence::Redo);
  251. menu->addAction(act);
  252. tool->addAction(act);
  253. menu->addSeparator();
  254. tool->addSeparator();
  255. actionCut = act = new QAction(icon("edit-cut"), tr("Cu&t"), this);
  256. act->setPriority(QAction::LowPriority);
  257. act->setShortcuts(QKeySequence::Cut);
  258. act->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  259. act->setEnabled(false);
  260. menu->addAction(act);
  261. tool->addAction(act);
  262. actionCopy = act = new QAction(icon("edit-copy"), tr("&Copy"), this);
  263. act->setPriority(QAction::LowPriority);
  264. act->setShortcuts(QKeySequence::Copy);
  265. act->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  266. act->setEnabled(false);
  267. menu->addAction(act);
  268. tool->addAction(act);
  269. actionPaste = act = new QAction(icon("edit-paste"), tr("&Paste"), this);
  270. act->setPriority(QAction::LowPriority);
  271. act->setShortcuts(QKeySequence::Paste);
  272. act->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  273. menu->addAction(act);
  274. tool->addAction(act);
  275. clipboardDataChanged();
  276. menu = menuBar()->addMenu(tr("Format"));
  277. tool = addToolBar(tr("Format"));
  278. actionTextBold = act = new QAction(icon("format-text-bold"), tr("Bold"), this);
  279. act->setShortcuts(QKeySequence::Bold);
  280. act->setCheckable(true);
  281. connect(act, SIGNAL(triggered()), SLOT(textBold()));
  282. menu->addAction(act);
  283. tool->addAction(act);
  284. actionTextItalic = act = new QAction(icon("format-text-italic"), tr("Italic"), this);
  285. act->setShortcuts(QKeySequence::Italic);
  286. act->setCheckable(true);
  287. connect(act, SIGNAL(triggered()), SLOT(textItalic()));
  288. menu->addAction(act);
  289. tool->addAction(act);
  290. actionTextStrike = act = new QAction(icon("format-text-strikethrough"), tr("Strikethrough"), this);
  291. act->setCheckable(true);
  292. connect(act, SIGNAL(triggered()), SLOT(textStrike()));
  293. menu->addAction(act);
  294. tool->addAction(act);
  295. actionTextSup = act = new QAction(icon("format-text-superscript"), tr("Superscript"), this);
  296. act->setCheckable(true);
  297. connect(act, SIGNAL(triggered()), SLOT(textSup()));
  298. menu->addAction(act);
  299. tool->addAction(act);
  300. actionTextSub = act = new QAction(icon("format-text-subscript"), tr("Subscript"), this);
  301. act->setCheckable(true);
  302. connect(act, SIGNAL(triggered()), SLOT(textSub()));
  303. menu->addAction(act);
  304. tool->addAction(act);
  305. menu = menuBar()->addMenu(tr("&View"));
  306. QAction * actText = act = new QAction(tr("&Text"), this);
  307. act->setCheckable(true);
  308. connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
  309. QAction * actQsci = act = new QAction(tr("&XML"), this);
  310. act->setCheckable(true);
  311. connect(act, SIGNAL(triggered()), this, SLOT(viewQsci()));
  312. QActionGroup * viewGroup = new QActionGroup(this);
  313. viewGroup->addAction(actText);
  314. viewGroup->addAction(actQsci);
  315. actText->setChecked(true);
  316. menu->addAction(actText);
  317. menu->addAction(actQsci);
  318. menu->addSeparator();
  319. tool = addToolBar(tr("Zoom"));
  320. actionZoomIn = act = new QAction(icon("zoom-in"), tr("Zoom in"), this);
  321. act->setShortcuts(QKeySequence::ZoomIn);
  322. menu->addAction(act);
  323. tool->addAction(act);
  324. actionZoomOut = act = new QAction(icon("zoom-out"), tr("Zoom out"), this);
  325. act->setShortcuts(QKeySequence::ZoomOut);
  326. menu->addAction(act);
  327. tool->addAction(act);
  328. actionZoomOrig = act = new QAction(icon("zoom-original"), tr("Zoom original"), this);
  329. menu->addAction(act);
  330. tool->addAction(act);
  331. menuBar()->addSeparator();
  332. menu = menuBar()->addMenu(tr("&Help"));
  333. act = new QAction(icon("help-about"), tr("&About"), this);
  334. act->setStatusTip(tr("Show the application's About box"));
  335. connect(act, SIGNAL(triggered()), this, SLOT(about()));
  336. menu->addAction(act);
  337. act = new QAction(tr("About &Qt"), this);
  338. act->setStatusTip(tr("Show the Qt library's About box"));
  339. connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  340. menu->addAction(act);
  341. }
  342. void MainWindow::connectTextDocument(QTextDocument * document)
  343. {
  344. connect(document, SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
  345. connect(document, SIGNAL(undoAvailable(bool)), actionUndo, SLOT(setEnabled(bool)));
  346. connect(document, SIGNAL(redoAvailable(bool)), actionRedo, SLOT(setEnabled(bool)));
  347. }
  348. void MainWindow::createText()
  349. {
  350. textEdit = new Fb2WebView(this);
  351. setCentralWidget(textEdit);
  352. textEdit->setFocus();
  353. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  354. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  355. connect(actionZoomOrig, SIGNAL(triggered()), textEdit, SLOT(zoomOrig()));
  356. }
  357. void MainWindow::createQsci()
  358. {
  359. // http://qtcoder.blogspot.com/2010/10/qscintills.html
  360. // http://www.riverbankcomputing.co.uk/static/Docs/QScintilla2/classQsciScintilla.html
  361. qsciEdit = new QsciScintilla;
  362. qsciEdit->setUtf8(true);
  363. qsciEdit->setCaretLineVisible(true);
  364. qsciEdit->setCaretLineBackgroundColor(QColor("gainsboro"));
  365. qsciEdit->setWrapMode(QsciScintilla::WrapWord);
  366. qsciEdit->setEolMode(QsciScintilla::EolWindows);
  367. qsciEdit->setAutoIndent(true);
  368. qsciEdit->setIndentationGuides(true);
  369. qsciEdit->setAutoCompletionSource(QsciScintilla::AcsAll);
  370. qsciEdit->setAutoCompletionCaseSensitivity(true);
  371. qsciEdit->setAutoCompletionReplaceWord(true);
  372. qsciEdit->setAutoCompletionShowSingle(true);
  373. qsciEdit->setAutoCompletionThreshold(2);
  374. qsciEdit->setMarginsBackgroundColor(QColor("gainsboro"));
  375. qsciEdit->setMarginWidth(0, 0);
  376. qsciEdit->setMarginLineNumbers(1, true);
  377. qsciEdit->setMarginWidth(1, QString("10000"));
  378. qsciEdit->setFolding(QsciScintilla::BoxedFoldStyle, 2);
  379. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  380. qsciEdit->setMatchedBraceBackgroundColor(Qt::yellow);
  381. qsciEdit->setUnmatchedBraceForegroundColor(Qt::blue);
  382. QFont font("Courier", 10);
  383. font.setStyleHint(QFont::TypeWriter);
  384. QsciLexerXML * lexer = new QsciLexerXML;
  385. lexer->setFont(font, -1);
  386. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  387. qsciEdit->setLexer(lexer);
  388. setCentralWidget(qsciEdit);
  389. qsciEdit->setFocus();
  390. // connect(qsciEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  391. // connect(qsciEdit, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(cursorMoved(int, int)));
  392. actionUndo->setEnabled(false);
  393. actionRedo->setEnabled(false);
  394. }
  395. void MainWindow::createStatusBar()
  396. {
  397. statusBar()->showMessage(tr("Ready"));
  398. }
  399. void MainWindow::readSettings()
  400. {
  401. QSettings settings;
  402. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  403. QSize size = settings.value("size", QSize(400, 400)).toSize();
  404. move(pos);
  405. resize(size);
  406. }
  407. void MainWindow::writeSettings()
  408. {
  409. QSettings settings;
  410. settings.setValue("pos", pos());
  411. settings.setValue("size", size());
  412. }
  413. bool MainWindow::maybeSave()
  414. {
  415. if (textEdit && textEdit->isModified()) {
  416. QMessageBox::StandardButton ret;
  417. ret = QMessageBox::warning(this, tr("SDI"),
  418. tr("The document has been modified.\n"
  419. "Do you want to save your changes?"),
  420. QMessageBox::Save | QMessageBox::Discard
  421. | QMessageBox::Cancel);
  422. if (ret == QMessageBox::Save)
  423. return fileSave();
  424. else if (ret == QMessageBox::Cancel)
  425. return false;
  426. }
  427. return true;
  428. }
  429. bool MainWindow::saveFile(const QString &fileName)
  430. {
  431. return false;
  432. QFile file(fileName);
  433. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  434. QMessageBox::warning(this, tr("SDI"),
  435. tr("Cannot write file %1:\n%2.")
  436. .arg(fileName)
  437. .arg(file.errorString()));
  438. return false;
  439. }
  440. QTextStream out(&file);
  441. QApplication::setOverrideCursor(Qt::WaitCursor);
  442. // out << textEdit->toPlainText();
  443. QApplication::restoreOverrideCursor();
  444. setCurrentFile(fileName);
  445. statusBar()->showMessage(tr("File saved"), 2000);
  446. return true;
  447. }
  448. void MainWindow::setCurrentFile(const QString &filename, const QString &html)
  449. {
  450. static int sequenceNumber = 1;
  451. QString title;
  452. isUntitled = filename.isEmpty();
  453. if (isUntitled) {
  454. curFile = tr("book%1.fb2").arg(sequenceNumber++);
  455. title = curFile;
  456. } else {
  457. QFileInfo info = filename;
  458. curFile = info.canonicalFilePath();
  459. title = info.fileName();
  460. }
  461. title += QString(" - ") += qApp->applicationName();
  462. textEdit->setHtml(html, QUrl("fb2://s/"));
  463. setWindowModified(false);
  464. setWindowFilePath(curFile);
  465. setWindowTitle(title);
  466. }
  467. MainWindow *MainWindow::findMainWindow(const QString &fileName)
  468. {
  469. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  470. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  471. MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
  472. if (mainWin && mainWin->curFile == canonicalFilePath)
  473. return mainWin;
  474. }
  475. return 0;
  476. }
  477. void MainWindow::viewQsci()
  478. {
  479. if (centralWidget() == qsciEdit) return;
  480. QString html;
  481. if (textEdit) {
  482. html = textEdit->page()->mainFrame()->toHtml();
  483. delete textEdit;
  484. textEdit = NULL;
  485. }
  486. createQsci();
  487. qsciEdit->setText(html);
  488. }
  489. void MainWindow::viewText()
  490. {
  491. if (centralWidget() == textEdit) return;
  492. if (qsciEdit) { delete qsciEdit; qsciEdit = NULL; }
  493. createText();
  494. }
  495. void MainWindow::cursorPositionChanged()
  496. {
  497. // alignmentChanged(textEdit->alignment());
  498. }
  499. void MainWindow::clipboardDataChanged()
  500. {
  501. if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
  502. actionPaste->setEnabled(md->hasText());
  503. }
  504. }
  505. void MainWindow::textBold()
  506. {
  507. }
  508. void MainWindow::textItalic()
  509. {
  510. }
  511. void MainWindow::textStrike()
  512. {
  513. }
  514. void MainWindow::textSub()
  515. {
  516. }
  517. void MainWindow::textSup()
  518. {
  519. }