source: calamares/trunk/fuentes/src/libcalamaresui/utils/CalamaresUtilsGui.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 6.4 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
4 *   Copyright 2017, Adriaan de Groot <groot@kde.org>
5 *
6 *   Calamares is free software: you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation, either version 3 of the License, or
9 *   (at your option) any later version.
10 *
11 *   Calamares is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *   GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "CalamaresUtilsGui.h"
21
22#include "ImageRegistry.h"
23
24#include <QBrush>
25#include <QFont>
26#include <QFontMetrics>
27#include <QLayout>
28#include <QPainter>
29#include <QPen>
30#include <QWidget>
31
32namespace CalamaresUtils
33{
34
35static int s_defaultFontSize   = 0;
36static int s_defaultFontHeight = 0;
37
38
39
40QPixmap
41defaultPixmap( ImageType type, ImageMode mode, const QSize& size )
42{
43    Q_UNUSED( mode );
44    QPixmap pixmap;
45
46    switch ( type )
47    {
48    case Yes:
49        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/yes.svgz", size );
50        break;
51
52    case No:
53        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/no.svgz", size );
54        break;
55
56    case Information:
57        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/information.svgz", size );
58        break;
59
60    case Fail:
61        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/fail.svgz", size );
62        break;
63
64    case Bugs:
65        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/bugs.svg", size );
66        break;
67
68    case Help:
69        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/help.svg", size );
70        break;
71
72    case Release:
73        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/release.svg", size );
74        break;
75
76    case PartitionDisk:
77        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-disk.svg", size );
78        break;
79
80    case PartitionPartition:
81        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-partition.svg", size );
82        break;
83
84    case PartitionAlongside:
85        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-alongside.svg", size );
86        break;
87
88    case PartitionEraseAuto:
89        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-erase-auto.svg", size );
90        break;
91
92    case PartitionManual:
93        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-manual.svg", size );
94        break;
95
96    case PartitionReplaceOs:
97        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-replace-os.svg", size );
98        break;
99
100    case PartitionTable:
101        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/partition-table.svg", size );
102        break;
103
104    case BootEnvironment:
105        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/boot-environment.svg", size );
106        break;
107
108    case Squid:
109        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/squid.svg", size );
110        break;
111
112    case StatusOk:
113        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/state-ok.svg", size );
114        break;
115
116    case StatusWarning:
117        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/state-warning.svg", size );
118        break;
119
120    case StatusError:
121        pixmap = ImageRegistry::instance()->pixmap( RESPATH "images/state-error.svg", size );
122        break;
123    }
124
125    if ( pixmap.isNull() )
126    {
127        Q_ASSERT( false );
128        return QPixmap();
129    }
130
131    return pixmap;
132}
133
134
135QPixmap
136createRoundedImage( const QPixmap& pixmap, const QSize& size, float frameWidthPct )
137{
138    int height;
139    int width;
140
141    if ( !size.isEmpty() )
142    {
143        height = size.height();
144        width = size.width();
145    }
146    else
147    {
148        height = pixmap.height();
149        width = pixmap.width();
150    }
151
152    if ( !height || !width )
153        return QPixmap();
154
155    QPixmap scaledAvatar = pixmap.scaled( width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
156    if ( frameWidthPct == 0.00f )
157        return scaledAvatar;
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( outerRect, qreal(frameWidthPct) * 100.0, qreal(frameWidthPct) * 100.0, Qt::RelativeSize );
174
175/*    painter.setBrush( Qt::transparent );
176    painter.setPen( Qt::white );
177    painter.drawRoundedRect( outerRect, frameWidthPct, frameWidthPct, Qt::RelativeSize ); */
178
179    return frame;
180}
181
182
183void
184unmarginLayout( QLayout* layout )
185{
186    layout->setContentsMargins( 0, 0, 0, 0 );
187    layout->setMargin( 0 );
188    layout->setSpacing( 0 );
189
190    for ( int i = 0; i < layout->count(); i++ )
191    {
192        QLayout* childLayout = layout->itemAt( i )->layout();
193        if ( childLayout )
194            unmarginLayout( childLayout );
195    }
196}
197
198
199int
200defaultFontSize()
201{
202    return s_defaultFontSize;
203}
204
205
206int
207defaultFontHeight()
208{
209    if ( s_defaultFontHeight <= 0 )
210    {
211        QFont f;
212        f.setPointSize( defaultFontSize() );
213        s_defaultFontHeight = QFontMetrics( f ).height();
214    }
215
216    return s_defaultFontHeight;
217}
218
219
220QFont
221defaultFont()
222{
223    QFont f;
224    f.setPointSize( defaultFontSize() );
225    return f;
226}
227
228
229QFont
230largeFont()
231{
232    QFont f;
233    f.setPointSize( defaultFontSize() + 4 );
234    return f;
235}
236
237
238void
239setDefaultFontSize( int points )
240{
241    s_defaultFontSize = points;
242    s_defaultFontHeight = 0;  // Recalculate on next call to defaultFontHeight()
243}
244
245
246QSize
247defaultIconSize()
248{
249    const int w = int(defaultFontHeight() * 1.6);
250    return QSize( w, w );
251}
252
253
254void
255clearLayout( QLayout* layout )
256{
257    while ( QLayoutItem* item = layout->takeAt( 0 ) )
258    {
259        if ( QWidget* widget = item->widget() )
260            widget->deleteLater();
261
262        if ( QLayout* childLayout = item->layout() )
263            clearLayout( childLayout );
264
265        delete item;
266    }
267}
268
269
270}
Note: See TracBrowser for help on using the repository browser.