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 "LengthCombo.h"
19 #include "support/debug.h"
20 #include "support/gettext.h"
24 #include "frontends/FileDialog.h"
25 #include "frontends/alert.h"
27 #include "support/filetools.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>
50 using support::addName;
51 using support::bformat;
52 using support::FileFilterList;
53 using support::FileName;
54 using support::getExtension;
55 using support::getVectorFromString;
56 using support::libFileSearch;
57 using support::makeAbsPath;
58 using support::makeRelPath;
59 using support::onlyFilename;
60 using support::onlyPath;
61 using support::package;
62 using support::prefixIs;
63 using support::quoteName;
64 using support::removeExtension;
65 using support::Systemcall;
67 using support::isStrDbl;
71 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
73 QString const length = input->text();
77 // Don't return unit-from-choice if the input(field) contains a unit
78 if (isValidGlueLength(fromqstr(length)))
79 return fromqstr(length);
81 Length::UNIT const unit = combo->currentLengthItem();
83 return Length(length.toDouble(), unit).asString();
87 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
89 QString const length = input->text();
93 // don't return unit-from-choice if the input(field) contains a unit
94 if (isValidGlueLength(fromqstr(length)))
95 return Length(fromqstr(length));
97 Length::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
99 return Length(length.toDouble(), unit);
103 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
104 Length const & len, Length::UNIT /*defaultUnit*/)
106 combo->setCurrentItem(len.unit());
107 input->setText(QString::number(Length(len).value()));
111 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
112 string const & len, Length::UNIT defaultUnit)
115 // no length (UNIT_NONE)
116 combo->setCurrentItem(defaultUnit);
118 } else if (!isValidLength(len) && !isStrDbl(len)) {
119 // use input field only for gluelengths
120 combo->setCurrentItem(defaultUnit);
121 input->setText(toqstr(len));
123 lengthToWidgets(input, combo, Length(len), defaultUnit);
128 void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo,
129 Length const & len, Length::UNIT defaultUnit)
131 if (len.value() == 0)
132 lengthToWidgets(input, combo, "auto", defaultUnit);
134 lengthToWidgets(input, combo, len, defaultUnit);
138 void setValid(QWidget * widget, bool valid)
141 widget->setPalette(QPalette());
143 QPalette pal = widget->palette();
144 pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
145 widget->setPalette(pal);
149 } // namespace frontend
151 QString const qt_(char const * str, const char *)
153 return toqstr(_(str));
157 QString const qt_(string const & str)
159 return toqstr(_(str));
167 #if !defined(USE_WCHAR_T) && defined(__GNUC__)
168 bool operator()(LanguagePair const & lhs, LanguagePair const & rhs) const {
169 return lhs.first < rhs.first;
172 Sorter() : loc_ok(true)
175 loc_ = std::locale("");
181 bool operator()(LanguagePair const & lhs,
182 LanguagePair const & rhs) const {
184 return loc_(lhs.first, rhs.first);
186 return lhs.first < rhs.first;
198 vector<LanguagePair> const getLanguageData(bool character_dlg)
200 size_t const size = languages.size() + (character_dlg ? 2 : 0);
202 vector<LanguagePair> langs(size);
205 langs[0].first = _("No change");
206 langs[0].second = "ignore";
207 langs[1].first = _("Reset");
208 langs[1].second = "reset";
211 size_t i = character_dlg ? 2 : 0;
212 for (Languages::const_iterator cit = languages.begin();
213 cit != languages.end(); ++cit) {
214 langs[i].first = _(cit->second.display());
215 langs[i].second = cit->second.lang();
219 // Don't sort "ignore" and "reset"
220 vector<LanguagePair>::iterator begin = character_dlg ?
221 langs.begin() + 2 : langs.begin();
223 std::sort(begin, langs.end(), Sorter());
229 docstring browseFile(docstring const & filename, docstring const & title,
230 FileFilterList const & filters, bool save,
231 docstring const & label1, docstring const & dir1,
232 docstring const & label2, docstring const & dir2)
234 docstring lastPath = from_ascii(".");
235 if (!filename.empty())
236 lastPath = from_utf8(onlyPath(to_utf8(filename)));
238 FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
239 dlg.setButton1(label1, dir1);
240 dlg.setButton2(label2, dir2);
242 FileDialog::Result result;
245 result = dlg.save(lastPath, filters,
246 from_utf8(onlyFilename(to_utf8(filename))));
248 result = dlg.open(lastPath, filters,
249 from_utf8(onlyFilename(to_utf8(filename))));
251 return result.second;
255 docstring browseRelFile(docstring const & filename, docstring const & refpath,
256 docstring const & title, FileFilterList const & filters, bool save,
257 docstring const & label1, docstring const & dir1,
258 docstring const & label2, docstring const & dir2)
260 docstring const fname = from_utf8(makeAbsPath(
261 to_utf8(filename), to_utf8(refpath)).absFilename());
263 docstring const outname = browseFile(fname, title, filters, save,
264 label1, dir1, label2, dir2);
265 docstring const reloutname = makeRelPath(outname, refpath);
266 if (prefixIs(reloutname, from_ascii("../")))
273 docstring browseLibFile(docstring const & dir, docstring const & name,
274 docstring const & ext, docstring const & title,
275 FileFilterList const & filters)
278 docstring const label1 = _("System files|#S#s");
279 docstring const dir1 =
280 from_utf8(addName(package().system_support().absFilename(), to_utf8(dir)));
282 docstring const label2 = _("User files|#U#u");
283 docstring const dir2 =
284 from_utf8(addName(package().user_support().absFilename(), to_utf8(dir)));
286 docstring const result = browseFile(from_utf8(
287 libFileSearch(to_utf8(dir), to_utf8(name), to_utf8(ext)).absFilename()),
288 title, filters, false, dir1, dir2);
290 // remove the extension if it is the default one
291 docstring noextresult;
292 if (from_utf8(getExtension(to_utf8(result))) == ext)
293 noextresult = from_utf8(removeExtension(to_utf8(result)));
295 noextresult = result;
297 // remove the directory, if it is the default one
298 docstring const file = from_utf8(onlyFilename(to_utf8(noextresult)));
299 if (from_utf8(libFileSearch(to_utf8(dir), to_utf8(file), to_utf8(ext)).absFilename()) == result)
306 docstring browseDir(docstring const & pathname, docstring const & title,
307 docstring const & label1, docstring const & dir1,
308 docstring const & label2, docstring const & dir2)
310 docstring lastPath = from_ascii(".");
311 if (!pathname.empty())
312 lastPath = from_utf8(onlyPath(to_utf8(pathname)));
314 FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
315 dlg.setButton1(label1, dir1);
316 dlg.setButton2(label2, dir2);
318 FileDialog::Result const result =
319 dlg.opendir(lastPath, from_utf8(onlyFilename(to_utf8(pathname))));
321 return result.second;
325 void rescanTexStyles()
327 // Run rescan in user lyx directory
328 support::PathChanger p(package().user_support());
329 FileName const command = libFileSearch("scripts", "TeXFiles.py");
331 int const status = one.startscript(Systemcall::Wait,
332 support::os::python() + ' ' +
333 quoteName(command.toFilesystemEncoding()));
337 frontend::Alert::error(_("Could not update TeX information"),
338 bformat(_("The script `%s' failed."), from_utf8(command.absFilename())));
342 void getTexFileList(string const & filename, std::vector<string> & list)
345 FileName const file = libFileSearch("", filename);
350 std::vector<docstring> doclist =
351 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
353 // Normalise paths like /foo//bar ==> /foo/bar
354 boost::RegEx regex("/{2,}");
355 std::vector<docstring>::iterator it = doclist.begin();
356 std::vector<docstring>::iterator end = doclist.end();
357 for (; it != end; ++it)
358 list.push_back(regex.Merge(to_utf8(*it), "/"));
360 // remove empty items and duplicates
361 list.erase(std::remove(list.begin(), list.end(), ""), list.end());
362 eliminate_duplicates(list);