File: plotter.C

package info (click to toggle)
ball 1.4.3~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 318,984 kB
  • sloc: cpp: 346,579; ansic: 4,097; python: 2,664; yacc: 1,778; lex: 1,099; xml: 964; sh: 688; sql: 316; awk: 118; makefile: 108
file content (235 lines) | stat: -rw-r--r-- 5,430 bytes parent folder | download | duplicates (2)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* TRANSLATOR BALL::QSAR

		Necessary for lupdate.
*/

#include <plotter.h>
#include <iostream>
#include <QPrinter>
#include <QFileDialog>
#include <QPrintDialog>
#include <qwt_plot_zoomer.h>

#include <BALL/MATHS/common.h>
#include <BALL/VIEW/KERNEL/iconLoader.h>
 
using namespace BALL::VIEW;
using namespace std;

Plotter::Plotter(DataItem* item)
{
	item_ = item;
	selected_activity_ = 0;
	
	data_symbol.setStyle(QwtSymbol::Ellipse);
	data_symbol.setSize(5,5);
	data_label_font.setPointSize(6);
	data_label_alignment=Qt::AlignRight;
	show_data_labels = 1;
	print_data_symbol = data_symbol;
	
	zoomer_ = NULL;
	qwt_plot_ = new QwtPlot;
	
	okButton_ = new QPushButton("Ok", this);
	show_labels_ = new QCheckBox("show labels",this);
	show_labels_->setChecked(show_data_labels);
	saveButton_ = new QPushButton("save",this);
	printButton_ = new QPushButton("print",this);
	
	buttonsLayout_ = new QHBoxLayout;
	buttonsLayout_->addWidget(okButton_);
	buttonsLayout_->addWidget(show_labels_);
	buttonsLayout_->addWidget(saveButton_);
	buttonsLayout_->addWidget(printButton_);
	activity_combobox_ = new QComboBox;
	buttonsLayout_->addWidget(activity_combobox_);
	buttonsLayout_->setAlignment(Qt::AlignLeft);
	
	main_layout_ = new QVBoxLayout;
	main_layout_->addWidget(qwt_plot_);
	main_layout_->addLayout(buttonsLayout_);
	setLayout(main_layout_);
	
	connect(okButton_, SIGNAL(clicked()), this, SLOT(close()));
	connect(show_labels_, SIGNAL(clicked()), this, SLOT(labelsChangeState()));
	connect(saveButton_, SIGNAL(clicked()), this, SLOT(save()));
	connect(printButton_,SIGNAL(clicked()),this,SLOT(print()));
	connect(activity_combobox_,SIGNAL(currentIndexChanged(int)),this,SLOT(activityChange()));
	
	activity_combobox_->hide();
	resize(600,400);
	qwt_plot_->resize(600,400);
	qwt_plot_->show();
}



Plotter::~Plotter()
{
	delete qwt_plot_;
 	delete okButton_;
 	delete show_labels_;
 	delete saveButton_;
	delete printButton_;
	delete buttonsLayout_;
	delete main_layout_;
}

// SLOT
void Plotter::labelsChangeState()
{
	
	int a = show_labels_->checkState();
	if(a==0) // unchecked
	{
		show_data_labels = 0;
		plot(0);
		qwt_plot_->replot();
	}
	else if(a==2) // checked
	{
		show_data_labels = 1;
		plot(0);
		qwt_plot_->replot();
	}
}

// SLOT
void Plotter::save()
{
	QwtSymbol symbol_backup = data_symbol;
	if(data_symbol!=print_data_symbol)
	{
		data_symbol = print_data_symbol;
		plot(0);
	}
	
	QString selected_ext;
	QString file = QFileDialog::getSaveFileName(this, tr("Save Plot"),
		QDir::homePath(), tr("PNG (*.png);;Encapsulated PostScript (*.eps);;Portable Document Format (*.pdf);;PostScript (*.ps);;JPG (*.jpg);;GIF (*.gif);;Windows Bitmap (*.bmp);;Tagged Image File Format (*.tif)"),&selected_ext);
	if(file=="") return; 
	
	String f=file.toStdString();
	bool valid_extension=false;
	if(f.find_last_of(".")!=string::npos) // check whether user ommitted file-extension
	{
		String ext=f.substr(f.find_last_of(".")+1);
		if(ext=="png"||ext=="eps"||ext=="pdf"||ext=="ps"||ext=="jpg"||ext=="gif"||ext=="bmp"||ext=="tif")
		{
			valid_extension=true;
		}
	}
	if(!valid_extension) // set extension according to selected filter
	{
		String s = selected_ext.toStdString().substr(0,3);
		if(s=="PNG") file+=".png";
		else if(s=="Enc") file+=".eps";
		else if(s=="Por") file+=".pdf";
		else if(s=="Pos") file+=".ps";
		else if(s=="JPG") file+=".jpg";
		else if(s=="GIF") file+=".gif";
		else if(s=="Win") file+=".bmp";
		else if(s=="Tag") file+=".tif";		
	}
	
	printToFile(file);
	
	if(data_symbol!=symbol_backup)
	{
		data_symbol = symbol_backup;
		plot(0);
	}
}

// SLOT
void Plotter::print()
{
	QwtSymbol symbol_backup = data_symbol;
	if(data_symbol!=print_data_symbol)
	{
		data_symbol = print_data_symbol;
		plot(0);
	}
	
	QPrinter printer(QPrinter::HighResolution);
	QPrintDialog print_dialog(&printer,this);
	if (print_dialog.exec() == QDialog::Accepted) 
	{
		printer.setResolution(600);
		qwt_plot_->print(printer);
	}
	
	if(data_symbol!=symbol_backup)
	{
		data_symbol = symbol_backup;
		plot(0);
	}
}


// SLOT
void Plotter::activityChange()
{
	delete zoomer_;
	selected_activity_ = activity_combobox_->itemData(activity_combobox_->currentIndex()).toInt();
	plot(1);
	qwt_plot_->replot();
	zoomer_ = new QwtPlotZoomer(qwt_plot_->canvas(),this); // if not creating a new zoomer, zooming will not work correctly
}


void Plotter::printToFile(QString& file)
{
	QwtSymbol symbol_backup = data_symbol;
	if(data_symbol!=print_data_symbol)
	{
		data_symbol = print_data_symbol;
		plot(0);
	}
	
	String ext = file.toStdString();
	if(ext.find_last_of(".")!=string::npos)
	{
		ext=ext.substr(ext.find_last_of(".")+1);
	}
	if(ext=="eps"||ext=="pdf"||ext=="ps")
	{
		QPrinter printer(QPrinter::HighResolution);
		printer.setOutputFileName(file);
		printer.setResolution(600);
		printer.setOrientation(QPrinter::Landscape);
		qwt_plot_->print(printer);
	}
	else
	{
		QImage image(qwt_plot_->width(),qwt_plot_->height(),QImage::Format_RGB32);
		QColor color(255,255,255);
		image.fill(color.rgb());
		qwt_plot_->print(image);
		image.save(file);		
	}
	
	if(data_symbol!=symbol_backup)
	{
		data_symbol = symbol_backup;
		plot(0);
	}
}

QColor Plotter::generateColor(int no_colors, int current_no)
{
	int k = BALL::Maths::round(pow(no_colors,1/3.));
	int n = pow(3.,k)-1;
	int width = 255./k;
	
	int R = (current_no/3)*width;
	int G = ((current_no+2)/3)*width;
	int B = ((n-current_no-1)/3)*width;
	
	return QColor(R,G,B);
}