fb2main.cpp 15 KB

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