]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.cpp
1fc4fbbdc6798934f4aff5476056432c73d6e765
[lyx.git] / src / frontends / qt4 / qt_helpers.cpp
1 /**
2  * \file qt_helpers.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  * \author Jürgen Spitzmüller
8  * \author Richard Heck
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "qt_helpers.h"
16
17 #include "LengthCombo.h"
18
19 #include "debug.h"
20 #include "gettext.h"
21 #include "Language.h"
22 #include "Length.h"
23
24 #include "frontends/FileDialog.h"
25 #include "frontends/alert.h"
26
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"
34
35 #include <QComboBox>
36 #include <QCheckBox>
37 #include <QPalette>
38 #include <QLineEdit>
39
40 #include <boost/cregex.hpp>
41
42 #include <algorithm>
43 #include <fstream>
44 #include <locale>
45
46 using std::string;
47 using std::vector;
48 using std::endl;
49
50 namespace lyx {
51
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;
68 using support::token;
69 using support::isStrDbl;
70
71 namespace frontend {
72
73 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
74 {
75         QString const length = input->text();
76         if (length.isEmpty())
77                 return string();
78
79         // Don't return unit-from-choice if the input(field) contains a unit
80         if (isValidGlueLength(fromqstr(length)))
81                 return fromqstr(length);
82
83         Length::UNIT const unit = combo->currentLengthItem();
84
85         return Length(length.toDouble(), unit).asString();
86 }
87
88
89 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
90 {
91         QString const length = input->text();
92         if (length.isEmpty())
93                 return Length();
94
95         // don't return unit-from-choice if the input(field) contains a unit
96         if (isValidGlueLength(fromqstr(length)))
97                 return Length(fromqstr(length));
98
99         Length::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
100
101         return Length(length.toDouble(), unit);
102 }
103
104
105 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
106                      Length const & len, Length::UNIT /*defaultUnit*/)
107 {
108         combo->setCurrentItem(len.unit());
109         input->setText(QString::number(Length(len).value()));
110 }
111
112
113 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
114         string const & len, Length::UNIT defaultUnit)
115 {
116         if (len.empty()) {
117                 // no length (UNIT_NONE)
118                 combo->setCurrentItem(defaultUnit);
119                 input->setText("");
120         } else if (!isValidLength(len) && !isStrDbl(len)) {
121                 // use input field only for gluelengths
122                 combo->setCurrentItem(defaultUnit);
123                 input->setText(toqstr(len));
124         } else {
125                 lengthToWidgets(input, combo, Length(len), defaultUnit);
126         }
127 }
128
129
130 void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo,
131         Length const & len, Length::UNIT defaultUnit)
132 {
133         if (len.value() == 0)
134                 lengthToWidgets(input, combo, "auto", defaultUnit);
135         else
136                 lengthToWidgets(input, combo, len, defaultUnit);
137 }
138
139
140 void setValid(QWidget * widget, bool valid)
141 {
142         if (valid) {
143                 widget->setPalette(QPalette());
144         } else {
145                 QPalette pal = widget->palette();
146                 pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
147                 widget->setPalette(pal);
148         }
149 }
150
151 } // namespace frontend
152
153 QString const qt_(char const * str, const char *)
154 {
155         return toqstr(_(str));
156 }
157
158
159 QString const qt_(string const & str)
160 {
161         return toqstr(_(str));
162 }
163
164 namespace {
165
166 class Sorter
167 {
168 public:
169 #if !defined(USE_WCHAR_T) && defined(__GNUC__)
170         bool operator()(LanguagePair const & lhs, LanguagePair const & rhs) const {
171                 return lhs.first < rhs.first;
172         }
173 #else
174         Sorter() : loc_ok(true)
175         {
176                 try {
177                         loc_ = std::locale("");
178                 } catch (...) {
179                         loc_ok = false;
180                 }
181         };
182
183         bool operator()(LanguagePair const & lhs,
184                         LanguagePair const & rhs) const {
185                 if (loc_ok)
186                         return loc_(lhs.first, rhs.first);
187                 else
188                         return lhs.first < rhs.first;
189         }
190 private:
191         std::locale loc_;
192         bool loc_ok;
193 #endif
194 };
195
196
197 } // namespace anon
198
199
200 vector<LanguagePair> const getLanguageData(bool character_dlg)
201 {
202         vector<LanguagePair>::size_type const size = character_dlg ?
203                 languages.size() + 2 : languages.size();
204
205         vector<LanguagePair> langs(size);
206
207         if (character_dlg) {
208                 langs[0].first = _("No change");
209                 langs[0].second = "ignore";
210                 langs[1].first = _("Reset");
211                 langs[1].second = "reset";
212         }
213
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();
219                 ++i;
220         }
221
222         // Don't sort "ignore" and "reset"
223         vector<LanguagePair>::iterator begin = character_dlg ?
224                 langs.begin() + 2 : langs.begin();
225
226         std::sort(begin, langs.end(), Sorter());
227
228         return langs;
229 }
230
231
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)
236 {
237         docstring lastPath = from_ascii(".");
238         if (!filename.empty())
239                 lastPath = from_utf8(onlyPath(to_utf8(filename)));
240
241         FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
242         dlg.setButton1(label1, dir1);
243         dlg.setButton2(label2, dir2);
244
245         FileDialog::Result result;
246
247         if (save)
248                 result = dlg.save(lastPath, filters,
249                                       from_utf8(onlyFilename(to_utf8(filename))));
250         else
251                 result = dlg.open(lastPath, filters,
252                                       from_utf8(onlyFilename(to_utf8(filename))));
253
254         return result.second;
255 }
256
257
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)
262 {
263         docstring const fname = from_utf8(makeAbsPath(
264                 to_utf8(filename), to_utf8(refpath)).absFilename());
265
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("../")))
270                 return outname;
271         else
272                 return reloutname;
273 }
274
275
276 docstring browseLibFile(docstring const & dir, docstring const & name,
277         docstring const & ext, docstring const & title,
278         FileFilterList const & filters)
279 {
280         // FIXME UNICODE
281         docstring const label1 = _("System files|#S#s");
282         docstring const dir1 =
283                 from_utf8(addName(package().system_support().absFilename(), to_utf8(dir)));
284
285         docstring const label2 = _("User files|#U#u");
286         docstring const dir2 =
287                 from_utf8(addName(package().user_support().absFilename(), to_utf8(dir)));
288
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);
292
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)));
297         else
298                 noextresult = result;
299
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)
303                 return file;
304         else
305                 return noextresult;
306 }
307
308
309 docstring browseDir(docstring const & pathname, docstring const & title,
310         docstring const & label1, docstring const & dir1,
311         docstring const & label2, docstring const & dir2)
312 {
313         docstring lastPath = from_ascii(".");
314         if (!pathname.empty())
315                 lastPath = from_utf8(onlyPath(to_utf8(pathname)));
316
317         FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
318         dlg.setButton1(label1, dir1);
319         dlg.setButton2(label2, dir2);
320
321         FileDialog::Result const result =
322                 dlg.opendir(lastPath, from_utf8(onlyFilename(to_utf8(pathname))));
323
324         return result.second;
325 }
326
327
328 void rescanTexStyles()
329 {
330         // Run rescan in user lyx directory
331         support::PathChanger p(package().user_support());
332         FileName const command = libFileSearch("scripts", "TeXFiles.py");
333         Systemcall one;
334         int const status = one.startscript(Systemcall::Wait,
335                         support::os::python() + ' ' +
336                         quoteName(command.toFilesystemEncoding()));
337         if (status == 0)
338                 return;
339         // FIXME UNICODE
340         frontend::Alert::error(_("Could not update TeX information"),
341                 bformat(_("The script `%s' failed."), from_utf8(command.absFilename())));
342 }
343
344
345 void getTexFileList(string const & filename, std::vector<string> & list)
346 {
347         list.clear();
348         FileName const file = libFileSearch("", filename);
349         if (file.empty())
350                 return;
351
352         list = getVectorFromString(file.fileContents(), "\n");
353
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), "/");
360
361         // remove empty items and duplicates
362         list.erase(std::remove(list.begin(), list.end(), ""), list.end());
363         eliminate_duplicates(list);
364 }
365
366 } // namespace lyx