fb2main.cpp 9.5 KB

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