]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiTexinfo.cpp
less string conversions as long as we stay in the frontend
[lyx.git] / src / frontends / qt4 / GuiTexinfo.cpp
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/debug.h"
19 #include "support/filetools.h"
20 #include "support/foreach.h"
21 #include "support/FileName.h"
22 #include "support/lstrings.h"
23
24 #include "qt_helpers.h"
25
26 #include <QCheckBox>
27 #include <QListWidget>
28 #include <QPushButton>
29
30 #include <fstream>
31 #include <algorithm>
32
33 using namespace std;
34 using namespace lyx::support;
35
36 namespace lyx {
37 namespace frontend {
38
39 static QString texFileFromList(QString const & file, QString const & type)
40 {
41         QString file_ = file;
42         // do we need to add the suffix?
43         if (getExtension(file) != type)
44                 file_ += '.' + type;
45
46         lyxerr << "Searching for file " << fromqstr(file_) << endl;
47
48         QString lstfile = type + "Files.lst";
49         if (type == "cls")
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;
60                 return QString();
61         }
62         // FIXME UNICODE
63         string const allClasses = to_utf8(abslstfile.fileContents("UTF-8"));
64         int entries = 0;
65         string classfile = token(allClasses, '\n', entries);
66         int count = 0;
67         while ((!contains(classfile, fromqstr(file))
68                                                 || support::onlyFilename(classfile) != fromqstr(file))
69                                         && ++count < 1000) {
70                 classfile = token(allClasses, '\n', ++entries);
71         }
72
73         // now we have filename with full path
74         lyxerr << "with full path: " << classfile << endl;
75
76         return toqstr(classfile);
77 }
78
79
80 GuiTexInfo::GuiTexInfo(GuiView & lv)
81         : GuiDialog(lv, "texinfo", qt_("TeX Information"))
82 {
83         setupUi(this);
84
85         warningPosted_ = false;
86         activeStyle_ = ClsType;
87
88         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
89
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()));
101
102         bc().setPolicy(ButtonPolicy::OkCancelPolicy);
103         bc().setCancel(closePB);
104 }
105
106
107 void GuiTexInfo::change_adaptor()
108 {
109         changed();
110 }
111
112
113 void GuiTexInfo::rescanClicked()
114 {
115         // build new *Files.lst
116         rescanTexStyles();
117         updateStyles();
118         enableViewPB();
119 }
120
121
122 void GuiTexInfo::viewClicked()
123 {
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_]);
131         viewFile(file);
132 }
133
134
135 void GuiTexInfo::updateView()
136 {
137         // takes advantage of enum order
138         updateStyles(static_cast<TexFileType>(whatStyleCO->currentIndex()));
139         enableViewPB();
140 }
141
142
143 void GuiTexInfo::enableViewPB()
144 {
145         viewPB->setEnabled(fileListLW->currentRow() > -1);
146 }
147
148
149 void GuiTexInfo::updateStyles(TexFileType type)
150 {
151         static QString const filenames[] = {
152                 "clsFile.lst", "styFiles.lst", "bstFiles.lst"
153         };
154
155         QString const filename = filenames[type];
156
157         QStringList data = texFileList(filename);
158         if (data.empty()) {
159                 // build filelists of all availabe bst/cls/sty-files.
160                 // Done through kpsewhich and an external script,
161                 // saved in *Files.lst
162                 rescanTexStyles();
163                 data = texFileList(filename);
164         }
165
166         if (!pathCB->isChecked()) {
167                 for (int i = 0; i != data.size(); ++i)
168                         data[i] = onlyFilename(data[i]);
169         }
170         // sort on filename only (no path)
171         data.sort();
172
173         fileListLW->clear();
174         foreach (QString const & item, data)
175                 fileListLW->addItem(item);
176
177         activeStyle_ = type;
178         texdata_[type] = data;
179 }
180
181
182 void GuiTexInfo::updateStyles()
183 {
184         updateStyles(activeStyle_);
185 }
186
187
188 void GuiTexInfo::viewFile(QString const & filename) const
189 {
190         dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + fromqstr(filename)));
191 }
192
193
194 /// get a class with full path from the list
195 /*
196 string GuiTexInfo::classOptions(string const & classname) const
197 {
198         FileName const filename(texFileFromList(classname, "cls"));
199         if (filename.empty())
200                 return string();
201         string optionList;
202         ifstream is(filename.toFilesystemEncoding().c_str());
203         while (is) {
204                 string s;
205                 is >> s;
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');
211                 }
212         }
213         return optionList;
214 }
215 */
216
217
218 Dialog * createGuiTexInfo(GuiView & lv) { return new GuiTexInfo(lv); }
219
220
221 } // namespace frontend
222 } // namespace lyx
223
224
225 #include "GuiTexinfo_moc.cpp"