PropertyVisPlugin.cc 13.6 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
40
41
42
43
44
45
46
47
48
49
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/

Jan Möbius's avatar
 
Jan Möbius committed
50
51
52
53
54
55
56
57
//=============================================================================
//
//  CLASS PropertyVisPlugin - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================

58
59
#include "PropertyVisPlugin.hh"

60
#include "PropertyModelFactory.hh"
61
#include "SingleObjectPropertyModel.hh"
Jan Möbius's avatar
 
Jan Möbius committed
62

63
#ifdef ENABLE_POLYHEDRALMESH_SUPPORT
64
    #include <ObjectTypes/PolyhedralMesh/PolyhedralMesh.hh>
65
#endif
66
#ifdef ENABLE_HEXAHEDRALMESH_SUPPORT
67
68
    #include <ObjectTypes/HexahedralMesh/HexahedralMesh.hh>
#endif
69
#ifdef ENABLE_TETRAHEDRALMESH_SUPPORT
70
    #include <ObjectTypes/TetrahedralMesh/TetrahedralMesh.hh>
71
72
#endif

Jan Möbius's avatar
 
Jan Möbius committed
73
74
75
76
77
78
//== IMPLEMENTATION ==========================================================

#define PROP_VIS "PropertyVisualization"

PropertyVisPlugin::PropertyVisPlugin() :
tool_(0),
79
propertyModel_(0)
Jan Möbius's avatar
 
Jan Möbius committed
80
81
82
83
84
{
}

void PropertyVisPlugin::initializePlugin()
{
85
86
	if ( OpenFlipper::Options::gui() ) {
	  tool_ = new PropertyVisToolbar();
Jan Möbius's avatar
 
Jan Möbius committed
87

88
89
	  QSize size(300,300);
	  tool_->resize(size);
Jan Möbius's avatar
 
Jan Möbius committed
90

91
	  tool_->meshNames->setModel(&objectListItemModel_);
Jan Möbius's avatar
 
Jan Möbius committed
92

93
	  emit addHiddenPickMode( PROP_VIS );
94

95
	  QIcon* toolIcon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"PropertyVisIcon.png");
96

97
98
	  emit addToolbox( tr("Property Visualization") , tool_, toolIcon );
	}
Jan Möbius's avatar
 
Jan Möbius committed
99
100
101
102
}

//-----------------------------------------------------------------------------

103
void PropertyVisPlugin::pluginsInitialized()
Jan Möbius's avatar
 
Jan Möbius committed
104
{
105
	if ( OpenFlipper::Options::gui() ) {
Max Lyon's avatar
Max Lyon committed
106

107
108
		// connect toolbox elements
		connect(tool_->meshNames,       SIGNAL( currentIndexChanged(int) ), this, SLOT( slotMeshChanged(int) ) );
Max Lyon's avatar
Max Lyon committed
109

110
111
		connect(tool_->visualizeButton, SIGNAL( clicked() ), this, SLOT( slotVisualize() ) );
		connect(tool_->clearButton,     SIGNAL( clicked() ), this, SLOT( slotAllCleared() ) );
Max Lyon's avatar
Max Lyon committed
112

113
114
115
		connect(tool_->refresh_property_names_tb, SIGNAL( clicked() ), this, SLOT( slotMeshChanged() ) );
		connect(tool_->duplicate_property_tb, SIGNAL( clicked() ), this, SLOT( slotDuplicateProperty() ) );
		connect(tool_->remove_property_tb, SIGNAL( clicked() ), this, SLOT( slotRemoveProperty() ) );
Max Lyon's avatar
Max Lyon committed
116

117
		connect(tool_, SIGNAL( widgetShown() ), this, SLOT( updateGUI() ) );
118

119
120
		setNewPropertyModel(-1);
	}
Jan Möbius's avatar
Jan Möbius committed
121
122
123
124
}

//-----------------------------------------------------------------------------

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
void PropertyVisPlugin::slotVisualizeProperty( int _id, const QString& _propname )
{
	PropertyModel* model = PropertyModelFactory::Instance().getModel(_id);

	if (model != 0)
	{
		model->gatherProperties();
		QModelIndex idx = model->indexFromPlainPropName(_propname);

		if (idx.isValid())
		{
			QModelIndexList list;
			list.append(idx);

			model->visualize(list);

			
			emit updateView();
			emit updatedObject( _id, UPDATE_COLOR );
		}
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    }
}

QScriptValue PropertyVisPlugin::getPropertyVisualizer(int _id, const QString &_propname)
{
    PropertyModel* model = PropertyModelFactory::Instance().getModel(_id);

    if (model == nullptr) { return QScriptValue::SpecialValue::NullValue; }

    model->gatherProperties();
    QModelIndex idx = model->indexFromPlainPropName(_propname);
    if (!idx.isValid()) { return QScriptValue::SpecialValue::NullValue; }

    QScriptEngine *engine;
    emit getScriptingEngine (engine);
    if (engine == nullptr) { return QScriptValue::SpecialValue::NullValue; }

    QScriptContext *ctx = engine->currentContext();
    if (ctx == nullptr) { return QScriptValue::SpecialValue::NullValue; }

    auto sopm = dynamic_cast<SingleObjectPropertyModel*>(model);
    if (!sopm) { return QScriptValue::SpecialValue::NullValue; }

    return sopm->getScriptObject(idx, ctx);
169
170
171
172
}

//-----------------------------------------------------------------------------

173
void PropertyVisPlugin::slotPickModeChanged( const std::string& _mode)
Jan Möbius's avatar
Jan Möbius committed
174
{
175
176
	if (propertyModel_ != 0)
		propertyModel_->pickModeChanged(_mode);
Jan Möbius's avatar
 
Jan Möbius committed
177
178
179
180
181
182
}

//-----------------------------------------------------------------------------

void PropertyVisPlugin::slotAllCleared()
{
183
184
	using namespace PluginFunctions;

185
186
187
188
189
190
191
	if (propertyModel_ != 0)
	{
		QModelIndexList selectedIndices = tool_->propertyName_lv->selectionModel()->selectedIndexes();
		propertyModel_->clear(selectedIndices);
		propertyModel_->objectUpdated();
		emit updateView();
	}
192
193
194
}

//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
195

196
197
void PropertyVisPlugin::objectDeleted(int _id)
{
198
199
200
	if( OpenFlipper::Options::gui() )
		objectListItemModel_.removeObject(_id);
	PropertyModelFactory::Instance().deleteModel(_id);
Jan Möbius's avatar
 
Jan Möbius committed
201
202
203
204
205
206
}

//-----------------------------------------------------------------------------

void PropertyVisPlugin::slotObjectUpdated( int _identifier, const UpdateType& _type )
{
207
208
209
210
211
212
213
214
215
216
217
218
219
	if( OpenFlipper::Options::gui() )
	{
		if ( tool_->isVisible() )
			updateGUI();
		PropertyModel* propertyModel = PropertyModelFactory::Instance().getModel(_identifier);
		if (propertyModel)
		{
			if (_type == UPDATE_ALL)
				propertyModel->gatherProperties();
			if (_type == (UPDATE_ALL | UPDATE_GEOMETRY))
				propertyModel->objectUpdated();
		}
	}
Jan Möbius's avatar
 
Jan Möbius committed
220
221
}

222
223
224
225
226
227
228
229
230
void PropertyVisPlugin::slotObjectPropertiesChanged(int _identifier)
{
	// We get this signal when properties are renamed
	if( OpenFlipper::Options::gui() && tool_->isVisible() )
	{
		updateGUI();
	}
}

231
232
void PropertyVisPlugin::updateGUI()
{
233
	DataType datatype = DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH);
234
#ifdef ENABLE_POLYHEDRALMESH_SUPPORT
235
	datatype |= DataType(DATA_POLYHEDRAL_MESH);
236
#endif
237
#ifdef ENABLE_HEXAHEDRALMESH_SUPPORT
238
239
        datatype |= DataType(DATA_HEXAHEDRAL_MESH);
#endif
240
#ifdef ENABLE_TETRAHEDRALMESH_SUPPORT
241
        datatype |= DataType(DATA_TETRAHEDRAL_MESH);
242
#endif
243
        objectListItemModel_.refresh(datatype);
Jan Möbius's avatar
 
Jan Möbius committed
244
245
246
247
}

//-----------------------------------------------------------------------------

248
249
void PropertyVisPlugin::propertySelectionChanged()
{
250
251
252
253
254
	if (propertyModel_ != 0)
	{
		QModelIndexList selectedIndices = tool_->propertyName_lv->selectionModel()->selectedIndexes();
		propertyModel_->updateWidget(selectedIndices);
	}
Jan Möbius's avatar
 
Jan Möbius committed
255
256
257
258
}

//-----------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
259

260
261
262
263
264
265
266
267
268
269
/*! \brief Exchanges the PropertyModel after the user selected a different object.
 *
 * This method exchanges the PropertyModel after the user selects a different object
 * for visualization. The widget of the old model needs to be hidden and the one of
 * the new model is shown. Also some signals need to be disconnected and connected.
 *
 * \param id The id of the selected object
 */
void PropertyVisPlugin::setNewPropertyModel(int id)
{
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
	if (propertyModel_)
	{
		propertyModel_->hideWidget();
		disconnect(propertyModel_, SIGNAL(log(Logtype,QString)), this, SLOT(slotLog(Logtype,QString)));
		disconnect(propertyModel_, SIGNAL(log(QString)), this, SLOT(slotLog(QString)));
	}
	propertyModel_ = PropertyModelFactory::Instance().getModel(id);
	if (propertyModel_ != 0)
	{

		tool_->propertyName_lv->setModel(propertyModel_);
		connect(propertyModel_, SIGNAL( modelReset() ), this, SLOT( propertySelectionChanged() ));
		connect(tool_->propertyName_lv->selectionModel(),
				SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
				this,
				SLOT( propertySelectionChanged() ));
		QWidget* widget = propertyModel_->getWidget();
		tool_->propertyWidgets->addWidget(widget);
		widget->show();
		propertyModel_->gatherProperties();
		connect(propertyModel_, SIGNAL(log(Logtype,QString)), this, SLOT(slotLog(Logtype,QString)));
		connect(propertyModel_, SIGNAL(log(QString)), this, SLOT(slotLog(QString)));
	}
	else
	{
		tool_->propertyName_lv->setModel(0);
	}
Jan Möbius's avatar
Jan Möbius committed
297
298
299
300
}

//-----------------------------------------------------------------------------

301
void PropertyVisPlugin::slotMeshChanged(int /*_index*/)
Jan Möbius's avatar
Jan Möbius committed
302
{
303
	int id = tool_->meshNames->itemData( tool_->meshNames->currentIndex() ).toInt();
304
	setNewPropertyModel(id);
Jan Möbius's avatar
Jan Möbius committed
305
306
307
308
}

//-----------------------------------------------------------------------------

309
void PropertyVisPlugin::slotVisualize()
Jan Möbius's avatar
Jan Möbius committed
310
{
311
	using namespace PluginFunctions;
312

313
314
315
316
317
	// return if nothing is selected
	if (propertyModel_ == 0) return;

	int selectedId = tool_->meshNames->itemData( tool_->meshNames->currentIndex() ).toInt();
	QModelIndexList selectedIndices = tool_->propertyName_lv->selectionModel()->selectedIndexes();
318

319
320
	// visualize property
	propertyModel_->visualize(selectedIndices);
321

322
	// emit updates
323
324
	emit updateView();

325
326
327
328
329
330
	if (selectedId >= 0)
	{
		emit updatedObject( selectedId, UPDATE_COLOR );
	}
	else
	{
331
		ObjectIterator o_it(ALL_OBJECTS, supportedDataTypes());
332
333
334
335
336
337
		while (o_it != objectsEnd())
		{
			emit updatedObject( o_it->id(), UPDATE_COLOR );
			++o_it;
		}
	}
Jan Möbius's avatar
Jan Möbius committed
338
339
340
341
}

//-----------------------------------------------------------------------------

342
void PropertyVisPlugin::slotMouseEvent( QMouseEvent* _event ) {
343
344
	if (propertyModel_ != 0)
		propertyModel_->mouseEvent(_event);
Jan Möbius's avatar
Jan Möbius committed
345
346
347
348
}

//-----------------------------------------------------------------------------

349
350
351
void PropertyVisPlugin::slotDuplicateProperty()
{
	using namespace PluginFunctions;
Jan Möbius's avatar
Jan Möbius committed
352

353
354
355
356
357
358
359
360
	if (propertyModel_ != 0)
	{
		QModelIndexList selectedIndices = tool_->propertyName_lv->selectionModel()->selectedIndexes();
		propertyModel_->duplicateProperty(selectedIndices);

		emit updateView();
		int id = tool_->meshNames->itemData( tool_->meshNames->currentIndex() ).toInt();
		slotMeshChanged();
361
362
363
364
365
366
367

		if (id >= 0)
		{
			emit updatedObject( id, UPDATE_ALL );
		}
		else
		{
368
			ObjectIterator o_it(ALL_OBJECTS, supportedDataTypes());
369
370
371
372
373
374
			while (o_it != objectsEnd())
			{
				emit updatedObject( o_it->id(), UPDATE_ALL );
				++o_it;
			}
		}
375
	}
Jan Möbius's avatar
Jan Möbius committed
376
377
}

378
void PropertyVisPlugin::slotRemoveProperty()
Jan Möbius's avatar
Jan Möbius committed
379
{
380
	using namespace PluginFunctions;
381
382
383
384
385
386
387
388
389

	if (propertyModel_ != 0)
	{
		QModelIndexList selectedIndices = tool_->propertyName_lv->selectionModel()->selectedIndexes();
		propertyModel_->removeProperty(selectedIndices);

		emit updateView();
		int id = tool_->meshNames->itemData( tool_->meshNames->currentIndex() ).toInt();

390
391
392
393
394
395
		if (id >= 0)
		{
			emit updatedObject( id, UPDATE_ALL );
		}
		else
		{
396
			ObjectIterator o_it(ALL_OBJECTS, supportedDataTypes());
397
398
399
400
401
402
			while (o_it != objectsEnd())
			{
				emit updatedObject( o_it->id(), UPDATE_ALL );
				++o_it;
			}
		}
403
	}
Jan Möbius's avatar
 
Jan Möbius committed
404
405
}

Matthias Möller's avatar
Matthias Möller committed
406
407
408
#if QT_VERSION < 0x050000
  Q_EXPORT_PLUGIN2( propertyvisplugin , PropertyVisPlugin );
#endif
Jan Möbius's avatar
 
Jan Möbius committed
409