MatrixDisplayWidget.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "MatrixDisplayWidget.h"
25
26#include <iostream>
27
28#include <pthread.h>
29
30#include <QPainter>
31
32#include <RobotAPI/gui-plugins/HapticUnitPlugin/ui_MatrixDisplayWidget.h>
33
34namespace armarx
35{
37 QWidget(parent), ui(new Ui::MatrixDisplayWidget)
38 {
39 ui->setupUi(this);
40 this->min = 0;
41 this->max = 1;
42 this->data = MatrixXf(1, 1);
43 this->data(0, 0) = 0;
44 QColor c[] = {QColor::fromHsv(0, 0, 0),
45 QColor::fromHsv(240, 255, 255),
46 QColor::fromHsv(270, 255, 255),
47 QColor::fromHsv(300, 255, 255),
48 QColor::fromHsv(0, 255, 255),
49 QColor::fromHsv(30, 255, 255),
50 QColor::fromHsv(60, 255, 255)};
51 this->colors = std::valarray<QColor>(c, sizeof c / sizeof c[0]);
52
53 //connect(this, SIGNAL(updateData(MatrixXf)), SLOT(setData(MatrixXf)), Qt::QueuedConnection);
54 connect(this, SIGNAL(doUpdate()), SLOT(update()), Qt::QueuedConnection);
55 }
56
61
62 void
64 {
65 mtx.lock();
66 MatrixXf data = this->data;
67
68 //cout << "[" << pthread_self() << "] MatrixDisplayWidget::paintEvent" << endl;
69
70 int pixelSize = std::min(width() / data.cols(), height() / data.rows());
71 int dx = (width() - pixelSize * data.cols()) / 2;
72 int dy = (height() - pixelSize * data.rows()) / 2;
73 QPainter painter(this);
74 painter.fillRect(rect(), QColor::fromRgb(0, 0, 0));
75 painter.setFont(QFont("Arial", 8));
76
77 for (int x = 0; x < data.cols(); x++)
78 {
79 for (int y = 0; y < data.rows(); y++)
80 {
81 QRect target = QRect(dx + x * pixelSize, dy + y * pixelSize, pixelSize, pixelSize);
82 painter.fillRect(target, getColor(data(y, x), min, max));
83 painter.drawText(target, Qt::AlignCenter, QString::number(data(y, x)));
84 }
85 }
86
87 painter.setFont(QFont("Arial", 12));
88 painter.drawText(rect(), Qt::AlignBottom | Qt::AlignRight, infoOverlay);
89
90 mtx.unlock();
91 }
92
93 QColor
94 MatrixDisplayWidget::getColor(float value, float min, float max)
95 {
96 value = (value - min) / (max - min) * (colors.size() - 1);
97
98 if (value < 0)
99 {
100 return colors[0];
101 }
102
103 if (value >= colors.size() - 1)
104 {
105 return colors[colors.size() - 1];
106 }
107
108 int i = (int)value;
109 float f2 = value - i;
110 float f1 = 1 - f2;
111 QColor c1 = colors[i];
112 QColor c2 = colors[i + 1];
113 return QColor((int)(c1.red() * f1 + c2.red() * f2),
114 (int)(c1.green() * f1 + c2.green() * f2),
115 (int)(c1.blue() * f1 + c2.blue() * f2));
116 }
117} // namespace armarx
constexpr T c
void paintEvent(QPaintEvent *) override
QColor getColor(float value, float min, float max)
ArmarX Headers.
This file offers overloads of toIce() and fromIce() functions for STL container types.