Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / inc / qt5 / QtTools.hxx
blob5a0032ccc352392f916b76e52e7a30b06f1345d8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #pragma once
22 #include <config_vclplug.h>
24 #include <QtCore/QPoint>
25 #include <QtCore/QRect>
26 #include <QtCore/QSize>
27 #include <QtCore/QString>
28 #include <QtGui/QImage>
30 #include <rtl/string.hxx>
31 #include <rtl/ustring.hxx>
32 #include <tools/color.hxx>
33 #include <tools/gen.hxx>
34 #include <vcl/bitmap/BitmapTypes.hxx>
36 #include <com/sun/star/uno/Sequence.hxx>
37 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
39 #include <memory>
41 class Image;
42 class QImage;
44 inline OUString toOUString(const QString& s)
46 // QString stores UTF16, just like OUString
47 return OUString(reinterpret_cast<const sal_Unicode*>(s.data()), s.length());
50 inline QString toQString(const OUString& s)
52 return QString::fromUtf16(s.getStr(), s.getLength());
55 inline QRect toQRect(const tools::Rectangle& rRect)
57 return QRect(rRect.Left(), rRect.Top(), rRect.GetWidth(), rRect.GetHeight());
60 inline QRect toQRect(const AbsoluteScreenPixelRectangle& rRect, const qreal fScale)
62 return QRect(floor(rRect.Left() * fScale), floor(rRect.Top() * fScale),
63 ceil(rRect.GetWidth() * fScale), ceil(rRect.GetHeight() * fScale));
66 inline QRect scaledQRect(const QRect& rRect, const qreal fScale)
68 return QRect(floor(rRect.x() * fScale), floor(rRect.y() * fScale), ceil(rRect.width() * fScale),
69 ceil(rRect.height() * fScale));
72 inline tools::Rectangle toRectangle(const QRect& rRect)
74 return tools::Rectangle(rRect.left(), rRect.top(), rRect.right(), rRect.bottom());
77 inline QSize toQSize(const Size& rSize) { return QSize(rSize.Width(), rSize.Height()); }
79 inline Size toSize(const QSize& rSize) { return Size(rSize.width(), rSize.height()); }
81 inline Point toPoint(const QPoint& rPoint) { return Point(rPoint.x(), rPoint.y()); }
83 inline QColor toQColor(const Color& rColor)
85 return QColor(rColor.GetRed(), rColor.GetGreen(), rColor.GetBlue(), rColor.GetAlpha());
88 Qt::DropActions toQtDropActions(sal_Int8 dragOperation);
89 sal_Int8 toVclDropActions(Qt::DropActions dragOperation);
90 sal_Int8 toVclDropAction(Qt::DropAction dragOperation);
91 Qt::DropAction getPreferredDropAction(sal_Int8 dragOperation);
93 inline QList<int> toQList(const css::uno::Sequence<sal_Int32>& aSequence)
95 QList<int> aList;
96 for (sal_Int32 i : aSequence)
98 aList.append(i);
100 return aList;
103 constexpr QImage::Format Qt_DefaultFormat32 = QImage::Format_ARGB32;
105 inline QImage::Format getBitFormat(vcl::PixelFormat ePixelFormat)
107 switch (ePixelFormat)
109 case vcl::PixelFormat::N8_BPP:
110 return QImage::Format_Indexed8;
111 case vcl::PixelFormat::N24_BPP:
112 return QImage::Format_RGB888;
113 case vcl::PixelFormat::N32_BPP:
114 return Qt_DefaultFormat32;
115 default:
116 std::abort();
117 break;
119 return QImage::Format_Invalid;
122 inline sal_uInt16 getFormatBits(QImage::Format eFormat)
124 switch (eFormat)
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 Qt_DefaultFormat32:
133 case QImage::Format_ARGB32_Premultiplied:
134 return 32;
135 default:
136 std::abort();
137 return 0;
141 typedef struct _cairo_surface cairo_surface_t;
142 struct CairoDeleter
144 void operator()(cairo_surface_t* pSurface) const;
147 typedef std::unique_ptr<cairo_surface_t, CairoDeleter> UniqueCairoSurface;
149 sal_uInt16 GetKeyModCode(Qt::KeyboardModifiers eKeyModifiers);
150 sal_uInt16 GetMouseModCode(Qt::MouseButtons eButtons);
152 QImage toQImage(const Image& rImage);
154 template <typename charT, typename traits>
155 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
156 const QString& rString)
158 return stream << toOUString(rString);
161 template <typename charT, typename traits>
162 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
163 const QRect& rRect)
165 return stream << toRectangle(rRect);
168 template <typename charT, typename traits>
169 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
170 const QSize& rSize)
172 return stream << toSize(rSize);
175 template <typename charT, typename traits>
176 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
177 const QPoint& rPoint)
179 return stream << toPoint(rPoint);
182 #define CHECK_QT5_USING_X11 (QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && QT5_USING_X11)
184 #define CHECK_QT6_USING_X11 (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) && QT6_USING_X11)
186 #define CHECK_ANY_QT_USING_X11 CHECK_QT5_USING_X11 || CHECK_QT6_USING_X11
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */