File: colorTable.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (136 lines) | stat: -rw-r--r-- 2,985 bytes parent folder | download | duplicates (8)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/VIEW/WIDGETS/colorTable.h>

namespace BALL
{
	namespace VIEW
	{
		ColorTable::ColorTable(QWidget* parent, const char* name)
			: QTableWidget(0, 2, parent),
				setting_content_(false)
		{
			setObjectName(name);
			setColumnCount(2);
			setHorizontalHeaderItem(0, new QTableWidgetItem());
			setHorizontalHeaderItem(1, new QTableWidgetItem());
			horizontalHeaderItem(1)->setText("Color");
			setGeometry(5,5, 400, 388);
			setColumnWidth(1, 230);
			setSelectionMode(NoSelection);
		}

		void ColorTable::setNamesTitle(const String& name)
		{
			horizontalHeaderItem(0)->setText(name.c_str());
		}

		String ColorTable::getNamesTitle() const
		{
			return ascii(horizontalHeaderItem(0)->text());
		}

		void ColorTable::setContent(const vector<String>& names, const vector<ColorRGBA>& colors)
		{
			setting_content_ = true;
			colors_ = colors;
			names_ = names;

			setRowCount(colors_.size());
			for (Position p = 0; p < names_.size(); p++)
			{
				QTableWidgetItem* item = new QTableWidgetItem(names_[p].c_str());
				setItem(p,0,item);
				item->setFlags(Qt::ItemIsEnabled);
				item = new QTableWidgetItem();
				item->setBackgroundColor(colors_[p].getQColor());
				setItem(p,1,item);
			}
			setting_content_ = false;
		}

		void ColorTable::setColors(const vector<ColorRGBA>& colors)
		{
			setting_content_ = true;
			colors_ = colors;

			setRowCount(colors_.size());
			for (Position p = 0; p < names_.size(); p++)
			{
				QTableWidgetItem* item = new QTableWidgetItem();
				item->setBackgroundColor(colors_[p].getQColor());
				setItem(p, 1, item);
			}
			setting_content_ = false;
		}

		void ColorTable::beginEdit(int row, int col)
		{
			if (col == 0 || setting_content_) return;
			ColorRGBA old_rgba(item(row,col)->backgroundColor());
			QColor qcolor = QColorDialog::getColor(old_rgba.getQColor());
			if (!qcolor.isValid()) return;

			ColorRGBA new_color(qcolor);
			item(row,col)->setBackgroundColor(new_color.getQColor());
			colors_[row] = new_color;
		}

		bool ColorTable::getValue(String& value) const
		{
			value.clear();

			vector<ColorRGBA> colors = getColors();

			for (Position p = 0; p < colors.size(); p ++)
			{
				value += colors[p];
				value += ";";
			}

			return true;
		}

		bool ColorTable::setValue(const String& value)
		{
			vector<String> fields;
			vector<ColorRGBA> colors;

			Size nr = value.split(fields, ";");

			ColorRGBA color;

			for (Position p = 0; p < nr; p ++)
			{
				color = fields[p];
				colors.push_back(color);
			}

			if (colors.size() != getNames().size())
			{
				BALLVIEW_DEBUG;
				return false;
			}

			setColors(colors);

			return true;
		}

		void ColorTable::mousePressEvent(QMouseEvent* event)
		{
			Index c = columnAt(event->pos().x());
			Index r = rowAt(event->pos().y());
			if (c == 0) return;
			if (c != 1 || r < 0)
			{
				QTableWidget::mousePressEvent(event);
				return;
			}

			beginEdit(r, c);
		}
	}
}