fb2main.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include <QtGui>
  2. #include <QtDebug>
  3. #include "fb2main.h"
  4. #include "fb2read.h"
  5. MainWindow::MainWindow()
  6. {
  7. init();
  8. setCurrentFile("");
  9. }
  10. MainWindow::MainWindow(const QString &filename, QTextDocument * document)
  11. {
  12. init();
  13. if (!document) document = LoadDocument(filename);
  14. setCurrentFile(filename, document);
  15. }
  16. QTextDocument * MainWindow::LoadDocument(const QString &filename)
  17. {
  18. if (filename.isEmpty()) return NULL;
  19. QFile file(filename);
  20. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  21. qCritical() << tr("Cannot read file %1:\n%2.").arg(filename).arg(file.errorString());
  22. return NULL;
  23. }
  24. QTextDocument * document = new QTextDocument;
  25. Fb2Handler handler(*document);
  26. QXmlSimpleReader reader;
  27. reader.setContentHandler(&handler);
  28. reader.setErrorHandler(&handler);
  29. QXmlInputSource source(&file);
  30. if (reader.parse(source)) {
  31. return document;
  32. } else {
  33. delete document;
  34. return NULL;
  35. }
  36. }
  37. void MainWindow::closeEvent(QCloseEvent *event)
  38. {
  39. if (maybeSave()) {
  40. writeSettings();
  41. event->accept();
  42. } else {
  43. event->ignore();
  44. }
  45. }
  46. void MainWindow::newFile()
  47. {
  48. MainWindow *other = new MainWindow;
  49. other->move(x() + 40, y() + 40);
  50. other->show();
  51. }
  52. void MainWindow::open()
  53. {
  54. QString filename = QFileDialog::getOpenFileName(this);
  55. if (filename.isEmpty()) return;
  56. MainWindow * existing = findMainWindow(filename);
  57. if (existing) {
  58. existing->show();
  59. existing->raise();
  60. existing->activateWindow();
  61. return;
  62. }
  63. QTextDocument * document = LoadDocument(filename);
  64. if (!document) return;
  65. if (isUntitled && textEdit->document()->isEmpty() && !isWindowModified()) {
  66. setCurrentFile(filename, document);
  67. } else {
  68. MainWindow * other = new MainWindow(filename, document);
  69. other->move(x() + 40, y() + 40);
  70. other->show();
  71. }
  72. }
  73. bool MainWindow::save()
  74. {
  75. if (isUntitled) {
  76. return saveAs();
  77. } else {
  78. return saveFile(curFile);
  79. }
  80. }
  81. bool MainWindow::saveAs()
  82. {
  83. QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), curFile);
  84. if (fileName.isEmpty()) return false;
  85. return saveFile(fileName);
  86. }
  87. void MainWindow::about()
  88. {
  89. QMessageBox::about(this, tr("About SDI"),
  90. tr("The <b>SDI</b> example demonstrates how to write single "
  91. "document interface applications using Qt."));
  92. }
  93. void MainWindow::documentWasModified()
  94. {
  95. QFileInfo info = windowFilePath();
  96. QString title = info.fileName();
  97. title += QString("[*]") += QString(" - ") += qApp->applicationName();
  98. setWindowTitle(title);
  99. setWindowModified(true);
  100. }
  101. void MainWindow::init()
  102. {
  103. setAttribute(Qt::WA_DeleteOnClose);
  104. isUntitled = true;
  105. textEdit = new QTextEdit;
  106. textEdit->setAcceptRichText(true);
  107. setCentralWidget(textEdit);
  108. createActions();
  109. createMenus();
  110. createToolBars();
  111. createStatusBar();
  112. readSettings();
  113. connect(textEdit->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
  114. setUnifiedTitleAndToolBarOnMac(true);
  115. }
  116. void MainWindow::createActions()
  117. {
  118. newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
  119. newAct->setShortcuts(QKeySequence::New);
  120. newAct->setStatusTip(tr("Create a new file"));
  121. connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
  122. openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
  123. openAct->setShortcuts(QKeySequence::Open);
  124. openAct->setStatusTip(tr("Open an existing file"));
  125. connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
  126. saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
  127. saveAct->setShortcuts(QKeySequence::Save);
  128. saveAct->setStatusTip(tr("Save the document to disk"));
  129. connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
  130. saveAsAct = new QAction(tr("Save &As..."), this);
  131. saveAsAct->setShortcuts(QKeySequence::SaveAs);
  132. saveAsAct->setStatusTip(tr("Save the document under a new name"));
  133. connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
  134. closeAct = new QAction(tr("&Close"), this);
  135. closeAct->setShortcut(tr("Ctrl+W"));
  136. closeAct->setStatusTip(tr("Close this window"));
  137. connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));
  138. exitAct = new QAction(tr("E&xit"), this);
  139. exitAct->setShortcuts(QKeySequence::Quit);
  140. exitAct->setStatusTip(tr("Exit the application"));
  141. connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  142. cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
  143. cutAct->setShortcuts(QKeySequence::Cut);
  144. cutAct->setStatusTip(tr("Cut the current selection's contents to the "
  145. "clipboard"));
  146. connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
  147. copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
  148. copyAct->setShortcuts(QKeySequence::Copy);
  149. copyAct->setStatusTip(tr("Copy the current selection's contents to the "
  150. "clipboard"));
  151. connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
  152. pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
  153. pasteAct->setShortcuts(QKeySequence::Paste);
  154. pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
  155. "selection"));
  156. connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
  157. aboutAct = new QAction(tr("&About"), this);
  158. aboutAct->setStatusTip(tr("Show the application's About box"));
  159. connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
  160. aboutQtAct = new QAction(tr("About &Qt"), this);
  161. aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
  162. connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  163. cutAct->setEnabled(false);
  164. copyAct->setEnabled(false);
  165. connect(textEdit, SIGNAL(copyAvailable(bool)),
  166. cutAct, SLOT(setEnabled(bool)));
  167. connect(textEdit, SIGNAL(copyAvailable(bool)),
  168. copyAct, SLOT(setEnabled(bool)));
  169. }
  170. //! [implicit tr context]
  171. void MainWindow::createMenus()
  172. {
  173. fileMenu = menuBar()->addMenu(tr("&File"));
  174. //! [implicit tr context]
  175. fileMenu->addAction(newAct);
  176. fileMenu->addAction(openAct);
  177. fileMenu->addAction(saveAct);
  178. fileMenu->addAction(saveAsAct);
  179. fileMenu->addSeparator();
  180. fileMenu->addAction(closeAct);
  181. fileMenu->addAction(exitAct);
  182. editMenu = menuBar()->addMenu(tr("&Edit"));
  183. editMenu->addAction(cutAct);
  184. editMenu->addAction(copyAct);
  185. editMenu->addAction(pasteAct);
  186. menuBar()->addSeparator();
  187. helpMenu = menuBar()->addMenu(tr("&Help"));
  188. helpMenu->addAction(aboutAct);
  189. helpMenu->addAction(aboutQtAct);
  190. }
  191. void MainWindow::createToolBars()
  192. {
  193. //! [0]
  194. fileToolBar = addToolBar(tr("File"));
  195. fileToolBar->addAction(newAct);
  196. fileToolBar->addAction(openAct);
  197. //! [0]
  198. fileToolBar->addAction(saveAct);
  199. editToolBar = addToolBar(tr("Edit"));
  200. editToolBar->addAction(cutAct);
  201. editToolBar->addAction(copyAct);
  202. editToolBar->addAction(pasteAct);
  203. }
  204. void MainWindow::createStatusBar()
  205. {
  206. statusBar()->showMessage(tr("Ready"));
  207. }
  208. void MainWindow::readSettings()
  209. {
  210. QSettings settings;
  211. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  212. QSize size = settings.value("size", QSize(400, 400)).toSize();
  213. move(pos);
  214. resize(size);
  215. }
  216. void MainWindow::writeSettings()
  217. {
  218. QSettings settings;
  219. settings.setValue("pos", pos());
  220. settings.setValue("size", size());
  221. }
  222. bool MainWindow::maybeSave()
  223. {
  224. if (textEdit->document()->isModified()) {
  225. QMessageBox::StandardButton ret;
  226. ret = QMessageBox::warning(this, tr("SDI"),
  227. tr("The document has been modified.\n"
  228. "Do you want to save your changes?"),
  229. QMessageBox::Save | QMessageBox::Discard
  230. | QMessageBox::Cancel);
  231. if (ret == QMessageBox::Save)
  232. return save();
  233. else if (ret == QMessageBox::Cancel)
  234. return false;
  235. }
  236. return true;
  237. }
  238. bool MainWindow::saveFile(const QString &fileName)
  239. {
  240. return false;
  241. QFile file(fileName);
  242. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  243. QMessageBox::warning(this, tr("SDI"),
  244. tr("Cannot write file %1:\n%2.")
  245. .arg(fileName)
  246. .arg(file.errorString()));
  247. return false;
  248. }
  249. QTextStream out(&file);
  250. QApplication::setOverrideCursor(Qt::WaitCursor);
  251. out << textEdit->toPlainText();
  252. QApplication::restoreOverrideCursor();
  253. setCurrentFile(fileName);
  254. statusBar()->showMessage(tr("File saved"), 2000);
  255. return true;
  256. }
  257. void MainWindow::setCurrentFile(const QString &filename, QTextDocument * document)
  258. {
  259. static int sequenceNumber = 1;
  260. QString title;
  261. isUntitled = filename.isEmpty();
  262. if (isUntitled) {
  263. curFile = tr("book%1.fb2").arg(sequenceNumber++);
  264. title = curFile;
  265. } else {
  266. QFileInfo info = filename;
  267. curFile = info.canonicalFilePath();
  268. title = info.fileName();
  269. }
  270. title += QString(" - ") += qApp->applicationName();
  271. if (document) textEdit->setDocument(document); else textEdit->clear();
  272. textEdit->document()->setModified(false);
  273. setWindowModified(false);
  274. setWindowFilePath(curFile);
  275. setWindowTitle(title);
  276. connect(textEdit->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
  277. }
  278. QString MainWindow::strippedName(const QString &fullFileName)
  279. {
  280. return QFileInfo(fullFileName).fileName();
  281. }
  282. MainWindow *MainWindow::findMainWindow(const QString &fileName)
  283. {
  284. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  285. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  286. MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
  287. if (mainWin && mainWin->curFile == canonicalFilePath)
  288. return mainWin;
  289. }
  290. return 0;
  291. }