]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiViewSource.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[lyx.git] / src / frontends / qt4 / GuiViewSource.cpp
1 /**
2  * \file GuiViewSource.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Bo Peng
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiApplication.h"
16 #include "GuiViewSource.h"
17 #include "LaTeXHighlighter.h"
18 #include "qt_helpers.h"
19
20 #include "BufferView.h"
21 #include "Buffer.h"
22 #include "Cursor.h"
23 #include "Paragraph.h"
24 #include "TexRow.h"
25
26 #include "support/assert.h"
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29
30 #include <QTextCursor>
31 #include <QTextDocument>
32
33 using namespace std;
34
35 namespace lyx {
36 namespace frontend {
37
38 ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller)
39         :       controller_(controller), document_(new QTextDocument(this)),
40                 highlighter_(new LaTeXHighlighter(document_))
41 {
42         setupUi(this);
43
44         connect(viewFullSourceCB, SIGNAL(clicked()),
45                 this, SLOT(updateView()));
46         connect(autoUpdateCB, SIGNAL(toggled(bool)),
47                 updatePB, SLOT(setDisabled(bool)));
48         connect(updatePB, SIGNAL(clicked()),
49                 this, SLOT(updateView()));
50
51         // setting a document at this point trigger an assertion in Qt
52         // so we disable the signals here:
53         document_->blockSignals(true);
54         viewSourceTV->setDocument(document_);
55         document_->blockSignals(false);
56         viewSourceTV->setReadOnly(true);
57         ///dialog_->viewSourceTV->setAcceptRichText(false);
58         // this is personal. I think source code should be in fixed-size font
59         QFont font(guiApp->typewriterFontName());
60         font.setKerning(false);
61         font.setFixedPitch(true);
62         font.setStyleHint(QFont::TypeWriter);
63         viewSourceTV->setFont(font);
64         // again, personal taste
65         viewSourceTV->setWordWrapMode(QTextOption::NoWrap);
66 }
67
68
69 void ViewSourceWidget::updateView()
70 {
71         if (autoUpdateCB->isChecked())
72                 update(viewFullSourceCB->isChecked());
73
74         GuiViewSource::Row row = controller_.getRows();
75         QTextCursor c = QTextCursor(viewSourceTV->document());
76         c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, row.begin);
77         c.select(QTextCursor::BlockUnderCursor);
78         c.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor,
79                 row.end - row.begin + 1);
80         viewSourceTV->setTextCursor(c);
81 }
82
83
84 void ViewSourceWidget::update(bool full_source)
85 {
86         document_->setPlainText(controller_.getContent(full_source));
87 }
88
89
90 GuiViewSource::GuiViewSource(GuiView & parent,
91                 Qt::DockWidgetArea area, Qt::WindowFlags flags)
92         : DockView(parent, "view-source", qt_("LaTeX Source"), area, flags)
93 {
94         widget_ = new ViewSourceWidget(*this);
95         setWidget(widget_);
96 }
97
98
99 GuiViewSource::~GuiViewSource()
100 {
101         delete widget_;
102 }
103
104
105 void GuiViewSource::updateView()
106 {
107         widget_->updateView();
108 }
109
110
111 bool GuiViewSource::initialiseParams(string const & /*source*/)
112 {
113         setWindowTitle(title());
114         return true;
115 }
116
117
118 QString GuiViewSource::getContent(bool fullSource)
119 {
120         // get the *top* level paragraphs that contain the cursor,
121         // or the selected text
122         pit_type par_begin;
123         pit_type par_end;
124
125         BufferView * view = bufferview();
126         if (!view->cursor().selection()) {
127                 par_begin = view->cursor().bottom().pit();
128                 par_end = par_begin;
129         } else {
130                 par_begin = view->cursor().selectionBegin().bottom().pit();
131                 par_end = view->cursor().selectionEnd().bottom().pit();
132         }
133         if (par_begin > par_end)
134                 swap(par_begin, par_end);
135         odocstringstream ostr;
136         view->buffer().getSourceCode(ostr, par_begin, par_end + 1, fullSource);
137         return toqstr(ostr.str());
138 }
139
140
141 GuiViewSource::Row GuiViewSource::getRows() const
142 {
143         BufferView const * view = bufferview();
144         CursorSlice beg = view->cursor().selectionBegin().bottom();
145         CursorSlice end = view->cursor().selectionEnd().bottom();
146
147         int begrow = view->buffer().texrow().
148                 getRowFromIdPos(beg.paragraph().id(), beg.pos());
149         int endrow = view->buffer().texrow().
150                 getRowFromIdPos(end.paragraph().id(), end.pos());
151         int nextendrow = view->buffer().texrow().
152                 getRowFromIdPos(end.paragraph().id(), end.pos() + 1);
153         Row row;
154         row.begin = begrow;
155         row.end = endrow == nextendrow ? endrow : (nextendrow - 1);
156         return row;
157 }
158
159
160 QString GuiViewSource::title() const
161 {
162         switch (docType()) {
163                 case LATEX:
164                         return qt_("LaTeX Source");
165                 case DOCBOOK:
166                         return qt_("DocBook Source");
167                 case LITERATE:
168                         return qt_("Literate Source");
169         }
170         LASSERT(false, /**/);
171         return QString();
172 }
173
174
175 Dialog * createGuiViewSource(GuiView & lv)
176 {
177         return new GuiViewSource(lv);
178 }
179
180
181 } // namespace frontend
182 } // namespace lyx
183
184 #include "GuiViewSource_moc.cpp"