fb2main.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. textEdit->setAcceptRichText(true);
  86. setCentralWidget(textEdit);
  87. createActions();
  88. createMenus();
  89. createToolBars();
  90. createStatusBar();
  91. readSettings();
  92. connect(textEdit->document(), SIGNAL(contentsChanged()),
  93. this, SLOT(documentWasModified()));
  94. setUnifiedTitleAndToolBarOnMac(true);
  95. }
  96. void MainWindow::createActions()
  97. {
  98. newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
  99. newAct->setShortcuts(QKeySequence::New);
  100. newAct->setStatusTip(tr("Create a new file"));
  101. connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
  102. openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
  103. openAct->setShortcuts(QKeySequence::Open);
  104. openAct->setStatusTip(tr("Open an existing file"));
  105. connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
  106. saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
  107. saveAct->setShortcuts(QKeySequence::Save);
  108. saveAct->setStatusTip(tr("Save the document to disk"));
  109. connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
  110. saveAsAct = new QAction(tr("Save &As..."), this);
  111. saveAsAct->setShortcuts(QKeySequence::SaveAs);
  112. saveAsAct->setStatusTip(tr("Save the document under a new name"));
  113. connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
  114. closeAct = new QAction(tr("&Close"), this);
  115. closeAct->setShortcut(tr("Ctrl+W"));
  116. closeAct->setStatusTip(tr("Close this window"));
  117. connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));
  118. exitAct = new QAction(tr("E&xit"), this);
  119. exitAct->setShortcuts(QKeySequence::Quit);
  120. exitAct->setStatusTip(tr("Exit the application"));
  121. connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
  122. cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
  123. cutAct->setShortcuts(QKeySequence::Cut);
  124. cutAct->setStatusTip(tr("Cut the current selection's contents to the "
  125. "clipboard"));
  126. connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
  127. copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
  128. copyAct->setShortcuts(QKeySequence::Copy);
  129. copyAct->setStatusTip(tr("Copy the current selection's contents to the "
  130. "clipboard"));
  131. connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
  132. pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
  133. pasteAct->setShortcuts(QKeySequence::Paste);
  134. pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
  135. "selection"));
  136. connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
  137. aboutAct = new QAction(tr("&About"), this);
  138. aboutAct->setStatusTip(tr("Show the application's About box"));
  139. connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
  140. aboutQtAct = new QAction(tr("About &Qt"), this);
  141. aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
  142. connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  143. cutAct->setEnabled(false);
  144. copyAct->setEnabled(false);
  145. connect(textEdit, SIGNAL(copyAvailable(bool)),
  146. cutAct, SLOT(setEnabled(bool)));
  147. connect(textEdit, SIGNAL(copyAvailable(bool)),
  148. copyAct, SLOT(setEnabled(bool)));
  149. }
  150. //! [implicit tr context]
  151. void MainWindow::createMenus()
  152. {
  153. fileMenu = menuBar()->addMenu(tr("&File"));
  154. //! [implicit tr context]
  155. fileMenu->addAction(newAct);
  156. fileMenu->addAction(openAct);
  157. fileMenu->addAction(saveAct);
  158. fileMenu->addAction(saveAsAct);
  159. fileMenu->addSeparator();
  160. fileMenu->addAction(closeAct);
  161. fileMenu->addAction(exitAct);
  162. editMenu = menuBar()->addMenu(tr("&Edit"));
  163. editMenu->addAction(cutAct);
  164. editMenu->addAction(copyAct);
  165. editMenu->addAction(pasteAct);
  166. menuBar()->addSeparator();
  167. helpMenu = menuBar()->addMenu(tr("&Help"));
  168. helpMenu->addAction(aboutAct);
  169. helpMenu->addAction(aboutQtAct);
  170. }
  171. void MainWindow::createToolBars()
  172. {
  173. //! [0]
  174. fileToolBar = addToolBar(tr("File"));
  175. fileToolBar->addAction(newAct);
  176. fileToolBar->addAction(openAct);
  177. //! [0]
  178. fileToolBar->addAction(saveAct);
  179. editToolBar = addToolBar(tr("Edit"));
  180. editToolBar->addAction(cutAct);
  181. editToolBar->addAction(copyAct);
  182. editToolBar->addAction(pasteAct);
  183. }
  184. void MainWindow::createStatusBar()
  185. {
  186. statusBar()->showMessage(tr("Ready"));
  187. }
  188. void MainWindow::readSettings()
  189. {
  190. QSettings settings;
  191. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
  192. QSize size = settings.value("size", QSize(400, 400)).toSize();
  193. move(pos);
  194. resize(size);
  195. }
  196. void MainWindow::writeSettings()
  197. {
  198. QSettings settings;
  199. settings.setValue("pos", pos());
  200. settings.setValue("size", size());
  201. }
  202. bool MainWindow::maybeSave()
  203. {
  204. if (textEdit->document()->isModified()) {
  205. QMessageBox::StandardButton ret;
  206. ret = QMessageBox::warning(this, tr("SDI"),
  207. tr("The document has been modified.\n"
  208. "Do you want to save your changes?"),
  209. QMessageBox::Save | QMessageBox::Discard
  210. | QMessageBox::Cancel);
  211. if (ret == QMessageBox::Save)
  212. return save();
  213. else if (ret == QMessageBox::Cancel)
  214. return false;
  215. }
  216. return true;
  217. }
  218. void MainWindow::loadFile(const QString &fileName)
  219. {
  220. QFile file(fileName);
  221. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  222. QMessageBox::warning(
  223. this,
  224. tr("fb2edit"),
  225. tr("Cannot read file %1:\n%2.")
  226. .arg(fileName)
  227. .arg(file.errorString())
  228. );
  229. return;
  230. }
  231. QApplication::setOverrideCursor(Qt::WaitCursor);
  232. QTextDocument * document = new QTextDocument;
  233. Fb2Handler handler(*document);
  234. QXmlSimpleReader reader;
  235. reader.setContentHandler(&handler);
  236. reader.setErrorHandler(&handler);
  237. QXmlInputSource source(&file);
  238. if (reader.parse(source)) {
  239. setCurrentFile(fileName);
  240. textEdit->clear();
  241. textEdit->setDocument(document);
  242. statusBar()->showMessage(tr("File loaded"), 2000);
  243. } else {
  244. delete document;
  245. }
  246. QApplication::restoreOverrideCursor();
  247. }
  248. bool MainWindow::saveFile(const QString &fileName)
  249. {
  250. return false;
  251. QFile file(fileName);
  252. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  253. QMessageBox::warning(this, tr("SDI"),
  254. tr("Cannot write file %1:\n%2.")
  255. .arg(fileName)
  256. .arg(file.errorString()));
  257. return false;
  258. }
  259. QTextStream out(&file);
  260. QApplication::setOverrideCursor(Qt::WaitCursor);
  261. out << textEdit->toPlainText();
  262. QApplication::restoreOverrideCursor();
  263. setCurrentFile(fileName);
  264. statusBar()->showMessage(tr("File saved"), 2000);
  265. return true;
  266. }
  267. void MainWindow::setCurrentFile(const QString &fileName)
  268. {
  269. static int sequenceNumber = 1;
  270. isUntitled = fileName.isEmpty();
  271. if (isUntitled) {
  272. curFile = tr("document%1.txt").arg(sequenceNumber++);
  273. } else {
  274. curFile = QFileInfo(fileName).canonicalFilePath();
  275. }
  276. textEdit->document()->setModified(false);
  277. setWindowModified(false);
  278. setWindowFilePath(curFile);
  279. }
  280. QString MainWindow::strippedName(const QString &fullFileName)
  281. {
  282. return QFileInfo(fullFileName).fileName();
  283. }
  284. MainWindow *MainWindow::findMainWindow(const QString &fileName)
  285. {
  286. QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
  287. foreach (QWidget *widget, qApp->topLevelWidgets()) {
  288. MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
  289. if (mainWin && mainWin->curFile == canonicalFilePath)
  290. return mainWin;
  291. }
  292. return 0;
  293. }