PropertyVisPlugin.cc 13 KB
Newer Older
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
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.              *
39 40 41
*                                                                            *
\*===========================================================================*/

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

43

44 45 46 47 48 49 50 51
//=============================================================================
//
//  CLASS PropertyVisPlugin - IMPLEMENTATION
//
//=============================================================================

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

52 53
#include "PropertyVisPlugin.hh"

Jan Möbius's avatar
Jan Möbius committed
54 55
#include "Models/PropertyModelFactory.hh"
#include "Models/SingleObjectPropertyModel.hh"
56

57
#ifdef ENABLE_POLYHEDRALMESH_SUPPORT
58
    #include <ObjectTypes/PolyhedralMesh/PolyhedralMesh.hh>
59
#endif
60
#ifdef ENABLE_HEXAHEDRALMESH_SUPPORT
61 62
    #include <ObjectTypes/HexahedralMesh/HexahedralMesh.hh>
#endif
63
#ifdef ENABLE_TETRAHEDRALMESH_SUPPORT
64
    #include <ObjectTypes/TetrahedralMesh/TetrahedralMesh.hh>
65 66
#endif

67 68 69 70 71 72
//== IMPLEMENTATION ==========================================================

#define PROP_VIS "PropertyVisualization"

PropertyVisPlugin::PropertyVisPlugin() :
tool_(0),
73
propertyModel_(0)
74 75 76 77 78
{
}

void PropertyVisPlugin::initializePlugin()
{
79 80
	if ( OpenFlipper::Options::gui() ) {
	  tool_ = new PropertyVisToolbar();
81

82 83
	  QSize size(300,300);
	  tool_->resize(size);
84

85
	  tool_->meshNames->setModel(&objectListItemModel_);
86

87
	  emit addHiddenPickMode( PROP_VIS );
88

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

91 92
	  emit addToolbox( tr("Property Visualization") , tool_, toolIcon );
	}
93 94 95 96
}

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

97
void PropertyVisPlugin::pluginsInitialized()
98
{
99
	if ( OpenFlipper::Options::gui() ) {
Max Lyon's avatar
Max Lyon committed
100

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

104 105
		connect(tool_->visualizeButton, SIGNAL( clicked() ), this, SLOT( slotVisualize() ) );
		connect(tool_->clearButton,     SIGNAL( clicked() ), this, SLOT( slotAllCleared() ) );
Max Lyon's avatar
Max Lyon committed
106

107 108 109
		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
110

111
		connect(tool_, SIGNAL( widgetShown() ), this, SLOT( updateGUI() ) );
112

113 114
		setNewPropertyModel(-1);
	}
115 116 117 118
}

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

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
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 );
		}
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    }
}

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);
163 164 165 166
}

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

167
void PropertyVisPlugin::slotPickModeChanged( const std::string& _mode)
168
{
169 170
	if (propertyModel_ != 0)
		propertyModel_->pickModeChanged(_mode);
171 172 173 174 175 176
}

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

void PropertyVisPlugin::slotAllCleared()
{
177 178
	using namespace PluginFunctions;

179 180 181 182 183 184 185
	if (propertyModel_ != 0)
	{
		QModelIndexList selectedIndices = tool_->propertyName_lv->selectionModel()->selectedIndexes();
		propertyModel_->clear(selectedIndices);
		propertyModel_->objectUpdated();
		emit updateView();
	}
186 187 188
}

//-----------------------------------------------------------------------------
189

190 191
void PropertyVisPlugin::objectDeleted(int _id)
{
192 193 194
	if( OpenFlipper::Options::gui() )
		objectListItemModel_.removeObject(_id);
	PropertyModelFactory::Instance().deleteModel(_id);
195 196 197 198 199 200
}

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

void PropertyVisPlugin::slotObjectUpdated( int _identifier, const UpdateType& _type )
{
201 202 203 204 205 206 207 208 209 210 211 212 213
	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();
		}
	}
214 215
}

216 217 218 219 220 221 222 223 224
void PropertyVisPlugin::slotObjectPropertiesChanged(int _identifier)
{
	// We get this signal when properties are renamed
	if( OpenFlipper::Options::gui() && tool_->isVisible() )
	{
		updateGUI();
	}
}

225 226
void PropertyVisPlugin::updateGUI()
{
227
	DataType datatype = DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH);
228
#ifdef ENABLE_POLYHEDRALMESH_SUPPORT
229
	datatype |= DataType(DATA_POLYHEDRAL_MESH);
230
#endif
231
#ifdef ENABLE_HEXAHEDRALMESH_SUPPORT
232 233
        datatype |= DataType(DATA_HEXAHEDRAL_MESH);
#endif
234
#ifdef ENABLE_TETRAHEDRALMESH_SUPPORT
235
        datatype |= DataType(DATA_TETRAHEDRAL_MESH);
236
#endif
237
        objectListItemModel_.refresh(datatype);
238 239 240 241
}

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

242 243
void PropertyVisPlugin::propertySelectionChanged()
{
244 245 246 247 248
	if (propertyModel_ != 0)
	{
		QModelIndexList selectedIndices = tool_->propertyName_lv->selectionModel()->selectedIndexes();
		propertyModel_->updateWidget(selectedIndices);
	}
249 250 251 252
}

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

253

254 255 256 257 258 259 260 261 262 263
/*! \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)
{
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
	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);
	}
291 292 293 294
}

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

295
void PropertyVisPlugin::slotMeshChanged(int /*_index*/)
296
{
297
	int id = tool_->meshNames->itemData( tool_->meshNames->currentIndex() ).toInt();
298
	setNewPropertyModel(id);
299 300 301 302
}

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

303
void PropertyVisPlugin::slotVisualize()
304
{
305
	using namespace PluginFunctions;
306

307 308 309 310 311
	// 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();
312

313 314
	// visualize property
	propertyModel_->visualize(selectedIndices);
315

316
	// emit updates
317 318
	emit updateView();

319 320 321 322 323 324
	if (selectedId >= 0)
	{
		emit updatedObject( selectedId, UPDATE_COLOR );
	}
	else
	{
325
		ObjectIterator o_it(ALL_OBJECTS, supportedDataTypes());
326 327 328 329 330 331
		while (o_it != objectsEnd())
		{
			emit updatedObject( o_it->id(), UPDATE_COLOR );
			++o_it;
		}
	}
332 333 334 335
}

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

336
void PropertyVisPlugin::slotMouseEvent( QMouseEvent* _event ) {
337 338
	if (propertyModel_ != 0)
		propertyModel_->mouseEvent(_event);
339 340 341 342
}

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

343 344 345
void PropertyVisPlugin::slotDuplicateProperty()
{
	using namespace PluginFunctions;
346

347 348 349 350 351 352 353 354
	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();
355 356 357 358 359 360 361

		if (id >= 0)
		{
			emit updatedObject( id, UPDATE_ALL );
		}
		else
		{
362
			ObjectIterator o_it(ALL_OBJECTS, supportedDataTypes());
363 364 365 366 367 368
			while (o_it != objectsEnd())
			{
				emit updatedObject( o_it->id(), UPDATE_ALL );
				++o_it;
			}
		}
369
	}
370 371
}

372
void PropertyVisPlugin::slotRemoveProperty()
373
{
374
	using namespace PluginFunctions;
375 376 377 378 379 380 381 382 383

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

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

384 385 386 387 388 389
		if (id >= 0)
		{
			emit updatedObject( id, UPDATE_ALL );
		}
		else
		{
390
			ObjectIterator o_it(ALL_OBJECTS, supportedDataTypes());
391 392 393 394 395 396
			while (o_it != objectsEnd())
			{
				emit updatedObject( o_it->id(), UPDATE_ALL );
				++o_it;
			}
		}
397
	}
398 399 400
}