]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.cpp
fix memory leaks
[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 "support/debug.h"
20 #include "support/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         size_t const size = languages.size() + (character_dlg ? 2 : 0);
203
204         vector<LanguagePair> langs(size);
205
206         if (character_dlg) {
207                 langs[0].first = _("No change");
208                 langs[0].second = "ignore";
209                 langs[1].first = _("Reset");
210                 langs[1].second = "reset";
211         }
212
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();
218                 ++i;
219         }
220
221         // Don't sort "ignore" and "reset"
222         vector<LanguagePair>::iterator begin = character_dlg ?
223                 langs.begin() + 2 : langs.begin();
224
225         std::sort(begin, langs.end(), Sorter());
226
227         return langs;
228 }
229
230
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)
235 {
236         docstring lastPath = from_ascii(".");
237         if (!filename.empty())
238                 lastPath = from_utf8(onlyPath(to_utf8(filename)));
239
240         FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
241         dlg.setButton1(label1, dir1);
242         dlg.setButton2(label2, dir2);
243
244         FileDialog::Result result;
245
246         if (save)
247                 result = dlg.save(lastPath, filters,
248                                       from_utf8(onlyFilename(to_utf8(filename))));
249         else
250                 result = dlg.open(lastPath, filters,
251                                       from_utf8(onlyFilename(to_utf8(filename))));
252
253         return result.second;
254 }
255
256
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)
261 {
262         docstring const fname = from_utf8(makeAbsPath(
263                 to_utf8(filename), to_utf8(refpath)).absFilename());
264
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("../")))
269                 return outname;
270         else
271                 return reloutname;
272 }
273
274
275 docstring browseLibFile(docstring const & dir, docstring const & name,
276         docstring const & ext, docstring const & title,
277         FileFilterList const & filters)
278 {
279         // FIXME UNICODE
280         docstring const label1 = _("System files|#S#s");
281         docstring const dir1 =
282                 from_utf8(addName(package().system_support().absFilename(), to_utf8(dir)));
283
284         docstring const label2 = _("User files|#U#u");
285         docstring const dir2 =
286                 from_utf8(addName(package().user_support().absFilename(), to_utf8(dir)));
287
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);
291
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)));
296         else
297                 noextresult = result;
298
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)
302                 return file;
303         else
304                 return noextresult;
305 }
306
307
308 docstring browseDir(docstring const & pathname, docstring const & title,
309         docstring const & label1, docstring const & dir1,
310         docstring const & label2, docstring const & dir2)
311 {
312         docstring lastPath = from_ascii(".");
313         if (!pathname.empty())
314                 lastPath = from_utf8(onlyPath(to_utf8(pathname)));
315
316         FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
317         dlg.setButton1(label1, dir1);
318         dlg.setButton2(label2, dir2);
319
320         FileDialog::Result const result =
321                 dlg.opendir(lastPath, from_utf8(onlyFilename(to_utf8(pathname))));
322
323         return result.second;
324 }
325
326
327 void rescanTexStyles()
328 {
329         // Run rescan in user lyx directory
330         support::PathChanger p(package().user_support());
331         FileName const command = libFileSearch("scripts", "TeXFiles.py");
332         Systemcall one;
333         int const status = one.startscript(Systemcall::Wait,
334                         support::os::python() + ' ' +
335                         quoteName(command.toFilesystemEncoding()));
336         if (status == 0)
337                 return;
338         // FIXME UNICODE
339         frontend::Alert::error(_("Could not update TeX information"),
340                 bformat(_("The script `%s' failed."), from_utf8(command.absFilename())));
341 }
342
343
344 void getTexFileList(string const & filename, std::vector<string> & list)
345 {
346         list.clear();
347         FileName const file = libFileSearch("", filename);
348         if (file.empty())
349                 return;
350
351         // FIXME Unicode.
352         std::vector<docstring> doclist = 
353                 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
354
355         // Normalise paths like /foo//bar ==> /foo/bar
356         boost::RegEx regex("/{2,}");
357         std::vector<docstring>::iterator it  = doclist.begin();
358         std::vector<docstring>::iterator end = doclist.end();
359         for (; it != end; ++it)
360                 list.push_back(regex.Merge(to_utf8(*it), "/"));
361
362         // remove empty items and duplicates
363         list.erase(std::remove(list.begin(), list.end(), ""), list.end());
364         eliminate_duplicates(list);
365 }
366
367 } // namespace lyx