fb2main.cpp 18 KB

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