source: calamares/trunk/fuentes/src/modules/partition/core/ColorUtils.cpp @ 7538

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

sync with github

File size: 5.6 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
4 *   Copyright 2015-2016, Teo Mrnjavac <teo@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 "core/ColorUtils.h"
21
22#include "core/KPMHelpers.h"
23#include "core/PartitionIterator.h"
24
25// KPMcore
26#include <kpmcore/core/partition.h>
27#include <kpmcore/fs/luks.h>
28
29// Qt
30#include <QColor>
31#include <QMap>
32
33static const int NUM_PARTITION_COLORS = 5;
34static const int NUM_NEW_PARTITION_COLORS = 4;
35//Let's try to use the Breeze palette
36static const QColor PARTITION_COLORS[ NUM_PARTITION_COLORS ] =
37{
38    "#2980b9", //Dark Plasma Blue
39    "#27ae60", //Dark Icon Green
40    "#c9ce3b", //Dirty Yellow
41    "#3daee9", //Plasma Blue
42    "#9b59b6", //Purple
43};
44static const QColor NEW_PARTITION_COLORS[ NUM_NEW_PARTITION_COLORS ] =
45{
46    "#c0392b", //Dark Icon Red
47    "#f39c1f", //Dark Icon Yellow
48    "#f1b7bc", //Light Salmon
49    "#fed999", //Light Orange
50};
51static QColor FREE_SPACE_COLOR = "#777777";
52static QColor EXTENDED_COLOR = "#aaaaaa";
53static QColor UNKNOWN_DISKLABEL_COLOR = "#4d4151";
54
55static QMap< QString, QColor > s_partitionColorsCache;
56
57
58namespace ColorUtils
59{
60
61QColor freeSpaceColor()
62{
63    return FREE_SPACE_COLOR;
64}
65
66QColor unknownDisklabelColor()
67{
68    return UNKNOWN_DISKLABEL_COLOR;
69}
70
71PartitionNode*
72_findRootForPartition( PartitionNode* partition )
73{
74    if ( partition->isRoot() ||
75         !partition->parent() )
76        return partition;
77
78    return _findRootForPartition( partition->parent() );
79}
80
81QColor
82colorForPartition( Partition* partition )
83{
84    if ( KPMHelpers::isPartitionFreeSpace( partition ) )
85        return FREE_SPACE_COLOR;
86    if ( partition->roles().has( PartitionRole::Extended ) )
87        return EXTENDED_COLOR;
88
89    if ( partition->fileSystem().supportGetUUID() != FileSystem::cmdSupportNone &&
90         !partition->fileSystem().uuid().isEmpty() )
91    {
92        if ( partition->fileSystem().type() == FileSystem::Luks )
93        {
94            FS::luks& luksFs = dynamic_cast< FS::luks& >( partition->fileSystem() );
95            if ( !luksFs.outerUuid().isEmpty() &&
96                 s_partitionColorsCache.contains( luksFs.outerUuid() ) )
97                return s_partitionColorsCache[ luksFs.outerUuid() ];
98        }
99
100        if ( s_partitionColorsCache.contains( partition->fileSystem().uuid() ) )
101            return s_partitionColorsCache[ partition->fileSystem().uuid() ];
102    }
103
104    // No partition-specific color needed, pick one from our list, but skip
105    // free space: we don't want a partition to change colors if space before
106    // it is inserted or removed
107    PartitionNode* parent = _findRootForPartition( partition );
108    PartitionTable* table = dynamic_cast< PartitionTable* >( parent );
109    Q_ASSERT( table );
110    int colorIdx = 0;
111    int newColorIdx = 0;
112    for ( PartitionIterator it = PartitionIterator::begin( table );
113          it != PartitionIterator::end( table );
114          ++it )
115    {
116        Partition* child = *it;
117        if ( child == partition )
118            break;
119        if ( !KPMHelpers::isPartitionFreeSpace( child ) &&
120             !child->hasChildren() )
121        {
122            if ( KPMHelpers::isPartitionNew( child ) )
123                ++newColorIdx;
124            ++colorIdx;
125        }
126    }
127
128    if ( KPMHelpers::isPartitionNew( partition ) )
129        return NEW_PARTITION_COLORS[ newColorIdx % NUM_NEW_PARTITION_COLORS ];
130
131    if ( partition->fileSystem().supportGetUUID() != FileSystem::cmdSupportNone &&
132         !partition->fileSystem().uuid().isEmpty() )
133    {
134        if ( partition->fileSystem().type() == FileSystem::Luks )
135        {
136            FS::luks& luksFs = dynamic_cast< FS::luks& >( partition->fileSystem() );
137            if ( !luksFs.outerUuid().isEmpty() )
138            {
139                s_partitionColorsCache.insert( luksFs.outerUuid(),
140                                               PARTITION_COLORS[ colorIdx % NUM_PARTITION_COLORS ] );
141            }
142        }
143        else
144            s_partitionColorsCache.insert( partition->fileSystem().uuid(),
145                                           PARTITION_COLORS[ colorIdx % NUM_PARTITION_COLORS ] );
146    }
147    return PARTITION_COLORS[ colorIdx % NUM_PARTITION_COLORS ];
148}
149
150
151QColor
152colorForPartitionInFreeSpace( Partition* partition )
153{
154    PartitionNode* parent = _findRootForPartition( partition );
155    PartitionTable* table = dynamic_cast< PartitionTable* >( parent );
156    Q_ASSERT( table );
157    int newColorIdx = 0;
158    for ( PartitionIterator it = PartitionIterator::begin( table );
159          it != PartitionIterator::end( table );
160          ++it )
161    {
162        Partition* child = *it;
163        if ( child == partition )
164            break;
165        if ( !KPMHelpers::isPartitionFreeSpace( child ) &&
166             !child->hasChildren() &&
167             KPMHelpers::isPartitionNew( child ) )
168            ++newColorIdx;
169    }
170    return NEW_PARTITION_COLORS[ newColorIdx % NUM_NEW_PARTITION_COLORS ];
171}
172
173
174void
175invalidateCache()
176{
177    s_partitionColorsCache.clear();
178}
179
180} // namespace
Note: See TracBrowser for help on using the repository browser.