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, 2014 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: 07.04.14 **
23 ** Version: 1.2.1 **
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 NOTIFY selectionChanged)
48  Q_PROPERTY(bool selected READ selected WRITE setSelected NOTIFY selectableChanged)
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  Q_SLOT void setSelectable(bool selectable);
68  Q_SLOT 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  void selectableChanged(bool selectable);
76 
77 protected:
78  // property members:
79  QCPLegend *mParentLegend;
80  QFont mFont;
81  QColor mTextColor;
82  QFont mSelectedFont;
83  QColor mSelectedTextColor;
84  bool mSelectable, mSelected;
85 
86  // reimplemented virtual methods:
87  virtual QCP::Interaction selectionCategory() const;
88  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
89  virtual QRect clipRect() const;
90  virtual void draw(QCPPainter *painter) = 0;
91  // events:
92  virtual void selectEvent(QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged);
93  virtual void deselectEvent(bool *selectionStateChanged);
94 
95 private:
96  Q_DISABLE_COPY(QCPAbstractLegendItem)
97 
98  friend class QCPLegend;
99 };
100 
101 
103 {
104  Q_OBJECT
105 public:
107 
108  // getters:
109  QCPAbstractPlottable *plottable() { return mPlottable; }
110 
111 protected:
112  // property members:
113  QCPAbstractPlottable *mPlottable;
114 
115  // reimplemented virtual methods:
116  virtual void draw(QCPPainter *painter);
117  virtual QSize minimumSizeHint() const;
118 
119  // non-virtual methods:
120  QPen getIconBorderPen() const;
121  QColor getTextColor() const;
122  QFont getFont() const;
123 };
124 
125 
126 class QCP_LIB_DECL QCPLegend : public QCPLayoutGrid
127 {
128  Q_OBJECT
130  Q_PROPERTY(QPen borderPen READ borderPen WRITE setBorderPen)
131  Q_PROPERTY(QBrush brush READ brush WRITE setBrush)
132  Q_PROPERTY(QFont font READ font WRITE setFont)
133  Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)
134  Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize)
135  Q_PROPERTY(int iconTextPadding READ iconTextPadding WRITE setIconTextPadding)
136  Q_PROPERTY(QPen iconBorderPen READ iconBorderPen WRITE setIconBorderPen)
137  Q_PROPERTY(SelectableParts selectableParts READ selectableParts WRITE setSelectableParts NOTIFY selectionChanged)
138  Q_PROPERTY(SelectableParts selectedParts READ selectedParts WRITE setSelectedParts NOTIFY selectableChanged)
139  Q_PROPERTY(QPen selectedBorderPen READ selectedBorderPen WRITE setSelectedBorderPen)
140  Q_PROPERTY(QPen selectedIconBorderPen READ selectedIconBorderPen WRITE setSelectedIconBorderPen)
141  Q_PROPERTY(QBrush selectedBrush READ selectedBrush WRITE setSelectedBrush)
142  Q_PROPERTY(QFont selectedFont READ selectedFont WRITE setSelectedFont)
143  Q_PROPERTY(QColor selectedTextColor READ selectedTextColor WRITE setSelectedTextColor)
145 public:
151  enum SelectablePart { spNone = 0x000
152  ,spLegendBox = 0x001
153  ,spItems = 0x002
154  };
155  Q_FLAGS(SelectablePart SelectableParts)
156  Q_DECLARE_FLAGS(SelectableParts, SelectablePart)
157 
158  explicit QCPLegend();
159  virtual ~QCPLegend();
160 
161  // getters:
162  QPen borderPen() const { return mBorderPen; }
163  QBrush brush() const { return mBrush; }
164  QFont font() const { return mFont; }
165  QColor textColor() const { return mTextColor; }
166  QSize iconSize() const { return mIconSize; }
167  int iconTextPadding() const { return mIconTextPadding; }
168  QPen iconBorderPen() const { return mIconBorderPen; }
169  SelectableParts selectableParts() const { return mSelectableParts; }
170  SelectableParts selectedParts() const;
171  QPen selectedBorderPen() const { return mSelectedBorderPen; }
172  QPen selectedIconBorderPen() const { return mSelectedIconBorderPen; }
173  QBrush selectedBrush() const { return mSelectedBrush; }
174  QFont selectedFont() const { return mSelectedFont; }
175  QColor selectedTextColor() const { return mSelectedTextColor; }
176 
177  // setters:
178  void setBorderPen(const QPen &pen);
179  void setBrush(const QBrush &brush);
180  void setFont(const QFont &font);
181  void setTextColor(const QColor &color);
182  void setIconSize(const QSize &size);
183  void setIconSize(int width, int height);
184  void setIconTextPadding(int padding);
185  void setIconBorderPen(const QPen &pen);
186  Q_SLOT void setSelectableParts(const SelectableParts &selectableParts);
187  Q_SLOT void setSelectedParts(const SelectableParts &selectedParts);
188  void setSelectedBorderPen(const QPen &pen);
189  void setSelectedIconBorderPen(const QPen &pen);
190  void setSelectedBrush(const QBrush &brush);
191  void setSelectedFont(const QFont &font);
192  void setSelectedTextColor(const QColor &color);
193 
194  // reimplemented virtual methods:
195  virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const;
196 
197  // non-virtual methods:
198  QCPAbstractLegendItem *item(int index) const;
199  QCPPlottableLegendItem *itemWithPlottable(const QCPAbstractPlottable *plottable) const;
200  int itemCount() const;
201  bool hasItem(QCPAbstractLegendItem *item) const;
202  bool hasItemWithPlottable(const QCPAbstractPlottable *plottable) const;
203  bool addItem(QCPAbstractLegendItem *item);
204  bool removeItem(int index);
205  bool removeItem(QCPAbstractLegendItem *item);
206  void clearItems();
207  QList<QCPAbstractLegendItem*> selectedItems() const;
208 
209 signals:
210  void selectionChanged(QCPLegend::SelectableParts parts);
211  void selectableChanged(QCPLegend::SelectableParts parts);
212 
213 protected:
214  // property members:
215  QPen mBorderPen, mIconBorderPen;
216  QBrush mBrush;
217  QFont mFont;
218  QColor mTextColor;
219  QSize mIconSize;
220  int mIconTextPadding;
221  SelectableParts mSelectedParts, mSelectableParts;
222  QPen mSelectedBorderPen, mSelectedIconBorderPen;
223  QBrush mSelectedBrush;
224  QFont mSelectedFont;
225  QColor mSelectedTextColor;
226 
227  // reimplemented virtual methods:
228  virtual void parentPlotInitialized(QCustomPlot *parentPlot);
229  virtual QCP::Interaction selectionCategory() const;
230  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
231  virtual void draw(QCPPainter *painter);
232  // events:
233  virtual void selectEvent(QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged);
234  virtual void deselectEvent(bool *selectionStateChanged);
235 
236  // non-virtual methods:
237  QPen getBorderPen() const;
238  QBrush getBrush() const;
239 
240 private:
241  Q_DISABLE_COPY(QCPLegend)
242 
243  friend class QCustomPlot;
244  friend class QCPAbstractLegendItem;
245 };
246 Q_DECLARE_OPERATORS_FOR_FLAGS(QCPLegend::SelectableParts)
247 Q_DECLARE_METATYPE(QCPLegend::SelectablePart)
248 
249 #endif // QCP_LAYOUTELEMENT_LEGEND_H