1
2 /******************************************************************************
3 * MODULE : qt_utilities.hpp
4 * DESCRIPTION: Utilities for QT
5 * COPYRIGHT : (C) 2008 Massimiliano Gubinelli
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11 #ifndef QT_UTILITIES_HPP
12 #define QT_UTILITIES_HPP
13
14 #include "message.hpp"
15
16 #include <QRect>
17 #include <QSize>
18 #include <QPoint>
19 #include <QString>
20 #include <QColor>
21 #include <QFont>
22
23 class QStringList;
24 class QKeySequence;
25
26 typedef quartet<SI,SI,SI,SI> coord4;
27 typedef pair<SI,SI> coord2;
28
29 void qt_dump (QObject* obj, int indent=0);
30
31 /******************************************************************************
32 * Conversion of data types
33 ******************************************************************************/
34
35 QColor to_qcolor (const string& );
36 QColor to_qcolor (color c);
37 string from_qcolor (const QColor& );
38 color to_color (const QColor& );
39
40 QRect to_qrect (const coord4 & p);
41 coord4 from_qrect (const QRect & rect);
42
43 QPoint to_qpoint (const coord2 & p);
44 coord2 from_qpoint (const QPoint & pt);
45
46 QSize to_qsize (const coord2 & p);
47 QSize to_qsize (const SI& w, const SI& h);
48 coord2 from_qsize (const QSize & s);
49
50 QFont to_qfont (int style, QFont font);
51 void qt_apply_tm_style (QWidget* qwid, int style);
52 void qt_apply_tm_style (QWidget* qwid, int style, color c);
53
54 QSize qt_decode_length (string width, string height,
55 const QSize& ref, const QFontMetrics& fm);
56
57 QKeySequence to_qkeysequence (string s);
58
59 QStringList to_qstringlist (array<string> l);
60 array<string> from_qstringlist (const QStringList& l);
61
62 ///// String conversion: Assumes UTF8 encodings both in QT and TeXmacs.
63
64 QString to_qstring (const string& s);
65 string from_qstring (const QString & s);
66 QString utf8_to_qstring (const string& s);
67 QString latin1_to_qstring (const string& s);
68 string from_qstring_utf8 (const QString & s);
69
70 /*! Returns a QString with the translation of the argument to the current
71 language.
72
73 NOTE: translations of gui items are always done in the scheme side using
74 (translate stuff-to-translate), and this is enabled by default for most widgets
75 displaying text. We need not and must not use Qt's mechanism for translations
76 nor even this function, unless the strings to be translated are hardcoded in
77 our code (which is wrong of course). While parsing widgets, etc. nothing is to
78 be done wrt. translations.
79 */
80 QString qt_translate (const string& s);
81
82
83 /******************************************************************************
84 * File formats and their conversion. Other stuff.
85 ******************************************************************************/
86
87 bool qt_supports (url u);
88 void qt_image_size (url image, int& w, int& h);
89 void qt_convert_image (url image, url dest, int w =0, int h =0);
90 void qt_image_to_eps (url image, url eps, int w_pt =0, int h_pt =0, int dpi= 0);
91 string qt_image_to_eps (url image, int w_pt =0, int h_pt =0, int dpi= 0);
92 void qt_image_data (url image, int& w, int&h, string& data, string& palette, string& mask);
93
94 string qt_application_directory ();
95 string qt_get_date (string lan, string fm);
96 string qt_pretty_time (int t);
97
98 bool qt_print (bool&, bool&, string&, url&, string&, string&, string&);
99
100 QPixmap as_pixmap (const QImage& im);
101
102 /******************************************************************************
103 * Type checking
104 ******************************************************************************/
105
106 inline void
check_type_void(blackbox bb,slot s)107 check_type_void (blackbox bb, slot s) {
108 if (!is_nil (bb)) {
109 failed_error << "slot type= " << as_string(s) << LF;
110 FAILED ("type mismatch");
111 }
112 }
113
114 template<class T> inline void
check_type_id(int type_id,slot s)115 check_type_id (int type_id, slot s) {
116 if (type_id != type_helper<T>::id) {
117 failed_error << "slot type= " << as_string(s) << LF;
118 FAILED ("type mismatch");
119 }
120 }
121
122 template<class T> void
check_type(blackbox bb,slot s)123 check_type (blackbox bb, slot s) {
124 if (type_box (bb) != type_helper<T>::id) {
125 failed_error << "slot type= " << as_string(s) << LF;
126 FAILED ("type mismatch");
127 }
128 }
129
130 template<class T1, class T2> inline void
check_type(blackbox bb,string s)131 check_type (blackbox bb, string s) {
132 check_type<pair<T1,T2> > (bb, s);
133 }
134
135 /*! the run-loop should exit when the number of windows is zero */
136 extern int nr_windows;
137
138 /******************************************************************************
139 * Some debugging infrastucture
140 ******************************************************************************/
141 extern tm_ostream& operator << (tm_ostream& out, QRect rect);
142
143 // deprecated, use check_type<T>(bb, slot) instead
144 //#define TYPE_CHECK(b) ASSERT (b, "type mismatch")
145
146 #define NOT_IMPLEMENTED(x) \
147 { if (DEBUG_QT) debug_qt << x << " not implemented yet.\n"; }
148
149 #endif // QT_UTILITIES_HPP
150