Quellcode durchsuchen

Load document in main window

Kandrashin Denis vor 13 Jahren
Ursprung
Commit
99d649826d
2 geänderte Dateien mit 67 neuen und 72 gelöschten Zeilen
  1. 63 69
      source/fb2main.cpp
  2. 4 3
      source/fb2main.h

+ 63 - 69
source/fb2main.cpp

@@ -9,10 +9,44 @@ MainWindow::MainWindow()
     setCurrentFile("");
     setCurrentFile("");
 }
 }
 
 
-MainWindow::MainWindow(const QString &fileName)
+MainWindow::MainWindow(const QString &filename, QTextDocument * document)
 {
 {
     init();
     init();
-    loadFile(fileName);
+
+    if (!document) document = LoadDocument(filename);
+    setCurrentFile(filename, document);
+}
+
+QTextDocument * MainWindow::LoadDocument(const QString &filename)
+{
+    if (filename.isEmpty()) return NULL;
+
+    QFile file(filename);
+    if (!file.open(QFile::ReadOnly | QFile::Text)) {
+        QMessageBox::warning(
+            NULL,
+            tr("fb2edit"),
+            tr("Cannot read file %1:\n%2.")
+                .arg(filename)
+                .arg(file.errorString())
+        );
+        return NULL;
+    }
+
+    QTextDocument * document = new QTextDocument;
+
+    Fb2Handler handler(*document);
+    QXmlSimpleReader reader;
+    reader.setContentHandler(&handler);
+    reader.setErrorHandler(&handler);
+    QXmlInputSource source(&file);
+
+    if (reader.parse(source)) {
+        return document;
+    } else {
+        delete document;
+        return NULL;
+    }
 }
 }
 
 
 void MainWindow::closeEvent(QCloseEvent *event)
 void MainWindow::closeEvent(QCloseEvent *event)
@@ -34,28 +68,26 @@ void MainWindow::newFile()
 
 
 void MainWindow::open()
 void MainWindow::open()
 {
 {
-    QString fileName = QFileDialog::getOpenFileName(this);
-    if (!fileName.isEmpty()) {
-        MainWindow *existing = findMainWindow(fileName);
-        if (existing) {
-            existing->show();
-            existing->raise();
-            existing->activateWindow();
-            return;
-        }
-
-        if (isUntitled && textEdit->document()->isEmpty()
-                && !isWindowModified()) {
-            loadFile(fileName);
-        } else {
-            MainWindow *other = new MainWindow(fileName);
-            if (other->isUntitled) {
-                delete other;
-                return;
-            }
-            other->move(x() + 40, y() + 40);
-            other->show();
-        }
+    QString filename = QFileDialog::getOpenFileName(this);
+    if (filename.isEmpty()) return;
+
+    MainWindow * existing = findMainWindow(filename);
+    if (existing) {
+        existing->show();
+        existing->raise();
+        existing->activateWindow();
+        return;
+    }
+
+    QTextDocument * document = LoadDocument(filename);
+    if (!document) return;
+
+    if (isUntitled && textEdit->document()->isEmpty() && !isWindowModified()) {
+        setCurrentFile(filename, document);
+    } else {
+        MainWindow * other = new MainWindow(filename, document);
+        other->move(x() + 40, y() + 40);
+        other->show();
     }
     }
 }
 }
 
 
@@ -70,11 +102,8 @@ bool MainWindow::save()
 
 
 bool MainWindow::saveAs()
 bool MainWindow::saveAs()
 {
 {
-    QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
-                                                    curFile);
-    if (fileName.isEmpty())
-        return false;
-
+    QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), curFile);
+    if (fileName.isEmpty()) return false;
     return saveFile(fileName);
     return saveFile(fileName);
 }
 }
 
 
@@ -259,42 +288,6 @@ bool MainWindow::maybeSave()
     return true;
     return true;
 }
 }
 
 
-void MainWindow::loadFile(const QString &fileName)
-{
-
-    QFile file(fileName);
-    if (!file.open(QFile::ReadOnly | QFile::Text)) {
-        QMessageBox::warning(
-            this,
-            tr("fb2edit"),
-            tr("Cannot read file %1:\n%2.")
-                .arg(fileName)
-                .arg(file.errorString())
-        );
-        return;
-    }
-
-    QApplication::setOverrideCursor(Qt::WaitCursor);
-
-    QTextDocument * document = new QTextDocument;
-    Fb2Handler handler(*document);
-    QXmlSimpleReader reader;
-    reader.setContentHandler(&handler);
-    reader.setErrorHandler(&handler);
-
-    QXmlInputSource source(&file);
-    if (reader.parse(source)) {
-        setCurrentFile(fileName);
-        textEdit->clear();
-        textEdit->setDocument(document);
-        statusBar()->showMessage(tr("File loaded"), 2000);
-    } else {
-        delete document;
-    }
-
-    QApplication::restoreOverrideCursor();
-}
-
 bool MainWindow::saveFile(const QString &fileName)
 bool MainWindow::saveFile(const QString &fileName)
 {
 {
     return false;
     return false;
@@ -318,17 +311,18 @@ bool MainWindow::saveFile(const QString &fileName)
     return true;
     return true;
 }
 }
 
 
-void MainWindow::setCurrentFile(const QString &fileName)
+void MainWindow::setCurrentFile(const QString &filename, QTextDocument * document)
 {
 {
     static int sequenceNumber = 1;
     static int sequenceNumber = 1;
 
 
-    isUntitled = fileName.isEmpty();
+    isUntitled = filename.isEmpty();
     if (isUntitled) {
     if (isUntitled) {
-        curFile = tr("document%1.txt").arg(sequenceNumber++);
+        curFile = tr("book%1.fb2").arg(sequenceNumber++);
     } else {
     } else {
-        curFile = QFileInfo(fileName).canonicalFilePath();
+        curFile = QFileInfo(filename).canonicalFilePath();
     }
     }
 
 
+    if (document) textEdit->setDocument(document); else textEdit->clear();
     textEdit->document()->setModified(false);
     textEdit->document()->setModified(false);
     setWindowModified(false);
     setWindowModified(false);
     setWindowFilePath(curFile);
     setWindowFilePath(curFile);

+ 4 - 3
source/fb2main.h

@@ -7,6 +7,7 @@ QT_BEGIN_NAMESPACE
 class QAction;
 class QAction;
 class QMenu;
 class QMenu;
 class QTextEdit;
 class QTextEdit;
+class QTextDocument;
 QT_END_NAMESPACE
 QT_END_NAMESPACE
 
 
 class MainWindow : public QMainWindow
 class MainWindow : public QMainWindow
@@ -15,7 +16,8 @@ class MainWindow : public QMainWindow
 
 
 public:
 public:
     MainWindow();
     MainWindow();
-    MainWindow(const QString &fileName);
+    MainWindow(const QString &filename, QTextDocument * document = NULL);
+    static QTextDocument * LoadDocument(const QString &filename);
 
 
 protected:
 protected:
     void closeEvent(QCloseEvent *event);
     void closeEvent(QCloseEvent *event);
@@ -37,9 +39,8 @@ private:
     void readSettings();
     void readSettings();
     void writeSettings();
     void writeSettings();
     bool maybeSave();
     bool maybeSave();
-    void loadFile(const QString &fileName);
     bool saveFile(const QString &fileName);
     bool saveFile(const QString &fileName);
-    void setCurrentFile(const QString &fileName);
+    void setCurrentFile(const QString &fileName, QTextDocument * document = NULL);
     QString strippedName(const QString &fullFileName);
     QString strippedName(const QString &fullFileName);
     MainWindow *findMainWindow(const QString &fileName);
     MainWindow *findMainWindow(const QString &fileName);