1
0

fb2main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #include <QtGui>
  2. #include <QtDebug>
  3. #include "fb2main.h"
  4. #include "fb2read.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. QTextDocument * 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. QTextDocument * document = new QTextDocument;
  47. Fb2Handler handler(*document);
  48. QXmlSimpleReader reader;
  49. reader.setContentHandler(&handler);
  50. reader.setErrorHandler(&handler);
  51. QXmlInputSource source(&file);
  52. if (reader.parse(source)) {
  53. return document;
  54. } else {
  55. delete document;
  56. return NULL;
  57. }
  58. }
  59. void MainWindow::closeEvent(QCloseEvent *event)
  60. {
  61. if (maybeSave()) {
  62. writeSettings();
  63. event->accept();
  64. } else {
  65. event->ignore();
  66. }
  67. }
  68. void MainWindow::newFile()
  69. {
  70. MainWindow *other = new MainWindow;
  71. other->move(x() + 40, y() + 40);
  72. other->show();
  73. }
  74. void MainWindow::open()
  75. {
  76. QString filename = QFileDialog::getOpenFileName(this);
  77. if (filename.isEmpty()) return;
  78. MainWindow * existing = findMainWindow(filename);
  79. if (existing) {
  80. existing->show();
  81. existing->raise();
  82. existing->activateWindow();
  83. return;
  84. }
  85. if (textEdit) {
  86. QTextDocument * document = loadFB2(filename);
  87. if (!document) return;
  88. if (isUntitled && textEdit->document()->isEmpty() && !isWindowModified()) {
  89. setCurrentFile(filename, document);
  90. } else {
  91. MainWindow * other = new MainWindow(filename, document);
  92. other->move(x() + 40, y() + 40);
  93. other->show();
  94. }
  95. } else if (qsciEdit) {
  96. if (isUntitled && !isWindowModified()) {
  97. loadXML(filename);
  98. setCurrentFile(filename);
  99. } else {
  100. MainWindow * other = new MainWindow(filename);
  101. other->move(x() + 40, y() + 40);
  102. other->show();
  103. }
  104. }
  105. }
  106. bool MainWindow::save()
  107. {
  108. if (isUntitled) {
  109. return saveAs();
  110. } else {
  111. return saveFile(curFile);
  112. }
  113. }
  114. bool MainWindow::saveAs()
  115. {
  116. QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), curFile);
  117. if (fileName.isEmpty()) return false;
  118. return saveFile(fileName);
  119. }
  120. void MainWindow::about()
  121. {
  122. QMessageBox::about(this, tr("About SDI"),
  123. tr("The <b>SDI</b> example demonstrates how to write single "
  124. "document interface applications using Qt."));
  125. }
  126. void MainWindow::documentWasModified()
  127. {
  128. QFileInfo info = windowFilePath();
  129. QString title = info.fileName();
  130. title += QString("[*]") += QString(" - ") += qApp->applicationName();
  131. setWindowTitle(title);
  132. setWindowModified(true);
  133. }
  134. void MainWindow::init()
  135. {
  136. setAttribute(Qt::WA_DeleteOnClose);
  137. isUntitled = true;
  138. createActions();
  139. createMenus();
  140. createToolBars();
  141. createStatusBar();
  142. readSettings();
  143. setUnifiedTitleAndToolBarOnMac(true);
  144. }
  145. void MainWindow::createText()
  146. {
  147. textEdit = new QTextEdit;
  148. textEdit->setAcceptRichText(true);
  149. setCentralWidget(textEdit);
  150. connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
  151. connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
  152. connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
  153. connect(textEdit->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
  154. connect(textEdit, SIGNAL(copyAvailable(bool)), cutAct, SLOT(setEnabled(bool)));
  155. connect(textEdit, SIGNAL(copyAvailable(bool)), copyAct, SLOT(setEnabled(bool)));
  156. }
  157. void MainWindow::createQsci()
  158. {
  159. // http://qtcoder.blogspot.com/2010/10/qscintills.html
  160. // http://www.riverbankcomputing.co.uk/static/Docs/QScintilla2/classQsciScintilla.html
  161. qsciEdit = new QsciScintilla;
  162. qsciEdit->setUtf8(true);
  163. qsciEdit->setCaretLineVisible(true);
  164. qsciEdit->setCaretLineBackgroundColor(QColor("gainsboro"));
  165. qsciEdit->setWrapMode(QsciScintilla::WrapWord);
  166. qsciEdit->setEolMode(QsciScintilla::EolWindows);
  167. qsciEdit->setAutoIndent(true);
  168. qsciEdit->setIndentationGuides(true);
  169. qsciEdit->setAutoCompletionSource(QsciScintilla::AcsAll);
  170. qsciEdit->setAutoCompletionCaseSensitivity(true);
  171. qsciEdit->setAutoCompletionReplaceWord(true);
  172. qsciEdit->setAutoCompletionShowSingle(true);
  173. qsciEdit->setAutoCompletionThreshold(2);
  174. qsciEdit->setMarginsBackgroundColor(QColor("gainsboro"));
  175. qsciEdit->setMarginWidth(0, 0);
  176. qsciEdit->setMarginLineNumbers(1, true);
  177. qsciEdit->setMarginWidth(1, QString("1000"));
  178. qsciEdit->setFolding(QsciScintilla::BoxedFoldStyle, 2);
  179. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  180. qsciEdit->setMatchedBraceBackgroundColor(Qt::yellow);
  181. qsciEdit->setUnmatchedBraceForegroundColor(Qt::blue);
  182. QFont font("Courier", 10);
  183. font.setStyleHint(QFont::TypeWriter);
  184. QsciLexerXML * lexer = new QsciLexerXML;
  185. lexer->setFont(font, -1);
  186. qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
  187. qsciEdit->setLexer(lexer);
  188. setCentralWidget(qsciEdit);
  189. qsciEdit->setFocus();
  190. // connect(qsciEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  191. // connect(qsciEdit, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(cursorMoved(int, int)));
  192. }
  193. void MainWindow::createActions()
  194. {
  195. newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
  196. newAct->setShortcuts(QKeySequence::New);
  197. newAct->setStatusTip(tr("Create a new file"));
  198. connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
  199. openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
  200. openAct->setShortcuts(QKeySequence::Open);
  201. openAct->setStatusTip(tr("Open an existing file"));
  202. connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
  203. saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
  204. saveAct->setShortcuts(QKeySequence::Save);
  205. saveAct->setStatusTip(tr("Save the document to disk"));
  206. connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
  207. saveAsAct = new QAction(tr("Save &As..."), this);
  208. saveAsAct->setShortcuts(QKeySequence::SaveAs);
  209. saveAsAct->setStatusTip(tr("Save the document under a new name"));
  210. connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
  211. closeAct = new QAction(tr("&Close"), this);
  212. closeAct->setShortcut(tr("Ctrl+W"));
  213. closeAct->setStatusTip(tr("Close this window"));
  214. connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));
  215. exitAct = new QAction(tr("E&xit"), this);
  216. exitAct->setShortcuts(QKeySequence::Quit);
  217. exitAct->setStatusTip(tr("Exit the application"));
  218. connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  219. cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
  220. cutAct->setShortcuts(QKeySequence::Cut);
  221. cutAct->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
  222. copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
  223. copyAct->setShortcuts(QKeySequence::Copy);
  224. copyAct->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
  225. pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
  226. pasteAct->setShortcuts(QKeySequence::Paste);
  227. pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
  228. textAct = new QAction(tr("&Text"), this);
  229. textAct->setCheckable(true);
  230. connect(textAct, SIGNAL(triggered()), this, SLOT(viewText()));
  231. qsciAct = new QAction(tr("&XML"), this);
  232. qsciAct->setCheckable(true);
  233. connect(qsciAct, SIGNAL(triggered()), this, SLOT(viewQsci()));
  234. QActionGroup * viewGroup = new QActionGroup(this);
  235. viewGroup->addAction(textAct);
  236. viewGroup->addAction(qsciAct);
  237. textAct->setChecked(true);
  238. aboutAct = new QAction(tr("&About"), this);
  239. aboutAct->setStatusTip(tr("Show the application's About box"));
  240. connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
  241. aboutQtAct = new QAction(tr("About &Qt"), this);
  242. aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
  243. connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  244. cutAct->setEnabled(false);
  245. copyAct->setEnabled(false);
  246. }
  247. void MainWindow::createMenus()
  248. {
  249. fileMenu = menuBar()->addMenu(tr("&File"));
  250. fileMenu->addAction(newAct);
  251. fileMenu->addAction(openAct);
  252. fileMenu->addAction(saveAct);
  253. fileMenu->addAction(saveAsAct);
  254. fileMenu->addSeparator();
  255. fileMenu->addAction(closeAct);
  256. fileMenu->addAction(exitAct);
  257. editMenu = menuBar()->addMenu(tr("&Edit"));
  258. editMenu->addAction(cutAct);
  259. editMenu->addAction(copyAct);
  260. editMenu->addAction(pasteAct);
  261. editMenu = menuBar()->addMenu(tr("&View"));
  262. editMenu->addAction(textAct);
  263. editMenu->addAction(qsciAct);
  264. menuBar()->addSeparator();
  265. helpMenu = menuBar()->addMenu(tr("&Help"));
  266. helpMenu->addAction(aboutAct);
  267. helpMenu->addAction(aboutQtAct);
  268. }
  269. void MainWindow::createToolBars()
  270. {
  271. fileToolBar = addToolBar(tr("File"));
  272. fileToolBar->addAction(newAct);
  273. fileToolBar->addAction(openAct);
  274. fileToolBar->addAction(saveAct);
  275. editToolBar = addToolBar(tr("Edit"));
  276. editToolBar->addAction(cutAct);
  277. editToolBar->addAction(copyAct);
  278. editToolBar->addAction(pasteAct);
  279. }
  280. void MainWindow::createStatusBar()
  281. {
  282. statusBar()->showMessage(tr("Ready"));
  283. }
  284. void MainWindow::readSettings()
  285. {
  286. QSettings settings;
  287. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  288. QSize size = settings.value("size", QSize(400, 400)).toSize();
  289. move(pos);
  290. resize(size);
  291. }
  292. void MainWindow::writeSettings()
  293. {
  294. QSettings settings;
  295. settings.setValue("pos", pos());
  296. settings.setValue("size", size());
  297. }
  298. bool MainWindow::maybeSave()
  299. {
  300. if (textEdit && textEdit->document()->isModified()) {
  301. QMessageBox::StandardButton ret;
  302. ret = QMessageBox::warning(this, tr("SDI"),
  303. tr("The document has been modified.\n"
  304. "Do you want to save your changes?"),
  305. QMessageBox::Save | QMessageBox::Discard
  306. | QMessageBox::Cancel);
  307. if (ret == QMessageBox::Save)
  308. return save();
  309. else if (ret == QMessageBox::Cancel)
  310. return false;
  311. }
  312. return true;
  313. }
  314. bool MainWindow::saveFile(const QString &fileName)
  315. {
  316. return false;
  317. QFile file(fileName);
  318. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  319. QMessageBox::warning(this, tr("SDI"),
  320. tr("Cannot write file %1:\n%2.")
  321. .arg(fileName)
  322. .arg(file.errorString()));
  323. return false;
  324. }
  325. QTextStream out(&file);
  326. QApplication::setOverrideCursor(Qt::WaitCursor);
  327. out << textEdit->toPlainText();
  328. QApplication::restoreOverrideCursor();
  329. setCurrentFile(fileName);
  330. statusBar()->showMessage(tr("File saved"), 2000);
  331. return true;
  332. }
  333. void MainWindow::setCurrentFile(const QString &filename, QTextDocument * document)
  334. {
  335. static int sequenceNumber = 1;
  336. QString title;
  337. isUntitled = filename.isEmpty();
  338. if (isUntitled) {
  339. curFile = tr("book%1.fb2").arg(sequenceNumber++);
  340. title = curFile;
  341. } else {
  342. QFileInfo info = filename;
  343. curFile = info.canonicalFilePath();
  344. title = info.fileName();
  345. }
  346. title += QString(" - ") += qApp->applicationName();
  347. if (document) {
  348. textEdit->setDocument(document);
  349. textEdit->document()->setModified(false);
  350. connect(textEdit->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
  351. }
  352. setWindowModified(false);
  353. setWindowFilePath(curFile);
  354. setWindowTitle(title);
  355. }
  356. MainWindow *MainWindow::findMainWindow(const QString &fileName)
  357. {
  358. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  359. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  360. MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
  361. if (mainWin && mainWin->curFile == canonicalFilePath)
  362. return mainWin;
  363. }
  364. return 0;
  365. }
  366. void MainWindow::viewQsci()
  367. {
  368. if (centralWidget() == qsciEdit) return;
  369. if (textEdit) { delete textEdit; textEdit = NULL; }
  370. createQsci();
  371. }
  372. void MainWindow::viewText()
  373. {
  374. if (centralWidget() == textEdit) return;
  375. if (qsciEdit) { delete qsciEdit; qsciEdit = NULL; }
  376. createText();
  377. }