File: datasetControl.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 (273 lines) | stat: -rw-r--r-- 5,915 bytes parent folder | download | duplicates (4)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/VIEW/WIDGETS/datasetControl.h>
#include <BALL/VIEW/DATATYPE/dataset.h>
#include <BALL/VIEW/KERNEL/message.h>

namespace BALL
{
	namespace VIEW
	{

DatasetControl::DatasetControl(QWidget* parent, const char* name)
	:	GenericControl(parent, name)
{
#ifdef BALL_VIEW_DEBUG
	Log.error() << "new DatasetControl " << this << std::endl;
#endif
	listview->setColumnCount(3);
	listview->headerItem()->setText(0, qApp->translate("BALL::VIEW::DatasetControl", "Name"));
	listview->headerItem()->setText(1, qApp->translate("BALL::VIEW::DatasetControl", "from"));
	listview->headerItem()->setText(2, qApp->translate("BALL::VIEW::DatasetControl", "Type"));
	listview->setSortingEnabled(true);
	default_visible_ = false;
	resize(50,50);
	registerWidget(this);
}


DatasetControl::~DatasetControl()
{
	#ifdef BALL_VIEW_DEBUG
		Log.error() << "Destructing object " << this << " of class DatasetControl" << std::endl;
	#endif 

	listview->clear();

	for (Position p = 0; p < controllers_.size(); p++)
	{
		delete controllers_[p];
	}
}


void DatasetControl::initializeWidget(MainControl& main_control)
{
	GenericControl::initializeWidget(main_control);
	for (Position p = 0; p < controllers_.size(); p++)
	{
		controllers_[p]->createMenuEntries();
	}
}


void DatasetControl::checkMenu(MainControl& main_control)
{
	for (Position p = 0; p < controllers_.size(); p++)
	{
		controllers_[p]->checkMenu(main_control);
	}
}


void DatasetControl::onNotify(Message *message)
{
#ifdef BALL_VIEW_DEBUG
	Log.error() << "DatasetControl "<<this<<  " onNotify " << message << std::endl;
#endif

	GenericControl::onNotify(message);
    if (!RTTI::isKindOf<DatasetMessage>(message)) return;
	
	DatasetMessage* msg = (DatasetMessage*) message;
	if (!msg->isValid())
	{
		BALLVIEW_DEBUG
		return;
	}

	String type = msg->getDataset()->getType();
	for (Position p = 0; p < controllers_.size(); p++)
	{
		if (controllers_[p]->getType() == type)
		{
			controllers_[p]->handle(msg);

			return;
		}
	}

	BALLVIEW_DEBUG
	Log.error() << (String)qApp->translate("BALL::VIEW::DatasetControl", "Unknown dataset type:") << type << std::endl;
   
}


void DatasetControl::updateSelection()
{
	GenericControl::updateSelection();

	ItemList list = getSelectedItems();
	ItemList::iterator it = list.begin();
	for (; it != list.end(); it++)
	{
		QTreeWidgetItem* item = *it;
		DatasetController* cont = getController(item);
		if (cont == 0) continue;

		Dataset* set = cont->getDataset(item);
		DatasetMessage* msg = new DatasetMessage(set, DatasetMessage::SELECTED);
		notify_(msg);
	}
}


DatasetControl::DatasetControl(const DatasetControl& control)
	: GenericControl(control)
{
}


void DatasetControl::showGuestContextMenu(const QPoint& pos)
{
	getSelectedItems();
	if (context_item_ == 0) return;
	
	for (Position p = 0; p < controllers_.size(); p++)
	{
		// find the corresponding controller
		QMenu* menu = controllers_[p]->buildContextMenu(context_item_);
		if (menu != 0)
		{
			BALL_ASSIGN_NAME(menu)
			menu->exec(mapToGlobal(pos));
			delete menu;
			return;
		}
	}
}

bool DatasetControl::canHandle(const String& fileformat) const 
{ 
	for (Position p = 0; p < controllers_.size(); p++)
	{
		vector<String> file_types = controllers_[p]->getSupportedFileFormats();
		for (Position t = 0; t < file_types.size(); t++)
		{
			if (file_types[t] == fileformat)
			{
				return true;
			}
		}
	}

	return false;
}


bool DatasetControl::openFile(const String& filename)
{
	vector<String> fields;
	String suffix(filename);
	suffix.split(fields, ".");
	if (fields.size() > 1) 
	{
		suffix = fields[fields.size() - 1];
		
		for (Position p = 0; p < controllers_.size(); p++)
		{
			vector<String> file_types = controllers_[p]->getSupportedFileFormats();
			for (Position t = 0; t < file_types.size(); t++)
			{
				if (file_types[t] == suffix)
				{
					return controllers_[p]->open(suffix, filename) != 0;
				}
			}
		}
	}

	setStatusbarText(String(qApp->translate("BALL::VIEW::DatasetControl", "Can not open unknown file type: ")) + suffix, true);

	return false;
}


vector<Dataset*> DatasetControl::getDatasets(const String& type)
{
	DatasetController* con = getController(type);
	if (con != 0) return con->getDatasets();

	return vector<Dataset*>();
}

DatasetController* DatasetControl::getController(const String& type)
{
	for (Position p = 0; p < controllers_.size(); p++)
	{
		if (controllers_[p]->getType() == type)
		{
			return controllers_[p];
		}
	}

	return 0;
}

DatasetController* DatasetControl::getController(QTreeWidgetItem* item)
{
	for (Position p = 0; p < controllers_.size(); p++)
	{
		if (controllers_[p]->hasItem(item))
		{
			return controllers_[p];
		}
	}

	BALLVIEW_DEBUG
	return 0;
}


String DatasetControl::getType(QTreeWidgetItem* item)
{
	for (Position p = 0; p < controllers_.size(); p++)
	{
		if (controllers_[p]->hasItem(item))
		{
			return controllers_[p]->getType();
		}
	}

	BALLVIEW_DEBUG
	return "";
}

bool DatasetControl::registerController(DatasetController* con)
{
	String type = con->getType();
	if (type == "" || type == "none")
	{
		Log.error() <<  (String)qApp->translate("BALL::VIEW::DatasetControl", "Can not register Controller with type ") << type << std::endl;
		return false;
	}

	if (getController(type) != 0)
	{
		Log.error() << (String)qApp->translate("BALL::VIEW::DatasetControl", "Can not register Controller with type ") << type << ": "
								<< (String)qApp->translate("BALL::VIEW::DatasetControl", "Such a controller was already registered!") << std::endl;
		return false;
	}

	controllers_.push_back(con);
	con->setDatasetControl(this);
	return true;
}

Size DatasetControl::getSelectionSize()
{
	return getSelectedItems().size();
}

void DatasetControl::deleteCurrentItems()
{
	for (Position p = 0; p < controllers_.size(); p++)
	{
		controllers_[p]->deleteDatasets();
	}
}

	} // namespace VIEW
} // namespace BALL