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>
33 
34 #include <com/sun/star/uno/Sequence.hxx>
35 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
36 
37 #include <memory>
38 
39 class Image;
40 class QImage;
41 
42 inline OUString toOUString(const QString& s)
43 {
44  // QString stores UTF16, just like OUString
45  return OUString(reinterpret_cast<const sal_Unicode*>(s.data()), s.length());
46 }
47 
48 inline QString toQString(const OUString& s)
49 {
50  return QString::fromUtf16(reinterpret_cast<ushort const*>(s.getStr()), s.getLength());
51 }
52 
53 inline QRect toQRect(const tools::Rectangle& rRect)
54 {
55  return QRect(rRect.Left(), rRect.Top(), rRect.GetWidth(), rRect.GetHeight());
56 }
57 
58 inline QRect toQRect(const tools::Rectangle& rRect, const qreal fScale)
59 {
60  return QRect(floor(rRect.Left() * fScale), floor(rRect.Top() * fScale),
61  ceil(rRect.GetWidth() * fScale), ceil(rRect.GetHeight() * fScale));
62 }
63 
64 inline QRect scaledQRect(const QRect& rRect, const qreal fScale)
65 {
66  return QRect(floor(rRect.x() * fScale), floor(rRect.y() * fScale), ceil(rRect.width() * fScale),
67  ceil(rRect.height() * fScale));
68 }
69 
70 inline tools::Rectangle toRectangle(const QRect& rRect)
71 {
72  return tools::Rectangle(rRect.left(), rRect.top(), rRect.right(), rRect.bottom());
73 }
74 
75 inline QSize toQSize(const Size& rSize) { return QSize(rSize.Width(), rSize.Height()); }
76 
77 inline Size toSize(const QSize& rSize) { return Size(rSize.width(), rSize.height()); }
78 
79 inline Point toPoint(const QPoint& rPoint) { return Point(rPoint.x(), rPoint.y()); }
80 
81 inline QColor toQColor(const Color& rColor)
82 {
83  return QColor(rColor.GetRed(), rColor.GetGreen(), rColor.GetBlue(), rColor.GetAlpha());
84 }
85 
86 Qt::DropActions toQtDropActions(sal_Int8 dragOperation);
87 sal_Int8 toVclDropActions(Qt::DropActions dragOperation);
88 sal_Int8 toVclDropAction(Qt::DropAction dragOperation);
89 Qt::DropAction getPreferredDropAction(sal_Int8 dragOperation);
90 
91 inline QList<int> toQList(const css::uno::Sequence<sal_Int32>& aSequence)
92 {
93  QList<int> aList;
94  for (sal_Int32 i : aSequence)
95  {
96  aList.append(i);
97  }
98  return aList;
99 }
100 
101 constexpr QImage::Format Qt5_DefaultFormat32 = QImage::Format_ARGB32;
102 
103 inline QImage::Format getBitFormat(vcl::PixelFormat ePixelFormat)
104 {
105  switch (ePixelFormat)
106  {
108  return QImage::Format_Mono;
110  return QImage::Format_Indexed8;
112  return QImage::Format_RGB888;
114  return Qt5_DefaultFormat32;
115  default:
116  std::abort();
117  break;
118  }
119  return QImage::Format_Invalid;
120 }
121 
122 inline sal_uInt16 getFormatBits(QImage::Format eFormat)
123 {
124  switch (eFormat)
125  {
126  case QImage::Format_Mono:
127  return 1;
128  case QImage::Format_Indexed8:
129  return 8;
130  case QImage::Format_RGB888:
131  return 24;
132  case Qt5_DefaultFormat32:
133  case QImage::Format_ARGB32_Premultiplied:
134  return 32;
135  default:
136  std::abort();
137  return 0;
138  }
139 }
140 
141 typedef struct _cairo_surface cairo_surface_t;
143 {
144  void operator()(cairo_surface_t* pSurface) const;
145 };
146 
147 typedef std::unique_ptr<cairo_surface_t, CairoDeleter> UniqueCairoSurface;
148 
149 sal_uInt16 GetKeyModCode(Qt::KeyboardModifiers eKeyModifiers);
150 sal_uInt16 GetMouseModCode(Qt::MouseButtons eButtons);
151 
152 QImage toQImage(const Image& rImage);
153 
154 template <typename charT, typename traits>
155 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
156  const QString& rString)
157 {
158  return stream << toOUString(rString);
159 }
160 
161 template <typename charT, typename traits>
162 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
163  const QRect& rRect)
164 {
165  return stream << toRectangle(rRect);
166 }
167 
168 template <typename charT, typename traits>
169 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
170  const QSize& rSize)
171 {
172  return stream << toSize(rSize);
173 }
174 
175 template <typename charT, typename traits>
176 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
177  const QPoint& rPoint)
178 {
179  return stream << toPoint(rPoint);
180 }
181 
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PixelFormat
Pixel format of the bitmap in bits per pixel.
Definition: BitmapTypes.hxx:19
sal_uInt8 GetAlpha() const
sal_uInt8 GetRed() const
signed char sal_Int8
sal_Int8 toVclDropActions(Qt::DropActions dragOperation)
sal_uInt16 GetKeyModCode(Qt::KeyboardModifiers eKeyModifiers)
sal_uInt16 getFormatBits(QImage::Format eFormat)
Definition: Qt5Tools.hxx:122
constexpr tools::Long Width() const
std::unique_ptr< cairo_surface_t, CairoDeleter > UniqueCairoSurface
Definition: Qt5Tools.hxx:147
tools::Long Left() const
void operator()(cairo_surface_t *pSurface) const
constexpr tools::Long GetWidth() const
sal_uInt16 GetMouseModCode(Qt::MouseButtons eButtons)
sal_uInt8 GetBlue() const
Qt::DropActions toQtDropActions(sal_Int8 dragOperation)
int i
QRect toQRect(const tools::Rectangle &rRect)
Definition: Qt5Tools.hxx:53
constexpr QImage::Format Qt5_DefaultFormat32
Definition: Qt5Tools.hxx:101
tools::Long Top() const
OUString toOUString(const QString &s)
Definition: Qt5Tools.hxx:42
Size toSize(const QSize &rSize)
Definition: Qt5Tools.hxx:77
Point toPoint(const QPoint &rPoint)
Definition: Qt5Tools.hxx:79
sal_Int8 toVclDropAction(Qt::DropAction dragOperation)
Reference< XOutputStream > stream
QString toQString(const OUString &s)
Definition: Qt5Tools.hxx:48
QRect scaledQRect(const QRect &rRect, const qreal fScale)
Definition: Qt5Tools.hxx:64
sal_uInt8 GetGreen() const
constexpr tools::Long Height() const
Definition: image.hxx:39
Qt::DropAction getPreferredDropAction(sal_Int8 dragOperation)
QImage toQImage(const Image &rImage)
QList< int > toQList(const css::uno::Sequence< sal_Int32 > &aSequence)
Definition: Qt5Tools.hxx:91
struct _cairo_surface cairo_surface_t
Definition: svpgdi.hxx:71
QSize toQSize(const Size &rSize)
Definition: Qt5Tools.hxx:75
QColor toQColor(const Color &rColor)
Definition: Qt5Tools.hxx:81
tools::Rectangle toRectangle(const QRect &rRect)
Definition: Qt5Tools.hxx:70
constexpr tools::Long GetHeight() const
QImage::Format getBitFormat(vcl::PixelFormat ePixelFormat)
Definition: Qt5Tools.hxx:103