PropertyVisPlugin.cc 13.5 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
*                                                                            *
\*===========================================================================*/

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

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

Jan Möbius's avatar
 
Jan Möbius committed
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"
Jan Möbius's avatar
 
Jan Möbius committed
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

Jan Möbius's avatar
 
Jan Möbius committed
67 68 69 70 71 72
//== IMPLEMENTATION ==========================================================

#define PROP_VIS "PropertyVisualization"

PropertyVisPlugin::PropertyVisPlugin() :
tool_(0),
73
propertyModel_(0)
Jan Möbius's avatar
 
Jan Möbius committed
74 75 76 77 78
{
}

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

82 83
	  QSize size(300,300);
	  tool_->resize(size);
Jan Möbius's avatar
 
Jan Möbius committed
84

85
	  tool_->meshNames->setModel(&objectListItemModel_);
Jan Möbius's avatar
 
Jan Möbius committed
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 );
	}
Jan Möbius's avatar
 
Jan Möbius committed
93 94 95 96
}

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

97
void PropertyVisPlugin::pluginsInitialized()
Jan Möbius's avatar
 
Jan Möbius committed
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);
	}
Jan Möbius's avatar
Jan Möbius committed
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 163
void PropertyVisPlugin::slotClear(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->clear(list);

            emit updateView();
            emit updatedObject( _id, UPDATE_COLOR );
        }
    }
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
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);
185 186 187 188
}

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

189
void PropertyVisPlugin::slotPickModeChanged( const std::string& _mode)
Jan Möbius's avatar
Jan Möbius committed
190
{
191 192
	if (propertyModel_ != 0)
		propertyModel_->pickModeChanged(_mode);
Jan Möbius's avatar
 
Jan Möbius committed
193 194 195 196 197 198
}

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

void PropertyVisPlugin::slotAllCleared()
{
199 200
	using namespace PluginFunctions;

201 202 203 204 205 206 207
	if (propertyModel_ != 0)
	{
		QModelIndexList selectedIndices = tool_->propertyName_lv->selectionModel()->selectedIndexes();
		propertyModel_->clear(selectedIndices);
		propertyModel_->objectUpdated();
		emit updateView();
	}
208 209 210
}

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

212 213
void PropertyVisPlugin::objectDeleted(int _id)
{
214 215 216
	if( OpenFlipper::Options::gui() )
		objectListItemModel_.removeObject(_id);
	PropertyModelFactory::Instance().deleteModel(_id);
Jan Möbius's avatar
 
Jan Möbius committed
217 218 219 220 221 222
}

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

void PropertyVisPlugin::slotObjectUpdated( int _identifier, const UpdateType& _type )
{
223 224 225 226 227 228 229 230 231 232 233 234 235
	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
236 237
}

238 239 240 241 242 243 244 245 246
void PropertyVisPlugin::slotObjectPropertiesChanged(int _identifier)
{
	// We get this signal when properties are renamed
	if( OpenFlipper::Options::gui() && tool_->isVisible() )
	{
		updateGUI();
	}
}

247 248
void PropertyVisPlugin::updateGUI()
{
249
	DataType datatype = DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH);
250
#ifdef ENABLE_POLYHEDRALMESH_SUPPORT
251
	datatype |= DataType(DATA_POLYHEDRAL_MESH);
252
#endif
253
#ifdef ENABLE_HEXAHEDRALMESH_SUPPORT
254 255
        datatype |= DataType(DATA_HEXAHEDRAL_MESH);
#endif
256
#ifdef ENABLE_TETRAHEDRALMESH_SUPPORT
257
        datatype |= DataType(DATA_TETRAHEDRAL_MESH);
258
#endif
259
        objectListItemModel_.refresh(datatype);
Jan Möbius's avatar
 
Jan Möbius committed
260 261 262 263
}

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

264 265
void PropertyVisPlugin::propertySelectionChanged()
{
266 267 268 269 270
	if (propertyModel_ != 0)
	{
		QModelIndexList selectedIndices = tool_->propertyName_lv->selectionModel()->selectedIndexes();
		propertyModel_->updateWidget(selectedIndices);
	}
Jan Möbius's avatar
 
Jan Möbius committed
271 272 273 274
}

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

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

276 277 278 279 280 281 282 283 284 285
/*! \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)
{
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	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
313 314 315 316
}

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

317
void PropertyVisPlugin::slotMeshChanged(int /*_index*/)
Jan Möbius's avatar
Jan Möbius committed
318
{
319
	int id = tool_->meshNames->itemData( tool_->meshNames->currentIndex() ).toInt();
320
	setNewPropertyModel(id);
Jan Möbius's avatar
Jan Möbius committed
321 322 323 324
}

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

325
void PropertyVisPlugin::slotVisualize()
Jan Möbius's avatar
Jan Möbius committed
326
{
327
	using namespace PluginFunctions;
328

329 330 331 332 333
	// 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();
334

335 336
	// visualize property
	propertyModel_->visualize(selectedIndices);
337

338
	// emit updates
339 340
	emit updateView();

341 342 343 344 345 346
	if (selectedId >= 0)
	{
		emit updatedObject( selectedId, UPDATE_COLOR );
	}
	else
	{
347
		ObjectIterator o_it(ALL_OBJECTS, supportedDataTypes());
348 349 350 351 352 353
		while (o_it != objectsEnd())
		{
			emit updatedObject( o_it->id(), UPDATE_COLOR );
			++o_it;
		}
	}
Jan Möbius's avatar
Jan Möbius committed
354 355 356 357
}

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

358
void PropertyVisPlugin::slotMouseEvent( QMouseEvent* _event ) {
359 360
	if (propertyModel_ != 0)
		propertyModel_->mouseEvent(_event);
Jan Möbius's avatar
Jan Möbius committed
361 362 363 364
}

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

365 366 367
void PropertyVisPlugin::slotDuplicateProperty()
{
	using namespace PluginFunctions;
Jan Möbius's avatar
Jan Möbius committed
368

369 370 371 372 373 374 375 376
	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();
377 378 379 380 381 382 383

		if (id >= 0)
		{
			emit updatedObject( id, UPDATE_ALL );
		}
		else
		{
384
			ObjectIterator o_it(ALL_OBJECTS, supportedDataTypes());
385 386 387 388 389 390
			while (o_it != objectsEnd())
			{
				emit updatedObject( o_it->id(), UPDATE_ALL );
				++o_it;
			}
		}
391
	}
Jan Möbius's avatar
Jan Möbius committed
392 393
}

394
void PropertyVisPlugin::slotRemoveProperty()
Jan Möbius's avatar
Jan Möbius committed
395
{
396
	using namespace PluginFunctions;
397 398 399 400 401 402 403 404 405

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

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

406 407 408 409 410 411
		if (id >= 0)
		{
			emit updatedObject( id, UPDATE_ALL );
		}
		else
		{
412
			ObjectIterator o_it(ALL_OBJECTS, supportedDataTypes());
413 414 415 416 417 418
			while (o_it != objectsEnd())
			{
				emit updatedObject( o_it->id(), UPDATE_ALL );
				++o_it;
			}
		}
419
	}
Jan Möbius's avatar
 
Jan Möbius committed
420 421 422
}