1 /**
2 * \file GuiTexinfo.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
5 *
6 * \author Edwin Leuven
7 * \author Herbert Voß
8 *
9 * Full author contact details are available in file CREDITS.
10 */
11
12 #include <config.h>
13
14 #include "GuiTexinfo.h"
15
16 #include "FuncRequest.h"
17
18 #include "support/filetools.h"
19 #include "support/FileName.h"
20
21 #include "qt_helpers.h"
22
23 #include <QCheckBox>
24 #include <QListWidget>
25 #include <QPushButton>
26 #include <QStringList>
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32 namespace frontend {
33
texFileFromList(QString const & file,QString const & type)34 static QString texFileFromList(QString const & file, QString const & type)
35 {
36 QString lstfile = type + "Files.lst";
37 FileName const abslstfile = libFileSearch(QString(), lstfile);
38 if (abslstfile.empty())
39 return QString();
40 QString cs = toqstr(abslstfile.fileContents("UTF-8"));
41 cs.replace("\r", "");
42 QStringList const result = cs.split("\n").filter(file);
43 if (result.empty())
44 return QString();
45 return result.at(0);
46 }
47
48
GuiTexInfo(GuiView & lv)49 GuiTexInfo::GuiTexInfo(GuiView & lv)
50 : GuiDialog(lv, "texinfo", qt_("TeX Information"))
51 {
52 setupUi(this);
53
54 warningPosted_ = false;
55 activeStyle_ = ClsType;
56
57 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
58
59 connect(viewPB, SIGNAL(clicked()), this, SLOT(viewClicked()));
60 connect(whatStyleCO, SIGNAL(activated(QString)),
61 this, SLOT(enableViewPB()));
62 connect(whatStyleCO, SIGNAL(activated(int)), this, SLOT(updateView()));
63 connect(pathCB, SIGNAL(stateChanged(int)), this, SLOT(updateView()));
64 connect(rescanPB, SIGNAL(clicked()), this, SLOT(enableViewPB()));
65 connect(rescanPB, SIGNAL(clicked()), this, SLOT(rescanClicked()));
66 connect(fileListLW, SIGNAL(itemClicked(QListWidgetItem *)),
67 this, SLOT(enableViewPB()));
68 connect(fileListLW, SIGNAL(itemSelectionChanged()),
69 this, SLOT(enableViewPB()));
70
71 bc().setPolicy(ButtonPolicy::OkCancelPolicy);
72 bc().setCancel(closePB);
73 }
74
75
change_adaptor()76 void GuiTexInfo::change_adaptor()
77 {
78 changed();
79 }
80
81
rescanClicked()82 void GuiTexInfo::rescanClicked()
83 {
84 // build new *Files.lst
85 rescanTexStyles();
86 updateStyles();
87 enableViewPB();
88 }
89
90
viewClicked()91 void GuiTexInfo::viewClicked()
92 {
93 // takes advantage of enum order
94 static QString const ext[] = { "cls", "sty", "bst", "bib", "bbx", "cbx" };
95 int const fitem = fileListLW->currentRow();
96 QStringList const & data = texdata_[activeStyle_];
97 QString file = data[fitem];
98 if (!pathCB->isChecked())
99 file = texFileFromList(data[fitem], ext[activeStyle_]);
100 viewFile(file);
101 }
102
103
updateView()104 void GuiTexInfo::updateView()
105 {
106 // takes advantage of enum order
107 updateStyles(static_cast<TexFileType>(whatStyleCO->currentIndex()));
108 enableViewPB();
109 }
110
111
enableViewPB()112 void GuiTexInfo::enableViewPB()
113 {
114 viewPB->setEnabled(fileListLW->currentRow() > -1);
115 }
116
117
updateStyles(TexFileType type)118 void GuiTexInfo::updateStyles(TexFileType type)
119 {
120 static QString const filenames[] = {
121 "clsFiles.lst", "styFiles.lst", "bstFiles.lst", "bibFiles.lst",
122 "bbxFiles.lst", "cbxFiles.lst"
123 };
124
125 QString const filename = filenames[type];
126
127 QStringList data = texFileList(filename);
128 if (data.empty()) {
129 // build filelists of all availabe bst/cls/sty-files.
130 // Done through kpsewhich and an external script,
131 // saved in *Files.lst
132 rescanTexStyles();
133 data = texFileList(filename);
134 }
135
136 if (!pathCB->isChecked()) {
137 for (int i = 0; i != data.size(); ++i)
138 data[i] = onlyFileName(data[i]);
139 }
140 // sort on filename only (no path)
141 data.sort();
142
143 fileListLW->clear();
144 for(QString const & item : data)
145 fileListLW->addItem(item);
146
147 activeStyle_ = type;
148 texdata_[type] = data;
149 }
150
151
updateStyles()152 void GuiTexInfo::updateStyles()
153 {
154 updateStyles(activeStyle_);
155 }
156
157
viewFile(QString const & filename) const158 void GuiTexInfo::viewFile(QString const & filename) const
159 {
160 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + fromqstr(filename)));
161 }
162
163
164 /// get a class with full path from the list
165 /*
166 string GuiTexInfo::classOptions(string const & classname) const
167 {
168 FileName const filename(texFileFromList(classname, "cls"));
169 if (filename.empty())
170 return string();
171 string optionList;
172 ifstream is(filename.toFilesystemEncoding().c_str());
173 while (is) {
174 string s;
175 is >> s;
176 if (contains(s, "DeclareOption")) {
177 s = s.substr(s.find("DeclareOption"));
178 s = split(s, '{'); // cut front
179 s = token(s, '}', 0); // cut end
180 optionList += (s + '\n');
181 }
182 }
183 return optionList;
184 }
185 */
186
187
createGuiTexInfo(GuiView & lv)188 Dialog * createGuiTexInfo(GuiView & lv) { return new GuiTexInfo(lv); }
189
190
191 } // namespace frontend
192 } // namespace lyx
193
194
195 #include "moc_GuiTexinfo.cpp"
196