SingleObjectPropertyModel.cc 13.3 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
*                                                                            *
\*===========================================================================*/

42 43
/*===========================================================================*\
*                                                                            *
44 45 46
*   $Revision: 21016 $                                                       *
*   $LastChangedBy: schultz $                                                *
*   $Date: 2015-07-16 16:48:42 +0200 (Thu, 16 Jul 2015) $                     *
47 48 49
*                                                                            *
\*===========================================================================*/

50
#include "SingleObjectPropertyModel.hh"
51

52 53
SingleObjectPropertyModel::SingleObjectPropertyModel(QObject *parent)
    : PropertyModel(parent),
54
      widgets(0),
55 56
      currentlySelectedIndices(),
      currentlyVisualizedIndices()
57 58 59 60 61 62 63
{
    gatherProperties();
    QVBoxLayout* layout = new QVBoxLayout();
    widgets = new QWidget();
    widgets->setLayout(layout);
}

64
SingleObjectPropertyModel::~SingleObjectPropertyModel()
65 66 67
{
    for (unsigned int i = 0; i < propertyVisualizers.size(); i++)
        delete propertyVisualizers[i];
68 69 70 71 72 73
}

/**
 * @brief Visualizes the selected properties.
 * @param selectedIndices The
 */
74
void SingleObjectPropertyModel::visualize(QModelIndexList selectedIndices, QWidgetList widgets)
75
{
76 77 78 79 80 81 82 83 84 85 86 87 88
	for (int i = 0; i < selectedIndices.size(); ++i)
	{
		const int row = selectedIndices[i].row();

		if (widgets.empty())
		{
			propertyVisualizers[row]->visualize(true, 0);
		}
		else
		{
			propertyVisualizers[row]->visualize(true, widgets[i]);
		}

89 90
        //delete index and reinsert so it is the last element.
        std::vector<unsigned int>& vec = currentlyVisualizedIndices;
91 92 93
        vec.erase(std::remove(vec.begin(), vec.end(), row), vec.end());
        vec.push_back(row);
	}
94 95
}

96
void SingleObjectPropertyModel::removeProperty(QModelIndexList selectedIndices)
97 98
{
    std::vector<unsigned int> deleteIndices;
99

100 101 102 103 104 105
    for (QModelIndexList::Iterator it = selectedIndices.begin(); it != selectedIndices.end(); ++it)
    {
        propertyVisualizers[it->row()]->removeProperty();
        QWidget* w = propertyVisualizers[it->row()]->getWidget();
        widgets->layout()->removeWidget(w);
        delete propertyVisualizers[it->row()];
106
        propertyVisualizers[it->row()] = 0;
107 108
        deleteIndices.push_back(it->row());
    }
109 110 111

    std::sort(deleteIndices.begin(), deleteIndices.end());

Jan Möbius's avatar
Jan Möbius committed
112
    for (int i = deleteIndices.size()-1; i >= 0; i--)
113
    {
Jan Möbius's avatar
Jan Möbius committed
114
        for (int j = currentlyVisualizedIndices.size()-1; j >= 0; j--)
115 116 117 118 119 120 121 122 123 124 125
        {
            if (currentlyVisualizedIndices[j] == deleteIndices[i])
                //erase so the deleted property will not be revisualized on updateObject
                currentlyVisualizedIndices.erase(currentlyVisualizedIndices.begin() + j);
            else if (currentlyVisualizedIndices[j] > deleteIndices[i])
                //decrease index by one since the index of all property visualizers in propertyVisualizers
                //shifts by one for each property visualizer that gets deleted in front of them
                currentlyVisualizedIndices[j]--;
        }
    }

Jan Möbius's avatar
Jan Möbius committed
126
    for (int i = deleteIndices.size()-1; i >= 0; i--){
127
        propertyVisualizers.erase(propertyVisualizers.begin() + deleteIndices[i]);
128 129 130
    }
}

131
void SingleObjectPropertyModel::duplicateProperty(QModelIndexList selectedIndices)
132 133 134 135 136
{
    for (QModelIndexList::Iterator it = selectedIndices.begin(); it != selectedIndices.end(); ++it)
        propertyVisualizers[it->row()]->duplicateProperty();
}

137
void SingleObjectPropertyModel::clear(QModelIndexList selectedIndices) {
138 139 140
    for (QModelIndexList::Iterator it = selectedIndices.begin(); it != selectedIndices.end(); ++it)
    {
        propertyVisualizers[it->row()]->clear();
141 142
        std::vector<unsigned int>& vec = currentlyVisualizedIndices;
        vec.erase(std::remove(vec.begin(), vec.end(), it->row()), vec.end());
143 144 145
    }
}

146
void SingleObjectPropertyModel::updateWidget(const QModelIndexList& selectedIndices)
147 148 149
{
    QLayout* layout = widgets->layout();

150
    currentlySelectedIndices = selectedIndices;
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    for (unsigned int i = 0; i < propertyVisualizers.size(); i++)
    {
        propertyVisualizers[i]->getWidget()->hide();
    }

    for (QModelIndexList::const_iterator it = selectedIndices.begin(), it_end = selectedIndices.end();
                it != it_end; ++it) {
        QWidget* widget = propertyVisualizers[it->row()]->getWidget();
        layout->addWidget(widget);
        widget->show();
    }
    widgets->setLayout(layout);
}

166
void SingleObjectPropertyModel::connectLogs(PropertyVisualizer* propViz)
167 168 169 170 171
{
    connect(propViz, SIGNAL(log(QString)), this, SLOT(slotLog(QString)));
    connect(propViz, SIGNAL(log(Logtype, QString)), this, SLOT(slotLog(Logtype, QString)));
}

172
int SingleObjectPropertyModel::rowCount(const QModelIndex & parent) const {
173 174 175
    return propertyVisualizers.size();
}

176
QVariant SingleObjectPropertyModel::data(const QModelIndex & index, int role) const {
177 178 179 180 181 182 183 184
    switch (role) {
        case Qt::DisplayRole:
            return propertyVisualizers[index.row()]->getName();
        default:
            return QVariant::Invalid;
    }
}

185
QVariant SingleObjectPropertyModel::headerData(int section, Qt::Orientation orientation, int role) const {
186 187 188 189 190 191 192 193 194
    switch (role) {
        case Qt::DisplayRole:
            return tr("Some header. %1 %2").arg(section).arg(orientation);
            break;
        default:
            return QAbstractListModel::headerData(section, orientation, role);
    }
}

195
void SingleObjectPropertyModel::objectUpdated()
196 197 198
{
    for (unsigned int i = 0; i < currentlyVisualizedIndices.size(); i++)
    {
199
        propertyVisualizers[currentlyVisualizedIndices[i]]->visualize(false, 0);
200 201
    }
}
202

203
QString SingleObjectPropertyModel::getLoadFilename()
204 205
{
  QString filter = getLoadFilenameFilter();
206
  QString selected_filter = tr("All Files (*)");
207

208
  QString fileName = QFileDialog::getOpenFileName(0, tr("Load Property"), QString(), filter, &selected_filter);
209 210 211 212

  return fileName;
}

213
QString SingleObjectPropertyModel::getLoadFilenameFilter()
214 215 216 217
{
    return tr("");
}

218
QString SingleObjectPropertyModel::getSaveFilename(unsigned int propId)
219 220 221 222 223 224 225 226 227
{
  QString filter(getSaveFilenameFilter(propId));
  QString defaultName = getDefaultSaveFilename(propId);

  QString fileName = QFileDialog::getSaveFileName(0, tr("Save Property"), defaultName, filter);

  return fileName;
}

228
QString SingleObjectPropertyModel::getSaveFilenameFilter(unsigned int propId)
229 230 231 232 233 234
{
    QString filter= tr("All Files (*)");

    return filter;
}

235
QString SingleObjectPropertyModel::getDefaultSaveFilename(unsigned int propId)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
{
    PropertyVisualizer* propViz = propertyVisualizers[propId];

    QString name = tr(propViz->getPropertyInfo().propName().c_str());

    if (propViz->getPropertyInfo().isVertexProp())
        name += tr(".vprop");
    else if (propViz->getPropertyInfo().isHalfedgeProp())
        name += tr(".hprop");
    else if (propViz->getPropertyInfo().isEdgeProp())
        name += tr(".eprop");
    else if (propViz->getPropertyInfo().isFaceProp())
        name += tr(".fprop");
    else if (propViz->getPropertyInfo().isHalffaceProp())
        name += tr(".hfprop");
    else if (propViz->getPropertyInfo().isCellProp())
        name += tr(".cprop");

    return name;
}

257
void SingleObjectPropertyModel::openFile(QString _filename, QFile& file_, QTextStream*& file_stream_)
258 259 260 261 262 263 264
{
  closeFile(file_, file_stream_);
  file_.setFileName(_filename);
  if(file_.open(QIODevice::ReadWrite | QIODevice::Text))
    file_stream_ = new QTextStream(&file_);
}

265
void SingleObjectPropertyModel::closeFile(QFile& file_, QTextStream*& file_stream_)
266 267 268 269 270 271 272 273 274 275 276 277
{
  if( file_stream_)
  {
    delete file_stream_;
    file_stream_ = 0;
  }
  if(file_.exists())
  {
      file_.close();
  }
}

278
QString SingleObjectPropertyModel::readLine(QTextStream* file_stream_)
279 280 281 282 283 284 285 286 287 288
{
    if(file_stream_)
    {
      QString s;
      s = file_stream_->readLine();
      return s;
    }
    else return QString("");
}

289
void SingleObjectPropertyModel::writeLine(QString _s, QTextStream*& file_stream_)
290 291 292 293 294 295 296 297
{
  if(file_stream_)
  {
    (*file_stream_) << _s << '\n';
  }
  else std::cerr << "Warning: filestream not available...\n";
}

298
void SingleObjectPropertyModel::saveProperty(unsigned int propId)
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
{
    PropertyVisualizer* propVis = propertyVisualizers[propId];

    QString filename = getSaveFilename(propId);
    if (filename == "") return;

    QFile file_;
    QTextStream* file_stream_ = 0;

    openFile(filename, file_, file_stream_);

    QString header = propVis->getHeader();
    writeLine(header, file_stream_);

    int n = propVis->getEntityCount();

    for (int i = 0; i < n; ++i)
    {
        QString propertyText = propVis->getPropertyText(i);
        writeLine(propertyText, file_stream_);
    }

    closeFile(file_, file_stream_);
}

324
void SingleObjectPropertyModel::loadProperty()
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
{

    QString filename = getLoadFilename();
    if (filename == "") return;

    QFile file_;
    QTextStream* file_stream_ = 0;

    openFile(filename, file_, file_stream_);

    QString header = readLine(file_stream_);

    PropertyVisualizer* propVis;
    unsigned int n;


    if (parseHeader(header, propVis, n))
    {
        setPropertyFromFile(file_stream_, n, propVis);
    }
    else
    {
        emit log("Property could not be loaded.");
    }

    closeFile(file_, file_stream_);
}

353
void SingleObjectPropertyModel::setPropertyFromFile(QTextStream*& file_stream_, unsigned int n, PropertyVisualizer* propVis)
354 355 356 357 358 359 360 361
{
    for (unsigned int i = 0; i < n; ++i)
    {
        QString propertyText = readLine(file_stream_);
        propVis->setPropertyFromText(i, propertyText);
    }
}

362
PropertyVisualizer* SingleObjectPropertyModel::getPropertyVisualizer(QString propName, PropertyInfo::ENTITY_FILTER filter, TypeInfoWrapper typeInfo)
363 364 365 366 367 368 369 370 371 372
{
    for (unsigned int i = 0; i < propertyVisualizers.size(); ++i)
    {
        const PropertyInfo& propInfo =  propertyVisualizers[i]->getPropertyInfo();
        if ((propInfo.entityType() == filter) && (QString::compare(tr(propInfo.propName().c_str()), propName)==0) && (propInfo.typeinfo() == typeInfo))
            return propertyVisualizers[i];
    }
    return 0;
}

373
bool SingleObjectPropertyModel::isPropertyFree(QString propName, PropertyInfo::ENTITY_FILTER filter, TypeInfoWrapper typeInfo)
374 375 376 377
{
    return getPropertyVisualizer(propName, filter, typeInfo) == 0;
}

378 379 380 381
PropertyInfo SingleObjectPropertyModel::getPropertyInfo(const QModelIndex index) const
{
	return propertyVisualizers[index.row()]->getPropertyInfo();
}