1
0

fb2main.cpp 10.0 KB

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