]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/qt_helpers.h
Allow entering localized lengths with unit (#11852)
[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 replace localized decimal separator by dot
76 QString unlocString(QString const & str);
77
78 /// Method to sort QStrings locale-aware (e.g. in combo widgets)
79 bool SortLocaleAware(QString const & lhs, QString const & rhs);
80
81 /// Method to sort colors by GUI name in combo widgets
82 bool ColorSorter(ColorCode lhs, ColorCode rhs);
83
84 /// colors a widget red if invalid
85 void setValid(QWidget * widget, bool valid);
86
87 // set focus and highlight the current item if there is no selection already
88 void focusAndHighlight(QAbstractItemView * w);
89
90 // Sets all widget in highlighted in highlighted colour, and their children in
91 // plain in standard colours.
92 void setMessageColour(std::list<QWidget *> highlighted,
93                       std::list<QWidget *> plain);
94
95 /// Qt5 changed setSectionMode to setSectionResizeMode
96 /// These wrappers work for Qt4 and Qt5
97 void setSectionResizeMode(QHeaderView * view,
98     int logicalIndex, QHeaderView::ResizeMode mode);
99 void setSectionResizeMode(QHeaderView * view,
100         QHeaderView::ResizeMode mode);
101 /// Shows a directory in OSs file browser
102 void showDirectory(support::FileName const & directory);
103 /// handle request for showing citation content - shows pdf/ps or
104 /// web page in target; external script can be used for pdf/ps view
105 /// \p pdfv takes a pad viewer, \p psv a ps viewer
106 void showTarget(std::string const & target,
107                 std::string const & pdfv,
108                 std::string const & psv);
109
110 } // namespace frontend
111
112
113 /**
114  * qt_ - i18nize string and convert to QString
115  *
116  * Use this in qt/ instead of _()
117  */
118 QString const qt_(std::string const & str);
119 QString const qt_(QString const & qstr);
120
121
122 ///
123 support::FileName libFileSearch(QString const & dir, QString const & name,
124                                 QString const & ext = QString(),
125                                 support::search_mode mode = support::must_exist);
126
127 ///
128 support::FileName imageLibFileSearch(QString & dir, QString const & name,
129                                 QString const & ext = QString(),
130                                 support::search_mode mode = support::must_exist);
131
132 /** Wrappers around browseFile which try to provide a filename
133         relative to relpath.
134
135 \param title: title for dialog
136
137 \param filters: *.ps, etc
138
139 \param save: whether to save dialog info (current path, etc) for next use.
140
141 The \param labelN and \param dirN arguments provide for extra buttons
142 in the dialog (e.g., "Templates" and a path to that directory).
143
144 The difference between the functions concerns when we think we have a
145 relative path.
146
147 In \c browseRelToParent, we return a relative path only if it IS NOT of
148         the form "../../foo.txt".
149
150 In \c browseRelToSub, we return a relative path only if it IS of the
151  form "../../foo.txt".
152 */
153 QString browseRelToParent(QString const & filename,
154         QString const & relpath,
155         QString const & title,
156         QStringList const & filters,
157         bool save = false,
158         QString const & label1 = QString(),
159         QString const & dir1 = QString(),
160         QString const & label2 = QString(),
161         QString const & dir2 = QString());
162
163 QString browseRelToSub(QString const & filename,
164         QString const & relpath,
165         QString const & title,
166         QStringList const & filters,
167         bool save = false,
168         QString const & label1 = QString(),
169         QString const & dir1 = QString(),
170         QString const & label2 = QString(),
171         QString const & dir2 = QString());
172
173 /** Build filelists of all available bst/cls/sty-files. Done through
174 *  kpsewhich and an external script, saved in *Files.lst.
175 *  \param arg: cls, sty, bst, or bib, as required by TeXFiles.py.
176 *         Can be a list of these, too.
177 */
178 void rescanTexStyles(std::string const & arg = empty_string());
179
180 /** Fill \c contents from one of the three texfiles.
181  *  Each entry in the file list is returned as a name_with_path
182  */
183 QStringList texFileList(QString const & filename);
184
185 /// Convert internal line endings to line endings as expected by the OS
186 QString const externalLineEnding(docstring const & str);
187
188 /// Convert line endings in any formnat to internal line endings
189 docstring const internalLineEnding(QString const & str);
190
191 // wrapper around the docstring versions
192 QString internalPath(QString const &);
193 QString onlyFileName(QString const & str);
194 QString onlyPath(QString const & str);
195 QStringList fileFilters(QString const & description);
196
197 /// Remove the extension from \p name
198 QString removeExtension(QString const & name);
199
200 /** Add the extension \p ext to \p name.
201  Use this instead of changeExtension if you know that \p name is without
202  extension, because changeExtension would wrongly interpret \p name if it
203  contains a dot.
204  */
205 QString addExtension(QString const & name, QString const & extension);
206
207 /// Return the extension of the file (not including the .)
208 QString getExtension(QString const & name);
209 QString makeAbsPath(QString const & relpath, QString const & base);
210 QString changeExtension(QString const & oldname, QString const & ext);
211
212 /// Format \param text for display as a ToolTip, breaking at lines of \param
213 /// width ems. Note: this function is expensive. Better call it in a delayed
214 /// manner, i.e. not to fill in a model (see for instance the function
215 /// ToolTipFormatter::eventFilter).
216 ///
217 /// When is it called automatically? Whenever the tooltip is not already rich
218 /// text beginning with <html>, and is defined by the following functions:
219 ///  - QWidget::setToolTip(),
220 ///  - QAbstractItemModel::setData(..., Qt::ToolTipRole),
221 ///  - Inset::toolTip()
222 ///
223 /// In other words, tooltips can use Qt html, and the tooltip will still be
224 /// correctly broken. Moreover, it is possible to specify an entirely custom
225 /// tooltip (not subject to automatic formatting) by giving it in its entirety,
226 /// i.e. starting with <html>.
227 QString formatToolTip(QString text, int width = 30);
228
229
230 #if QT_VERSION < 0x050300
231 // Very partial implementation of QSignalBlocker for archaic qt versions.
232 class QSignalBlocker {
233 public:
234         explicit QSignalBlocker(QObject * o)
235                 : obj(o), init_state(obj && obj->blockSignals(true)) {}
236
237         ~QSignalBlocker() {
238                 if (obj)
239                         obj->blockSignals(init_state);
240         }
241 private:
242         QObject * obj;
243         bool init_state;
244 };
245 #endif
246
247
248 // Check if text is understood as rich text (Qt HTML) and if so, produce a
249 // rendering in plain text.
250 QString qtHtmlToPlainText(QString const & text);
251
252
253 } // namespace lyx
254
255 #endif // QTHELPERS_H