From 2f3ac2d20f44de8b84f9ccceb3d419c4881c3e37 Mon Sep 17 00:00:00 2001 From: Bo Peng Date: Sat, 15 Apr 2006 14:13:41 +0000 Subject: [PATCH] Initial Qt4 support for view-source feature (r13610), from Bo Peng (ben.bob@gmail.com) * add src/frontend/qt4/QViewSource.h/C, QViewSourceDialog.h/C, ui/QViewSourceUi.ui * modify corresponding qt4/Makefile.am Makefile.dialogs, Dialogs.C git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@13682 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/qt4/Dialogs.C | 8 +- src/frontends/qt4/Makefile.am | 1 + src/frontends/qt4/Makefile.dialogs | 3 + src/frontends/qt4/QViewSource.C | 110 ++++++++++++++++++++++++++ src/frontends/qt4/QViewSource.h | 68 ++++++++++++++++ src/frontends/qt4/QViewSourceDialog.C | 40 ++++++++++ src/frontends/qt4/QViewSourceDialog.h | 36 +++++++++ src/frontends/qt4/ui/QViewSourceUi.ui | 77 ++++++++++++++++++ 8 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 src/frontends/qt4/QViewSource.C create mode 100644 src/frontends/qt4/QViewSource.h create mode 100644 src/frontends/qt4/QViewSourceDialog.C create mode 100644 src/frontends/qt4/QViewSourceDialog.h create mode 100644 src/frontends/qt4/ui/QViewSourceUi.ui diff --git a/src/frontends/qt4/Dialogs.C b/src/frontends/qt4/Dialogs.C index 128fc08a19..11a805c186 100644 --- a/src/frontends/qt4/Dialogs.C +++ b/src/frontends/qt4/Dialogs.C @@ -27,6 +27,7 @@ #include "ControlGraphics.h" #include "ControlInclude.h" #include "ControlLog.h" +#include "ControlViewSource.h" #include "ControlMath.h" #include "ControlNote.h" #include "ControlParagraph.h" @@ -67,6 +68,7 @@ #include "QInclude.h" #include "QIndex.h" #include "QLog.h" +#include "QViewSource.h" #include "QMath.h" #include "QNote.h" #include "QParagraph.h" @@ -104,7 +106,7 @@ namespace { char const * const dialognames[] = { "aboutlyx", "bibitem", "bibtex", "box", "branch", "changes", "character", "citation", "document", "errorlist", "ert", "external", "file", -"findreplace", "float", "graphics", "include", "index", "label", "log", +"findreplace", "float", "graphics", "include", "index", "label", "log", "view-source", "mathpanel", "mathdelimiter", "mathmatrix", "note", "paragraph", "preamble", "prefs", "print", "ref", "sendto", "spellchecker","tabular", "tabularcreate", @@ -245,6 +247,10 @@ Dialogs::DialogPtr Dialogs::build(string const & name) dialog->setController(new ControlLog(*dialog)); dialog->setView(new QLog(*dialog)); dialog->bc().bp(new OkCancelPolicy); + } else if (name == "view-source") { + dialog->setController(new ControlViewSource(*dialog)); + dialog->setView(new QViewSource(*dialog)); + dialog->bc().bp(new OkCancelPolicy); } else if (name == "mathpanel") { dialog->setController(new ControlMath(*dialog)); dialog->setView(new QMath(*dialog)); diff --git a/src/frontends/qt4/Makefile.am b/src/frontends/qt4/Makefile.am index 66fbaff335..6599ad0599 100644 --- a/src/frontends/qt4/Makefile.am +++ b/src/frontends/qt4/Makefile.am @@ -50,6 +50,7 @@ libqt4_la_SOURCES = \ QLAction.C QLAction.h \ QLImage.C QLImage.h \ QLog.C QLog.h \ + QViewSource.C QViewSource.h \ QLPainter.C QLPainter.h \ QLyXKeySym.C QLyXKeySym.h \ QMath.C QMath.h \ diff --git a/src/frontends/qt4/Makefile.dialogs b/src/frontends/qt4/Makefile.dialogs index 58edbe546a..07ab73b28d 100644 --- a/src/frontends/qt4/Makefile.dialogs +++ b/src/frontends/qt4/Makefile.dialogs @@ -33,6 +33,7 @@ UIFILES = \ QIncludeUi.ui \ QIndexUi.ui \ QLogUi.ui \ + QViewSourceUi.ui \ QMathUi.ui \ QMathMatrixUi.ui \ QNoteUi.ui \ @@ -101,6 +102,8 @@ MOCFILES = \ QIndexDialog.C QIndexDialog.h \ QLAction.C QLAction.h \ QLogDialog.C QLogDialog.h \ + QViewSourceDialog.C QViewSourceDialog.h \ + QViewSource.C QViewSource.h \ QLMenubar.C QLMenubar.h \ QLPopupMenu.C QLPopupMenu.h \ QLPrintDialog.C QLPrintDialog.h \ diff --git a/src/frontends/qt4/QViewSource.C b/src/frontends/qt4/QViewSource.C new file mode 100644 index 0000000000..f492e67854 --- /dev/null +++ b/src/frontends/qt4/QViewSource.C @@ -0,0 +1,110 @@ +/** + * \file QViewSource.C + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author John Levon + * \author Bo Peng + * + * Full author contact details are available in file CREDITS. + */ + +#include + +#include "QViewSource.h" +#include "QViewSourceDialog.h" +#include "qt_helpers.h" +#include "lyx_gui.h" + +#include "controllers/ControlViewSource.h" + +#include + +#include +#include + +namespace lyx { +namespace frontend { + +typedef QController > base_class; + + +QViewSource::QViewSource(Dialog & parent) + : base_class(parent, "") +{} + + +latexHighlighter::latexHighlighter(QTextDocument * parent) : + QSyntaxHighlighter(parent) +{ + keywordFormat.setForeground(Qt::darkBlue); + keywordFormat.setFontWeight(QFont::Bold); + commentFormat.setForeground(Qt::gray); + mathFormat.setForeground(Qt::red); +} + + +void latexHighlighter::highlightBlock(QString const & text) +{ + // comment + QRegExp exprComment("^%.*$"); + int index = text.indexOf(exprComment); + while (index >= 0) { + int length = exprComment.matchedLength(); + setFormat(index, length, commentFormat); + index = text.indexOf(exprComment, index + length); + } + // $ $ + QRegExp exprMath("\\$[^\\$]*\\$"); + index = text.indexOf(exprMath); + while (index >= 0) { + int length = exprMath.matchedLength(); + setFormat(index, length, mathFormat); + index = text.indexOf(exprMath, index + length); + } + // [ ] + QRegExp exprDispMath("\\[[^\\]]*\\]"); + index = text.indexOf(exprDispMath); + while (index >= 0) { + int length = exprDispMath.matchedLength(); + setFormat(index, length, mathFormat); + index = text.indexOf(exprDispMath, index + length); + } + // \whatever + QRegExp exprKeyword("\\\\[A-Za-z]+"); + index = text.indexOf(exprKeyword); + while (index >= 0) { + int length = exprKeyword.matchedLength(); + setFormat(index, length, keywordFormat); + index = text.indexOf(exprKeyword, index + length); + } +} + + +void QViewSource::build_dialog() +{ + dialog_.reset(new QViewSourceDialog(this)); + // set syntex highlighting + highlighter = new latexHighlighter(dialog_->viewSourceTV->document()); + // + dialog_->viewSourceTV->setReadOnly(true); + dialog_->viewSourceTV->setTextFormat(Qt::PlainText); + // this is personal. I think source code should be in fixed-size font + QFont font(toqstr(lyx_gui::typewriter_font_name())); + font.setFixedPitch(true); + font.setStyleHint(QFont::TypeWriter); + dialog_->viewSourceTV->setFont(font); + // again, personal taste + dialog_->viewSourceTV->setWordWrapMode(QTextOption::NoWrap); +} + + +void QViewSource::update_contents() +{ + setTitle(controller().title()); + dialog_->viewSourceTV->setText(toqstr(controller().updateContent())); +} + + +} // namespace frontend +} // namespace lyx diff --git a/src/frontends/qt4/QViewSource.h b/src/frontends/qt4/QViewSource.h new file mode 100644 index 0000000000..108fdbf403 --- /dev/null +++ b/src/frontends/qt4/QViewSource.h @@ -0,0 +1,68 @@ +// -*- C++ -*- +/** + * \file QViewSource.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author John Levon + * \author Bo Peng + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef QVIEWSOURCE_H +#define QVIEWSOURCE_H + +#include "QDialogView.h" +#include + +namespace lyx { +namespace frontend { + +class ControlViewSource; +class QViewSourceDialog; +class latexHighlighter; + +/// +class QViewSource + : public QController > +{ +public: + /// + friend class QViewSourceDialog; + /// + QViewSource(Dialog &); +private: + /// Apply changes + virtual void apply() {} + /// update + virtual void update_contents(); + /// build the dialog + virtual void build_dialog(); + /// latex syntax highlighter + latexHighlighter * highlighter; +}; + + +/// +class latexHighlighter : public QSyntaxHighlighter +{ + Q_OBJECT + +public: + latexHighlighter(QTextDocument * parent); + +protected: + void highlightBlock(QString const & text); + +private: + QTextCharFormat commentFormat; + QTextCharFormat keywordFormat; + QTextCharFormat mathFormat; +}; + + +} // namespace frontend +} // namespace lyx + +#endif // QVIEWSOURCE_H diff --git a/src/frontends/qt4/QViewSourceDialog.C b/src/frontends/qt4/QViewSourceDialog.C new file mode 100644 index 0000000000..5ddd43fb26 --- /dev/null +++ b/src/frontends/qt4/QViewSourceDialog.C @@ -0,0 +1,40 @@ +/** + * \file QViewSourceDialog.C + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author John Levon + * \author Bo Peng + * + * Full author contact details are available in file CREDITS. + */ + +#include + +#include "QViewSourceDialog.h" +#include "QViewSource.h" + +#include + + +namespace lyx { +namespace frontend { + +QViewSourceDialog::QViewSourceDialog(QViewSource * form) + : form_(form) +{ + setupUi(this); + + connect(closePB, SIGNAL(clicked()), form, SLOT(slotClose())); +} + + +void QViewSourceDialog::closeEvent(QCloseEvent * e) +{ + form_->slotWMHide(); + e->accept(); +} + + +} // namespace frontend +} // namespace lyx diff --git a/src/frontends/qt4/QViewSourceDialog.h b/src/frontends/qt4/QViewSourceDialog.h new file mode 100644 index 0000000000..1c332e5b52 --- /dev/null +++ b/src/frontends/qt4/QViewSourceDialog.h @@ -0,0 +1,36 @@ +// -*- C++ -*- +/** + * \file QViewSourceDialog.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author John Levon + * \author Bo Peng + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef QVIEWSOURCEDIALOG_H +#define QVIEWSOURCEDIALOG_H + +#include "ui/QViewSourceUi.h" + +namespace lyx { +namespace frontend { + +class QViewSource; + +class QViewSourceDialog : public QDialog, public Ui::QViewSourceUi { + Q_OBJECT +public: + QViewSourceDialog(QViewSource * form); +protected: + virtual void closeEvent(QCloseEvent * e); +private: + QViewSource * form_; +}; + +} // namespace frontend +} // namespace lyx + +#endif // QVIEWSOURCEDIALOG_H diff --git a/src/frontends/qt4/ui/QViewSourceUi.ui b/src/frontends/qt4/ui/QViewSourceUi.ui new file mode 100644 index 0000000000..ab741362dc --- /dev/null +++ b/src/frontends/qt4/ui/QViewSourceUi.ui @@ -0,0 +1,77 @@ + + + + + QViewSourceUi + + + + 0 + 0 + 420 + 328 + + + + + + + true + + + + 11 + + + 6 + + + + + <html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:Sans Serif; font-size:13pt; font-weight:400; font-style:normal; text-decoration:none;"><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"></p></body></html> + + + + + + + 0 + + + 6 + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 20 + 20 + + + + + + + + &Close + + + + + + + + qPixmapFromMimeSource + + viewSourceTV + closePB + + + +