3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "qt_helpers.h"
17 #include "FileDialog.h"
18 #include "LengthCombo.h"
20 #include "frontends/alert.h"
25 #include "support/debug.h"
26 #include "support/filetools.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29 #include "support/lyxalgo.h"
30 #include "support/os.h"
31 #include "support/Package.h"
32 #include "support/Path.h"
33 #include "support/Systemcall.h"
40 #include <boost/cregex.hpp>
47 using namespace lyx::support;
52 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
54 QString const length = input->text();
58 // Don't return unit-from-choice if the input(field) contains a unit
59 if (isValidGlueLength(fromqstr(length)))
60 return fromqstr(length);
62 Length::UNIT const unit = combo->currentLengthItem();
64 return Length(length.toDouble(), unit).asString();
68 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
70 QString const length = input->text();
74 // don't return unit-from-choice if the input(field) contains a unit
75 if (isValidGlueLength(fromqstr(length)))
76 return Length(fromqstr(length));
78 Length::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
80 return Length(length.toDouble(), unit);
84 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
85 Length const & len, Length::UNIT /*defaultUnit*/)
87 combo->setCurrentItem(len.unit());
88 input->setText(QString::number(Length(len).value()));
92 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
93 string const & len, Length::UNIT defaultUnit)
96 // no length (UNIT_NONE)
97 combo->setCurrentItem(defaultUnit);
99 } else if (!isValidLength(len) && !isStrDbl(len)) {
100 // use input field only for gluelengths
101 combo->setCurrentItem(defaultUnit);
102 input->setText(toqstr(len));
104 lengthToWidgets(input, combo, Length(len), defaultUnit);
109 void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo,
110 Length const & len, Length::UNIT defaultUnit)
112 if (len.value() == 0)
113 lengthToWidgets(input, combo, "auto", defaultUnit);
115 lengthToWidgets(input, combo, len, defaultUnit);
119 void setValid(QWidget * widget, bool valid)
122 widget->setPalette(QPalette());
124 QPalette pal = widget->palette();
125 pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
126 widget->setPalette(pal);
130 } // namespace frontend
132 QString const qt_(char const * str, const char *)
134 return toqstr(_(str));
138 QString const qt_(string const & str)
140 return toqstr(_(str));
148 #if !defined(USE_WCHAR_T) && defined(__GNUC__)
149 bool operator()(LanguagePair const & lhs, LanguagePair const & rhs) const {
150 return lhs.first < rhs.first;
153 Sorter() : loc_ok(true)
162 bool operator()(LanguagePair const & lhs,
163 LanguagePair const & rhs) const {
165 return loc_(lhs.first, rhs.first);
167 return lhs.first < rhs.first;
179 vector<LanguagePair> const getLanguageData(bool character_dlg)
181 size_t const size = languages.size() + (character_dlg ? 2 : 0);
183 vector<LanguagePair> langs(size);
186 langs[0].first = _("No change");
187 langs[0].second = "ignore";
188 langs[1].first = _("Reset");
189 langs[1].second = "reset";
192 size_t i = character_dlg ? 2 : 0;
193 for (Languages::const_iterator cit = languages.begin();
194 cit != languages.end(); ++cit) {
195 langs[i].first = _(cit->second.display());
196 langs[i].second = cit->second.lang();
200 // Don't sort "ignore" and "reset"
201 vector<LanguagePair>::iterator begin = character_dlg ?
202 langs.begin() + 2 : langs.begin();
204 sort(begin, langs.end(), Sorter());
210 docstring browseFile(docstring const & filename, docstring const & title,
211 FileFilterList const & filters, bool save,
212 docstring const & label1, docstring const & dir1,
213 docstring const & label2, docstring const & dir2)
215 docstring lastPath = from_ascii(".");
216 if (!filename.empty())
217 lastPath = from_utf8(onlyPath(to_utf8(filename)));
219 FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
220 dlg.setButton1(label1, dir1);
221 dlg.setButton2(label2, dir2);
223 FileDialog::Result result;
226 result = dlg.save(lastPath, filters,
227 from_utf8(onlyFilename(to_utf8(filename))));
229 result = dlg.open(lastPath, filters,
230 from_utf8(onlyFilename(to_utf8(filename))));
232 return result.second;
236 docstring browseRelFile(docstring const & filename, docstring const & refpath,
237 docstring const & title, FileFilterList const & filters, bool save,
238 docstring const & label1, docstring const & dir1,
239 docstring const & label2, docstring const & dir2)
241 docstring const fname = from_utf8(makeAbsPath(
242 to_utf8(filename), to_utf8(refpath)).absFilename());
244 docstring const outname = browseFile(fname, title, filters, save,
245 label1, dir1, label2, dir2);
246 docstring const reloutname = makeRelPath(outname, refpath);
247 if (prefixIs(reloutname, from_ascii("../")))
254 docstring browseLibFile(docstring const & dir, docstring const & name,
255 docstring const & ext, docstring const & title,
256 FileFilterList const & filters)
259 docstring const label1 = _("System files|#S#s");
260 docstring const dir1 =
261 from_utf8(addName(package().system_support().absFilename(), to_utf8(dir)));
263 docstring const label2 = _("User files|#U#u");
264 docstring const dir2 =
265 from_utf8(addName(package().user_support().absFilename(), to_utf8(dir)));
267 docstring const result = browseFile(from_utf8(
268 libFileSearch(to_utf8(dir), to_utf8(name), to_utf8(ext)).absFilename()),
269 title, filters, false, dir1, dir2);
271 // remove the extension if it is the default one
272 docstring noextresult;
273 if (from_utf8(getExtension(to_utf8(result))) == ext)
274 noextresult = from_utf8(removeExtension(to_utf8(result)));
276 noextresult = result;
278 // remove the directory, if it is the default one
279 docstring const file = from_utf8(onlyFilename(to_utf8(noextresult)));
280 if (from_utf8(libFileSearch(to_utf8(dir), to_utf8(file), to_utf8(ext)).absFilename()) == result)
287 docstring browseDir(docstring const & pathname, docstring const & title,
288 docstring const & label1, docstring const & dir1,
289 docstring const & label2, docstring const & dir2)
291 docstring lastPath = from_ascii(".");
292 if (!pathname.empty())
293 lastPath = from_utf8(onlyPath(to_utf8(pathname)));
295 FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
296 dlg.setButton1(label1, dir1);
297 dlg.setButton2(label2, dir2);
299 FileDialog::Result const result =
300 dlg.opendir(lastPath, from_utf8(onlyFilename(to_utf8(pathname))));
302 return result.second;
306 void rescanTexStyles()
308 // Run rescan in user lyx directory
309 PathChanger p(package().user_support());
310 FileName const command = libFileSearch("scripts", "TeXFiles.py");
312 int const status = one.startscript(Systemcall::Wait,
314 quoteName(command.toFilesystemEncoding()));
318 frontend::Alert::error(_("Could not update TeX information"),
319 bformat(_("The script `%s' failed."), from_utf8(command.absFilename())));
323 void getTexFileList(string const & filename, vector<string> & list)
326 FileName const file = libFileSearch("", filename);
331 vector<docstring> doclist =
332 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
334 // Normalise paths like /foo//bar ==> /foo/bar
335 boost::RegEx regex("/{2,}");
336 vector<docstring>::iterator it = doclist.begin();
337 vector<docstring>::iterator end = doclist.end();
338 for (; it != end; ++it)
339 list.push_back(regex.Merge(to_utf8(*it), "/"));
341 // remove empty items and duplicates
342 list.erase(remove(list.begin(), list.end(), ""), list.end());
343 eliminate_duplicates(list);