Browse Source

Switching between editing modes: text. head, xml

Kandrashin Denis 13 years ago
parent
commit
0e32656328
5 changed files with 75 additions and 52 deletions
  1. 36 21
      source/fb2main.cpp
  2. 28 18
      source/fb2read.cpp
  3. 2 1
      source/fb2read.h
  4. 7 6
      source/fb2view.cpp
  5. 2 6
      source/fb2view.h

+ 36 - 21
source/fb2main.cpp

@@ -413,31 +413,18 @@ void Fb2MainWindow::createTree()
     treeView->setFocus();
 }
 
-void Fb2MainWindow::createHead()
+void Fb2MainWindow::loadFinished(bool ok)
 {
-    if (headTree) return;
-    headTree = new QTreeView(this);
-    headTree->header()->setDefaultSectionSize(200);
-    if (textEdit) {
-        this->setFocus();
-        textEdit->setParent(NULL);
-        setCentralWidget(headTree);
-        textEdit->setParent(this);
+    if (headTree) {
         Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
         headTree->setModel(model);
         model->expand(headTree);
-    } else {
-        setCentralWidget(headTree);
     }
-    headTree->setFocus();
-}
-
-void Fb2MainWindow::loadFinished(bool ok)
-{
-    if (!treeView) return ;
-    Fb2TreeModel *model = new Fb2TreeModel(*textEdit, treeView);
-    treeView->setModel(model);
-    model->expand(treeView);
+    if (treeView) {
+        Fb2TreeModel *model = new Fb2TreeModel(*textEdit, treeView);
+        treeView->setModel(model);
+        model->expand(treeView);
+    }
 }
 
 void Fb2MainWindow::selectionChanged()
@@ -662,6 +649,8 @@ void Fb2MainWindow::viewQsci()
 void Fb2MainWindow::viewText()
 {
     if (centralWidget() == textEdit) return;
+    QString xml;
+    if (qsciEdit) xml = qsciEdit->text();
     FB2DELETE(qsciEdit);
     FB2DELETE(headTree);
     if (!textEdit) {
@@ -694,6 +683,8 @@ void Fb2MainWindow::viewText()
     connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
     connect(actionZoomOrig, SIGNAL(triggered()), textEdit, SLOT(zoomOrig()));
 
+    if (!xml.isEmpty()) textEdit->load(curFile, xml);
+
     FB2DELETE(toolEdit);
     QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
     tool->setMovable(false);
@@ -780,10 +771,34 @@ void Fb2MainWindow::viewText()
 void Fb2MainWindow::viewHead()
 {
     if (centralWidget() == headTree) return;
+
+    QString xml;
+    if (qsciEdit) xml = qsciEdit->text();
+
     FB2DELETE(dockTree);
     FB2DELETE(qsciEdit);
     FB2DELETE(toolEdit);
-    createHead();
+
+    if (!headTree) {
+        headTree = new QTreeView(this);
+        headTree->header()->setDefaultSectionSize(200);
+    }
+    if (textEdit) {
+        this->setFocus();
+        textEdit->setParent(NULL);
+        setCentralWidget(headTree);
+        textEdit->setParent(this);
+        Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
+        headTree->setModel(model);
+        model->expand(headTree);
+    } else {
+        textEdit = new Fb2WebView(this);
+        connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
+        setCentralWidget(headTree);
+    }
+    headTree->setFocus();
+
+    if (!xml.isEmpty()) textEdit->load(curFile, xml);
 
     if (textEdit) {
         actionUndo->disconnect();

+ 28 - 18
source/fb2read.cpp

@@ -8,10 +8,11 @@
 //  Fb2ReadThread
 //---------------------------------------------------------------------------
 
-Fb2ReadThread::Fb2ReadThread(QObject *parent, const QString &filename)
+Fb2ReadThread::Fb2ReadThread(QObject *parent, const QString &filename, const QString &xml)
     : QThread(parent)
     , m_filename(filename)
     , m_abort(false)
+    , m_xml(xml)
 {
     connect(this, SIGNAL(html(QString, QString)), parent, SLOT(html(QString, QString)));
 }
@@ -36,25 +37,34 @@ void Fb2ReadThread::run()
 
 bool Fb2ReadThread::parse()
 {
-    QFile file(m_filename);
-    if (!file.open(QFile::ReadOnly | QFile::Text)) {
-        qCritical() << QObject::tr("Cannot read file %1: %2.").arg(m_filename).arg(file.errorString());
-        return false;
-    }
     QXmlStreamWriter writer(&m_html);
     Fb2ReadHandler handler(*this, writer);
-#ifdef _WIN32
-    QXmlSimpleReader reader;
-    reader.setContentHandler(&handler);
-    reader.setErrorHandler(&handler);
-    QXmlInputSource source(&file);
-    return reader.parse(source);
-#else
-    XML2::XmlReader reader;
-    reader.setContentHandler(&handler);
-    reader.setErrorHandler(&handler);
-    return reader.parse(file);
-#endif
+    if (m_xml.isEmpty()) {
+        QFile file(m_filename);
+        if (!file.open(QFile::ReadOnly | QFile::Text)) {
+            qCritical() << QObject::tr("Cannot read file %1: %2.").arg(m_filename).arg(file.errorString());
+            return false;
+        }
+        #ifdef _WIN32
+            QXmlSimpleReader reader;
+            reader.setContentHandler(&handler);
+            reader.setErrorHandler(&handler);
+            QXmlInputSource source(&file);
+            return reader.parse(source);
+        #else
+            XML2::XmlReader reader;
+            reader.setContentHandler(&handler);
+            reader.setErrorHandler(&handler);
+            return reader.parse(file);
+        #endif
+    } else {
+        QXmlSimpleReader reader;
+        reader.setContentHandler(&handler);
+        reader.setErrorHandler(&handler);
+        QXmlInputSource source;
+        source.setData(m_xml);
+        return reader.parse(source);
+    }
 }
 
 //---------------------------------------------------------------------------

+ 2 - 1
source/fb2read.h

@@ -13,7 +13,7 @@ class Fb2ReadThread : public QThread
     Q_OBJECT
 
 public:
-    Fb2ReadThread(QObject *parent, const QString &filename);
+    Fb2ReadThread(QObject *parent, const QString &filename, const QString &xml = QString());
     ~Fb2ReadThread();
     QString * data() { return &m_html; }
 
@@ -31,6 +31,7 @@ private:
 
 private:
     const QString m_filename;
+    const QString m_xml;
     QString m_html;
     bool m_abort;
     QMutex mutex;

+ 7 - 6
source/fb2view.cpp

@@ -73,7 +73,6 @@ QString Fb2WebView::toBodyXml()
 
 void Fb2WebView::fixContents()
 {
-    m_empty = false;
     foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
         span.removeAttribute("style");
     }
@@ -84,11 +83,10 @@ void Fb2WebView::linkHovered(const QString &link, const QString &title, const QS
     QToolTip::showText(QPoint(100, 100), link);
 }
 
-void Fb2WebView::load(const QString &filename)
+void Fb2WebView::load(const QString &filename, const QString &xml)
 {
-    m_empty = false;
     if (m_thread) return;
-    m_thread = new Fb2ReadThread(this, filename);
+    m_thread = new Fb2ReadThread(this, filename, xml);
     m_thread->start();
 }
 
@@ -105,13 +103,16 @@ bool Fb2WebView::save(QIODevice *device)
 
 bool Fb2WebView::save(QString *string)
 {
-    Fb2SaveHandler handler(*this, string);
+    QByteArray data;
+    Fb2SaveHandler handler(*this, &data);
     QXmlInputSource source;
     source.setData(toBodyXml());
     XML2::HtmlReader reader;
     reader.setContentHandler(&handler);
     reader.setErrorHandler(&handler);
-    return reader.parse(source);
+    bool ok = reader.parse(source);
+    if (ok) *string = QString::fromUtf8(data.data());
+    return ok;
 }
 
 QTemporaryFile * Fb2WebView::file(const QString &name)

+ 2 - 6
source/fb2view.h

@@ -16,7 +16,7 @@ class Fb2BaseWebView : public QWebView
 
 public:
     Fb2BaseWebView(QWidget* parent = 0)
-        : QWebView(parent), m_empty(true)
+        : QWebView(parent)
     {
           m_timer.setInterval(100);
           m_timer.setSingleShot(true);
@@ -32,14 +32,10 @@ protected slots:
 
 protected:
      void resizeEvent(QResizeEvent* event) {
-          if (m_empty) return QWebView::resizeEvent(event);
           if (!m_timer.isActive()) m_size = event->oldSize();
           m_timer.start();
      }
 
-protected:
-    bool m_empty;
-
 private:
     QTimer m_timer;
     QSize m_size;
@@ -62,7 +58,7 @@ class Fb2WebView : public Fb2BaseWebView
 public:
     explicit Fb2WebView(QWidget *parent = 0);
     virtual ~Fb2WebView();
-    void load(const QString &filename);
+    void load(const QString &filename, const QString &xml = QString());
     bool save(QIODevice *device);
     bool save(QString *string);
     QString fileName(const QString &path);