comparison libgui/graphics/QtHandlesUtils.cc @ 18505:fb96b7f55242 gui-release

rename file to avoid clash on case-insenstive filesystems (bug #41658) * libgui/graphics/QtHandlesUtils.cc: Rename from Utils.cc. * libgui/graphics/QtHandlesUtils.h: Rename from Utils.h. Change all uses. * libgui/graphics/module.mk: Update lists.
author John W. Eaton <jwe@octave.org>
date Fri, 21 Feb 2014 11:35:36 -0500
parents libgui/graphics/Utils.cc@523878f76518
children fe0e34be5576 eb9b2160e878
comparison
equal deleted inserted replaced
18504:b75adad91fc6 18505:fb96b7f55242
1 /*
2
3 Copyright (C) 2011-2014 Michael Goffioul
4
5 This file is part of Octave.
6
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <QApplication>
28 #include <QKeyEvent>
29 #include <QMouseEvent>
30
31 #include <list>
32
33 #include "ov.h"
34 #include "graphics.h"
35
36 #include "Backend.h"
37 #include "Container.h"
38 #include "KeyMap.h"
39 #include "Object.h"
40 #include "QtHandlesUtils.h"
41
42 namespace QtHandles
43 {
44
45 namespace Utils
46 {
47
48 QString fromStdString (const std::string& s)
49 {
50 return QString::fromLocal8Bit (s.c_str ());
51 }
52
53 std::string toStdString (const QString& s)
54 {
55 return std::string (s.toLocal8Bit ().data ());
56 }
57
58 QStringList fromStringVector (const string_vector& v)
59 {
60 QStringList l;
61 octave_idx_type n = v.length ();
62
63 for (octave_idx_type i = 0; i < n; i++)
64 l << fromStdString (v[i]);
65
66 return l;
67 }
68
69 string_vector toStringVector (const QStringList& l)
70 {
71 string_vector v (l.length ());
72 int i = 0;
73
74 foreach (const QString& s, l)
75 v[i++] = toStdString (s);
76
77 return v;
78 }
79
80 template <class T>
81 QFont computeFont (const typename T::properties& props, int height)
82 {
83 QFont f (fromStdString (props.get_fontname ()));
84
85 static std::map<std::string, QFont::Weight> weightMap;
86 static std::map<std::string, QFont::Style> angleMap;
87 static bool mapsInitialized = false;
88
89 if (! mapsInitialized)
90 {
91 weightMap[std::string ("normal")] = QFont::Normal;
92 weightMap[std::string ("light")] = QFont::Light;
93 weightMap[std::string ("demi")] = QFont::DemiBold;
94 weightMap[std::string ("bold")] = QFont::Normal;
95
96 angleMap[std::string ("normal")] = QFont::StyleNormal;
97 angleMap[std::string ("italic")] = QFont::StyleItalic;
98 angleMap[std::string ("oblique")] = QFont::StyleOblique;
99
100 mapsInitialized = true;
101 }
102
103 f.setPointSizeF (props.get_fontsize_points (height));
104 f.setWeight (weightMap[props.get_fontweight ()]);
105 f.setStyle (angleMap[props.get_fontangle ()]);
106
107 return f;
108 }
109
110 template QFont computeFont<uicontrol> (const uicontrol::properties& props,
111 int height);
112 template QFont computeFont<uipanel> (const uipanel::properties& props,
113 int height);
114
115 QColor fromRgb (const Matrix& rgb)
116 {
117 QColor c;
118
119 if (rgb.numel () == 3)
120 c.setRgbF (rgb(0), rgb(1), rgb(2));
121
122 return c;
123 }
124
125 Matrix toRgb (const QColor& c)
126 {
127 Matrix rgb (1, 3);
128 double* rgbData = rgb.fortran_vec ();
129
130 c.getRgbF (rgbData, rgbData+1, rgbData+2);
131
132 return rgb;
133 }
134
135 std::string figureSelectionType (QMouseEvent* event, bool isDoubleClick)
136 {
137 if (isDoubleClick)
138 return std::string ("open");
139 else
140 {
141 Qt::MouseButtons buttons = event->buttons ();
142 Qt::KeyboardModifiers mods = event->modifiers ();
143
144 if (mods == Qt::NoModifier)
145 {
146 if (buttons == Qt::LeftButton)
147 return std::string ("normal");
148 else if (buttons == Qt::RightButton)
149 return std::string ("alt");
150 #if defined (Q_WS_WIN)
151 else if (buttons == (Qt::LeftButton|Qt::RightButton))
152 return std::string ("extend");
153 #elif defined (Q_WS_X11)
154 else if (buttons == Qt::MidButton)
155 return std::string ("extend");
156 #endif
157 }
158 else if (buttons == Qt::LeftButton)
159 {
160 if (mods == Qt::ShiftModifier)
161 return std::string ("extend");
162 else if (mods == Qt::ControlModifier)
163 return std::string ("alt");
164 }
165 }
166
167 return std::string ("normal");
168 }
169
170 Matrix figureCurrentPoint (const graphics_object& fig, QMouseEvent* event)
171 {
172 Object* tkFig = Backend::toolkitObject (fig);
173
174 if (tkFig)
175 {
176 Container* c = tkFig->innerContainer ();
177
178 if (c)
179 {
180 QPoint qp = c->mapFromGlobal (event->globalPos ());
181
182 return
183 tkFig->properties<figure> ().map_from_boundingbox (qp.x (),
184 qp.y ());
185 }
186 }
187
188 return Matrix (1, 2, 0.0);
189 }
190
191 Qt::Alignment fromHVAlign (const caseless_str& halign,
192 const caseless_str& valign)
193 {
194 Qt::Alignment flags;
195
196 if (halign.compare ("left"))
197 flags |= Qt::AlignLeft;
198 else if (halign.compare ("center"))
199 flags |= Qt::AlignHCenter;
200 else if (halign.compare ("right"))
201 flags |= Qt::AlignRight;
202 else
203 flags |= Qt::AlignLeft;
204
205 if (valign.compare ("middle"))
206 flags |= Qt::AlignVCenter;
207 else if (valign.compare ("top"))
208 flags |= Qt::AlignTop;
209 else if (valign.compare ("bottom"))
210 flags |= Qt::AlignBottom;
211 else
212 flags |= Qt::AlignVCenter;
213
214 return flags;
215 }
216
217 QImage makeImageFromCData (const octave_value& v, int width, int height)
218 {
219 dim_vector dv (v.dims ());
220
221 if (dv.length () == 3 && dv(2) == 3)
222 {
223 int w = qMin (dv(1), width);
224 int h = qMin (dv(0), height);
225
226 int x_off = (w < width ? (width - w) / 2 : 0);
227 int y_off = (h < height ? (height - h) / 2 : 0);
228
229 QImage img (width, height, QImage::Format_ARGB32);
230 img.fill (qRgba (0, 0, 0, 0));
231
232 if (v.is_uint8_type ())
233 {
234 uint8NDArray d = v.uint8_array_value ();
235
236 for (int i = 0; i < w; i++)
237 for (int j = 0; j < h; j++)
238 {
239 int r = d(j, i, 0);
240 int g = d(j, i, 1);
241 int b = d(j, i, 2);
242 int a = 255;
243
244 img.setPixel (x_off + i, y_off + j, qRgba (r, g, b, a));
245 }
246 }
247 else if (v.is_single_type ())
248 {
249 FloatNDArray f = v.float_array_value ();
250
251 for (int i = 0; i < w; i++)
252 for (int j = 0; j < h; j++)
253 {
254 float r = f(j, i, 0);
255 float g = f(j, i, 1);
256 float b = f(j, i, 2);
257 int a = (xisnan (r) || xisnan (g) || xisnan (b) ? 0 : 255);
258
259 img.setPixel (x_off + i, y_off + j,
260 qRgba (xround (r * 255),
261 xround (g * 255),
262 xround (b * 255),
263 a));
264 }
265 }
266 else if (v.is_real_type ())
267 {
268 NDArray d = v.array_value ();
269
270 for (int i = 0; i < w; i++)
271 for (int j = 0; j < h; j++)
272 {
273 double r = d(j, i, 0);
274 double g = d(j, i, 1);
275 double b = d(j, i, 2);
276 int a = (xisnan (r) || xisnan (g) || xisnan (b) ? 0 : 255);
277
278 img.setPixel (x_off + i, y_off + j,
279 qRgba (xround (r * 255),
280 xround (g * 255),
281 xround (b * 255),
282 a));
283 }
284 }
285
286 return img;
287 }
288
289 return QImage ();
290 }
291
292 octave_scalar_map makeKeyEventStruct (QKeyEvent* event)
293 {
294 octave_scalar_map retval;
295
296 retval.setfield ("Key", KeyMap::qKeyToKeyString (event->key ()));
297 retval.setfield ("Character", toStdString (event->text ()));
298
299 std::list<std::string> modList;
300 Qt::KeyboardModifiers mods = event->modifiers ();
301
302 if (mods & Qt::ShiftModifier)
303 modList.push_back ("shift");
304 if (mods & Qt::ControlModifier)
305 #ifdef Q_OS_MAC
306 modList.push_back ("command");
307 #else
308 modList.push_back ("control");
309 #endif
310 if (mods & Qt::AltModifier)
311 modList.push_back ("alt");
312 #ifdef Q_OS_MAC
313 if (mods & Qt::MetaModifier)
314 modList.push_back ("control");
315 #endif
316
317 retval.setfield ("Modifier", Cell (modList));
318
319 return retval;
320 }
321
322 }; // namespace Utils
323
324 }; // namespace QtHandles