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 = getFileContents(abslstfile);
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(LyXView & lv)
89 : GuiDialog(lv, "texinfo"), Controller(this)
92 setViewTitle(_("TeX Information"));
93 setController(this, false);
95 warningPosted = false;
96 activeStyle = ClsType;
98 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
100 connect(viewPB, SIGNAL(clicked()), this, SLOT(viewClicked()));
101 connect(whatStyleCO, SIGNAL(activated(QString)),
102 this, SLOT(enableViewPB()));
103 connect(whatStyleCO, SIGNAL(activated(int)), this, SLOT(updateView()));
104 connect(pathCB, SIGNAL(stateChanged(int)), this, SLOT(updateView()));
105 connect(rescanPB, SIGNAL(clicked()), this, SLOT(enableViewPB()));
106 connect(rescanPB, SIGNAL(clicked()), this, SLOT(rescanClicked()));
107 connect(fileListLW, SIGNAL(itemClicked(QListWidgetItem *)),
108 this, SLOT(enableViewPB()));
109 connect(fileListLW, SIGNAL(itemSelectionChanged()),
110 this, SLOT(enableViewPB()));
112 updateStyles(ClsType);
114 bc().setPolicy(ButtonPolicy::OkCancelPolicy);
115 bc().setCancel(closePB);
119 void GuiTexInfo::change_adaptor()
125 void GuiTexInfo::closeEvent(QCloseEvent * e)
132 void GuiTexInfo::rescanClicked()
134 // build new *Files.lst
141 void GuiTexInfo::viewClicked()
143 size_t const fitem = fileListLW->currentRow();
144 vector<string> const & data = texdata_[activeStyle];
145 string file = data[fitem];
146 if (!pathCB->isChecked())
147 file = texFileFromList(data[fitem], fileType(activeStyle));
152 void GuiTexInfo::updateView()
154 // takes advantage of enum order
155 updateStyles(static_cast<TexFileType>(whatStyleCO->currentIndex()));
160 void GuiTexInfo::enableViewPB()
162 viewPB->setEnabled(fileListLW->currentRow() > -1);
166 void GuiTexInfo::updateStyles(TexFileType type)
168 ContentsType & data = texdata_[type];
170 static string filenames[] = { "clsFile.lst", "styFiles.lst", "bstFiles.lst" };
171 string filename = filenames[type];
173 getTexFileList(filename, data);
175 // build filelists of all availabe bst/cls/sty-files.
176 // Done through kpsewhich and an external script,
177 // saved in *Files.lst
179 getTexFileList(filename, data);
181 bool const withFullPath = pathCB->isChecked();
184 vector<string>::iterator it1 = data.begin();
185 vector<string>::iterator end1 = data.end();
186 for (; it1 != end1; ++it1)
187 *it1 = support::onlyFilename(*it1);
189 // sort on filename only (no path)
190 std::sort(data.begin(), data.end());
193 ContentsType::const_iterator it = data.begin();
194 ContentsType::const_iterator end = data.end();
195 for (; it != end; ++it)
196 fileListLW->addItem(toqstr(*it));
202 void GuiTexInfo::updateStyles()
204 updateStyles(activeStyle);
208 void GuiTexInfo::viewFile(string const & filename) const
210 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + filename));
214 /// get a class with full path from the list
215 string GuiTexInfo::classOptions(string const & classname) const
217 FileName const filename(texFileFromList(classname, "cls"));
218 if (filename.empty())
221 std::ifstream is(filename.toFilesystemEncoding().c_str());
225 if (contains(s, "DeclareOption")) {
226 s = s.substr(s.find("DeclareOption"));
227 s = split(s, '{'); // cut front
228 s = token(s, '}', 0); // cut end
229 optionList += (s + '\n');
236 string GuiTexInfo::fileType(TexFileType type) const
238 // takes advantage of enum order
239 static string const ext[] = { "cls", "sty", "bst" };
244 Dialog * createGuiTexInfo(LyXView & lv) { return new GuiTexInfo(lv); }
247 } // namespace frontend
251 #include "GuiTexinfo_moc.cpp"