1 /** @file ui/defs.h  Common de::ui namespace definitions.
2  *
3  * @ingroup uidata
4  *
5  * @authors Copyright (c) 2013-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
6  *
7  * @par License
8  * LGPL: http://www.gnu.org/licenses/lgpl.html
9  *
10  * <small>This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or (at your
13  * option) any later version. This program is distributed in the hope that it
14  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
16  * General Public License for more details. You should have received a copy of
17  * the GNU Lesser General Public License along with this program; if not, see:
18  * http://www.gnu.org/licenses</small>
19  */
20 
21 #ifndef LIBAPPFW_UI_DEFS_H
22 #define LIBAPPFW_UI_DEFS_H
23 
24 #include "../libappfw.h"
25 #include <QFlags>
26 #include <de/Vector>
27 
28 namespace de {
29 namespace ui {
30 
31 /**
32  * Basic directions.
33  */
34 enum Direction
35 {
36     Left,
37     Up,
38     Right,
39     Down,
40     NoDirection
41 };
42 
opposite(Direction dir)43 inline Direction opposite(Direction dir) {
44     switch (dir) {
45     case Left:        return Right;
46     case Right:       return Left;
47     case Up:          return Down;
48     case Down:        return Up;
49     case NoDirection: return NoDirection;
50     }
51 }
52 
isHorizontal(Direction dir)53 inline bool isHorizontal(Direction dir) {
54     return dir == ui::Left || dir == ui::Right;
55 }
56 
isVertical(Direction dir)57 inline bool isVertical(Direction dir) {
58     return dir == ui::Up || dir == ui::Down;
59 }
60 
directionVector(Direction dir)61 inline Vector2f directionVector(Direction dir) {
62     return dir == ui::Left?  Vector2f(-1,  0) :
63            dir == ui::Right? Vector2f( 1,  0) :
64            dir == ui::Up?    Vector2f( 0, -1) :
65            dir == ui::Down?  Vector2f( 0,  1) :
66                              Vector2f();
67 }
68 
69 /**
70  * Flags for specifying alignment.
71  */
72 enum AlignmentFlag
73 {
74     AlignTop         = 0x1,
75     AlignBottom      = 0x2,
76     AlignLeft        = 0x4,
77     AlignRight       = 0x8,
78 
79     AlignTopLeft     = AlignTop | AlignLeft,
80     AlignTopRight    = AlignTop | AlignRight,
81     AlignBottomLeft  = AlignBottom | AlignLeft,
82     AlignBottomRight = AlignBottom | AlignRight,
83 
84     AlignCenter      = 0,
85 
86     DefaultAlignment = AlignCenter
87 };
Q_DECLARE_FLAGS(Alignment,AlignmentFlag)88 Q_DECLARE_FLAGS(Alignment, AlignmentFlag)
89 Q_DECLARE_OPERATORS_FOR_FLAGS(Alignment)
90 
91 template <typename SizeType, typename RectType>
92 typename RectType::Corner applyAlignment(Alignment align, SizeType const &size, RectType const &bounds)
93 {
94     typename RectType::Corner p = bounds.topLeft;
95 
96     if (align.testFlag(AlignRight))
97     {
98         p.x += int(bounds.width()) - int(size.x);
99     }
100     else if (!align.testFlag(AlignLeft))
101     {
102         p.x += (int(bounds.width()) - int(size.x)) / 2;
103     }
104 
105     if (align.testFlag(AlignBottom))
106     {
107         p.y += int(bounds.height()) - int(size.y);
108     }
109     else if (!align.testFlag(AlignTop))
110     {
111         p.y += de::floor((double(bounds.height()) - double(size.y)) / 2.0);
112     }
113 
114     return p;
115 }
116 
117 template <typename RectType, typename BoundsRectType>
applyAlignment(Alignment align,RectType & alignedRect,BoundsRectType const & boundsRect)118 void applyAlignment(Alignment align, RectType &alignedRect, BoundsRectType const &boundsRect)
119 {
120     alignedRect.moveTopLeft(applyAlignment(align, alignedRect.size(), boundsRect));
121 }
122 
123 /**
124  * Flags for specifying content fitting/scaling.
125  */
126 enum ContentFitFlag
127 {
128     OriginalSize        = 0,
129     FitToWidth          = 0x1,
130     FitToHeight         = 0x2,
131     OriginalAspectRatio = 0x4,
132     CoverArea           = 0x8, ///< Entire available area should be covered, even if
133                                ///< one dimension doesn't fit.
134 
135     FitToSize = FitToWidth | FitToHeight
136 };
137 Q_DECLARE_FLAGS(ContentFit, ContentFitFlag)
138 Q_DECLARE_OPERATORS_FOR_FLAGS(ContentFit)
139 
140 /**
141  * Policy for controlling size.
142  */
143 enum SizePolicy
144 {
145     Fixed,  ///< Size is fixed, content positioned inside.
146     Filled, ///< Size is fixed, content expands to fill entire area.
147     Expand  ///< Size depends on content, expands/contracts to fit.
148 };
149 
150 } // namespace ui
151 } // namespace de
152 
153 #endif // LIBAPPFW_UI_DEFS_H
154