Forráskód Böngészése

Keywords: poem, stanza, v

Kandrashin Denis 13 éve
szülő
commit
2cfc016539
2 módosított fájl, 52 hozzáadás és 8 törlés
  1. 43 7
      source/fb2read.cpp
  2. 9 1
      source/fb2read.h

+ 43 - 7
source/fb2read.cpp

@@ -41,7 +41,7 @@ bool Fb2Handler::RootHandler::doStart(const QString &name, const QXmlAttributes
         case Body   : return m_owner.setHandler(new BodyHandler(this));
         case Binary : return m_owner.setHandler(new BinaryHandler(this, attributes));
     }
-    qCritical() << QObject::tr("Unknown XML tag: ") << name;
+    qCritical() << QObject::tr("Unknown XML tag: %1").arg(name);
     return false;
 }
 
@@ -79,6 +79,9 @@ Fb2Handler::BodyHandler::KeywordHash::KeywordHash()
     insert("p",       Paragraph);
     insert("section", Section);
     insert("title",   Title);
+    insert("poem",    Poem);
+    insert("stanza",  Stanza);
+    insert("v",       Verse);
 }
 
 Fb2Handler::BodyHandler::Keyword Fb2Handler::BodyHandler::toKeyword(const QString &name)
@@ -88,6 +91,13 @@ Fb2Handler::BodyHandler::Keyword Fb2Handler::BodyHandler::toKeyword(const QStrin
     return i == map.end() ? None : i.value();
 }
 
+/*
+Keyword m_keyword;
+QTextBlockFormat m_blockFormat;
+QTextCharFormat m_charFormat;
+QTextFrame * m_frame;
+*/
+
 bool Fb2Handler::BodyHandler::doStart(const QString &name, const QXmlAttributes &attributes)
 {
     switch (toKeyword(name)) {
@@ -97,14 +107,29 @@ bool Fb2Handler::BodyHandler::doStart(const QString &name, const QXmlAttributes
             if (!image.isEmpty()) m_cursor.insertImage(image);
         } break;
         case Paragraph: {
-            m_cursor.insertBlock();
+            QTextBlockFormat blockFormat = m_cursor.blockFormat();
+            blockFormat.setTopMargin(4);
+            blockFormat.setBottomMargin(4);
+            m_cursor.setBlockFormat(blockFormat);
+            if (m_empty) {
+                m_cursor.setBlockFormat(blockFormat);
+                m_empty = false;
+            } else {
+                m_cursor.insertBlock(blockFormat);
+            }
+        } break;
+        case Poem: {
+
         } break;
         case Section: {
             m_frames << m_cursor.currentFrame();
-            QTextFrameFormat format;
-            format.setBorder(1);
-            format.setPadding(8);
-            m_cursor.insertFrame(format);
+            QTextFrameFormat frameFormat;
+            frameFormat.setBorder(1);
+            frameFormat.setPadding(8);
+            frameFormat.setTopMargin(4);
+            frameFormat.setBottomMargin(4);
+            m_cursor.insertFrame(frameFormat);
+            m_empty = true;
         } break;
     }
     return true;
@@ -176,6 +201,7 @@ Fb2Handler::Fb2Handler(QTextDocument & document)
     m_cursor.beginEditBlock();
     document.clear();
     m_cursor.movePosition(QTextCursor::Start);
+
 }
 
 Fb2Handler::~Fb2Handler()
@@ -201,9 +227,19 @@ bool Fb2Handler::startElement(const QString & namespaceURI, const QString & loca
     }
 }
 
+static bool isWhiteSpace(const QString &str)
+{
+    int size = str.size();
+    for (int i = 0; i < size; i++) if (str[i] != ' ') return false;
+    return true;
+}
+
 bool Fb2Handler::characters(const QString &str)
 {
-    return m_handler && m_handler->doText(str);
+    QString s = str;
+    s.replace(QRegExp("[\t\n\v\f\r]"), " ");
+    if (isWhiteSpace(s)) return true;
+    return m_handler && m_handler->doText(s);
 }
 
 Fb2Handler::ContentHandler * Fb2Handler::doDelete(Fb2Handler::ContentHandler * handler)

+ 9 - 1
source/fb2read.h

@@ -4,6 +4,9 @@
 #include <QXmlDefaultHandler>
 #include <QTextCursor>
 #include <QStringList>
+#include <QTextFrameFormat>
+#include <QTextBlockFormat>
+#include <QTextCharFormat>
 
 QT_BEGIN_NAMESPACE
 class QTextEdit;
@@ -69,7 +72,7 @@ private:
     class BodyHandler : public ContentHandler
     {
     public:
-        BodyHandler(ContentHandler * parent) : ContentHandler(parent) {}
+        BodyHandler(ContentHandler * parent) : ContentHandler(parent), m_empty(true) {}
         virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
         virtual bool doText(const QString &text);
         virtual bool doEnd(const QString &name, bool & exit);
@@ -81,11 +84,16 @@ private:
             Paragraph,
             Section,
             Title,
+            Poem,
+            Stanza,
+            Verse,
+
         };
         class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
         static Keyword toKeyword(const QString & name);
     private:
         QList<QTextFrame*> m_frames;
+        bool m_empty;
     };
 
     class BinaryHandler : public ContentHandler