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