fb2main.cpp 13 KB

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