3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "GuiTexinfo.h"
16 #include "FuncRequest.h"
19 #include "support/filetools.h"
20 #include "support/FileName.h"
21 #include "support/lstrings.h"
23 #include "qt_helpers.h"
25 #include <QCloseEvent>
27 #include <QListWidget>
28 #include <QPushButton>
40 using support::FileName;
41 using support::contains;
44 using support::getExtension;
45 using support::libFileSearch;
46 using support::onlyFilename;
48 static string texFileFromList(string const & file, string const & type)
51 // do we need to add the suffix?
52 if (!(getExtension(file) == type))
55 lyxerr << "Searching for file " << file_ << endl;
57 string lstfile = type + "Files.lst";
59 lstfile = "clsFiles.lst";
60 else if (type == "sty")
61 lstfile = "styFiles.lst";
62 else if (type == "bst")
63 lstfile = "bstFiles.lst";
64 else if (type == "bib")
65 lstfile = "bibFiles.lst";
66 FileName const abslstfile = libFileSearch(string(), lstfile);
67 if (abslstfile.empty()) {
68 lyxerr << "File `'" << lstfile << "' not found." << endl;
71 string const allClasses = abslstfile.fileContents();
73 string classfile = token(allClasses, '\n', entries);
75 while ((!contains(classfile, file) ||
76 (onlyFilename(classfile) != file)) &&
78 classfile = token(allClasses, '\n', ++entries);
81 // now we have filename with full path
82 lyxerr << "with full path: " << classfile << endl;
88 GuiTexInfo::GuiTexInfo(GuiView & lv)
89 : GuiDialog(lv, "texinfo")
92 setViewTitle(_("TeX Information"));
94 warningPosted = false;
95 activeStyle = ClsType;
97 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
99 connect(viewPB, SIGNAL(clicked()), this, SLOT(viewClicked()));
100 connect(whatStyleCO, SIGNAL(activated(QString)),
101 this, SLOT(enableViewPB()));
102 connect(whatStyleCO, SIGNAL(activated(int)), this, SLOT(updateView()));
103 connect(pathCB, SIGNAL(stateChanged(int)), this, SLOT(updateView()));
104 connect(rescanPB, SIGNAL(clicked()), this, SLOT(enableViewPB()));
105 connect(rescanPB, SIGNAL(clicked()), this, SLOT(rescanClicked()));
106 connect(fileListLW, SIGNAL(itemClicked(QListWidgetItem *)),
107 this, SLOT(enableViewPB()));
108 connect(fileListLW, SIGNAL(itemSelectionChanged()),
109 this, SLOT(enableViewPB()));
111 updateStyles(ClsType);
113 bc().setPolicy(ButtonPolicy::OkCancelPolicy);
114 bc().setCancel(closePB);
118 void GuiTexInfo::change_adaptor()
124 void GuiTexInfo::closeEvent(QCloseEvent * e)
131 void GuiTexInfo::rescanClicked()
133 // build new *Files.lst
140 void GuiTexInfo::viewClicked()
142 size_t const fitem = fileListLW->currentRow();
143 vector<string> const & data = texdata_[activeStyle];
144 string file = data[fitem];
145 if (!pathCB->isChecked())
146 file = texFileFromList(data[fitem], fileType(activeStyle));
151 void GuiTexInfo::updateView()
153 // takes advantage of enum order
154 updateStyles(static_cast<TexFileType>(whatStyleCO->currentIndex()));
159 void GuiTexInfo::enableViewPB()
161 viewPB->setEnabled(fileListLW->currentRow() > -1);
165 void GuiTexInfo::updateStyles(TexFileType type)
167 ContentsType & data = texdata_[type];
169 static string filenames[] = { "clsFile.lst", "styFiles.lst", "bstFiles.lst" };
170 string filename = filenames[type];
172 getTexFileList(filename, data);
174 // build filelists of all availabe bst/cls/sty-files.
175 // Done through kpsewhich and an external script,
176 // saved in *Files.lst
178 getTexFileList(filename, data);
180 bool const withFullPath = pathCB->isChecked();
183 vector<string>::iterator it1 = data.begin();
184 vector<string>::iterator end1 = data.end();
185 for (; it1 != end1; ++it1)
186 *it1 = support::onlyFilename(*it1);
188 // sort on filename only (no path)
189 std::sort(data.begin(), data.end());
192 ContentsType::const_iterator it = data.begin();
193 ContentsType::const_iterator end = data.end();
194 for (; it != end; ++it)
195 fileListLW->addItem(toqstr(*it));
201 void GuiTexInfo::updateStyles()
203 updateStyles(activeStyle);
207 void GuiTexInfo::viewFile(string const & filename) const
209 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + filename));
213 /// get a class with full path from the list
214 string GuiTexInfo::classOptions(string const & classname) const
216 FileName const filename(texFileFromList(classname, "cls"));
217 if (filename.empty())
220 std::ifstream is(filename.toFilesystemEncoding().c_str());
224 if (contains(s, "DeclareOption")) {
225 s = s.substr(s.find("DeclareOption"));
226 s = split(s, '{'); // cut front
227 s = token(s, '}', 0); // cut end
228 optionList += (s + '\n');
235 string GuiTexInfo::fileType(TexFileType type) const
237 // takes advantage of enum order
238 static string const ext[] = { "cls", "sty", "bst" };
243 Dialog * createGuiTexInfo(GuiView & lv) { return new GuiTexInfo(lv); }
246 } // namespace frontend
250 #include "GuiTexinfo_moc.cpp"