1 #pragma once
2 
3 #ifndef GUTIL_H
4 #define GUTIL_H
5 
6 #include "tcommon.h"
7 #include <QImage>
8 #include <QFrame>
9 #include <QColor>
10 #include "traster.h"
11 #include "toonz/preferences.h"
12 
13 #undef DVAPI
14 #undef DVVAR
15 #ifdef TOONZQT_EXPORTS
16 #define DVAPI DV_EXPORT_API
17 #define DVVAR DV_EXPORT_VAR
18 #else
19 #define DVAPI DV_IMPORT_API
20 #define DVVAR DV_IMPORT_VAR
21 #endif
22 
23 // forward declaration
24 class TMouseEvent;
25 class QMouseEvent;
26 class QTabletEvent;
27 class QKeyEvent;
28 class QUrl;
29 class TFilePath;
30 
31 //=============================================================================
32 // Constant definition
33 //-----------------------------------------------------------------------------
34 
35 namespace {
36 const QColor grey120(120, 120, 120);
37 const QColor grey210(210, 210, 210);
38 const QColor grey225(225, 225, 225);
39 const QColor grey190(190, 190, 190);
40 const QColor grey150(150, 150, 150);
41 
42 }  // namespace
43 
44 class QPainter;
45 class QIcon;
46 class TFilePath;
47 class QPainterPath;
48 class TStroke;
49 
50 //-----------------------------------------------------------------------------
51 
52 QString DVAPI getIconThemePath(const QString &filePath);
53 
54 //-----------------------------------------------------------------------------
55 
56 QString DVAPI fileSizeString(qint64 size, int precision = 2);
57 
58 //-----------------------------------------------------------------------------
59 
60 QImage DVAPI rasterToQImage(const TRasterP &ras, bool premultiplied = true,
61                             bool mirrored = true);
62 
63 //-----------------------------------------------------------------------------
64 
65 QPixmap DVAPI rasterToQPixmap(const TRaster32P &ras, bool premultiplied = true,
66                               bool setDevPixRatio = false);
67 
68 //-----------------------------------------------------------------------------
69 
70 TRaster32P DVAPI rasterFromQImage(QImage image, bool premultiply = true,
71                                   bool mirror = true);
72 
73 //-----------------------------------------------------------------------------
74 
75 TRaster32P DVAPI rasterFromQPixmap(QPixmap pixmap, bool premultiply = true,
76                                    bool mirror = true);
77 
78 //-----------------------------------------------------------------------------
79 
80 void DVAPI drawPolygon(QPainter &p, const std::vector<QPointF> &points,
81                        bool fill = false, const QColor colorFill = Qt::white,
82                        const QColor colorLine = Qt::black);
83 
84 //-----------------------------------------------------------------------------
85 
86 void DVAPI drawArrow(QPainter &p, const QPointF a, const QPointF b,
87                      const QPointF c, bool fill = false,
88                      const QColor colorFill = Qt::white,
89                      const QColor colorLine = Qt::black);
90 
91 //-----------------------------------------------------------------------------
92 
93 QPixmap DVAPI scalePixmapKeepingAspectRatio(QPixmap p, QSize size,
94                                             QColor color = Qt::white);
95 
96 //-----------------------------------------------------------------------------
97 
98 QPixmap DVAPI
99 svgToPixmap(const QString &svgFilePath, const QSize &size = QSize(),
100             Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio,
101             QColor bgColor                      = Qt::transparent);
102 
103 //-----------------------------------------------------------------------------
104 // returns device-pixel ratio. It is 1 for normal monitors and 2 (or higher
105 // ratio) for high DPI monitors. Setting "Display > Set custom text size(DPI)"
106 // for Windows corresponds to this ratio.
107 int DVAPI getDevPixRatio();
108 
109 //-----------------------------------------------------------------------------
110 
111 QPixmap DVAPI compositePixmap(QPixmap pixmap, const qreal &opacity = 0.8,
112                               const QSize &size = QSize(),
113                               const int leftAdj = 0, const int topAdj = 0,
114                               QColor bgColor = Qt::transparent);
115 QPixmap DVAPI recolorPixmap(
116     QPixmap pixmap, QColor color = Preferences::instance()->getIconTheme()
117                                        ? Qt::black
118                                        : Qt::white);
119 QIcon DVAPI createQIcon(const char *iconSVGName, bool useFullOpacity = false,
120                         bool isForMenuItem = false);
121 QIcon DVAPI createQIconPNG(const char *iconPNGName);
122 QIcon DVAPI createQIconOnOffPNG(const char *iconPNGName, bool withOver = true);
123 QIcon DVAPI createTemporaryIconFromName(const char *commandName);
124 
dimension2QSize(const TDimension & sz)125 inline QSize dimension2QSize(const TDimension &sz) {
126   return QSize(sz.lx, sz.ly);
127 }
qsize2Dimension(const QSize & sz)128 inline TDimension qsize2Dimension(const QSize &sz) {
129   return TDimension(sz.width(), sz.height());
130 }
131 QString DVAPI toQString(const TFilePath &path);
132 bool DVAPI isSpaceString(const QString &str);
133 bool DVAPI isValidFileName(const QString &fileName);
134 bool DVAPI isValidFileName_message(const QString &fileName);
135 bool DVAPI isReservedFileName(const QString &fileName);
136 bool DVAPI isReservedFileName_message(const QString &fileName);
137 
138 QString DVAPI elideText(const QString &columnName, const QFont &font,
139                         int width);
140 QString DVAPI elideText(const QString &columnName, const QFontMetrics &fm,
141                         int width, const QString &elideSymbol);
142 QUrl DVAPI pathToUrl(const TFilePath &path);
143 
144 bool DVAPI isResource(const QString &path);
145 bool DVAPI isResource(const QUrl &url);
146 bool DVAPI isResourceOrFolder(const QUrl &url);
147 
148 bool DVAPI acceptResourceDrop(const QList<QUrl> &urls);
149 bool DVAPI acceptResourceOrFolderDrop(const QList<QUrl> &urls);
150 
toQPointF(const TPointD & p)151 inline QPointF toQPointF(const TPointD &p) { return QPointF(p.x, p.y); }
toQPointF(const TPoint & p)152 inline QPointF toQPointF(const TPoint &p) { return QPointF(p.x, p.y); }
toQPoint(const TPoint & p)153 inline QPoint toQPoint(const TPoint &p) { return QPoint(p.x, p.y); }
toTPointD(const QPointF & p)154 inline TPointD toTPointD(const QPointF &p) { return TPointD(p.x(), p.y()); }
toTPointD(const QPoint & p)155 inline TPointD toTPointD(const QPoint &p) { return TPointD(p.x(), p.y()); }
toTPoint(const QPoint & p)156 inline TPoint toTPoint(const QPoint &p) { return TPoint(p.x(), p.y()); }
157 
toQRect(const TRect & r)158 inline QRect toQRect(const TRect &r) {
159   return QRect(r.x0, r.y0, r.getLx(), r.getLy());
160 }
toQRectF(const TRectD & r)161 inline QRectF toQRectF(const TRectD &r) {
162   return QRectF(r.x0, r.y0, r.getLx(), r.getLy());
163 }
toQRectF(const TRect & r)164 inline QRectF toQRectF(const TRect &r) {
165   return QRectF(r.x0, r.y0, r.getLx(), r.getLy());
166 }
toTRect(const QRect & r)167 inline TRect toTRect(const QRect &r) {
168   return TRect(r.left(), r.top(), r.right(), r.bottom());
169 }
toTRectD(const QRectF & r)170 inline TRectD toTRectD(const QRectF &r) {
171   return TRectD(r.left(), r.top(), r.right(), r.bottom());
172 }
toTRectD(const QRect & r)173 inline TRectD toTRectD(const QRect &r) {
174   return TRectD(r.left(), r.top(), r.right() + 1, r.bottom() + 1);
175 }
176 
177 QPainterPath DVAPI strokeToPainterPath(TStroke *stroke);
178 
179 //-----------------------------------------------------------------------------
180 // This widget is only used to set the background color of the tabBar
181 // using the styleSheet.
182 // It is also used to take 6px on the left before the tabBar
183 
184 class DVAPI TabBarContainter final : public QFrame {
185   Q_OBJECT
186 public:
187   TabBarContainter(QWidget *parent = 0);
188 
189 protected:
190   QColor m_bottomBelowLineColor, m_bottomAboveLineColor;
191   Q_PROPERTY(QColor BottomBelowLineColor READ getBottomBelowLineColor WRITE
192                  setBottomBelowLineColor);
193   Q_PROPERTY(QColor BottomAboveLineColor READ getBottomAboveLineColor WRITE
194                  setBottomAboveLineColor);
195   void paintEvent(QPaintEvent *event) override;
setBottomBelowLineColor(const QColor & color)196   void setBottomBelowLineColor(const QColor &color) {
197     m_bottomBelowLineColor = color;
198   }
getBottomBelowLineColor()199   QColor getBottomBelowLineColor() const { return m_bottomBelowLineColor; }
setBottomAboveLineColor(const QColor & color)200   void setBottomAboveLineColor(const QColor &color) {
201     m_bottomAboveLineColor = color;
202   }
getBottomAboveLineColor()203   QColor getBottomAboveLineColor() const { return m_bottomAboveLineColor; }
204 };
205 
206 //-----------------------------------------------------------------------------
207 // This widget is only used to set the background color of the playToolBar
208 // using the styleSheet. And to put a line in the upper zone
209 
210 class DVAPI ToolBarContainer final : public QFrame {
211 public:
212   ToolBarContainer(QWidget *parent = 0);
213 
214 protected:
215   void paintEvent(QPaintEvent *event) override;
216 };
217 
218 QString DVAPI operator+(const QString &a, const TFilePath &fp);
219 
220 #endif  // GUTIL_H
221