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/foreach.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30 #include "support/lyxalgo.h"
31 #include "support/os.h"
32 #include "support/Package.h"
33 #include "support/Path.h"
34 #include "support/Systemcall.h"
47 using namespace lyx::support;
51 FileName libFileSearch(QString const & dir, QString const & name,
54 return support::libFileSearch(fromqstr(dir), fromqstr(name), fromqstr(ext));
59 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
61 QString const length = input->text();
65 // Don't return unit-from-choice if the input(field) contains a unit
66 if (isValidGlueLength(fromqstr(length)))
67 return fromqstr(length);
69 Length::UNIT const unit = combo->currentLengthItem();
71 return Length(length.toDouble(), unit).asString();
75 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
77 QString const length = input->text();
81 // don't return unit-from-choice if the input(field) contains a unit
82 if (isValidGlueLength(fromqstr(length)))
83 return Length(fromqstr(length));
85 Length::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
87 return Length(length.toDouble(), unit);
91 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
92 Length const & len, Length::UNIT /*defaultUnit*/)
94 combo->setCurrentItem(len.unit());
95 input->setText(QString::number(Length(len).value()));
99 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
100 string const & len, Length::UNIT defaultUnit)
103 // no length (UNIT_NONE)
104 combo->setCurrentItem(defaultUnit);
106 } else if (!isValidLength(len) && !isStrDbl(len)) {
107 // use input field only for gluelengths
108 combo->setCurrentItem(defaultUnit);
109 input->setText(toqstr(len));
111 lengthToWidgets(input, combo, Length(len), defaultUnit);
116 void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo,
117 Length const & len, Length::UNIT defaultUnit)
119 if (len.value() == 0)
120 lengthToWidgets(input, combo, "auto", defaultUnit);
122 lengthToWidgets(input, combo, len, defaultUnit);
126 void setValid(QWidget * widget, bool valid)
129 widget->setPalette(QPalette());
131 QPalette pal = widget->palette();
132 pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
133 widget->setPalette(pal);
137 } // namespace frontend
139 QString const qt_(char const * str, const char *)
141 return toqstr(_(str));
145 QString const qt_(string const & str)
147 return toqstr(_(str));
155 #if !defined(USE_WCHAR_T) && defined(__GNUC__)
156 bool operator()(LanguagePair const & lhs, LanguagePair const & rhs) const
158 return lhs.first < rhs.first;
161 Sorter() : loc_ok(true)
170 bool operator()(LanguagePair const & lhs, LanguagePair const & rhs) const
172 // FIXME: would that be "QString::localeAwareCompare()"?
174 return loc_(fromqstr(lhs.first), fromqstr(rhs.first));
176 return lhs.first < rhs.first;
188 QList<LanguagePair> languageData(bool character_dlg)
190 size_t const offset = character_dlg ? 2 : 0;
191 vector<LanguagePair> langs(languages.size() + offset);
194 langs[0].first = qt_("No change");
195 langs[0].second = "ignore";
196 langs[1].first = qt_("Reset");
197 langs[1].second = "reset";
200 Languages::const_iterator it = languages.begin();
201 for (size_t i = 0; i != languages.size(); ++i, ++it) {
202 langs[i + offset].first = qt_(it->second.display());
203 langs[i + offset].second = toqstr(it->second.lang());
206 // Don't sort "ignore" and "reset"
207 vector<LanguagePair>::iterator begin = langs.begin() + offset;
208 sort(begin, langs.end(), Sorter());
210 QList<LanguagePair> list;
211 foreach (LanguagePair const & l, langs)
217 void rescanTexStyles()
219 // Run rescan in user lyx directory
220 PathChanger p(package().user_support());
221 FileName const command = libFileSearch("scripts", "TeXFiles.py");
223 int const status = one.startscript(Systemcall::Wait,
225 quoteName(command.toFilesystemEncoding()));
229 frontend::Alert::error(_("Could not update TeX information"),
230 bformat(_("The script `%s' failed."), from_utf8(command.absFilename())));
234 QStringList texFileList(QString const & filename)
237 FileName const file = libFileSearch(QString(), filename);
242 vector<docstring> doclist =
243 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
245 // Normalise paths like /foo//bar ==> /foo/bar
247 for (size_t i = 0; i != doclist.size(); ++i) {
248 QString file = toqstr(doclist[i]);
249 file.replace("\r", "");
250 while (file.contains("//"))
251 file.replace("//", "/");
257 return QList<QString>::fromSet(set);
261 QString internalPath(const QString & str)
263 return toqstr(os::internal_path(fromqstr(str)));
267 QString onlyFilename(const QString & str)
269 return toqstr(support::onlyFilename(fromqstr(str)));
273 QString onlyPath(const QString & str)
275 return toqstr(support::onlyPath(fromqstr(str)));
279 QString changeExtension(QString const & oldname, QString const & ext)
281 return toqstr(support::changeExtension(fromqstr(oldname), fromqstr(ext)));
284 /// Remove the extension from \p name
285 QString removeExtension(QString const & name)
287 return toqstr(support::removeExtension(fromqstr(name)));
290 /** Add the extension \p ext to \p name.
291 Use this instead of changeExtension if you know that \p name is without
292 extension, because changeExtension would wrongly interpret \p name if it
295 QString addExtension(QString const & name, QString const & ext)
297 return toqstr(support::addExtension(fromqstr(name), fromqstr(ext)));
300 /// Return the extension of the file (not including the .)
301 QString getExtension(QString const & name)
303 return toqstr(support::getExtension(fromqstr(name)));