浏览代码

View document as XML

Kandrashin Denis 13 年之前
父节点
当前提交
43e7098b30
共有 6 个文件被更改,包括 81 次插入36 次删除
  1. 24 14
      source/fb2main.cpp
  2. 3 1
      source/fb2main.h
  3. 34 3
      source/fb2save.cpp
  4. 8 2
      source/fb2save.h
  5. 10 13
      source/fb2view.cpp
  6. 2 3
      source/fb2view.h

+ 24 - 14
source/fb2main.cpp

@@ -51,6 +51,7 @@ void Fb2MainWindow::init()
     noteEdit = NULL;
     qsciEdit = NULL;
     treeView = NULL;
+    headTree = NULL;
     messageEdit = NULL;
 
     readSettings();
@@ -330,21 +331,27 @@ void Fb2MainWindow::createActions()
 
     menu = menuBar()->addMenu(tr("&View"));
 
-    QAction * actText = act = new QAction(tr("&Text"), this);
+    QActionGroup * viewGroup = new QActionGroup(this);
+
+    act = new QAction(tr("&Text"), this);
     act->setCheckable(true);
+    act->setChecked(true);
     connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
+    viewGroup->addAction(act);
+    menu->addAction(act);
 
-    QAction * actQsci = act = new QAction(tr("&XML"), this);
+    act = new QAction(tr("&Head"), this);
     act->setCheckable(true);
-    connect(act, SIGNAL(triggered()), this, SLOT(viewQsci()));
+    connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
+    viewGroup->addAction(act);
+    menu->addAction(act);
 
-    QActionGroup * viewGroup = new QActionGroup(this);
-    viewGroup->addAction(actText);
-    viewGroup->addAction(actQsci);
-    actText->setChecked(true);
+    act = new QAction(tr("&XML"), this);
+    act->setCheckable(true);
+    connect(act, SIGNAL(triggered()), this, SLOT(viewQsci()));
+    viewGroup->addAction(act);
+    menu->addAction(act);
 
-    menu->addAction(actText);
-    menu->addAction(actQsci);
     menu->addSeparator();
 
     tool = addToolBar(tr("Zoom"));
@@ -558,8 +565,7 @@ bool Fb2MainWindow::saveFile(const QString &fileName)
         QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
         return false;
     }
-
-    if (textEdit) return textEdit->save(file);
+    if (textEdit) return textEdit->save(&file);
     return true;
 
 /*
@@ -609,9 +615,9 @@ Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
 void Fb2MainWindow::viewQsci()
 {
     if (centralWidget() == qsciEdit) return;
-    QString html;
+    QString xml;
     if (textEdit) {
-        html = textEdit->toXml();
+        if (!textEdit->save(&xml)) return;
         delete textEdit;
         textEdit = NULL;
     }
@@ -620,7 +626,7 @@ void Fb2MainWindow::viewQsci()
         dockTree = NULL;
     }
     createQsci();
-    qsciEdit->setText(html);
+    qsciEdit->setText(xml);
 }
 
 void Fb2MainWindow::viewText()
@@ -630,6 +636,10 @@ void Fb2MainWindow::viewText()
     createText();
 }
 
+void Fb2MainWindow::viewHead()
+{
+}
+
 void Fb2MainWindow::clipboardDataChanged()
 {
     if (const QMimeData *md = QApplication::clipboard()->mimeData()) {

+ 3 - 1
source/fb2main.h

@@ -46,6 +46,7 @@ private slots:
     void logShowed();
     void viewQsci();
     void viewText();
+    void viewHead();
 
     void clipboardDataChanged();
     void loadFinished(bool ok);
@@ -73,11 +74,12 @@ private:
     Fb2MainWindow *findFb2MainWindow(const QString &fileName);
 
     Fb2WebView *textEdit;
+    QTreeView *headTree;
     QTextEdit *noteEdit;
     QTextEdit *messageEdit;
     QDockWidget *dockTree;
     QsciScintilla *qsciEdit;
-    QTreeView * treeView;
+    QTreeView *treeView;
     QString curFile;
     bool isUntitled;
 

+ 34 - 3
source/fb2save.cpp

@@ -8,9 +8,28 @@
 //  Fb2SaveWriter
 //---------------------------------------------------------------------------
 
-Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QIODevice &device)
-    : QXmlStreamWriter(&device)
+Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QIODevice *device)
+    : QXmlStreamWriter(device)
     , m_view(view)
+{
+    Init();
+}
+
+Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QByteArray *array)
+    : QXmlStreamWriter(array)
+    , m_view(view)
+{
+    Init();
+}
+
+Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QString *string)
+    : QXmlStreamWriter(string)
+    , m_view(view)
+{
+    Init();
+}
+
+void Fb2SaveWriter::Init()
 {
     setAutoFormatting(true);
     setAutoFormattingIndent(2);
@@ -232,12 +251,24 @@ void Fb2SaveHandler::ParagHandler::start()
 //  Fb2SaveHandler
 //---------------------------------------------------------------------------
 
-Fb2SaveHandler::Fb2SaveHandler(Fb2WebView &view, QIODevice &device)
+Fb2SaveHandler::Fb2SaveHandler(Fb2WebView &view, QIODevice *device)
     : Fb2XmlHandler()
     , m_writer(view, device)
 {
 }
 
+Fb2SaveHandler::Fb2SaveHandler(Fb2WebView &view, QByteArray *array)
+    : Fb2XmlHandler()
+    , m_writer(view, array)
+{
+}
+
+Fb2SaveHandler::Fb2SaveHandler(Fb2WebView &view, QString *string)
+    : Fb2XmlHandler()
+    , m_writer(view, string)
+{
+}
+
 Fb2XmlHandler::NodeHandler * Fb2SaveHandler::CreateRoot(const QString &name, const QXmlAttributes &atts)
 {
     if (name == "body") return new RootHandler(m_writer, name, atts);

+ 8 - 2
source/fb2save.h

@@ -16,11 +16,15 @@ class Fb2WebView;
 class Fb2SaveWriter : public QXmlStreamWriter
 {
 public:
-    explicit Fb2SaveWriter(Fb2WebView &view, QIODevice &device);
+    explicit Fb2SaveWriter(Fb2WebView &view, QIODevice *device);
+    explicit Fb2SaveWriter(Fb2WebView &view, QByteArray *array);
+    explicit Fb2SaveWriter(Fb2WebView &view, QString *string);
     virtual ~Fb2SaveWriter();
     QString getFile(const QString &path);
     QString getData(const QString &name);
     void writeFiles();
+private:
+    void Init();
 private:
     Fb2WebView &m_view;
     typedef QHash<QString, QString> StringHash;
@@ -32,7 +36,9 @@ private:
 class Fb2SaveHandler : public Fb2XmlHandler
 {
 public:
-    explicit Fb2SaveHandler(Fb2WebView &view, QIODevice &device);
+    explicit Fb2SaveHandler(Fb2WebView &view, QIODevice *device);
+    explicit Fb2SaveHandler(Fb2WebView &view, QByteArray *array);
+    explicit Fb2SaveHandler(Fb2WebView &view, QString *string);
 
 private:
     class BodyHandler : public NodeHandler

+ 10 - 13
source/fb2view.cpp

@@ -64,12 +64,6 @@ QWebElement Fb2WebView::doc()
     return page()->mainFrame()->documentElement();
 }
 
-QString Fb2WebView::toXml()
-{
-    return toBodyXml();
-    return doc().toOuterXml();
-}
-
 QString Fb2WebView::toBodyXml()
 {
     QWebElement body = doc().findFirst("body");
@@ -98,17 +92,20 @@ void Fb2WebView::load(const QString &filename)
     m_thread->start();
 }
 
-bool Fb2WebView::save(const QString &filename)
+bool Fb2WebView::save(QIODevice *device)
 {
-    QFile file(filename);
-    if (file.open(QFile::WriteOnly | QFile::Text)) return save(file);
-    qCritical() << QObject::tr("Cannot write file %1: %2.").arg(filename).arg(file.errorString());
-    return false;
+    Fb2SaveHandler handler(*this, device);
+    QXmlInputSource source;
+    source.setData(toBodyXml());
+    XML2::HtmlReader reader;
+    reader.setContentHandler(&handler);
+    reader.setErrorHandler(&handler);
+    return reader.parse(source);
 }
 
-bool Fb2WebView::save(QIODevice &device)
+bool Fb2WebView::save(QString *string)
 {
-    Fb2SaveHandler handler(*this, device);
+    Fb2SaveHandler handler(*this, string);
     QXmlInputSource source;
     source.setData(toBodyXml());
     XML2::HtmlReader reader;

+ 2 - 3
source/fb2view.h

@@ -63,12 +63,11 @@ public:
     explicit Fb2WebView(QWidget *parent = 0);
     virtual ~Fb2WebView();
     void load(const QString &filename);
-    bool save(const QString &filename);
-    bool save(QIODevice &device);
+    bool save(QIODevice *device);
+    bool save(QString *string);
     QString fileName(const QString &path);
     QString fileData(const QString &name);
     QString toBodyXml();
-    QString toXml();
 
     bool UndoEnabled();
     bool RedoEnabled();