1 /*
2  * CompilePdfPreferencesPane.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 package org.rstudio.studio.client.workbench.prefs.views;
16 
17 import com.google.gwt.dom.client.Style.Unit;
18 import com.google.gwt.resources.client.ImageResource;
19 import com.google.gwt.user.client.ui.CheckBox;
20 import com.google.gwt.user.client.ui.Label;
21 import com.google.inject.Inject;
22 
23 import org.rstudio.core.client.prefs.PreferencesDialogBaseResources;
24 import org.rstudio.core.client.prefs.RestartRequirement;
25 import org.rstudio.core.client.resources.ImageResource2x;
26 import org.rstudio.core.client.widget.HelpButton;
27 import org.rstudio.core.client.widget.SelectWidget;
28 import org.rstudio.studio.client.common.latex.LatexProgramSelectWidget;
29 import org.rstudio.studio.client.common.rnw.RnwWeaveSelectWidget;
30 import org.rstudio.studio.client.common.synctex.SynctexUtils;
31 import org.rstudio.studio.client.workbench.prefs.model.UserPrefs;
32 
33 public class CompilePdfPreferencesPane extends PreferencesPane
34 {
35    @Inject
CompilePdfPreferencesPane(UserPrefs prefs, PreferencesDialogResources res)36    public CompilePdfPreferencesPane(UserPrefs prefs,
37                                     PreferencesDialogResources res)
38    {
39       prefs_ = prefs;
40       res_ = res;
41       PreferencesDialogBaseResources baseRes = PreferencesDialogBaseResources.INSTANCE;
42 
43       add(headerLabel("PDF Generation"));
44 
45       defaultSweaveEngine_ = new RnwWeaveSelectWidget();
46       defaultSweaveEngine_.setValue(
47                               prefs.defaultSweaveEngine().getGlobalValue());
48       add(defaultSweaveEngine_);
49 
50       defaultLatexProgram_ = new LatexProgramSelectWidget();
51       defaultLatexProgram_.setValue(
52                               prefs.defaultLatexProgram().getGlobalValue());
53       add(defaultLatexProgram_);
54 
55       Label perProjectLabel = new Label(
56             "NOTE: The Rnw weave and LaTeX compilation options are also set on a " +
57             "per-project (and optionally per-file) basis. Click the help " +
58             "icons above for more details.");
59 
60       perProjectLabel.addStyleName(baseRes.styles().infoLabel());
61       nudgeRight(perProjectLabel);
62       spaced(perProjectLabel);
63       add(perProjectLabel);
64 
65       add(headerLabel("LaTeX Editing and Compilation"));
66 
67       chkUseTinytex_ = new CheckBox("Use tinytex when compiling .tex files");
68       spaced(chkUseTinytex_);
69       add(chkUseTinytex_);
70 
71       chkCleanTexi2DviOutput_ = new CheckBox("Clean auxiliary output after compile");
72       spaced(chkCleanTexi2DviOutput_);
73       add(chkCleanTexi2DviOutput_);
74 
75       chkEnableShellEscape_ = new CheckBox("Enable shell escape commands");
76       spaced(chkEnableShellEscape_);
77       add(chkEnableShellEscape_);
78 
79       add(spaced(checkboxPref(
80             "Insert numbered sections and subsections",
81             prefs_.insertNumberedLatexSections(), false /*defaultSpace*/)));
82 
83       Label previewingOptionsLabel = headerLabel("PDF Preview");
84       previewingOptionsLabel.getElement().getStyle().setMarginTop(8, Unit.PX);
85       add(previewingOptionsLabel);
86 
87       pdfPreview_ = new PdfPreviewSelectWidget();
88       add(pdfPreview_);
89 
90       add(spaced(checkboxPref(
91             "Always enable Rnw concordance (required for synctex)",
92             prefs_.alwaysEnableRnwConcordance(),
93             false /*defaultSpaces*/)));
94    }
95 
96    private class PdfPreviewSelectWidget extends SelectWidget
97    {
PdfPreviewSelectWidget()98       public PdfPreviewSelectWidget()
99       {
100          super(
101             "Preview PDF after compile using:",
102             new String[]{},
103             new String[]{},
104             false,
105             true,
106             false);
107 
108          HelpButton.addHelpButton(this, "pdf_preview", "Help on previewing PDF files");
109       }
110    }
111 
112 
113 
114    @Override
getIcon()115    public ImageResource getIcon()
116    {
117       return new ImageResource2x(PreferencesDialogBaseResources.INSTANCE.iconCompilePdf2x());
118    }
119 
120    @Override
validate()121    public boolean validate()
122    {
123       return true;
124    }
125 
126    @Override
getName()127    public String getName()
128    {
129       return "Sweave";
130    }
131 
132    @Override
initialize(UserPrefs prefs)133    protected void initialize(UserPrefs prefs)
134    {
135       chkUseTinytex_.setValue(prefs.useTinytex().getValue());
136       chkCleanTexi2DviOutput_.setValue(prefs.cleanTexi2dviOutput().getValue());
137       chkEnableShellEscape_.setValue(prefs.latexShellEscape().getValue());
138 
139       pdfPreview_.addChoice("(No Preview)", UserPrefs.PDF_PREVIEWER_NONE);
140 
141       String desktopSynctexViewer = SynctexUtils.getDesktopSynctexViewer();
142       if (desktopSynctexViewer.length() > 0)
143       {
144          pdfPreview_.addChoice(desktopSynctexViewer  + " (Recommended)",
145                                UserPrefs.PDF_PREVIEWER_DESKTOP_SYNCTEX);
146       }
147 
148       pdfPreview_.addChoice("RStudio Viewer",
149                             UserPrefs.PDF_PREVIEWER_RSTUDIO);
150 
151       pdfPreview_.addChoice("System Viewer",
152                             UserPrefs.PDF_PREVIEWER_SYSTEM);
153 
154       pdfPreview_.setValue(prefs_.pdfPreviewer().getValue());
155    }
156 
157    @Override
onApply(UserPrefs rPrefs)158    public RestartRequirement onApply(UserPrefs rPrefs)
159    {
160       RestartRequirement restartRequirement = super.onApply(rPrefs);
161 
162       prefs_.defaultSweaveEngine().setGlobalValue(
163                                     defaultSweaveEngine_.getValue());
164       prefs_.defaultLatexProgram().setGlobalValue(
165                                     defaultLatexProgram_.getValue());
166 
167       prefs_.pdfPreviewer().setGlobalValue(pdfPreview_.getValue());
168 
169       prefs_.useTinytex().setGlobalValue(chkUseTinytex_.getValue());
170 
171       prefs_.cleanTexi2dviOutput().setGlobalValue(
172             chkCleanTexi2DviOutput_.getValue());
173 
174       prefs_.latexShellEscape().setGlobalValue(
175             chkEnableShellEscape_.getValue());
176 
177       return restartRequirement;
178    }
179 
180    private final UserPrefs prefs_;
181 
182    @SuppressWarnings("unused")
183    private final PreferencesDialogResources res_;
184 
185    private RnwWeaveSelectWidget defaultSweaveEngine_;
186    private LatexProgramSelectWidget defaultLatexProgram_;
187    private CheckBox chkUseTinytex_;
188    private CheckBox chkCleanTexi2DviOutput_;
189    private CheckBox chkEnableShellEscape_;
190    private PdfPreviewSelectWidget pdfPreview_;
191 
192 }
193