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());
209 void rescanTexStyles()
211 // Run rescan in user lyx directory
212 PathChanger p(package().user_support());
213 FileName const command = libFileSearch("scripts", "TeXFiles.py");
215 int const status = one.startscript(Systemcall::Wait,
217 quoteName(command.toFilesystemEncoding()));
221 frontend::Alert::error(_("Could not update TeX information"),
222 bformat(_("The script `%s' failed."), from_utf8(command.absFilename())));
226 void getTexFileList(string const & filename, vector<string> & list)
229 FileName const file = libFileSearch("", filename);
234 vector<docstring> doclist =
235 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
237 // Normalise paths like /foo//bar ==> /foo/bar
238 boost::RegEx regex("/{2,}");
239 vector<docstring>::iterator it = doclist.begin();
240 vector<docstring>::iterator end = doclist.end();
241 for (; it != end; ++it)
242 list.push_back(regex.Merge(to_utf8(*it), "/"));
244 // remove empty items and duplicates
245 list.erase(remove(list.begin(), list.end(), ""), list.end());
246 eliminate_duplicates(list);
250 QString internalPath(const QString & str)
252 return toqstr(os::internal_path(fromqstr(str)));
256 QString onlyFilename(const QString & str)
258 return toqstr(support::onlyFilename(fromqstr(str)));
262 QString onlyPath(const QString & str)
264 return toqstr(support::onlyPath(fromqstr(str)));