]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiViewSource.h
cd96789be654ec54fd26680b2fb7d11bc06d9c14
[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 "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 class ControlViewSource;
35 class LaTeXHighlighter;
36
37 class GuiViewSourceDialog : public QWidget, public Ui::ViewSourceUi
38 {
39         Q_OBJECT
40
41 public:
42         GuiViewSourceDialog(ControlViewSource &);
43
44 public Q_SLOTS:
45         // update content
46         void updateView();
47         ///
48         QTextDocument * document() { return document_; }
49         ///
50         void update(bool full_source);
51
52 private:
53         ///
54         ControlViewSource & controller_;        
55         ///
56         QTextDocument * document_;
57         /// LaTeX syntax highlighter
58         LaTeXHighlighter * highlighter_;
59 };
60
61 /**
62  * A controller for a read-only text browser.
63  */
64 class ControlViewSource : public Controller {
65 public:
66         ///
67         ControlViewSource(Dialog &);
68         /** \param source source code to be displayed
69          */
70         bool initialiseParams(std::string const & source);
71         ///
72         void clearParams() {}
73         ///
74         void dispatchParams() {}
75         ///
76         bool isBufferDependent() const { return true; }
77         ///
78         bool canApply() const { return true; }
79         ///
80         bool canApplyToReadOnly() const { return true; }
81
82         /// The title displayed by the dialog reflects source type.
83         docstring const title() const;
84
85         /** get the source code of selected paragraphs, or the whole document
86                 \param fullSource get full source code
87          */
88         docstring const updateContent(bool fullSource);
89         /** get the cursor position in the source code
90          */
91         std::pair<int, int> getRows() const;
92 };
93
94
95 class GuiViewSource : public QDockWidget, public Dialog
96 {
97 public:
98         GuiViewSource(GuiViewBase & parent)
99                 : QDockWidget(&parent, Qt::WindowFlags(0)), name_("view-source")
100         {
101                 ControlViewSource * c = new ControlViewSource(*this);
102                 controller_ = c;
103                 controller_->setLyXView(parent);
104                 widget_ = new GuiViewSourceDialog(*c);
105                 setWidget(widget_);
106                 setWindowTitle(widget_->windowTitle());
107                 parent.addDockWidget(Qt::BottomDockWidgetArea, this);
108         }
109         ~GuiViewSource() { delete widget_; delete controller_; }
110
111         /// Dialog inherited methods
112         //@{
113         void applyView() {}
114         void hideView() { QDockWidget::hide(); }
115         void showData(std::string const & data)
116         {
117                 controller_->initialiseParams(data);
118                 showView();
119         }
120         void showView()
121         {
122                 widget_->updateView();  // make sure its up-to-date
123                 QDockWidget::show();
124         }
125         bool isVisibleView() const { return QDockWidget::isVisible(); }
126         void checkStatus() { updateView(); }
127         void redraw() { redrawView(); }
128         void redrawView() {}
129         void updateData(std::string const & data)
130         {
131                 controller_->initialiseParams(data);
132                 updateView();
133         }
134         void updateView()
135         {
136                 widget_->updateView();
137                 QDockWidget::update();
138         }
139         bool isClosing() const { return false; }
140         void partialUpdateView(int /*id*/) {}
141         Controller & controller() { return *controller_; }
142         std::string name() const { return name_; }
143         //@}
144 private:
145         /// The encapsulated widget.
146         GuiViewSourceDialog * widget_;
147         Controller * controller_;
148         std::string name_;
149 };
150
151 } // namespace frontend
152 } // namespace lyx
153
154 #endif // GUIVIEWSOURCE_H