fb2main.cpp 18 KB

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