qthistodraw.cpp 10.5 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
///*******************************************************************************
//* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
//* version 0.1                                                                  *
//* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
//*                                                                              *
//* This library is free software; you can redistribute it and/or modify it      *
//* under the terms of the GNU Lesser General Public License as published by the *
//* Free Software Foundation; either version 2.1 of the License, or (at your     *
//* option) any later version.                                                   *
//*                                                                              *
//* This library is distributed in the hope that it will be useful, but WITHOUT  *
//* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
//* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
//* for more details.                                                            *
//*                                                                              *
//* You should have received a copy of the GNU Lesser General Public License     *
//* along with this library; if not, write to the Free Software Foundation,      *
//* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
//*                                                                              *
//* Web site: http://cgogn.unistra.fr/                                           *
//* Contact information: cgogn@unistra.fr                                        *
//*                                                                              *
//*******************************************************************************/
//#define CGoGN_UTILS_DLL_EXPORT 1
//
//#include <QtSvg/QSvgGenerator>
//#include <QPen>
//#include <cmath>
//#include "Utils/Qt/qthistodraw.h"
//
//namespace CGoGN
//{
//
//namespace Utils
//{
//
//namespace QT
//{
//
//
//RenderHistogram::RenderHistogram(QWidget* parent, Algo::Histogram::Histogram& histo, bool drawAxis )
//:QWidget(parent),m_histo(histo), m_drawHisto(true), m_drawQuantiles(false), m_histoFront(true),m_opacity(0.5f),m_drawAxis(drawAxis)
//{
//    setBackgroundRole(QPalette::Base);
//    setAutoFillBackground(true);
//}
//
//
//void RenderHistogram::axeVals()
//{
//	int m = m_histo.getMaxBar();
//	m_vals_ax_left.clear();
//	m_vals_ax_left.reserve(4);
//	m_axl_nbd = floor(log10(double(m)));
//
//	int x = int (floor(m / pow(10.0,m_axl_nbd)));
//	int y = int(pow(10.0,m_axl_nbd));
//
//	switch(x)
//	{
//	case 9:
//	case 8:
//		m_vals_ax_left.push_back(2*y);m_vals_ax_left.push_back(4*y);m_vals_ax_left.push_back(6*y);m_vals_ax_left.push_back(8*y);
//		break;
//	case 7:
//	case 6:
//		m_vals_ax_left.push_back(2*y);m_vals_ax_left.push_back(4*y);m_vals_ax_left.push_back(6*y);
//		break;
//	case 5:
//	case 4:
//		m_vals_ax_left.push_back(y);m_vals_ax_left.push_back(2*y);m_vals_ax_left.push_back(3*y);m_vals_ax_left.push_back(4*y);
//		break;
//	case 3:
//		m_vals_ax_left.push_back(y);m_vals_ax_left.push_back(2*y);m_vals_ax_left.push_back(3*y);
//		break;
//	case 2:
//		m_vals_ax_left.push_back(y/2);m_vals_ax_left.push_back(y);m_vals_ax_left.push_back(3*y/2);m_vals_ax_left.push_back(2*y);m_vals_ax_left.push_back(5*y/2);
//		break;
//	case 1:
//		{
//			m_vals_ax_left.push_back(y/4);m_vals_ax_left.push_back(y/2);m_vals_ax_left.push_back(3*y/4);m_vals_ax_left.push_back(y);
//			int yy = y + y/4;
//			while (yy <= m)
//			{
//				m_vals_ax_left.push_back(yy);
//				yy += y/4;
//			}
//		}
//		break;
//	}
//	// store number of digits
//	m_axl_nbd++;
//}
//
//
//QSize RenderHistogram::minimumSizeHint() const
//{
//    return QSize(100, 100);
//}
//
//QSize RenderHistogram::sizeHint() const
//{
//    return QSize(400, 200);
//}
//
//void RenderHistogram::draw(QPainter& painter)
//{
//	QRect bb = painter.viewport();
//
//	axeVals();
//
//	unsigned int sz_bottonAxis = 50;
//
//	m_h = bb.height()- (2*m_frameWidth);
//
//	if (m_drawAxis)
//		m_h -= sz_bottonAxis;
//
//	m_w = bb.width()- (2*m_frameWidth + 8*m_axl_nbd);
//
//	if (m_histoFront)
//	{
//		painter.setOpacity(1.0);
//		if (m_drawQuantiles)
//		{
//			drawQuantiles(painter);
//			painter.setOpacity(m_opacity);
//		}
//		if (m_drawHisto)
//			drawHisto(painter);
//	}
//	else
//	{
//		painter.setOpacity(1.0);
//		if (m_drawHisto)
//		{
//			drawHisto(painter);
//			painter.setOpacity(m_opacity);
//		}
//		if (m_drawQuantiles)
//			drawQuantiles(painter);
//	}
//
//}
//
//void RenderHistogram::drawHisto(QPainter& painter)
//{
//	unsigned int nbmax = m_histo.getMaxBar();
//
//	int widthAxl = 8*m_axl_nbd;
//	const std::vector<unsigned int>& pop = m_histo.getPopulation();
//	m_l = m_w/uint32(pop.size());
//
//	qreal op = painter.opacity();
//	painter.setOpacity(1.0);
//
//	if (m_drawAxis)
//	{
//		// draw axes:
//		axeVals();
//
//		for (unsigned int i = 0; i< m_vals_ax_left.size(); ++i)
//		{
//			int h = m_h+m_frameWidth - (m_vals_ax_left[i]*m_h)/nbmax;
//			painter.setPen(QColor(100,100,100));
//			painter.drawLine(5+widthAxl, h ,widthAxl+m_w+m_frameWidth, h);
//			painter.setPen(QColor(0,0,0));
//			std::ostringstream oss(std::ostringstream::out);
//			oss << m_vals_ax_left[i];
//			std::string sv =  oss.str();
//			const char* ptr =sv.c_str();
//			QString qsv(ptr);
//
//			painter.drawText (1,h-6,widthAxl,15,Qt::AlignRight,qsv);
//		}
//
//		painter.setPen(QColor(0,0,0));
//		QFont qf = painter.font();
//		if (qf.pointSize() > int(m_l))
//		{
//			qf.setPointSize( m_l-2);
//			painter.setFont(qf);
//		}
//		double lb = (m_histo.getMax() - m_histo.getMin())/pop.size();
//		for (unsigned int i=0; i<=pop.size(); ++i)
//		{
//			double val =  m_histo.getMin() + lb*i;
//			std::ostringstream oss(std::ostringstream::out);
//			oss <<val;
//			std::string sv = oss.str();
//			const char* ptr = sv.c_str();
//			QString qsv(ptr);
//			painter.save();
//			painter.translate(widthAxl+m_frameWidth+i*m_l, m_h+ m_frameWidth+8);
//			painter.rotate(60);
//			painter.drawText (0,0,qsv);
//			painter.restore();
//		}
//	}
//
//	painter.drawLine(widthAxl+m_frameWidth, m_h+m_frameWidth ,widthAxl+m_w+m_frameWidth, m_h+m_frameWidth);
//	painter.drawLine(widthAxl+m_frameWidth, m_frameWidth ,widthAxl+m_frameWidth, m_h+m_frameWidth);
//
//
//	painter.setOpacity(op);
//
//
//	for (unsigned int i = 0; i< pop.size(); ++i)
//	{
//		int v = (pop[i]*m_h)/nbmax;
//
//		QRect rect(widthAxl+m_frameWidth+i*m_l, m_h+m_frameWidth-v, m_l, v);
//		Geom::Vec3f col = m_histo.colorMap().colorIndex(i);
//		painter.setBrush(QColor(255*col[0],255*col[1],255*col[2]));
//		painter.drawRect(rect);
//	}
//}
//
//void RenderHistogram::drawQuantiles(QPainter& painter)
//{
//	/// vector of intervals
//	const std::vector<double>& interv = m_histo.getQuantilesIntervals();
//	const std::vector<double>& pop = m_histo.getQuantilesHeights();
//
//	if (interv.empty())
//		return;
//
//	int widthAxl = 8*m_axl_nbd;
//
//	painter.setPen(QColor(0,0,0));
//	double nbmax = m_histo.getMaxQBar();
//
//	double lw = (m_histo.getMax() - m_histo.getMin()) / double(m_w);
//	painter.setBrush(QBrush(QColor(0,0,0)));
//	for (unsigned int i = 0; i< pop.size(); ++i)
//	{
//		double i0 = interv[i];
//		if (i0 < m_histo.getMin())
//			i0 = m_histo.getMin();
//		double i1 = interv[i+1];
//		if (i1 < m_histo.getMin())
//			i1 = m_histo.getMin();
//		if (i1 > m_histo.getMax())
//			i1 = m_histo.getMax();
//
//		double x0 =	(i0 - m_histo.getMin()) / lw;
//		double xw =	(i1 - i0) / lw;
//		int v = (pop[i]*m_h)/nbmax;
//		QRect rect(widthAxl+m_frameWidth+int(x0), m_h+m_frameWidth-v, int(xw), v);
//
//		if (i < m_qcolors.size())
//		{
//			Geom::Vec3f& col = m_qcolors[i];
//			painter.setBrush(QBrush(QColor(255*col[0],255*col[1],255*col[2])));
//		}
//		painter.drawRect(rect);
//	}
//}
//
//
//void RenderHistogram::paintEvent(QPaintEvent* /*ev*/)
//{
//	QPainter painter;
//	painter.begin(this);
//	draw(painter);
//	painter.end();
//}
//
//
//void RenderHistogram::svgExport(const std::string& filename)
//{
//	 QSvgGenerator generator;
//	 generator.setFileName(QString(filename.c_str()));
//
//
//	 // use screen drawing size
//
//	 generator.setSize(QSize(this->width(),this->height()));
//	 generator.setViewBox(QRect(0, 0, this->width(),this->height()));
//	 generator.setTitle(tr("Histogram"));
//	 generator.setDescription(tr("An SVG histogram created by CGoGN."));
//
//	QPainter painter;
//	painter.begin(&generator);
//
//	draw(painter);
// 	painter.end();
//}
//
//void RenderHistogram::mousePressEvent(QMouseEvent* event)
//{
//	int x = NONE;
//	int widthAxl = 8*m_axl_nbd;
//
//	const std::vector<unsigned int>& pop = m_histo.getPopulation();
//	if (m_drawHisto)
//	{
//		x = (event->x()- widthAxl - m_frameWidth-2)/m_l;
//		if ((x>=0) && (x<int(pop.size())))
//		{
//			int v = (pop[x]*m_h)/m_histo.getMaxBar();
//			int y = event->y();
//			if ((y<(m_h+m_frameWidth-v)) || (y>m_h+(2*m_frameWidth)))
//			{
//				x=-1;
//			}
//		}
//	}
//
//	const std::vector<double>& interv = m_histo.getQuantilesIntervals();
//	int xx = NONE;
//	if ( m_drawQuantiles && !interv.empty())
//	{
//		double lw = (m_histo.getMax() - m_histo.getMin()) / double(m_w);
//		double xd = m_histo.getMin() + double(event->x()- widthAxl - m_frameWidth -2) * lw;
//
//		xx = 0;
//		while (xx<int(interv.size()) && (xd > interv[xx]))
//			xx++;
//
//		if (xx < int(interv.size()))
//		{
//			xx--; // back from [1,n] to [0,n-1]
//
//			const std::vector<double>& popq = m_histo.getQuantilesHeights();
//			if ((xx>=0) && (xx<int(popq.size())))
//			{
//				double v = (popq[xx]*m_h)/m_histo.getMaxQBar();
//				int y = event->y();
//				if ((y<(m_h+m_frameWidth-v)) || (y>m_h+(2*m_frameWidth)))
//					xx=-1;
//			}
//		}
//		else
//		{
//			xx = -1;
//		}
//	}
//
//	if ((x>=0) || (xx>=0))
//	{
//		emit clicked(x,xx);
//	}
//}
//
//
//void RenderHistogram::setHistoPosition(bool hf)
//{
//	m_histoFront=hf;
//}
//
//void RenderHistogram::setHistoDraw(bool d)
//{
//	m_drawHisto=d;
//}
//
//void RenderHistogram::setQuantilesDraw(bool d)
//{
//	m_drawQuantiles=d;
//}
//
//void RenderHistogram::setOpacity(float op)
//{
//	m_opacity = op;
//}
//
//
//bool RenderHistogram::getHistoDraw()
//{
//	return m_drawHisto;
//}
//
//bool RenderHistogram::getQuantilesDraw()
//{
//	return m_drawQuantiles;
//}
//
//float RenderHistogram::getOpacity()
//{
//	return m_opacity;
//}
//
//
//
//void RenderHistogram::setQuantilesColors(const std::vector<Geom::Vec3f>& colors)
//{
//	m_qcolors.assign(colors.begin(), colors.end());
//}
//
//void RenderHistogram::update()
//{
//	paintEvent(NULL);
//}
//
//}
//}
//}