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"
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>
52 using support::addName;
53 using support::bformat;
54 using support::FileFilterList;
55 using support::FileName;
56 using support::getExtension;
57 using support::getVectorFromString;
58 using support::libFileSearch;
59 using support::makeAbsPath;
60 using support::makeRelPath;
61 using support::onlyFilename;
62 using support::onlyPath;
63 using support::package;
64 using support::prefixIs;
65 using support::quoteName;
66 using support::removeExtension;
67 using support::Systemcall;
69 using support::isStrDbl;
73 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
75 QString const length = input->text();
79 // Don't return unit-from-choice if the input(field) contains a unit
80 if (isValidGlueLength(fromqstr(length)))
81 return fromqstr(length);
83 Length::UNIT const unit = combo->currentLengthItem();
85 return Length(length.toDouble(), unit).asString();
89 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
91 QString const length = input->text();
95 // don't return unit-from-choice if the input(field) contains a unit
96 if (isValidGlueLength(fromqstr(length)))
97 return Length(fromqstr(length));
99 Length::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
101 return Length(length.toDouble(), unit);
105 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
106 Length const & len, Length::UNIT /*defaultUnit*/)
108 combo->setCurrentItem(len.unit());
109 input->setText(QString::number(Length(len).value()));
113 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
114 string const & len, Length::UNIT defaultUnit)
117 // no length (UNIT_NONE)
118 combo->setCurrentItem(defaultUnit);
120 } else if (!isValidLength(len) && !isStrDbl(len)) {
121 // use input field only for gluelengths
122 combo->setCurrentItem(defaultUnit);
123 input->setText(toqstr(len));
125 lengthToWidgets(input, combo, Length(len), defaultUnit);
130 void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo,
131 Length const & len, Length::UNIT defaultUnit)
133 if (len.value() == 0)
134 lengthToWidgets(input, combo, "auto", defaultUnit);
136 lengthToWidgets(input, combo, len, defaultUnit);
140 void setValid(QWidget * widget, bool valid)
143 widget->setPalette(QPalette());
145 QPalette pal = widget->palette();
146 pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
147 widget->setPalette(pal);
151 } // namespace frontend
153 QString const qt_(char const * str, const char *)
155 return toqstr(_(str));
159 QString const qt_(string const & str)
161 return toqstr(_(str));
169 #if !defined(USE_WCHAR_T) && defined(__GNUC__)
170 bool operator()(LanguagePair const & lhs, LanguagePair const & rhs) const {
171 return lhs.first < rhs.first;
174 Sorter() : loc_ok(true)
177 loc_ = std::locale("");
183 bool operator()(LanguagePair const & lhs,
184 LanguagePair const & rhs) const {
186 return loc_(lhs.first, rhs.first);
188 return lhs.first < rhs.first;
200 vector<LanguagePair> const getLanguageData(bool character_dlg)
202 size_t const size = languages.size() + (character_dlg ? 2 : 0);
204 vector<LanguagePair> langs(size);
207 langs[0].first = _("No change");
208 langs[0].second = "ignore";
209 langs[1].first = _("Reset");
210 langs[1].second = "reset";
213 size_t i = character_dlg ? 2 : 0;
214 for (Languages::const_iterator cit = languages.begin();
215 cit != languages.end(); ++cit) {
216 langs[i].first = _(cit->second.display());
217 langs[i].second = cit->second.lang();
221 // Don't sort "ignore" and "reset"
222 vector<LanguagePair>::iterator begin = character_dlg ?
223 langs.begin() + 2 : langs.begin();
225 std::sort(begin, langs.end(), Sorter());
231 docstring browseFile(docstring const & filename, docstring const & title,
232 FileFilterList const & filters, bool save,
233 docstring const & label1, docstring const & dir1,
234 docstring const & label2, docstring const & dir2)
236 docstring lastPath = from_ascii(".");
237 if (!filename.empty())
238 lastPath = from_utf8(onlyPath(to_utf8(filename)));
240 FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
241 dlg.setButton1(label1, dir1);
242 dlg.setButton2(label2, dir2);
244 FileDialog::Result result;
247 result = dlg.save(lastPath, filters,
248 from_utf8(onlyFilename(to_utf8(filename))));
250 result = dlg.open(lastPath, filters,
251 from_utf8(onlyFilename(to_utf8(filename))));
253 return result.second;
257 docstring browseRelFile(docstring const & filename, docstring const & refpath,
258 docstring const & title, FileFilterList const & filters, bool save,
259 docstring const & label1, docstring const & dir1,
260 docstring const & label2, docstring const & dir2)
262 docstring const fname = from_utf8(makeAbsPath(
263 to_utf8(filename), to_utf8(refpath)).absFilename());
265 docstring const outname = browseFile(fname, title, filters, save,
266 label1, dir1, label2, dir2);
267 docstring const reloutname = makeRelPath(outname, refpath);
268 if (prefixIs(reloutname, from_ascii("../")))
275 docstring browseLibFile(docstring const & dir, docstring const & name,
276 docstring const & ext, docstring const & title,
277 FileFilterList const & filters)
280 docstring const label1 = _("System files|#S#s");
281 docstring const dir1 =
282 from_utf8(addName(package().system_support().absFilename(), to_utf8(dir)));
284 docstring const label2 = _("User files|#U#u");
285 docstring const dir2 =
286 from_utf8(addName(package().user_support().absFilename(), to_utf8(dir)));
288 docstring const result = browseFile(from_utf8(
289 libFileSearch(to_utf8(dir), to_utf8(name), to_utf8(ext)).absFilename()),
290 title, filters, false, dir1, dir2);
292 // remove the extension if it is the default one
293 docstring noextresult;
294 if (from_utf8(getExtension(to_utf8(result))) == ext)
295 noextresult = from_utf8(removeExtension(to_utf8(result)));
297 noextresult = result;
299 // remove the directory, if it is the default one
300 docstring const file = from_utf8(onlyFilename(to_utf8(noextresult)));
301 if (from_utf8(libFileSearch(to_utf8(dir), to_utf8(file), to_utf8(ext)).absFilename()) == result)
308 docstring browseDir(docstring const & pathname, docstring const & title,
309 docstring const & label1, docstring const & dir1,
310 docstring const & label2, docstring const & dir2)
312 docstring lastPath = from_ascii(".");
313 if (!pathname.empty())
314 lastPath = from_utf8(onlyPath(to_utf8(pathname)));
316 FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
317 dlg.setButton1(label1, dir1);
318 dlg.setButton2(label2, dir2);
320 FileDialog::Result const result =
321 dlg.opendir(lastPath, from_utf8(onlyFilename(to_utf8(pathname))));
323 return result.second;
327 void rescanTexStyles()
329 // Run rescan in user lyx directory
330 support::PathChanger p(package().user_support());
331 FileName const command = libFileSearch("scripts", "TeXFiles.py");
333 int const status = one.startscript(Systemcall::Wait,
334 support::os::python() + ' ' +
335 quoteName(command.toFilesystemEncoding()));
339 frontend::Alert::error(_("Could not update TeX information"),
340 bformat(_("The script `%s' failed."), from_utf8(command.absFilename())));
344 void getTexFileList(string const & filename, std::vector<string> & list)
347 FileName const file = libFileSearch("", filename);
351 list = getVectorFromString(file.fileContents(), "\n");
353 // Normalise paths like /foo//bar ==> /foo/bar
354 boost::RegEx regex("/{2,}");
355 std::vector<string>::iterator it = list.begin();
356 std::vector<string>::iterator end = list.end();
357 for (; it != end; ++it)
358 *it = regex.Merge((*it), "/");
360 // remove empty items and duplicates
361 list.erase(std::remove(list.begin(), list.end(), ""), list.end());
362 eliminate_duplicates(list);