Main Page · Class Overview · Hierarchy · All Classes
layoutelement-legend.h
1 /***************************************************************************
2 ** **
3 ** QCustomPlot, an easy to use, modern plotting widget for Qt **
4 ** Copyright (C) 2011, 2012, 2013 Emanuel Eichhammer **
5 ** **
6 ** This program 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 ** This program 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 this program. If not, see http://www.gnu.org/licenses/. **
18 ** **
19 ****************************************************************************
20 ** Author: Emanuel Eichhammer **
21 ** Website/Contact: http://www.qcustomplot.com/ **
22 ** Date: 04.11.13 **
23 ** Version: 1.1.0 **
24 ****************************************************************************/
25 
26 #ifndef QCP_LAYOUTELEMENT_LEGEND_H
27 #define QCP_LAYOUTELEMENT_LEGEND_H
28 
29 #include "../global.h"
30 #include "../layer.h"
31 #include "../layout.h"
32 
33 class QCPPainter;
34 class QCustomPlot;
36 class QCPLegend;
37 
38 class QCP_LIB_DECL QCPAbstractLegendItem : public QCPLayoutElement
39 {
40  Q_OBJECT
42  Q_PROPERTY(QCPLegend* parentLegend READ parentLegend)
43  Q_PROPERTY(QFont font READ font WRITE setFont)
44  Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)
45  Q_PROPERTY(QFont selectedFont READ selectedFont WRITE setSelectedFont)
46  Q_PROPERTY(QColor selectedTextColor READ selectedTextColor WRITE setSelectedTextColor)
47  Q_PROPERTY(bool selectable READ selectable WRITE setSelectable)
48  Q_PROPERTY(bool selected READ selected WRITE setSelected)
50 public:
51  explicit QCPAbstractLegendItem(QCPLegend *parent);
52 
53  // getters:
54  QCPLegend *parentLegend() const { return mParentLegend; }
55  QFont font() const { return mFont; }
56  QColor textColor() const { return mTextColor; }
57  QFont selectedFont() const { return mSelectedFont; }
58  QColor selectedTextColor() const { return mSelectedTextColor; }
59  bool selectable() const { return mSelectable; }
60  bool selected() const { return mSelected; }
61 
62  // setters:
63  void setFont(const QFont &font);
64  void setTextColor(const QColor &color);
65  void setSelectedFont(const QFont &font);
66  void setSelectedTextColor(const QColor &color);
67  void setSelectable(bool selectable);
68  void setSelected(bool selected);
69 
70  // reimplemented virtual methods:
71  virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const;
72 
73 signals:
74  void selectionChanged(bool selected);
75 
76 protected:
77  // property members:
78  QCPLegend *mParentLegend;
79  QFont mFont;
80  QColor mTextColor;
81  QFont mSelectedFont;
82  QColor mSelectedTextColor;
83  bool mSelectable, mSelected;
84 
85  // reimplemented virtual methods:
86  virtual QCP::Interaction selectionCategory() const;
87  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
88  virtual QRect clipRect() const;
89  virtual void draw(QCPPainter *painter) = 0;
90  // events:
91  virtual void selectEvent(QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged);
92  virtual void deselectEvent(bool *selectionStateChanged);
93 
94 private:
95  Q_DISABLE_COPY(QCPAbstractLegendItem)
96 
97  friend class QCPLegend;
98 };
99 
100 
102 {
103  Q_OBJECT
104 public:
106 
107  // getters:
108  QCPAbstractPlottable *plottable() { return mPlottable; }
109 
110 protected:
111  // property members:
112  QCPAbstractPlottable *mPlottable;
113 
114  // reimplemented virtual methods:
115  virtual void draw(QCPPainter *painter);
116  virtual QSize minimumSizeHint() const;
117 
118  // non-virtual methods:
119  QPen getIconBorderPen() const;
120  QColor getTextColor() const;
121  QFont getFont() const;
122 };
123 
124 
125 class QCP_LIB_DECL QCPLegend : public QCPLayoutGrid
126 {
127  Q_OBJECT
129  Q_PROPERTY(QPen borderPen READ borderPen WRITE setBorderPen)
130  Q_PROPERTY(QBrush brush READ brush WRITE setBrush)
131  Q_PROPERTY(QFont font READ font WRITE setFont)
132  Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)
133  Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize)
134  Q_PROPERTY(int iconTextPadding READ iconTextPadding WRITE setIconTextPadding)
135  Q_PROPERTY(QPen iconBorderPen READ iconBorderPen WRITE setIconBorderPen)
136  Q_PROPERTY(SelectableParts selectableParts READ selectableParts WRITE setSelectableParts)
137  Q_PROPERTY(SelectableParts selectedParts READ selectedParts WRITE setSelectedParts)
138  Q_PROPERTY(QPen selectedBorderPen READ selectedBorderPen WRITE setSelectedBorderPen)
139  Q_PROPERTY(QPen selectedIconBorderPen READ selectedIconBorderPen WRITE setSelectedIconBorderPen)
140  Q_PROPERTY(QBrush selectedBrush READ selectedBrush WRITE setSelectedBrush)
141  Q_PROPERTY(QFont selectedFont READ selectedFont WRITE setSelectedFont)
142  Q_PROPERTY(QColor selectedTextColor READ selectedTextColor WRITE setSelectedTextColor)
144 public:
150  enum SelectablePart { spNone = 0x000
151  ,spLegendBox = 0x001
152  ,spItems = 0x002
153  };
154  Q_FLAGS(SelectablePart SelectableParts)
155  Q_DECLARE_FLAGS(SelectableParts, SelectablePart)
156 
157  explicit QCPLegend();
158  virtual ~QCPLegend();
159 
160  // getters:
161  QPen borderPen() const { return mBorderPen; }
162  QBrush brush() const { return mBrush; }
163  QFont font() const { return mFont; }
164  QColor textColor() const { return mTextColor; }
165  QSize iconSize() const { return mIconSize; }
166  int iconTextPadding() const { return mIconTextPadding; }
167  QPen iconBorderPen() const { return mIconBorderPen; }
168  SelectableParts selectableParts() const { return mSelectableParts; }
169  SelectableParts selectedParts() const;
170  QPen selectedBorderPen() const { return mSelectedBorderPen; }
171  QPen selectedIconBorderPen() const { return mSelectedIconBorderPen; }
172  QBrush selectedBrush() const { return mSelectedBrush; }
173  QFont selectedFont() const { return mSelectedFont; }
174  QColor selectedTextColor() const { return mSelectedTextColor; }
175 
176  // setters:
177  void setBorderPen(const QPen &pen);
178  void setBrush(const QBrush &brush);
179  void setFont(const QFont &font);
180  void setTextColor(const QColor &color);
181  void setIconSize(const QSize &size);
182  void setIconSize(int width, int height);
183  void setIconTextPadding(int padding);
184  void setIconBorderPen(const QPen &pen);
185  void setSelectableParts(const SelectableParts &selectableParts);
186  void setSelectedParts(const SelectableParts &selectedParts);
187  void setSelectedBorderPen(const QPen &pen);
188  void setSelectedIconBorderPen(const QPen &pen);
189  void setSelectedBrush(const QBrush &brush);
190  void setSelectedFont(const QFont &font);
191  void setSelectedTextColor(const QColor &color);
192 
193  // reimplemented virtual methods:
194  virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const;
195 
196  // non-virtual methods:
197  QCPAbstractLegendItem *item(int index) const;
198  QCPPlottableLegendItem *itemWithPlottable(const QCPAbstractPlottable *plottable) const;
199  int itemCount() const;
200  bool hasItem(QCPAbstractLegendItem *item) const;
201  bool hasItemWithPlottable(const QCPAbstractPlottable *plottable) const;
202  bool addItem(QCPAbstractLegendItem *item);
203  bool removeItem(int index);
204  bool removeItem(QCPAbstractLegendItem *item);
205  void clearItems();
206  QList<QCPAbstractLegendItem*> selectedItems() const;
207 
208 signals:
209  void selectionChanged(QCPLegend::SelectableParts selection);
210 
211 protected:
212  // property members:
213  QPen mBorderPen, mIconBorderPen;
214  QBrush mBrush;
215  QFont mFont;
216  QColor mTextColor;
217  QSize mIconSize;
218  int mIconTextPadding;
219  SelectableParts mSelectedParts, mSelectableParts;
220  QPen mSelectedBorderPen, mSelectedIconBorderPen;
221  QBrush mSelectedBrush;
222  QFont mSelectedFont;
223  QColor mSelectedTextColor;
224 
225  // reimplemented virtual methods:
226  virtual void parentPlotInitialized(QCustomPlot *parentPlot);
227  virtual QCP::Interaction selectionCategory() const;
228  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
229  virtual void draw(QCPPainter *painter);
230  // events:
231  virtual void selectEvent(QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged);
232  virtual void deselectEvent(bool *selectionStateChanged);
233 
234  // non-virtual methods:
235  QPen getBorderPen() const;
236  QBrush getBrush() const;
237 
238 private:
239  Q_DISABLE_COPY(QCPLegend)
240 
241  friend class QCustomPlot;
242  friend class QCPAbstractLegendItem;
243 };
244 Q_DECLARE_OPERATORS_FOR_FLAGS(QCPLegend::SelectableParts)
245 Q_DECLARE_METATYPE(QCPLegend::SelectablePart)
246 
247 #endif // QCP_LAYOUTELEMENT_LEGEND_H