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 vector<LanguagePair>::size_type const size = character_dlg ?
203 languages.size() + 2 : languages.size();
205 vector<LanguagePair> langs(size);
208 langs[0].first = _("No change");
209 langs[0].second = "ignore";
210 langs[1].first = _("Reset");
211 langs[1].second = "reset";
214 vector<string>::size_type i = character_dlg ? 2 : 0;
215 for (Languages::const_iterator cit = languages.begin();
216 cit != languages.end(); ++cit) {
217 langs[i].first = _(cit->second.display());
218 langs[i].second = cit->second.lang();
222 // Don't sort "ignore" and "reset"
223 vector<LanguagePair>::iterator begin = character_dlg ?
224 langs.begin() + 2 : langs.begin();
226 std::sort(begin, langs.end(), Sorter());
232 docstring browseFile(docstring const & filename, docstring const & title,
233 FileFilterList const & filters, bool save,
234 docstring const & label1, docstring const & dir1,
235 docstring const & label2, docstring const & dir2)
237 docstring lastPath = from_ascii(".");
238 if (!filename.empty())
239 lastPath = from_utf8(onlyPath(to_utf8(filename)));
241 FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
242 dlg.setButton1(label1, dir1);
243 dlg.setButton2(label2, dir2);
245 FileDialog::Result result;
248 result = dlg.save(lastPath, filters,
249 from_utf8(onlyFilename(to_utf8(filename))));
251 result = dlg.open(lastPath, filters,
252 from_utf8(onlyFilename(to_utf8(filename))));
254 return result.second;
258 docstring browseRelFile(docstring const & filename, docstring const & refpath,
259 docstring const & title, FileFilterList const & filters, bool save,
260 docstring const & label1, docstring const & dir1,
261 docstring const & label2, docstring const & dir2)
263 docstring const fname = from_utf8(makeAbsPath(
264 to_utf8(filename), to_utf8(refpath)).absFilename());
266 docstring const outname = browseFile(fname, title, filters, save,
267 label1, dir1, label2, dir2);
268 docstring const reloutname = makeRelPath(outname, refpath);
269 if (prefixIs(reloutname, from_ascii("../")))
276 docstring browseLibFile(docstring const & dir, docstring const & name,
277 docstring const & ext, docstring const & title,
278 FileFilterList const & filters)
281 docstring const label1 = _("System files|#S#s");
282 docstring const dir1 =
283 from_utf8(addName(package().system_support().absFilename(), to_utf8(dir)));
285 docstring const label2 = _("User files|#U#u");
286 docstring const dir2 =
287 from_utf8(addName(package().user_support().absFilename(), to_utf8(dir)));
289 docstring const result = browseFile(from_utf8(
290 libFileSearch(to_utf8(dir), to_utf8(name), to_utf8(ext)).absFilename()),
291 title, filters, false, dir1, dir2);
293 // remove the extension if it is the default one
294 docstring noextresult;
295 if (from_utf8(getExtension(to_utf8(result))) == ext)
296 noextresult = from_utf8(removeExtension(to_utf8(result)));
298 noextresult = result;
300 // remove the directory, if it is the default one
301 docstring const file = from_utf8(onlyFilename(to_utf8(noextresult)));
302 if (from_utf8(libFileSearch(to_utf8(dir), to_utf8(file), to_utf8(ext)).absFilename()) == result)
309 docstring browseDir(docstring const & pathname, docstring const & title,
310 docstring const & label1, docstring const & dir1,
311 docstring const & label2, docstring const & dir2)
313 docstring lastPath = from_ascii(".");
314 if (!pathname.empty())
315 lastPath = from_utf8(onlyPath(to_utf8(pathname)));
317 FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
318 dlg.setButton1(label1, dir1);
319 dlg.setButton2(label2, dir2);
321 FileDialog::Result const result =
322 dlg.opendir(lastPath, from_utf8(onlyFilename(to_utf8(pathname))));
324 return result.second;
328 void rescanTexStyles()
330 // Run rescan in user lyx directory
331 support::PathChanger p(package().user_support());
332 FileName const command = libFileSearch("scripts", "TeXFiles.py");
334 int const status = one.startscript(Systemcall::Wait,
335 support::os::python() + ' ' +
336 quoteName(command.toFilesystemEncoding()));
340 frontend::Alert::error(_("Could not update TeX information"),
341 bformat(_("The script `%s' failed."), from_utf8(command.absFilename())));
345 void getTexFileList(string const & filename, std::vector<string> & list)
348 FileName const file = libFileSearch("", filename);
352 list = getVectorFromString(file.fileContents(), "\n");
354 // Normalise paths like /foo//bar ==> /foo/bar
355 boost::RegEx regex("/{2,}");
356 std::vector<string>::iterator it = list.begin();
357 std::vector<string>::iterator end = list.end();
358 for (; it != end; ++it)
359 *it = regex.Merge((*it), "/");
361 // remove empty items and duplicates
362 list.erase(std::remove(list.begin(), list.end(), ""), list.end());
363 eliminate_duplicates(list);