fb2main.cpp 9.7 KB

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