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