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