Răsfoiți Sursa

Rename: qsciEdit => codeEdit

Kandrashin Denis 13 ani în urmă
părinte
comite
1d3baf43f3
7 a modificat fișierele cu 94 adăugiri și 72 ștergeri
  1. 54 53
      source/fb2main.cpp
  2. 1 1
      source/fb2main.h
  3. 29 7
      source/fb2read.cpp
  4. 0 6
      source/fb2save.cpp
  5. 0 1
      source/fb2save.h
  6. 7 2
      source/fb2view.cpp
  7. 3 2
      source/fb2xml2.cpp

+ 54 - 53
source/fb2main.cpp

@@ -29,6 +29,7 @@ Fb2MainWindow::Fb2MainWindow(const QString &filename, ViewMode mode)
     init();
     setCurrentFile(filename);
     if (mode == FB2) {
+        textEdit = new Fb2WebView(this);
         viewText();
         textEdit->load(filename);
     } else {
@@ -50,7 +51,7 @@ void Fb2MainWindow::init()
 
     textEdit = NULL;
     noteEdit = NULL;
-    qsciEdit = NULL;
+    codeEdit = NULL;
     treeView = NULL;
     headTree = NULL;
     toolEdit = NULL;
@@ -104,8 +105,8 @@ bool Fb2MainWindow::loadXML(const QString &filename)
     if (!filename.isEmpty()) {
         QFile file(filename);
         if (file.open(QFile::ReadOnly | QFile::Text)) {
-            qsciEdit->clear();
-            return qsciEdit->read(&file);
+            codeEdit->clear();
+            return codeEdit->read(&file);
         }
     }
     return false;
@@ -150,7 +151,7 @@ void Fb2MainWindow::fileOpen()
             other->move(x() + 40, y() + 40);
             other->show();
         }
-    } else if (qsciEdit) {
+    } else if (codeEdit) {
         if (isUntitled && !isWindowModified()) {
             setCurrentFile(filename);
             loadXML(filename);
@@ -193,7 +194,7 @@ void Fb2MainWindow::documentWasModified()
     title += QString(" - ") += qApp->applicationName();
     setWindowTitle(title);
     setWindowModified(true);
-    if (qsciEdit) disconnect(qsciEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
+    if (codeEdit) disconnect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
     if (textEdit) disconnect(textEdit->page(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
 }
 
@@ -457,34 +458,34 @@ void Fb2MainWindow::createQsci()
     //  http://qtcoder.blogspot.com/2010/10/qscintills.html
     //  http://www.riverbankcomputing.co.uk/static/Docs/QScintilla2/classQsciScintilla.html
 
-    if (qsciEdit) return;
-    qsciEdit = new QsciScintilla;
-    qsciEdit->zoomTo(1);
-    qsciEdit->setUtf8(true);
-    qsciEdit->setCaretLineVisible(true);
-    qsciEdit->setCaretLineBackgroundColor(QColor("gainsboro"));
-    qsciEdit->setWrapMode(QsciScintilla::WrapWord);
+    if (codeEdit) return;
+    codeEdit = new QsciScintilla;
+    codeEdit->zoomTo(1);
+    codeEdit->setUtf8(true);
+    codeEdit->setCaretLineVisible(true);
+    codeEdit->setCaretLineBackgroundColor(QColor("gainsboro"));
+    codeEdit->setWrapMode(QsciScintilla::WrapWord);
 
-    qsciEdit->setEolMode(QsciScintilla::EolWindows);
+    codeEdit->setEolMode(QsciScintilla::EolWindows);
 
-    qsciEdit->setAutoIndent(true);
-    qsciEdit->setIndentationGuides(true);
+    codeEdit->setAutoIndent(true);
+    codeEdit->setIndentationGuides(true);
 
-    qsciEdit->setAutoCompletionSource(QsciScintilla::AcsAll);
-    qsciEdit->setAutoCompletionCaseSensitivity(true);
-    qsciEdit->setAutoCompletionReplaceWord(true);
-    qsciEdit->setAutoCompletionShowSingle(true);
-    qsciEdit->setAutoCompletionThreshold(2);
+    codeEdit->setAutoCompletionSource(QsciScintilla::AcsAll);
+    codeEdit->setAutoCompletionCaseSensitivity(true);
+    codeEdit->setAutoCompletionReplaceWord(true);
+    codeEdit->setAutoCompletionShowSingle(true);
+    codeEdit->setAutoCompletionThreshold(2);
 
-    qsciEdit->setMarginsBackgroundColor(QColor("gainsboro"));
-    qsciEdit->setMarginWidth(0, 0);
-    qsciEdit->setMarginLineNumbers(1, true);
-    qsciEdit->setMarginWidth(1, QString("10000"));
-    qsciEdit->setFolding(QsciScintilla::BoxedFoldStyle, 2);
+    codeEdit->setMarginsBackgroundColor(QColor("gainsboro"));
+    codeEdit->setMarginWidth(0, 0);
+    codeEdit->setMarginLineNumbers(1, true);
+    codeEdit->setMarginWidth(1, QString("10000"));
+    codeEdit->setFolding(QsciScintilla::BoxedFoldStyle, 2);
 
-    qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
-    qsciEdit->setMatchedBraceBackgroundColor(Qt::yellow);
-    qsciEdit->setUnmatchedBraceForegroundColor(Qt::blue);
+    codeEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
+    codeEdit->setMatchedBraceBackgroundColor(Qt::yellow);
+    codeEdit->setUnmatchedBraceForegroundColor(Qt::blue);
 
     QFont font("Courier", 10);
     font.setStyleHint(QFont::TypeWriter);
@@ -492,11 +493,11 @@ void Fb2MainWindow::createQsci()
     QsciLexerXML * lexer = new QsciLexerXML;
     lexer->setFont(font, -1);
 
-    qsciEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
-    qsciEdit->setLexer(lexer);
+    codeEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch);
+    codeEdit->setLexer(lexer);
 
-    setCentralWidget(qsciEdit);
-    qsciEdit->setFocus();
+    setCentralWidget(codeEdit);
+    codeEdit->setFocus();
 }
 
 void Fb2MainWindow::createStatusBar()
@@ -592,26 +593,26 @@ Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
 
 void Fb2MainWindow::zoomOrig()
 {
-    if (qsciEdit) qsciEdit->zoomTo(1);
+    if (codeEdit) codeEdit->zoomTo(1);
 }
 
 void Fb2MainWindow::checkScintillaUndo()
 {
-    if (!qsciEdit) return;
-    actionUndo->setEnabled(qsciEdit->isUndoAvailable());
-    actionRedo->setEnabled(qsciEdit->isRedoAvailable());
+    if (!codeEdit) return;
+    actionUndo->setEnabled(codeEdit->isUndoAvailable());
+    actionRedo->setEnabled(codeEdit->isRedoAvailable());
 }
 
 void Fb2MainWindow::viewQsci()
 {
-    if (centralWidget() == qsciEdit) return;
+    if (centralWidget() == codeEdit) return;
     QString xml;
     if (textEdit) textEdit->save(&xml);
     FB2DELETE(textEdit);
     FB2DELETE(dockTree);
     FB2DELETE(headTree);
     createQsci();
-    qsciEdit->setText(xml);
+    codeEdit->setText(xml);
 
     FB2DELETE(toolEdit);
     QToolBar *tool = toolEdit = addToolBar(tr("Edit"));
@@ -628,21 +629,21 @@ void Fb2MainWindow::viewQsci()
     tool->addAction(actionZoomOrig);
     tool->setMovable(false);
 
-    connect(qsciEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
-    connect(qsciEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
+    connect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
+    connect(codeEdit, SIGNAL(textChanged()), this, SLOT(checkScintillaUndo()));
 
-    connect(qsciEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
-    connect(qsciEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
+    connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
+    connect(codeEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
 
-    connect(actionUndo, SIGNAL(triggered()), qsciEdit, SLOT(undo()));
-    connect(actionRedo, SIGNAL(triggered()), qsciEdit, SLOT(redo()));
+    connect(actionUndo, SIGNAL(triggered()), codeEdit, SLOT(undo()));
+    connect(actionRedo, SIGNAL(triggered()), codeEdit, SLOT(redo()));
 
-    connect(actionCut, SIGNAL(triggered()), qsciEdit, SLOT(cut()));
-    connect(actionCopy, SIGNAL(triggered()), qsciEdit, SLOT(copy()));
-    connect(actionPaste, SIGNAL(triggered()), qsciEdit, SLOT(paste()));
+    connect(actionCut, SIGNAL(triggered()), codeEdit, SLOT(cut()));
+    connect(actionCopy, SIGNAL(triggered()), codeEdit, SLOT(copy()));
+    connect(actionPaste, SIGNAL(triggered()), codeEdit, SLOT(paste()));
 
-    connect(actionZoomIn, SIGNAL(triggered()), qsciEdit, SLOT(zoomIn()));
-    connect(actionZoomOut, SIGNAL(triggered()), qsciEdit, SLOT(zoomOut()));
+    connect(actionZoomIn, SIGNAL(triggered()), codeEdit, SLOT(zoomIn()));
+    connect(actionZoomOut, SIGNAL(triggered()), codeEdit, SLOT(zoomOut()));
     connect(actionZoomOrig, SIGNAL(triggered()), this, SLOT(zoomOrig()));
 }
 
@@ -650,8 +651,8 @@ void Fb2MainWindow::viewText()
 {
     if (centralWidget() == textEdit) return;
     QString xml;
-    if (qsciEdit) xml = qsciEdit->text();
-    FB2DELETE(qsciEdit);
+    if (codeEdit) xml = codeEdit->text();
+    FB2DELETE(codeEdit);
     FB2DELETE(headTree);
     if (!textEdit) {
         textEdit = new Fb2WebView(this);
@@ -773,10 +774,10 @@ void Fb2MainWindow::viewHead()
     if (centralWidget() == headTree) return;
 
     QString xml;
-    if (qsciEdit) xml = qsciEdit->text();
+    if (codeEdit) xml = codeEdit->text();
 
     FB2DELETE(dockTree);
-    FB2DELETE(qsciEdit);
+    FB2DELETE(codeEdit);
     FB2DELETE(toolEdit);
 
     if (!headTree) {

+ 1 - 1
source/fb2main.h

@@ -83,7 +83,7 @@ private:
     QTextEdit *noteEdit;
     QTextEdit *messageEdit;
     QDockWidget *dockTree;
-    QsciScintilla *qsciEdit;
+    QsciScintilla *codeEdit;
     QTreeView *treeView;
     QString curFile;
     bool isUntitled;

+ 29 - 7
source/fb2read.cpp

@@ -35,25 +35,45 @@ void Fb2ReadThread::run()
     if (parse()) emit html(m_filename, m_html);
 }
 
+#ifdef _WIN32
+
 bool Fb2ReadThread::parse()
 {
     QXmlStreamWriter writer(&m_html);
     Fb2ReadHandler handler(*this, writer);
-    #ifdef _WIN32
-        QXmlSimpleReader reader;
-    #else
-        XML2::XmlReader reader;
-    #endif
+    QXmlSimpleReader reader;
     reader.setContentHandler(&handler);
     reader.setErrorHandler(&handler);
+    QXmlInputSource source;
     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;
         }
-        QXmlInputSource source(&file);
-        return reader.parse(source);
+        source.setData(file.readAll());
+    } else {
+        source.setData(m_xml);
+    }
+    return reader.parse(source);
+}
+
+#else
+
+bool Fb2ReadThread::parse()
+{
+    QXmlStreamWriter writer(&m_html);
+    Fb2ReadHandler handler(*this, writer);
+    XML2::XmlReader reader;
+    reader.setContentHandler(&handler);
+    reader.setErrorHandler(&handler);
+    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;
+        }
+        return reader.parse(file);
     } else {
         QXmlInputSource source;
         source.setData(m_xml);
@@ -61,6 +81,8 @@ bool Fb2ReadThread::parse()
     }
 }
 
+#endif
+
 //---------------------------------------------------------------------------
 //  Fb2ReadHandler::RootHandler
 //---------------------------------------------------------------------------

+ 0 - 6
source/fb2save.cpp

@@ -263,12 +263,6 @@ Fb2SaveHandler::Fb2SaveHandler(Fb2WebView &view, QByteArray *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);

+ 0 - 1
source/fb2save.h

@@ -38,7 +38,6 @@ class Fb2SaveHandler : public Fb2XmlHandler
 public:
     explicit Fb2SaveHandler(Fb2WebView &view, QIODevice *device);
     explicit Fb2SaveHandler(Fb2WebView &view, QByteArray *array);
-    explicit Fb2SaveHandler(Fb2WebView &view, QString *string);
 
 private:
     class BodyHandler : public NodeHandler

+ 7 - 2
source/fb2view.cpp

@@ -103,13 +103,18 @@ bool Fb2WebView::save(QIODevice *device)
 
 bool Fb2WebView::save(QString *string)
 {
-    Fb2SaveHandler handler(*this, string);
+    // Use class QByteArray instead QString
+    // to store information about encoding.
+    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)

+ 3 - 2
source/fb2xml2.cpp

@@ -4,6 +4,7 @@
 #include <libxml/parser.h>
 #include <libxml/HTMLparser.h>
 #include <libxml/xmlreader.h>
+#include <QtDebug>
 
 namespace XML2 {
 
@@ -86,7 +87,7 @@ void HtmlReaderPrivate::parse(const QXmlInputSource* input)
     handler.ignorableWhitespace   = &HtmlReaderPrivate::ignorableWhitespace;
     handler.internalSubset        = &HtmlReaderPrivate::internalSubset;
 
-    this->context = htmlCreatePushParserCtxt(&handler, this, arr.data(), arr.size(), "", XML_CHAR_ENCODING_UTF8);
+    this->context = htmlCreatePushParserCtxt(&handler, this, arr.constData(), arr.size(), "", XML_CHAR_ENCODING_UTF8);
     htmlParseChunk(this->context, NULL, 0, 1);
     htmlFreeParserCtxt(this->context);
     xmlCleanupParser();
@@ -448,7 +449,7 @@ bool XmlReaderPrivate::parse(const QXmlInputSource* input)
 {
     QByteArray arr = input->data().toUtf8();
     int options = XML_PARSE_RECOVER | XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET;
-    m_reader = xmlReaderForMemory(arr.data(), arr.size(), NULL, NULL, options);
+    m_reader = xmlReaderForMemory(arr.constData(), arr.size(), NULL, NULL, options);
     if (!m_reader) return false;
     xmlTextReaderSetErrorHandler(m_reader, &XmlReaderPrivate::onError, this);
     while (xmlTextReaderRead(m_reader) == 1) process(m_reader);