1 /***************************************************************************
2  *   Copyright (C) 2012 by the fifechan team                               *
3  *   http://fifechan.github.com/fifechan                                   *
4  *   This file is part of fifechan.                                        *
5  *                                                                         *
6  *   fifechan is free software; you can redistribute it and/or             *
7  *   modify it under the terms of the GNU Lesser General Public            *
8  *   License as published by the Free Software Foundation; either          *
9  *   version 2.1 of the License, or (at your option) any later version.    *
10  *                                                                         *
11  *   This library 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 GNU     *
14  *   Lesser General Public License for more details.                       *
15  *                                                                         *
16  *   You should have received a copy of the GNU Lesser General Public      *
17  *   License along with this library; if not, write to the                 *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
20  ***************************************************************************/
21 
22 #include <fifechan/exception.hpp>
23 #include <fifechan/graphics.hpp>
24 #include <fifechan/widgets/piegraph.hpp>
25 
26 
27 namespace fcn {
28 
PieGraph()29     PieGraph::PieGraph():
30         m_opaque(false),
31         m_center(),
32         m_radius(10) {
33     }
34 
PieGraph(const Point & center)35     PieGraph::PieGraph(const Point& center):
36         m_opaque(false),
37         m_center(center),
38         m_radius(10) {
39     }
40 
setCenterX(int x)41     void PieGraph::setCenterX(int x) {
42         m_center.x = x;
43     }
44 
setCenterY(int y)45     void PieGraph::setCenterY(int y) {
46         m_center.y = y;
47     }
48 
setCenter(int x,int y)49     void PieGraph::setCenter(int x, int y) {
50         m_center.x = x;
51         m_center.y = y;
52     }
53 
getCenterX() const54     int PieGraph::getCenterX() const {
55         return m_center.x;
56     }
57 
getCenterY() const58     int PieGraph::getCenterY() const {
59         return m_center.y;
60     }
61 
setCenter(const Point & center)62     void PieGraph::setCenter(const Point& center) {
63         m_center = center;
64     }
65 
getCenter() const66     const Point& PieGraph::getCenter() const {
67         return m_center;
68     }
69 
setRadius(int radius)70     void PieGraph::setRadius(int radius) {
71         m_radius = radius;
72     }
73 
getRadius() const74     int PieGraph::getRadius() const {
75         return m_radius;
76     }
77 
addSegment(int startAngle,int stopAngle,const Color & color)78     void PieGraph::addSegment(int startAngle, int stopAngle, const Color& color) {
79         PieGraphSegment segment;
80         segment.startAngle = startAngle;
81         segment.stopAngle = stopAngle;
82         segment.color = color;
83         m_segments.push_back(segment);
84     }
85 
clearSegments()86     void PieGraph::clearSegments() {
87         m_segments.clear();
88     }
89 
setOpaque(bool opaque)90     void PieGraph::setOpaque(bool opaque) {
91         m_opaque = opaque;
92     }
93 
isOpaque() const94     bool PieGraph::isOpaque() const {
95         return m_opaque;
96     }
97 
draw(Graphics * graphics)98     void PieGraph::draw(Graphics* graphics) {
99         bool active = isFocused();
100 
101         if (isOpaque()) {
102             // Fill the background around the content
103             if (active && ((getSelectionMode() & Widget::Selection_Background) == Widget::Selection_Background)) {
104                 graphics->setColor(getSelectionColor());
105             } else {
106                 graphics->setColor(getBackgroundColor());
107             }
108             graphics->fillRectangle(getBorderSize(), getBorderSize(),
109                 getWidth() - 2 * getBorderSize(), getHeight() - 2 * getBorderSize());
110         }
111         // draw border or frame
112         if (getBorderSize() > 0) {
113             if (active && (getSelectionMode() & Widget::Selection_Border) == Widget::Selection_Border) {
114                 drawSelectionFrame(graphics);
115             } else {
116                 drawBorder(graphics);
117             }
118         }
119 
120         if (m_segments.empty() || m_radius < 1) {
121             return;
122         }
123         // draw circle segments
124         std::vector<PieGraphSegment>::iterator it = m_segments.begin();
125         for (; it != m_segments.end(); ++it) {
126             graphics->setColor((*it).color);
127             graphics->drawFillCircleSegment(m_center, m_radius, (*it).startAngle, (*it).stopAngle);
128         }
129     }
130 
131 };