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