1 /* === This file is part of Calamares - <https://calamares.io> ===
2 *
3 * SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
4 * SPDX-FileCopyrightText: 2017-2018 Adriaan de Groot <groot@kde.org>
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 *
7 * Calamares is Free Software: see the License-Identifier above.
8 *
9 */
10
11 #include "CalamaresUtilsGui.h"
12
13 #include "ImageRegistry.h"
14
15 #include <QBrush>
16 #include <QFont>
17 #include <QFontMetrics>
18 #include <QLayout>
19 #include <QPainter>
20 #include <QPen>
21 #include <QWidget>
22
23 #define RESPATH ":/data/"
24
25 namespace CalamaresUtils
26 {
27
28 static int s_defaultFontSize = 0;
29 static int s_defaultFontHeight = 0;
30
31
32 QPixmap
defaultPixmap(ImageType type,ImageMode mode,const QSize & size)33 defaultPixmap( ImageType type, ImageMode mode, const QSize& size )
34 {
35 Q_UNUSED( mode )
36 QPixmap pixmap;
37
38 switch ( type )
39 {
40 case Yes:
41 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/yes.svgz", size );
42 break;
43
44 case No:
45 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/no.svgz", size );
46 break;
47
48 case Information:
49 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/information.svgz", size );
50 break;
51
52 case Fail:
53 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/fail.svgz", size );
54 break;
55
56 case Bugs:
57 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/bugs.svg", size );
58 break;
59
60 case Help:
61 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/help.svg", size );
62 break;
63
64 case Release:
65 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/release.svg", size );
66 break;
67
68 case Donate:
69 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/donate.svg", size );
70 break;
71
72 case PartitionDisk:
73 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-disk.svg", size );
74 break;
75
76 case PartitionPartition:
77 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-partition.svg", size );
78 break;
79
80 case PartitionAlongside:
81 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-alongside.svg", size );
82 break;
83
84 case PartitionEraseAuto:
85 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-erase-auto.svg", size );
86 break;
87
88 case PartitionManual:
89 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-manual.svg", size );
90 break;
91
92 case PartitionReplaceOs:
93 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-replace-os.svg", size );
94 break;
95
96 case PartitionTable:
97 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-table.svg", size );
98 break;
99
100 case BootEnvironment:
101 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/boot-environment.svg", size );
102 break;
103
104 case Squid:
105 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/squid.svg", size );
106 break;
107
108 case StatusOk:
109 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/state-ok.svg", size );
110 break;
111
112 case StatusWarning:
113 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/state-warning.svg", size );
114 break;
115
116 case StatusError:
117 pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/state-error.svg", size );
118 break;
119 }
120
121 if ( pixmap.isNull() )
122 {
123 Q_ASSERT( false );
124 return QPixmap();
125 }
126
127 return pixmap;
128 }
129
130
131 QPixmap
createRoundedImage(const QPixmap & pixmap,const QSize & size,float frameWidthPct)132 createRoundedImage( const QPixmap& pixmap, const QSize& size, float frameWidthPct )
133 {
134 int height;
135 int width;
136
137 if ( !size.isEmpty() )
138 {
139 height = size.height();
140 width = size.width();
141 }
142 else
143 {
144 height = pixmap.height();
145 width = pixmap.width();
146 }
147
148 if ( !height || !width )
149 {
150 return QPixmap();
151 }
152
153 QPixmap scaledAvatar = pixmap.scaled( width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
154 if ( frameWidthPct == 0.00f )
155 {
156 return scaledAvatar;
157 }
158
159 QPixmap frame( width, height );
160 frame.fill( Qt::transparent );
161
162 QPainter painter( &frame );
163 painter.setRenderHint( QPainter::Antialiasing );
164
165 QRect outerRect( 0, 0, width, height );
166 QBrush brush( scaledAvatar );
167 QPen pen;
168 pen.setColor( Qt::transparent );
169 pen.setJoinStyle( Qt::RoundJoin );
170
171 painter.setBrush( brush );
172 painter.setPen( pen );
173 painter.drawRoundedRect(
174 outerRect, qreal( frameWidthPct ) * 100.0, qreal( frameWidthPct ) * 100.0, Qt::RelativeSize );
175
176 return frame;
177 }
178
179
180 void
unmarginLayout(QLayout * layout)181 unmarginLayout( QLayout* layout )
182 {
183 layout->setContentsMargins( 0, 0, 0, 0 );
184 layout->setMargin( 0 );
185 layout->setSpacing( 0 );
186
187 for ( int i = 0; i < layout->count(); i++ )
188 {
189 QLayout* childLayout = layout->itemAt( i )->layout();
190 if ( childLayout )
191 {
192 unmarginLayout( childLayout );
193 }
194 }
195 }
196
197
198 int
defaultFontSize()199 defaultFontSize()
200 {
201 if ( s_defaultFontSize <= 0 )
202 {
203 s_defaultFontSize = QFont().pointSize();
204 }
205 return s_defaultFontSize;
206 }
207
208
209 int
defaultFontHeight()210 defaultFontHeight()
211 {
212 if ( s_defaultFontHeight <= 0 )
213 {
214 QFont f;
215 f.setPointSize( defaultFontSize() );
216 s_defaultFontHeight = QFontMetrics( f ).height();
217 }
218
219 return s_defaultFontHeight;
220 }
221
222
223 QFont
defaultFont()224 defaultFont()
225 {
226 QFont f;
227 f.setPointSize( defaultFontSize() );
228 return f;
229 }
230
231
232 QFont
largeFont()233 largeFont()
234 {
235 QFont f;
236 f.setPointSize( defaultFontSize() + 4 );
237 return f;
238 }
239
240
241 void
setDefaultFontSize(int points)242 setDefaultFontSize( int points )
243 {
244 s_defaultFontSize = points;
245 s_defaultFontHeight = 0; // Recalculate on next call to defaultFontHeight()
246 }
247
248
249 QSize
defaultIconSize()250 defaultIconSize()
251 {
252 const int w = int( defaultFontHeight() * 1.6 );
253 return QSize( w, w );
254 }
255
256
257 void
clearLayout(QLayout * layout)258 clearLayout( QLayout* layout )
259 {
260 while ( QLayoutItem* item = layout->takeAt( 0 ) )
261 {
262 if ( QWidget* widget = item->widget() )
263 {
264 widget->deleteLater();
265 }
266
267 if ( QLayout* childLayout = item->layout() )
268 {
269 clearLayout( childLayout );
270 }
271
272 delete item;
273 }
274 }
275
276 } // namespace CalamaresUtils
277