LibreOffice Module vcl (master)  1
Qt5Tools.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #pragma once
21 
22 #include <QtCore/QPoint>
23 #include <QtCore/QRect>
24 #include <QtCore/QSize>
25 #include <QtCore/QString>
26 #include <QtGui/QImage>
27 
28 #include <rtl/string.hxx>
29 #include <rtl/ustring.hxx>
30 #include <tools/color.hxx>
31 #include <tools/gen.hxx>
32 
33 #include <com/sun/star/uno/Sequence.hxx>
34 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
35 
36 #include <memory>
37 
38 class Image;
39 class QImage;
40 
41 inline OUString toOUString(const QString& s)
42 {
43  // QString stores UTF16, just like OUString
44  return OUString(reinterpret_cast<const sal_Unicode*>(s.data()), s.length());
45 }
46 
47 inline QString toQString(const OUString& s)
48 {
49  return QString::fromUtf16(reinterpret_cast<ushort const*>(s.getStr()), s.getLength());
50 }
51 
52 inline QRect toQRect(const tools::Rectangle& rRect)
53 {
54  return QRect(rRect.Left(), rRect.Top(), rRect.GetWidth(), rRect.GetHeight());
55 }
56 
57 inline QRect toQRect(const tools::Rectangle& rRect, const qreal fScale)
58 {
59  return QRect(floor(rRect.Left() * fScale), floor(rRect.Top() * fScale),
60  ceil(rRect.GetWidth() * fScale), ceil(rRect.GetHeight() * fScale));
61 }
62 
63 inline QRect scaledQRect(const QRect& rRect, const qreal fScale)
64 {
65  return QRect(floor(rRect.x() * fScale), floor(rRect.y() * fScale), ceil(rRect.width() * fScale),
66  ceil(rRect.height() * fScale));
67 }
68 
69 inline tools::Rectangle toRectangle(const QRect& rRect)
70 {
71  return tools::Rectangle(rRect.left(), rRect.top(), rRect.right(), rRect.bottom());
72 }
73 
74 inline QSize toQSize(const Size& rSize) { return QSize(rSize.Width(), rSize.Height()); }
75 
76 inline Size toSize(const QSize& rSize) { return Size(rSize.width(), rSize.height()); }
77 
78 inline Point toPoint(const QPoint& rPoint) { return Point(rPoint.x(), rPoint.y()); }
79 
80 inline QColor toQColor(const Color& rColor)
81 {
82  return QColor(rColor.GetRed(), rColor.GetGreen(), rColor.GetBlue(), rColor.GetAlpha());
83 }
84 
85 Qt::DropActions toQtDropActions(sal_Int8 dragOperation);
86 sal_Int8 toVclDropActions(Qt::DropActions dragOperation);
87 sal_Int8 toVclDropAction(Qt::DropAction dragOperation);
88 Qt::DropAction getPreferredDropAction(sal_Int8 dragOperation);
89 
90 inline QList<int> toQList(const css::uno::Sequence<sal_Int32>& aSequence)
91 {
92  QList<int> aList;
93  for (sal_Int32 i : aSequence)
94  {
95  aList.append(i);
96  }
97  return aList;
98 }
99 
100 constexpr QImage::Format Qt5_DefaultFormat32 = QImage::Format_ARGB32;
101 
102 inline QImage::Format getBitFormat(sal_uInt16 nBitCount)
103 {
104  switch (nBitCount)
105  {
106  case 1:
107  return QImage::Format_Mono;
108  case 8:
109  return QImage::Format_Indexed8;
110  case 24:
111  return QImage::Format_RGB888;
112  case 32:
113  return Qt5_DefaultFormat32;
114  default:
115  std::abort();
116  break;
117  }
118  return QImage::Format_Invalid;
119 }
120 
121 inline sal_uInt16 getFormatBits(QImage::Format eFormat)
122 {
123  switch (eFormat)
124  {
125  case QImage::Format_Mono:
126  return 1;
127  case QImage::Format_Indexed8:
128  return 8;
129  case QImage::Format_RGB888:
130  return 24;
131  case Qt5_DefaultFormat32:
132  case QImage::Format_ARGB32_Premultiplied:
133  return 32;
134  default:
135  std::abort();
136  return 0;
137  }
138 }
139 
140 typedef struct _cairo_surface cairo_surface_t;
142 {
143  void operator()(cairo_surface_t* pSurface) const;
144 };
145 
146 typedef std::unique_ptr<cairo_surface_t, CairoDeleter> UniqueCairoSurface;
147 
148 sal_uInt16 GetKeyModCode(Qt::KeyboardModifiers eKeyModifiers);
149 sal_uInt16 GetMouseModCode(Qt::MouseButtons eButtons);
150 
151 QImage toQImage(const Image& rImage);
152 
153 template <typename charT, typename traits>
154 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
155  const QString& rString)
156 {
157  return stream << toOUString(rString);
158 }
159 
160 template <typename charT, typename traits>
161 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
162  const QRect& rRect)
163 {
164  return stream << toRectangle(rRect);
165 }
166 
167 template <typename charT, typename traits>
168 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
169  const QSize& rSize)
170 {
171  return stream << toSize(rSize);
172 }
173 
174 template <typename charT, typename traits>
175 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
176  const QPoint& rPoint)
177 {
178  return stream << toPoint(rPoint);
179 }
180 
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 GetAlpha() const
sal_uInt8 GetRed() const
signed char sal_Int8
sal_Int8 toVclDropActions(Qt::DropActions dragOperation)
tools::Long GetWidth() const
sal_uInt16 GetKeyModCode(Qt::KeyboardModifiers eKeyModifiers)
sal_uInt16 getFormatBits(QImage::Format eFormat)
Definition: Qt5Tools.hxx:121
std::unique_ptr< cairo_surface_t, CairoDeleter > UniqueCairoSurface
Definition: Qt5Tools.hxx:146
tools::Long Left() const
void operator()(cairo_surface_t *pSurface) const
sal_uInt16 GetMouseModCode(Qt::MouseButtons eButtons)
sal_uInt8 GetBlue() const
Qt::DropActions toQtDropActions(sal_Int8 dragOperation)
int i
tools::Long Width() const
QRect toQRect(const tools::Rectangle &rRect)
Definition: Qt5Tools.hxx:52
constexpr QImage::Format Qt5_DefaultFormat32
Definition: Qt5Tools.hxx:100
tools::Long Top() const
OUString toOUString(const QString &s)
Definition: Qt5Tools.hxx:41
Size toSize(const QSize &rSize)
Definition: Qt5Tools.hxx:76
Point toPoint(const QPoint &rPoint)
Definition: Qt5Tools.hxx:78
sal_Int8 toVclDropAction(Qt::DropAction dragOperation)
Reference< XOutputStream > stream
QString toQString(const OUString &s)
Definition: Qt5Tools.hxx:47
QRect scaledQRect(const QRect &rRect, const qreal fScale)
Definition: Qt5Tools.hxx:63
sal_uInt8 GetGreen() const
Definition: image.hxx:39
tools::Long Height() const
Qt::DropAction getPreferredDropAction(sal_Int8 dragOperation)
tools::Long GetHeight() const
QImage::Format getBitFormat(sal_uInt16 nBitCount)
Definition: Qt5Tools.hxx:102
QImage toQImage(const Image &rImage)
QList< int > toQList(const css::uno::Sequence< sal_Int32 > &aSequence)
Definition: Qt5Tools.hxx:90
struct _cairo_surface cairo_surface_t
Definition: svpgdi.hxx:71
QSize toQSize(const Size &rSize)
Definition: Qt5Tools.hxx:74
QColor toQColor(const Color &rColor)
Definition: Qt5Tools.hxx:80
tools::Rectangle toRectangle(const QRect &rRect)
Definition: Qt5Tools.hxx:69