Browse Source

Modify class Fb2Scintilla

Kandrashin Denis 13 years ago
parent
commit
d4b894e689
8 changed files with 74 additions and 32 deletions
  1. 34 7
      source/fb2code.cpp
  2. 1 0
      source/fb2code.h
  3. 13 8
      source/fb2main.cpp
  4. 2 3
      source/fb2main.h
  5. 9 6
      source/fb2save.cpp
  6. 3 2
      source/fb2save.h
  7. 11 6
      source/fb2view.cpp
  8. 1 0
      source/fb2view.h

+ 34 - 7
source/fb2code.cpp

@@ -16,34 +16,56 @@ Fb2Scintilla::Fb2Scintilla(QWidget *parent) :
     setUtf8(true);
     setCaretLineVisible(true);
     setCaretLineBackgroundColor(QColor("gainsboro"));
-    setWrapMode(QsciScintilla::WrapWord);
-
-    setEolMode(QsciScintilla::EolWindows);
+//    setWrapMode(QsciScintilla::WrapWord);
 
     setAutoIndent(true);
     setIndentationGuides(true);
 
+    //setup autocompletion
     setAutoCompletionSource(QsciScintilla::AcsAll);
     setAutoCompletionCaseSensitivity(true);
     setAutoCompletionReplaceWord(true);
     setAutoCompletionShowSingle(true);
     setAutoCompletionThreshold(2);
 
+    //setup margins
     setMarginsBackgroundColor(QColor("gainsboro"));
     setMarginLineNumbers(0, true);
     setFolding(QsciScintilla::BoxedFoldStyle, 1);
 
+    //setup brace matching
     setBraceMatching(QsciScintilla::SloppyBraceMatch);
     setMatchedBraceBackgroundColor(Qt::yellow);
     setUnmatchedBraceForegroundColor(Qt::blue);
 
-    QFont font("Courier", 10);
-    font.setStyleHint(QFont::TypeWriter);
+//    this->setFolding(QsciScintilla::CircledTreeFoldStyle, 1);
+    this->setIndentation(true,4);
+    this->setAutoIndent(true);
+
+    //setup end-of-line mode
+    #if defined Q_WS_X11
+    this->setEolMode(QsciScintilla::EolUnix);
+    #elif defined Q_WS_WIN
+    this->setEolMode(QsciScintilla::EolWindows);
+    #elif defined Q_WS_MAC
+    this->setEolMode(QsciScintilla::EolMac);
+    #endif
+
+    //setup auto-indentation
+    this->setAutoIndent(true);
+    this->setIndentationGuides(true);
+    this->setIndentationsUseTabs(false);
+    this->setIndentationWidth(2);
 
     QsciLexerXML * lexer = new QsciLexerXML;
-    lexer->setFont(font, -1);
+    lexer->setFoldPreprocessor(true);
+
+    #ifdef Q_WS_WIN
+    lexer->setFont(QFont("Courier New", 8));
+    #else
+    lexer->setFont(QFont("Monospace", 8));
+    #endif
 
-    setBraceMatching(QsciScintilla::SloppyBraceMatch);
     setLexer(lexer);
 
     connect(this, SIGNAL(linesChanged()), SLOT(linesChanged()));
@@ -55,3 +77,8 @@ void Fb2Scintilla::linesChanged()
     setMarginWidth(0, width);
 }
 
+void Fb2Scintilla::load(const QByteArray &array)
+{
+    SendScintilla(SCI_SETTEXT, array.constData());
+    SendScintilla(SCI_EMPTYUNDOBUFFER);
+}

+ 1 - 0
source/fb2code.h

@@ -8,6 +8,7 @@ class Fb2Scintilla : public QsciScintilla
     Q_OBJECT
 public:
     explicit Fb2Scintilla(QWidget *parent = 0);
+    void load(const QByteArray &array);
     
 signals:
 

+ 13 - 8
source/fb2main.cpp

@@ -17,7 +17,6 @@ Fb2MainWindow::Fb2MainWindow()
 {
     init();
     setCurrentFile();
-    textEdit = new Fb2WebView(this);
     viewText();
     textEdit->load(":blank.fb2");
 }
@@ -27,7 +26,6 @@ Fb2MainWindow::Fb2MainWindow(const QString &filename, ViewMode mode)
     init();
     setCurrentFile(filename);
     if (mode == FB2) {
-        textEdit = new Fb2WebView(this);
         viewText();
         textEdit->load(filename);
     } else {
@@ -602,9 +600,16 @@ void Fb2MainWindow::checkScintillaUndo()
 
 void Fb2MainWindow::viewCode()
 {
-    if (centralWidget() == codeEdit) return;
-    QString xml;
-    if (textEdit) textEdit->save(&xml);
+    if (codeEdit && centralWidget() == codeEdit) return;
+
+    bool load = false;
+    QByteArray xml;
+    QList<int> folds;
+    if (textEdit) {
+        textEdit->save(&xml, &folds);
+        load = true;
+    }
+
     FB2DELETE(textEdit);
     FB2DELETE(dockTree);
     FB2DELETE(headTree);
@@ -612,7 +617,7 @@ void Fb2MainWindow::viewCode()
     if (!codeEdit) {
         codeEdit = new Fb2Scintilla;
     }
-    codeEdit->setText(xml);
+    if (load) codeEdit->load(xml);
     setCentralWidget(codeEdit);
     codeEdit->setFocus();
 
@@ -651,7 +656,7 @@ void Fb2MainWindow::viewCode()
 
 void Fb2MainWindow::viewText()
 {
-    if (centralWidget() == textEdit) return;
+    if (textEdit && centralWidget() == textEdit) return;
     QString xml;
     if (codeEdit) xml = codeEdit->text();
     FB2DELETE(codeEdit);
@@ -775,7 +780,7 @@ void Fb2MainWindow::viewText()
 
 void Fb2MainWindow::viewHead()
 {
-    if (centralWidget() == headTree) return;
+    if (headTree && centralWidget() == headTree) return;
 
     QString xml;
     if (codeEdit) xml = codeEdit->text();

+ 2 - 3
source/fb2main.h

@@ -14,8 +14,7 @@ class QTreeView;
 class QWebInspector;
 QT_END_NAMESPACE
 
-class QsciScintilla;
-class Fb2MainDocument;
+class Fb2Scintilla;
 class Fb2WebView;
 
 class Fb2MainWindow : public QMainWindow
@@ -83,7 +82,7 @@ private:
     QTextEdit *noteEdit;
     QTextEdit *messageEdit;
     QDockWidget *dockTree;
-    QsciScintilla *codeEdit;
+    Fb2Scintilla *codeEdit;
     QTreeView *treeView;
     QString curFile;
     bool isUntitled;

+ 9 - 6
source/fb2save.cpp

@@ -8,15 +8,17 @@
 //  Fb2SaveWriter
 //---------------------------------------------------------------------------
 
-Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QIODevice *device)
-    : QXmlStreamWriter(device)
+Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QByteArray *array, QList<int> *folds)
+    : QXmlStreamWriter(array)
+    , m_folds(folds)
     , m_view(view)
 {
     Init();
 }
 
-Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QByteArray *array)
-    : QXmlStreamWriter(array)
+Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QIODevice *device)
+    : QXmlStreamWriter(device)
+    , m_folds(0)
     , m_view(view)
 {
     Init();
@@ -24,6 +26,7 @@ Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QByteArray *array)
 
 Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QString *string)
     : QXmlStreamWriter(string)
+    , m_folds(0)
     , m_view(view)
 {
     Init();
@@ -256,9 +259,9 @@ Fb2SaveHandler::Fb2SaveHandler(Fb2WebView &view, QIODevice *device)
 {
 }
 
-Fb2SaveHandler::Fb2SaveHandler(Fb2WebView &view, QByteArray *array)
+Fb2SaveHandler::Fb2SaveHandler(Fb2WebView &view, QByteArray *array, QList<int> *folds)
     : Fb2XmlHandler()
-    , m_writer(view, array)
+    , m_writer(view, array, folds)
 {
 }
 

+ 3 - 2
source/fb2save.h

@@ -16,8 +16,8 @@ class Fb2WebView;
 class Fb2SaveWriter : public QXmlStreamWriter
 {
 public:
+    explicit Fb2SaveWriter(Fb2WebView &view, QByteArray *array, QList<int> *folds = 0);
     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);
@@ -26,6 +26,7 @@ public:
 private:
     void Init();
 private:
+    QList<int> *m_folds;
     Fb2WebView &m_view;
     typedef QHash<QString, QString> StringHash;
     typedef QList<QString> StringList;
@@ -36,8 +37,8 @@ private:
 class Fb2SaveHandler : public Fb2XmlHandler
 {
 public:
+    explicit Fb2SaveHandler(Fb2WebView &view, QByteArray *array, QList<int> *folds);
     explicit Fb2SaveHandler(Fb2WebView &view, QIODevice *device);
-    explicit Fb2SaveHandler(Fb2WebView &view, QByteArray *array);
 
 private:
     class BodyHandler : public NodeHandler

+ 11 - 6
source/fb2view.cpp

@@ -102,18 +102,23 @@ bool Fb2WebView::save(QIODevice *device)
     return reader.parse(source);
 }
 
-bool Fb2WebView::save(QString *string)
+bool Fb2WebView::save(QByteArray *array, QList<int> *folds)
 {
-    // Use class QByteArray instead QString
-    // to store information about encoding.
-    QByteArray data;
-    Fb2SaveHandler handler(*this, &data);
+    Fb2SaveHandler handler(*this, array, folds);
     QXmlInputSource source;
     source.setData(toBodyXml());
     XML2::HtmlReader reader;
     reader.setContentHandler(&handler);
     reader.setErrorHandler(&handler);
-    bool ok = reader.parse(source);
+    return reader.parse(source);
+}
+
+bool Fb2WebView::save(QString *string)
+{
+    // Use class QByteArray instead QString
+    // to store information about encoding.
+    QByteArray data;
+    bool ok = save(&data);
     if (ok) *string = QString::fromUtf8(data.data());
     return ok;
 }

+ 1 - 0
source/fb2view.h

@@ -59,6 +59,7 @@ public:
     explicit Fb2WebView(QWidget *parent = 0);
     virtual ~Fb2WebView();
     void load(const QString &filename, const QString &xml = QString());
+    bool save(QByteArray *array, QList<int> *folds = 0);
     bool save(QIODevice *device);
     bool save(QString *string);
     QString fileName(const QString &path);