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