]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/qt_helpers.h
Centralize use of "all files" wildcard in dialogs so it's consistent.
[lyx.git] / src / frontends / qt / qt_helpers.h
1 // -*- C++ -*-
2 /**
3  * \file qt_helpers.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Dekel Tsur
8  * \author Richard Kimberly Heck
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef QTHELPERS_H
14 #define QTHELPERS_H
15
16 #include "ColorSet.h"
17 #include "support/Length.h"
18 #include "support/qstring_helpers.h"
19 #include "support/filetools.h"
20 #include "qt_i18n.h"
21
22 #include <list>
23
24 #include <QHeaderView>
25
26 class QComboBox;
27 class QLineEdit;
28 class QString;
29 class QWidget;
30
31 namespace lyx {
32
33 namespace support { class FileName; }
34
35 namespace frontend {
36
37 class LengthCombo;
38
39 /// method to get a Length from widgets (LengthCombo)
40 std::string widgetsToLength(QLineEdit const * input, LengthCombo const * combo);
41 /// method to get a Length from widgets (QComboBox)
42 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo);
43
44 /// method to set widgets from a Length
45 //FIXME Remove default_unit argument for the first form. FIXME Change
46 // all the code to remove default_unit argument when equal to the
47 // default.
48 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
49                      Length const & len,
50                      Length::UNIT default_unit = Length::defaultUnit());
51 /// method to set widgets from a string
52 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
53                      std::string const & len,
54                      Length::UNIT default_unit = Length::defaultUnit());
55 /// method to set widgets from a docstring
56 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
57 docstring const & len, Length::UNIT default_unit);
58
59 /// method to get a double value from a localized widget (QLineEdit)
60 double widgetToDouble(QLineEdit const * input);
61 /// method to get a double value from a localized widget (QLineEdit)
62 std::string widgetToDoubleStr(QLineEdit const * input);
63 /// method to set a (localized) double value in a widget (QLineEdit)
64 void doubleToWidget(QLineEdit * input, double value,
65         char f = 'g', int prec = 6);
66 /// method to set a (localized) double value in a widget (QLineEdit)
67 void doubleToWidget(QLineEdit * input, std::string const & value,
68         char f = 'g', int prec = 6);
69 /**
70  * method to format localized floating point numbers without
71  * ever using scientific notation
72  */
73 QString formatLocFPNumber(double d);
74
75 /// Method to sort QStrings locale-aware (e.g. in combo widgets)
76 bool SortLocaleAware(QString const & lhs, QString const & rhs);
77
78 /// Method to sort colors by GUI name in combo widgets
79 bool ColorSorter(ColorCode lhs, ColorCode rhs);
80
81 /// colors a widget red if invalid
82 void setValid(QWidget * widget, bool valid);
83
84 // set focus and highlight the current item if there is no selection already
85 void focusAndHighlight(QAbstractItemView * w);
86
87 // Sets all widget in highlighted in highlighted colour, and their children in
88 // plain in standard colours.
89 void setMessageColour(std::list<QWidget *> highlighted,
90                       std::list<QWidget *> plain);
91
92 /// Shows a directory in OSs file browser
93 void showDirectory(support::FileName const & directory);
94 /// handle request for showing citation content - shows pdf/ps or
95 /// web page in target; external script can be used for pdf/ps view
96 /// \p pdfv takes a pad viewer, \p psv a ps viewer
97 void showTarget(std::string const & target,
98                 std::string const & pdfv,
99                 std::string const & psv);
100
101 } // namespace frontend
102
103
104 /**
105  * qt_ - i18nize string and convert to QString
106  *
107  * Use this in qt/ instead of _()
108  */
109 QString const qt_(std::string const & str);
110 QString const qt_(QString const & qstr);
111
112
113 ///
114 support::FileName libFileSearch(QString const & dir, QString const & name,
115                                 QString const & ext = QString(),
116                                 support::search_mode mode = support::must_exist);
117
118 ///
119 support::FileName imageLibFileSearch(QString & dir, QString const & name,
120                                 QString const & ext = QString(),
121                                 support::search_mode mode = support::must_exist);
122
123 /** Wrappers around browseFile which try to provide a filename
124         relative to relpath.
125
126 \param title: title for dialog
127
128 \param filters: *.ps, etc
129
130 \param save: whether to save dialog info (current path, etc) for next use.
131
132 The \param labelN and \param dirN arguments provide for extra buttons
133 in the dialog (e.g., "Templates" and a path to that directory).
134
135 The difference between the functions concerns when we think we have a
136 relative path.
137
138 In \c browseRelToParent, we return a relative path only if it IS NOT of
139         the form "../../foo.txt".
140
141 In \c browseRelToSub, we return a relative path only if it IS of the
142  form "../../foo.txt".
143 */
144 QString browseRelToParent(QString const & filename,
145         QString const & relpath,
146         QString const & title,
147         QStringList const & filters,
148         bool save = false,
149         QString const & label1 = QString(),
150         QString const & dir1 = QString(),
151         QString const & label2 = QString(),
152         QString const & dir2 = QString());
153
154 QString browseRelToSub(QString const & filename,
155         QString const & relpath,
156         QString const & title,
157         QStringList const & filters,
158         bool save = false,
159         QString const & label1 = QString(),
160         QString const & dir1 = QString(),
161         QString const & label2 = QString(),
162         QString const & dir2 = QString());
163
164 /** Build filelists of all available bst/cls/sty-files. Done through
165 *  kpsewhich and an external script, saved in *Files.lst.
166 *  \param arg: cls, sty, bst, or bib, as required by TeXFiles.py.
167 *         Can be a list of these, too.
168 */
169 void rescanTexStyles(std::string const & arg = empty_string());
170
171 /** Fill \c contents from one of the three texfiles.
172  *  Each entry in the file list is returned as a name_with_path
173  */
174 QStringList texFileList(QString const & filename);
175
176 /// Convert internal line endings to line endings as expected by the OS
177 QString const externalLineEnding(docstring const & str);
178
179 /// Convert line endings in any formnat to internal line endings
180 docstring const internalLineEnding(QString const & str);
181
182 // wrapper around the docstring versions
183 QString internalPath(QString const &);
184 QString onlyFileName(QString const & str);
185 QString onlyPath(QString const & str);
186 QStringList fileFilters(QString const & description);
187 /// all files wildcard for filter in qt dialogs (crossplatform)
188 QString wildcardAllFiles();
189
190 /// Remove the extension from \p name
191 QString removeExtension(QString const & name);
192
193 /** Add the extension \p ext to \p name.
194  Use this instead of changeExtension if you know that \p name is without
195  extension, because changeExtension would wrongly interpret \p name if it
196  contains a dot.
197  */
198 QString addExtension(QString const & name, QString const & extension);
199
200 /// Return the extension of the file (not including the .)
201 QString getExtension(QString const & name);
202 QString makeAbsPath(QString const & relpath, QString const & base);
203 QString changeExtension(QString const & oldname, QString const & ext);
204
205 /// Format \param text for display as a ToolTip, breaking at lines of \param
206 /// width ems. Note: this function is expensive. Better call it in a delayed
207 /// manner, i.e. not to fill in a model (see for instance the function
208 /// ToolTipFormatter::eventFilter).
209 ///
210 /// When is it called automatically? Whenever the tooltip is not already rich
211 /// text beginning with <html>, and is defined by the following functions:
212 ///  - QWidget::setToolTip(),
213 ///  - QAbstractItemModel::setData(..., Qt::ToolTipRole),
214 ///  - Inset::toolTip()
215 ///
216 /// In other words, tooltips can use Qt html, and the tooltip will still be
217 /// correctly broken. Moreover, it is possible to specify an entirely custom
218 /// tooltip (not subject to automatic formatting) by giving it in its entirety,
219 /// i.e. starting with <html>.
220 QString formatToolTip(QString text, int width = 30);
221
222
223 #if QT_VERSION < 0x050300
224 // Very partial implementation of QSignalBlocker for archaic qt versions.
225 class QSignalBlocker {
226 public:
227         explicit QSignalBlocker(QObject * o)
228                 : obj(o), init_state(obj && obj->blockSignals(true)) {}
229
230         ~QSignalBlocker() {
231                 if (obj)
232                         obj->blockSignals(init_state);
233         }
234 private:
235         QObject * obj;
236         bool init_state;
237 };
238 #endif
239
240
241 // Check if text is understood as rich text (Qt HTML) and if so, produce a
242 // rendering in plain text.
243 QString qtHtmlToPlainText(QString const & text);
244
245
246 } // namespace lyx
247
248 #endif // QTHELPERS_H