1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2009-12-01
7  * Description : class GroupStateComputer
8  *
9  * Copyright (C) 2010-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  * Copyright (C) 2009-2010 by Michael G. Hansen <mike at mghansen dot de>
11  *
12  * This program is free software; you can redistribute it
13  * and/or modify it under the terms of the GNU General
14  * Public License as published by the Free Software Foundation;
15  * either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * ============================================================ */
24 
25 // Local includes
26 
27 #include "groupstatecomputer.h"
28 #include "geoifacetypes.h"
29 #include "digikam_debug.h"
30 
31 namespace Digikam
32 {
33 
34 class Q_DECL_HIDDEN GroupStateComputer::Private
35 {
36 public:
37 
Private()38     explicit Private()
39       : state(SelectedNone),
40         stateMask(SelectedNone)
41     {
42     }
43 
44     GeoGroupState state;
45     GeoGroupState stateMask;
46 };
47 
GroupStateComputer()48 GroupStateComputer::GroupStateComputer()
49     : d(new Private)
50 {
51 }
52 
~GroupStateComputer()53 GroupStateComputer::~GroupStateComputer()
54 {
55 }
56 
getState() const57 GeoGroupState GroupStateComputer::getState() const
58 {
59     return d->state;
60 }
61 
clear()62 void GroupStateComputer::clear()
63 {
64     d->state     = SelectedNone;
65     d->stateMask = SelectedNone;
66 }
67 
addState(const GeoGroupState state)68 void GroupStateComputer::addState(const GeoGroupState state)
69 {
70     addSelectedState(state);
71     addFilteredPositiveState(state);
72     addRegionSelectedState(state);
73 }
74 
addSelectedState(const GeoGroupState state)75 void GroupStateComputer::addSelectedState(const GeoGroupState state)
76 {
77     if (!(d->stateMask & SelectedMask))
78     {
79         d->state     |= state;
80         d->stateMask |= SelectedMask;
81     }
82     else
83     {
84         if      ((state&SelectedMask) == SelectedAll)
85         {
86             d->state |= SelectedAll;
87         }
88         else if ((d->state&SelectedMask) == SelectedAll)
89         {
90             d->state |= SelectedSome;
91         }
92         else
93         {
94             d->state |= state;
95         }
96     }
97 }
98 
addFilteredPositiveState(const GeoGroupState state)99 void GroupStateComputer::addFilteredPositiveState(const GeoGroupState state)
100 {
101     if (!(d->stateMask & FilteredPositiveMask))
102     {
103         d->state     |= state;
104         d->stateMask |= FilteredPositiveMask;
105     }
106     else
107     {
108         if      ((state&FilteredPositiveMask) == FilteredPositiveAll)
109         {
110             d->state |= FilteredPositiveAll;
111         }
112         else if ((d->state&FilteredPositiveMask) == FilteredPositiveAll)
113         {
114             d->state |= FilteredPositiveSome;
115         }
116         else
117         {
118             d->state |= state;
119         }
120     }
121 }
122 
addRegionSelectedState(const GeoGroupState state)123 void GroupStateComputer::addRegionSelectedState(const GeoGroupState state)
124 {
125     if (!(d->stateMask & RegionSelectedMask))
126     {
127         d->state     |= state;
128         d->stateMask |= RegionSelectedMask;
129     }
130     else
131     {
132         if      ((state&RegionSelectedMask) == RegionSelectedAll)
133         {
134             d->state |= RegionSelectedAll;
135         }
136         else if ((d->state&RegionSelectedMask) == RegionSelectedAll)
137         {
138             d->state |= RegionSelectedSome;
139         }
140         else
141         {
142             d->state |= state;
143         }
144     }
145 }
146 
147 } // namespace Digikam
148