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"
18 #include "support/debug.h"
19 #include "support/filetools.h"
20 #include "support/foreach.h"
21 #include "support/FileName.h"
22 #include "support/lstrings.h"
24 #include "qt_helpers.h"
27 #include <QListWidget>
28 #include <QPushButton>
34 using namespace lyx::support;
39 static QString texFileFromList(QString const & file, QString const & type)
42 // do we need to add the suffix?
43 if (getExtension(file) != type)
46 lyxerr << "Searching for file " << fromqstr(file_) << endl;
48 QString lstfile = type + "Files.lst";
50 lstfile = "clsFiles.lst";
51 else if (type == "sty")
52 lstfile = "styFiles.lst";
53 else if (type == "bst")
54 lstfile = "bstFiles.lst";
55 else if (type == "bib")
56 lstfile = "bibFiles.lst";
57 FileName const abslstfile = libFileSearch(QString(), lstfile);
58 if (abslstfile.empty()) {
59 lyxerr << "File `'" << fromqstr(lstfile) << "' not found." << endl;
63 string const allClasses = to_utf8(abslstfile.fileContents("UTF-8"));
65 string classfile = token(allClasses, '\n', entries);
67 while ((!contains(classfile, fromqstr(file))
68 || support::onlyFilename(classfile) != fromqstr(file))
70 classfile = token(allClasses, '\n', ++entries);
73 // now we have filename with full path
74 lyxerr << "with full path: " << classfile << endl;
76 return toqstr(classfile);
80 GuiTexInfo::GuiTexInfo(GuiView & lv)
81 : GuiDialog(lv, "texinfo", qt_("TeX Information"))
85 warningPosted_ = false;
86 activeStyle_ = ClsType;
88 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
90 connect(viewPB, SIGNAL(clicked()), this, SLOT(viewClicked()));
91 connect(whatStyleCO, SIGNAL(activated(QString)),
92 this, SLOT(enableViewPB()));
93 connect(whatStyleCO, SIGNAL(activated(int)), this, SLOT(updateView()));
94 connect(pathCB, SIGNAL(stateChanged(int)), this, SLOT(updateView()));
95 connect(rescanPB, SIGNAL(clicked()), this, SLOT(enableViewPB()));
96 connect(rescanPB, SIGNAL(clicked()), this, SLOT(rescanClicked()));
97 connect(fileListLW, SIGNAL(itemClicked(QListWidgetItem *)),
98 this, SLOT(enableViewPB()));
99 connect(fileListLW, SIGNAL(itemSelectionChanged()),
100 this, SLOT(enableViewPB()));
102 bc().setPolicy(ButtonPolicy::OkCancelPolicy);
103 bc().setCancel(closePB);
107 void GuiTexInfo::change_adaptor()
113 void GuiTexInfo::rescanClicked()
115 // build new *Files.lst
122 void GuiTexInfo::viewClicked()
124 // takes advantage of enum order
125 static QString const ext[] = { "cls", "sty", "bst" };
126 int const fitem = fileListLW->currentRow();
127 QStringList const & data = texdata_[activeStyle_];
128 QString file = data[fitem];
129 if (!pathCB->isChecked())
130 file = texFileFromList(data[fitem], ext[activeStyle_]);
135 void GuiTexInfo::updateView()
137 // takes advantage of enum order
138 updateStyles(static_cast<TexFileType>(whatStyleCO->currentIndex()));
143 void GuiTexInfo::enableViewPB()
145 viewPB->setEnabled(fileListLW->currentRow() > -1);
149 void GuiTexInfo::updateStyles(TexFileType type)
151 static QString const filenames[] = {
152 "clsFile.lst", "styFiles.lst", "bstFiles.lst"
155 QString const filename = filenames[type];
157 QStringList data = texFileList(filename);
159 // build filelists of all availabe bst/cls/sty-files.
160 // Done through kpsewhich and an external script,
161 // saved in *Files.lst
163 data = texFileList(filename);
166 if (!pathCB->isChecked()) {
167 for (int i = 0; i != data.size(); ++i)
168 data[i] = onlyFilename(data[i]);
170 // sort on filename only (no path)
174 foreach (QString const & item, data)
175 fileListLW->addItem(item);
178 texdata_[type] = data;
182 void GuiTexInfo::updateStyles()
184 updateStyles(activeStyle_);
188 void GuiTexInfo::viewFile(QString const & filename) const
190 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + fromqstr(filename)));
194 /// get a class with full path from the list
196 string GuiTexInfo::classOptions(string const & classname) const
198 FileName const filename(texFileFromList(classname, "cls"));
199 if (filename.empty())
202 ifstream is(filename.toFilesystemEncoding().c_str());
206 if (contains(s, "DeclareOption")) {
207 s = s.substr(s.find("DeclareOption"));
208 s = split(s, '{'); // cut front
209 s = token(s, '}', 0); // cut end
210 optionList += (s + '\n');
218 Dialog * createGuiTexInfo(GuiView & lv) { return new GuiTexInfo(lv); }
221 } // namespace frontend
225 #include "GuiTexinfo_moc.cpp"