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