/
Legend.cpp
174 lines (154 loc) · 4.95 KB
/
Legend.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linkoping University,
* Department of Computer and Information Science,
* SE-58183 Linkoping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linkoping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
* Main Author 2011: Adeel Asghar
*
*/
#include "Legend.h"
#include "iostream"
#include "qwt_legend_item.h"
using namespace OMPlot;
Legend::Legend(Plot *pParent)
{
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(legendMenu(const QPoint&)));
mpPlot = pParent;
// create actions for context menu
mpChangeColorAction = new QAction(QString("Change Color"), this);
connect(mpChangeColorAction, SIGNAL(triggered()), this, SLOT(selectColor()));
mpAutomaticColorAction = new QAction(QString("Automatic Color"), this);
mpAutomaticColorAction->setCheckable(true);
mpAutomaticColorAction->setChecked(true);
connect(mpAutomaticColorAction, SIGNAL(triggered(bool)), this, SLOT(automaticColor(bool)));
mpHideAction = new QAction(QString("Hide"), this);
mpHideAction->setCheckable(true);
connect(mpHideAction, SIGNAL(triggered(bool)), this, SLOT(toggleHide(bool)));
}
Legend::~Legend()
{
}
void Legend::legendMenu(const QPoint& pos)
{
QwtLegendItem *lgdItem = dynamic_cast<QwtLegendItem*>(childAt(pos));
if(lgdItem)
{
mLegendItemStr = lgdItem->text().text();
QMenu menu(mpPlot);
menu.addAction(mpChangeColorAction);
menu.addAction(mpAutomaticColorAction);
menu.addSeparator();
menu.addAction(mpHideAction);
menu.exec(mapToGlobal(pos));
}
}
void Legend::selectColor()
{
QColor c = QColorDialog::getColor();
QList<PlotCurve*> list = mpPlot->getPlotCurvesList();
if(c.isValid())
{
for(int i = 0; i < list.length(); i++)
{
if(list[i]->title() == mLegendItemStr)
{
list[i]->setCustomColor(true);
QPen pen = list[i]->pen();
pen.setColor(c);
list[i]->setPen(pen);
mpAutomaticColorAction->setChecked(false);
}
}
mpPlot->replot();
}
}
void Legend::toggleHide(bool hide)
{
QList<PlotCurve*> list = mpPlot->getPlotCurvesList();
for(int i = 0; i < list.length(); i++)
{
if(list[i]->title().text() == mLegendItemStr)
{
if (hide)
{
QwtText text = list[i]->title();
text.setColor(QColor(Qt::gray));
list[i]->setTitle(text);
list[i]->setVisible(false);
}
else
{
QwtText text = list[i]->title();
text.setColor(QColor(Qt::black));
list[i]->setTitle(text.text());
list[i]->setVisible(true);
}
}
}
mpPlot->replot();
}
void Legend::automaticColor(bool automatic)
{
QList<PlotCurve*> list = mpPlot->getPlotCurvesList();
for(int i = 0; i < list.length(); i++)
{
if(list[i]->title().text() == mLegendItemStr)
{
if (automatic)
{
list[i]->setCustomColor(false);
}
else
{
if (list[i]->hasCustomColor())
{
list[i]->setCustomColor(true);
}
else
{
mpAutomaticColorAction->blockSignals(true);
mpAutomaticColorAction->setChecked(true);
mpAutomaticColorAction->blockSignals(false);
}
}
}
}
mpPlot->replot();
}
void Legend::setLegendItemStr(QString value)
{
mLegendItemStr = value;
}
QAction* Legend::getAutomaticColorAction()
{
return mpAutomaticColorAction;
}
QAction* Legend::getHideAction()
{
return mpHideAction;
}