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"
17 #include "support/debug.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>
34 using namespace lyx::support;
40 static string texFileFromList(string const & file, string const & type)
43 // do we need to add the suffix?
44 if (!(getExtension(file) == type))
47 lyxerr << "Searching for file " << file_ << endl;
49 string lstfile = type + "Files.lst";
51 lstfile = "clsFiles.lst";
52 else if (type == "sty")
53 lstfile = "styFiles.lst";
54 else if (type == "bst")
55 lstfile = "bstFiles.lst";
56 else if (type == "bib")
57 lstfile = "bibFiles.lst";
58 FileName const abslstfile = libFileSearch(string(), lstfile);
59 if (abslstfile.empty()) {
60 lyxerr << "File `'" << lstfile << "' not found." << endl;
64 string const allClasses = to_utf8(abslstfile.fileContents("UTF-8"));
66 string classfile = token(allClasses, '\n', entries);
68 while ((!contains(classfile, file) ||
69 (onlyFilename(classfile) != file)) &&
71 classfile = token(allClasses, '\n', ++entries);
74 // now we have filename with full path
75 lyxerr << "with full path: " << classfile << endl;
81 GuiTexInfo::GuiTexInfo(GuiView & lv)
82 : GuiDialog(lv, "texinfo")
85 setViewTitle(_("TeX Information"));
87 warningPosted = false;
88 activeStyle = ClsType;
90 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
92 connect(viewPB, SIGNAL(clicked()), this, SLOT(viewClicked()));
93 connect(whatStyleCO, SIGNAL(activated(QString)),
94 this, SLOT(enableViewPB()));
95 connect(whatStyleCO, SIGNAL(activated(int)), this, SLOT(updateView()));
96 connect(pathCB, SIGNAL(stateChanged(int)), this, SLOT(updateView()));
97 connect(rescanPB, SIGNAL(clicked()), this, SLOT(enableViewPB()));
98 connect(rescanPB, SIGNAL(clicked()), this, SLOT(rescanClicked()));
99 connect(fileListLW, SIGNAL(itemClicked(QListWidgetItem *)),
100 this, SLOT(enableViewPB()));
101 connect(fileListLW, SIGNAL(itemSelectionChanged()),
102 this, SLOT(enableViewPB()));
104 updateStyles(ClsType);
106 bc().setPolicy(ButtonPolicy::OkCancelPolicy);
107 bc().setCancel(closePB);
111 void GuiTexInfo::change_adaptor()
117 void GuiTexInfo::closeEvent(QCloseEvent * e)
124 void GuiTexInfo::rescanClicked()
126 // build new *Files.lst
133 void GuiTexInfo::viewClicked()
135 size_t const fitem = fileListLW->currentRow();
136 vector<string> const & data = texdata_[activeStyle];
137 string file = data[fitem];
138 if (!pathCB->isChecked())
139 file = texFileFromList(data[fitem], fileType(activeStyle));
144 void GuiTexInfo::updateView()
146 // takes advantage of enum order
147 updateStyles(static_cast<TexFileType>(whatStyleCO->currentIndex()));
152 void GuiTexInfo::enableViewPB()
154 viewPB->setEnabled(fileListLW->currentRow() > -1);
158 void GuiTexInfo::updateStyles(TexFileType type)
160 ContentsType & data = texdata_[type];
162 static string filenames[] = { "clsFile.lst", "styFiles.lst", "bstFiles.lst" };
163 string filename = filenames[type];
165 getTexFileList(filename, data);
167 // build filelists of all availabe bst/cls/sty-files.
168 // Done through kpsewhich and an external script,
169 // saved in *Files.lst
171 getTexFileList(filename, data);
173 bool const withFullPath = pathCB->isChecked();
176 vector<string>::iterator it1 = data.begin();
177 vector<string>::iterator end1 = data.end();
178 for (; it1 != end1; ++it1)
179 *it1 = onlyFilename(*it1);
181 // sort on filename only (no path)
182 sort(data.begin(), data.end());
185 ContentsType::const_iterator it = data.begin();
186 ContentsType::const_iterator end = data.end();
187 for (; it != end; ++it)
188 fileListLW->addItem(toqstr(*it));
194 void GuiTexInfo::updateStyles()
196 updateStyles(activeStyle);
200 void GuiTexInfo::viewFile(string const & filename) const
202 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + filename));
206 /// get a class with full path from the list
207 string GuiTexInfo::classOptions(string const & classname) const
209 FileName const filename(texFileFromList(classname, "cls"));
210 if (filename.empty())
213 ifstream is(filename.toFilesystemEncoding().c_str());
217 if (contains(s, "DeclareOption")) {
218 s = s.substr(s.find("DeclareOption"));
219 s = split(s, '{'); // cut front
220 s = token(s, '}', 0); // cut end
221 optionList += (s + '\n');
228 string GuiTexInfo::fileType(TexFileType type) const
230 // takes advantage of enum order
231 static string const ext[] = { "cls", "sty", "bst" };
236 Dialog * createGuiTexInfo(GuiView & lv) { return new GuiTexInfo(lv); }
239 } // namespace frontend
243 #include "GuiTexinfo_moc.cpp"