]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiTexinfo.cpp
6a1e64aa9b41d981065b9a12ebdb5ba81f922ae0
[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 #include <QStringList>
30
31 #include <fstream>
32 #include <algorithm>
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38 namespace frontend {
39
40 static QString texFileFromList(QString const & file, QString const & type)
41 {
42         QString lstfile = type + "Files.lst";
43         FileName const abslstfile = libFileSearch(QString(), lstfile);
44         if (abslstfile.empty())
45                 return QString();
46         QString cs = toqstr(abslstfile.fileContents("UTF-8"));
47         cs.replace("\r", "");
48         QStringList const result = cs.split("\n").filter(file);
49         if (result.empty())
50                 return QString();
51         return result.at(0);
52 }
53
54
55 GuiTexInfo::GuiTexInfo(GuiView & lv)
56         : GuiDialog(lv, "texinfo", qt_("TeX Information"))
57 {
58         setupUi(this);
59
60         warningPosted_ = false;
61         activeStyle_ = ClsType;
62
63         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
64
65         connect(viewPB, SIGNAL(clicked()), this, SLOT(viewClicked()));
66         connect(whatStyleCO, SIGNAL(activated(QString)),
67                 this, SLOT(enableViewPB()));
68         connect(whatStyleCO, SIGNAL(activated(int)), this, SLOT(updateView()));
69         connect(pathCB, SIGNAL(stateChanged(int)), this, SLOT(updateView()));
70         connect(rescanPB, SIGNAL(clicked()), this, SLOT(enableViewPB()));
71         connect(rescanPB, SIGNAL(clicked()), this, SLOT(rescanClicked()));
72         connect(fileListLW, SIGNAL(itemClicked(QListWidgetItem *)),
73                 this, SLOT(enableViewPB()));
74         connect(fileListLW, SIGNAL(itemSelectionChanged()),
75                 this, SLOT(enableViewPB()));
76
77         bc().setPolicy(ButtonPolicy::OkCancelPolicy);
78         bc().setCancel(closePB);
79 }
80
81
82 void GuiTexInfo::change_adaptor()
83 {
84         changed();
85 }
86
87
88 void GuiTexInfo::rescanClicked()
89 {
90         // build new *Files.lst
91         rescanTexStyles();
92         updateStyles();
93         enableViewPB();
94 }
95
96
97 void GuiTexInfo::viewClicked()
98 {
99         // takes advantage of enum order
100         static QString const ext[] = { "cls", "sty", "bst" };
101         int const fitem = fileListLW->currentRow();
102         QStringList const & data = texdata_[activeStyle_];
103         QString file = data[fitem];
104         if (!pathCB->isChecked())
105                 file = texFileFromList(data[fitem], ext[activeStyle_]);
106         viewFile(file);
107 }
108
109
110 void GuiTexInfo::updateView()
111 {
112         // takes advantage of enum order
113         updateStyles(static_cast<TexFileType>(whatStyleCO->currentIndex()));
114         enableViewPB();
115 }
116
117
118 void GuiTexInfo::enableViewPB()
119 {
120         viewPB->setEnabled(fileListLW->currentRow() > -1);
121 }
122
123
124 void GuiTexInfo::updateStyles(TexFileType type)
125 {
126         static QString const filenames[] = {
127                 "clsFiles.lst", "styFiles.lst", "bstFiles.lst"
128         };
129
130         QString const filename = filenames[type];
131
132         QStringList data = texFileList(filename);
133         if (data.empty()) {
134                 // build filelists of all availabe bst/cls/sty-files.
135                 // Done through kpsewhich and an external script,
136                 // saved in *Files.lst
137                 rescanTexStyles();
138                 data = texFileList(filename);
139         }
140
141         if (!pathCB->isChecked()) {
142                 for (int i = 0; i != data.size(); ++i)
143                         data[i] = onlyFilename(data[i]);
144         }
145         // sort on filename only (no path)
146         data.sort();
147
148         fileListLW->clear();
149         foreach (QString const & item, data)
150                 fileListLW->addItem(item);
151
152         activeStyle_ = type;
153         texdata_[type] = data;
154 }
155
156
157 void GuiTexInfo::updateStyles()
158 {
159         updateStyles(activeStyle_);
160 }
161
162
163 void GuiTexInfo::viewFile(QString const & filename) const
164 {
165         dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + fromqstr(filename)));
166 }
167
168
169 /// get a class with full path from the list
170 /*
171 string GuiTexInfo::classOptions(string const & classname) const
172 {
173         FileName const filename(texFileFromList(classname, "cls"));
174         if (filename.empty())
175                 return string();
176         string optionList;
177         ifstream is(filename.toFilesystemEncoding().c_str());
178         while (is) {
179                 string s;
180                 is >> s;
181                 if (contains(s, "DeclareOption")) {
182                         s = s.substr(s.find("DeclareOption"));
183                         s = split(s, '{');              // cut front
184                         s = token(s, '}', 0);           // cut end
185                         optionList += (s + '\n');
186                 }
187         }
188         return optionList;
189 }
190 */
191
192
193 Dialog * createGuiTexInfo(GuiView & lv) { return new GuiTexInfo(lv); }
194
195
196 } // namespace frontend
197 } // namespace lyx
198
199
200 #include "GuiTexinfo_moc.cpp"