]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiViewSource.h
50b5259ea066cf9da106f0dedb20d6f30b2310ca
[lyx.git] / src / frontends / qt4 / GuiViewSource.h
1 // -*- C++ -*-
2 /**
3  * \file GuiViewSource.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author John Levon
8  * \author Bo Peng
9  * \author Abdelrazak Younes
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef GUIVIEWSOURCE_H
15 #define GUIVIEWSOURCE_H
16
17 #include "ui_ViewSourceUi.h"
18
19 #include "controllers/Dialog.h"
20 #include "GuiView.h"
21 #include "qt_helpers.h"
22 #include "debug.h"
23
24 #include <QDockWidget>
25 #include <QWidget>
26 #include <QSyntaxHighlighter>
27 #include <QTextCharFormat>
28
29 class QTextDocument;
30
31 namespace lyx {
32 namespace frontend {
33
34 // used already twice...
35 class LaTeXHighlighter : public QSyntaxHighlighter
36 {
37 public:
38         LaTeXHighlighter(QTextDocument * parent);
39
40 protected:
41         void highlightBlock(QString const & text);
42
43 private:
44         QTextCharFormat commentFormat;
45         QTextCharFormat keywordFormat;
46         QTextCharFormat mathFormat;
47 };
48
49 class ControlViewSource;
50
51 class GuiViewSourceDialog : public QWidget, public Ui::ViewSourceUi
52 {
53         Q_OBJECT
54
55 public:
56         GuiViewSourceDialog(ControlViewSource &);
57
58 public Q_SLOTS:
59         // update content
60         void updateView();
61         ///
62         QTextDocument * document() { return document_; }
63         ///
64         void update(bool full_source);
65
66 private:
67         ///
68         ControlViewSource & controller_;        
69         ///
70         QTextDocument * document_;
71         /// LaTeX syntax highlighter
72         LaTeXHighlighter * highlighter_;
73 };
74
75 /**
76  * A controller for a read-only text browser.
77  */
78 class ControlViewSource : public Controller {
79 public:
80         ///
81         ControlViewSource(Dialog &);
82         /** \param source source code to be displayed
83          */
84         bool initialiseParams(std::string const & source);
85         ///
86         void clearParams() {}
87         ///
88         void dispatchParams() {}
89         ///
90         bool isBufferDependent() const { return true; }
91         ///
92         bool canApply() const { return true; }
93         ///
94         bool canApplyToReadOnly() const { return true; }
95
96         /// The title displayed by the dialog reflects source type.
97         docstring const title() const;
98
99         /** get the source code of selected paragraphs, or the whole document
100                 \param fullSource get full source code
101          */
102         docstring const updateContent(bool fullSource);
103         /** get the cursor position in the source code
104          */
105         std::pair<int, int> getRows() const;
106 };
107
108
109 class GuiViewSource : public QDockWidget, public Dialog
110 {
111 public:
112         GuiViewSource(GuiViewBase & parent)
113                 : QDockWidget(&parent, Qt::WindowFlags(0)), name_("view-source")
114         {
115                 ControlViewSource * c = new ControlViewSource(*this);
116                 controller_ = c;
117                 controller_->setLyXView(parent);
118                 widget_ = new GuiViewSourceDialog(*c);
119                 setWidget(widget_);
120                 setWindowTitle(widget_->windowTitle());
121                 parent.addDockWidget(Qt::BottomDockWidgetArea, this);
122         }
123         ~GuiViewSource() { delete widget_; delete controller_; }
124
125         /// Dialog inherited methods
126         //@{
127         void applyView() {}
128         void hideView() { QDockWidget::hide(); }
129         void showData(std::string const & data)
130         {
131                 controller_->initialiseParams(data);
132                 showView();
133         }
134         void showView()
135         {
136                 widget_->updateView();  // make sure its up-to-date
137                 QDockWidget::show();
138         }
139         bool isVisibleView() const { return QDockWidget::isVisible(); }
140         void checkStatus() { updateView(); }
141         void redraw() { redrawView(); }
142         void redrawView() {}
143         void updateData(std::string const & data)
144         {
145                 controller_->initialiseParams(data);
146                 updateView();
147         }
148         void updateView()
149         {
150                 widget_->updateView();
151                 QDockWidget::update();
152         }
153         bool isClosing() const { return false; }
154         void partialUpdateView(int /*id*/) {}
155         Controller & controller() { return *controller_; }
156         std::string name() const { return name_; }
157         //@}
158 private:
159         /// The encapsulated widget.
160         GuiViewSourceDialog * widget_;
161         Controller * controller_;
162         std::string name_;
163 };
164
165 } // namespace frontend
166 } // namespace lyx
167
168 #endif // GUIVIEWSOURCE_H