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