]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiTexinfo.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[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(buttonBox, SIGNAL(clicked(QAbstractButton *)),
58                 this, SLOT(slotButtonBox(QAbstractButton *)));
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(buttonBox->button(QDialogButtonBox::Cancel));
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", "bbx", "cbx" };
96         int const fitem = fileListLW->currentRow();
97         QStringList const & sdata = texdata_[activeStyle_];
98         QString file = sdata[fitem];
99         if (!pathCB->isChecked())
100                 file = texFileFromList(sdata[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                 "bbxFiles.lst", "cbxFiles.lst"
124         };
125
126         QString const filename = filenames[type];
127
128         QStringList flist = texFileList(filename);
129         if (flist.empty()) {
130                 // build filelists of all availabe bst/cls/sty-files.
131                 // Done through kpsewhich and an external script,
132                 // saved in *Files.lst
133                 rescanTexStyles();
134                 flist = texFileList(filename);
135         }
136
137         if (!pathCB->isChecked()) {
138                 for (int i = 0; i != flist.size(); ++i)
139                         flist[i] = onlyFileName(flist[i]);
140         }
141         // sort on filename only (no path)
142         flist.sort();
143
144         fileListLW->clear();
145         for(QString const & item : flist)
146                 fileListLW->addItem(item);
147
148         activeStyle_ = type;
149         texdata_[type] = flist;
150 }
151
152
153 void GuiTexInfo::updateStyles()
154 {
155         updateStyles(activeStyle_);
156 }
157
158
159 void GuiTexInfo::viewFile(QString const & filename) const
160 {
161         dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + fromqstr(filename)));
162 }
163
164
165 /// get a class with full path from the list
166 /*
167 string GuiTexInfo::classOptions(string const & classname) const
168 {
169         FileName const filename(texFileFromList(classname, "cls"));
170         if (filename.empty())
171                 return string();
172         string optionList;
173         ifstream is(filename.toFilesystemEncoding().c_str());
174         while (is) {
175                 string s;
176                 is >> s;
177                 if (contains(s, "DeclareOption")) {
178                         s = s.substr(s.find("DeclareOption"));
179                         s = split(s, '{');              // cut front
180                         s = token(s, '}', 0);           // cut end
181                         optionList += (s + '\n');
182                 }
183         }
184         return optionList;
185 }
186 */
187
188
189 Dialog * createGuiTexInfo(GuiView & lv) { return new GuiTexInfo(lv); }
190
191
192 } // namespace frontend
193 } // namespace lyx
194
195
196 #include "moc_GuiTexinfo.cpp"