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