source: orange/source/orangeqt/point.h @ 10916:9c26e90af882

Revision 10916:9c26e90af882, 5.5 KB checked in by mstajdohar, 22 months ago (diff)

Added text() method to orangeqt.Point class.

Line 
1/*
2    This file is part of the plot module for Orange
3    Copyright (C) 2011  Miha Čančula <miha@noughmad.eu>
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef POINT_H
20#define POINT_H
21
22#include <QtGui/QGraphicsObject>
23#include <QtCore/QDebug>
24#include <QtCore/QPropertyAnimation>
25
26struct DataPoint
27{
28  double x;
29  double y;
30 
31  operator QPointF() const;
32};
33
34Q_DECLARE_METATYPE(DataPoint)
35
36QDebug& operator<<(QDebug& stream, const DataPoint& point);
37bool operator==(const DataPoint& one, const DataPoint& other);
38
39struct PointData
40{
41    PointData(int size, int symbol, const QColor& color, int state, bool transparent) : size(size), symbol(symbol), color(color), state(state), transparent(transparent) {}
42    int size;
43    int symbol;
44    QColor color;
45    int state;
46    bool transparent;
47};
48
49class LabelItem : public QGraphicsTextItem
50{
51public:
52    LabelItem(QGraphicsItem *parent = 0);
53    LabelItem(const QString &text, QGraphicsItem *parent = 0);
54    ~LabelItem();
55
56    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
57};
58
59class Point : public QGraphicsObject
60{
61    Q_OBJECT
62    Q_PROPERTY(QColor color READ color WRITE set_color)
63    Q_PROPERTY(int symbol READ symbol WRITE set_symbol)
64    Q_PROPERTY(int size READ size WRITE set_size)
65    //Q_PROPERTY(QString label READ label WRITE set_label)
66    Q_PROPERTY(DataPoint coordinates READ coordinates WRITE set_coordinates)
67    Q_PROPERTY(bool marked READ is_marked WRITE set_marked STORED false)
68    Q_PROPERTY(bool selected READ is_selected WRITE set_selected STORED false)
69   
70public:
71    enum DisplayMode
72    {
73        DisplayPixmap,
74        DisplayPath
75    };
76   
77  /**
78   * @brief Point symbol
79   *
80   * The symbols list here matches the one from QwtPlotCurve.
81   **/
82  enum Symbol {
83    NoSymbol = -1,
84    Ellipse = 0,
85    Rect = 1,
86    Diamond = 2,
87    Triangle = 3,
88    DTriangle = 4,
89    UTriangle = 5,
90    LTriangle = 6,
91    RTriangle = 7,
92    Cross = 8,
93    XCross = 9,
94    HLine = 10,
95    VLine = 11,
96    Star1 = 12,
97    Star2 = 13,
98    Hexagon = 14,
99    UserStyle = 1000
100  };
101 
102    enum StateFlag
103    {
104        Normal = 0x00,
105        Marked = 0x01,
106        Selected = 0x02
107    };
108   
109    Q_DECLARE_FLAGS(State, StateFlag)
110 
111    enum 
112    {
113        Type = UserType + 1
114    };
115   
116    virtual int type() const
117    {
118        return Type;
119    }
120   
121    explicit Point(QGraphicsItem* parent = 0);
122    Point(int symbol, QColor color, int size, QGraphicsItem* parent = 0);
123    virtual ~Point();
124   
125    virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
126    virtual QRectF boundingRect() const;
127   
128    void set_symbol(int symbol);
129    int symbol() const;
130   
131    void set_color(const QColor& color);
132    QColor color() const;
133   
134    void set_size(int size);
135    int size() const;
136   
137    void set_display_mode(DisplayMode mode);
138    DisplayMode display_mode() const;
139   
140    void set_state(State state);
141    State state() const;
142    void set_state_flag(StateFlag flag, bool on);
143    bool state_flag(StateFlag flag) const;
144   
145    void set_selected(bool selected);
146    bool is_selected() const;
147
148    void set_marked(bool marked);
149    bool is_marked() const;
150
151    bool is_transparent();
152    void set_transparent(bool transparent);
153   
154    DataPoint coordinates() const;
155    void set_coordinates(const DataPoint& data_point);
156   
157    //void set_label(const QString& label);
158    QString text() const;
159   
160    /**
161    * Creates a path from a symbol and a size
162    *
163    * @param symbol the point symbol to use
164    * @param size the size of the resulting path
165    * @return a path that can be used in a QGraphicsPathItem or in QPainter::drawPath()
166    **/
167    static QPainterPath path_for_symbol(int symbol, int size);
168   
169    static QPixmap pixmap_for_symbol(int symbol, QColor color, int size);
170    static QRectF rect_for_size(double size);
171   
172    static void clear_cache();
173
174    static QHash<PointData, QPixmap> pixmap_cache;
175
176    LabelItem* label;
177
178
179
180private:
181    static QPainterPath trianglePath(double d, double rot);
182    static QPainterPath crossPath(double d, double rot);
183    static QPainterPath hexPath(double d, bool star);
184
185    int m_symbol;
186    QColor m_color;
187    int m_size;
188    DisplayMode m_display_mode;
189    State m_state;
190    bool m_transparent;
191   
192    DataPoint m_coordinates;
193    //QString m_label;
194};
195
196struct PointPosUpdater
197{
198  PointPosUpdater(QTransform t) : t(t) {}
199  void operator()(Point* point)
200  {
201    QPointF p = t.map(QPointF(point->coordinates().x, point->coordinates().y));
202    point->setPos(p);
203    if (point->label)
204    {
205        point->label->setPos(p);
206    }
207  }
208 
209private:
210    QTransform t;
211};
212
213struct PointPropertyUpdater
214{
215    PointPropertyUpdater(const QByteArray& property, const QVariant& value) : property(property), value(value) {}
216    void operator()(Point* point)
217    {
218        point->setProperty(property, value);
219    }
220   
221private:
222    QByteArray property;
223    QVariant value;
224};
225
226#endif // POINT_H
Note: See TracBrowser for help on using the repository browser.