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 274 275 276 277 278 279 280 281 282 283 284 285 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 313 314 315 316 317 318 319 320 321 322 323 324 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2006 Chris Cannam.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "LayerTree.h"
#include "view/PaneStack.h"
#include "base/PlayParameters.h"
#include "view/Pane.h"
#include "layer/Layer.h"
#include "data/model/Model.h"
#include "data/model/WaveFileModel.h"
#include <QIcon>
#include <iostream>
namespace sv {
ModelMetadataModel::ModelMetadataModel(PaneStack *stack, bool waveModelsOnly,
QObject *parent) :
QAbstractItemModel(parent),
m_stack(stack),
m_waveModelsOnly(waveModelsOnly)
{
if (m_waveModelsOnly) {
m_modelTypeColumn = -1;
m_modelNameColumn = 0;
m_modelMakerColumn = 1;
m_modelSourceColumn = 2;
m_columnCount = 3;
} else {
m_modelTypeColumn = 0;
m_modelNameColumn = 1;
m_modelMakerColumn = 2;
m_modelSourceColumn = 3;
m_columnCount = 4;
}
connect(stack, SIGNAL(paneAdded()), this, SLOT(paneAdded()));
connect(stack, SIGNAL(paneDeleted()), this, SLOT(paneDeleted()));
for (int i = 0; i < stack->getPaneCount(); ++i) {
Pane *pane = stack->getPane(i);
if (!pane) continue;
connect(pane, SIGNAL(propertyContainerAdded(PropertyContainer *)),
this, SLOT(propertyContainerAdded(PropertyContainer *)));
connect(pane, SIGNAL(propertyContainerRemoved(PropertyContainer *)),
this, SLOT(propertyContainerRemoved(PropertyContainer *)));
connect(pane, SIGNAL(propertyContainerSelected(PropertyContainer *)),
this, SLOT(propertyContainerSelected(PropertyContainer *)));
connect(pane, SIGNAL(propertyContainerPropertyChanged(PropertyContainer *)),
this, SLOT(propertyContainerPropertyChanged(PropertyContainer *)));
connect(pane, SIGNAL(propertyContainerNameChanged(PropertyContainer *)),
this, SLOT(propertyContainerPropertyChanged(PropertyContainer *)));
connect(pane, SIGNAL(layerModelChanged()),
this, SLOT(paneLayerModelChanged()));
}
rebuildModelSet();
}
ModelMetadataModel::~ModelMetadataModel()
{
}
void
ModelMetadataModel::rebuildModelSet()
{
std::set<ModelId> unfound = m_models;
for (int i = 0; i < m_stack->getPaneCount(); ++i) {
Pane *pane = m_stack->getPane(i);
if (!pane) continue;
for (int j = 0; j < pane->getLayerCount(); ++j) {
Layer *layer = pane->getLayer(j);
if (!layer) continue;
ModelId modelId = layer->getModel();
if (modelId.isNone()) continue;
if (m_waveModelsOnly) {
if (!ModelById::getAs<WaveFileModel>(modelId)) continue;
}
if (m_models.find(modelId) == m_models.end()) {
m_models.insert(modelId);
} else {
unfound.erase(modelId);
}
}
}
for (ModelId m: unfound) {
m_models.erase(m);
}
SVDEBUG << "ModelMetadataModel::rebuildModelSet: " << m_models.size() << " models" << endl;
}
void
ModelMetadataModel::paneAdded()
{
rebuildModelSet();
emit layoutChanged();
}
void
ModelMetadataModel::paneDeleted()
{
rebuildModelSet();
emit layoutChanged();
}
void
ModelMetadataModel::paneLayerModelChanged()
{
rebuildModelSet();
emit layoutChanged();
}
void
ModelMetadataModel::propertyContainerAdded(PropertyContainer *)
{
rebuildModelSet();
emit layoutChanged();
}
void
ModelMetadataModel::propertyContainerRemoved(PropertyContainer *)
{
rebuildModelSet();
emit layoutChanged();
}
void
ModelMetadataModel::propertyContainerSelected(PropertyContainer *)
{
}
void
ModelMetadataModel::propertyContainerPropertyChanged(PropertyContainer *)
{
}
void
ModelMetadataModel::playParametersAudibilityChanged(bool )
{
}
QVariant
ModelMetadataModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
// QObject *obj = static_cast<QObject *>(index.internalPointer());
int row = index.row(), col = index.column();
//!!! not exactly the ideal use of a std::set
std::set<ModelId>::iterator itr = m_models.begin();
for (int i = 0; i < row && itr != m_models.end(); ++i, ++itr);
if (itr == m_models.end()) return QVariant();
auto model = ModelById::get(*itr);
if (!model) return QVariant();
if (role != Qt::DisplayRole) {
if (m_waveModelsOnly && col == m_modelNameColumn &&
role == Qt::DecorationRole) {
// There is no meaningful icon for a model, in general --
// the icons we have represent layer types and it would be
// misleading to use them for models. However, if we're
// only showing wave models, we use the waveform icon just
// for decorative purposes.
return QVariant(QIcon(QString(":/icons/waveform.png")));
}
return QVariant();
}
if (col == m_modelTypeColumn) {
return QVariant(model->getTypeName());
} else if (col == m_modelNameColumn) {
return QVariant(model->objectName());
} else if (col == m_modelMakerColumn) {
return QVariant(model->getMaker());
} else if (col == m_modelSourceColumn) {
return QVariant(model->getLocation());
}
return QVariant();
}
bool
ModelMetadataModel::setData(const QModelIndex &, const QVariant &, int )
{
return false;
}
Qt::ItemFlags
ModelMetadataModel::flags(const QModelIndex &) const
{
Qt::ItemFlags flags = Qt::ItemIsEnabled;
return flags;
}
QVariant
ModelMetadataModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == m_modelTypeColumn) return QVariant(tr("Type"));
else if (section == m_modelNameColumn) return QVariant(tr("Name"));
else if (section == m_modelMakerColumn) return QVariant(tr("Maker"));
else if (section == m_modelSourceColumn) return QVariant(tr("Source"));
}
return QVariant();
}
QModelIndex
ModelMetadataModel::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid()) {
if (row >= (int)m_models.size()) return QModelIndex();
return createIndex(row, column, (void *)nullptr);
}
return QModelIndex();
}
QModelIndex
ModelMetadataModel::parent(const QModelIndex &) const
{
return QModelIndex();
}
int
ModelMetadataModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid()) return int(m_models.size());
return 0;
}
int
ModelMetadataModel::columnCount(const QModelIndex &) const
{
return m_columnCount;
}
LayerTreeModel::LayerTreeModel(PaneStack *stack, QObject *parent) :
QAbstractItemModel(parent),
m_stack(stack)
{
m_layerNameColumn = 0;
m_layerVisibleColumn = 1;
m_layerPlayedColumn = 2;
m_modelNameColumn = 3;
m_columnCount = 4;
connect(stack, SIGNAL(paneAdded()), this, SLOT(paneAdded()));
connect(stack, SIGNAL(paneAboutToBeDeleted(Pane *)),
this, SLOT(paneAboutToBeDeleted(Pane *)));
for (int i = 0; i < stack->getPaneCount(); ++i) {
Pane *pane = stack->getPane(i);
if (!pane) continue;
connect(pane, SIGNAL(propertyContainerAdded(PropertyContainer *)),
this, SLOT(propertyContainerAdded(PropertyContainer *)));
connect(pane, SIGNAL(propertyContainerRemoved(PropertyContainer *)),
this, SLOT(propertyContainerRemoved(PropertyContainer *)));
connect(pane, SIGNAL(propertyContainerSelected(PropertyContainer *)),
this, SLOT(propertyContainerSelected(PropertyContainer *)));
connect(pane, SIGNAL(propertyContainerPropertyChanged(PropertyContainer *)),
this, SLOT(propertyContainerPropertyChanged(PropertyContainer *)));
connect(pane, SIGNAL(propertyContainerNameChanged(PropertyContainer *)),
this, SLOT(propertyContainerPropertyChanged(PropertyContainer *)));
connect(pane, SIGNAL(layerModelChanged()),
this, SLOT(paneLayerModelChanged()));
for (int j = 0; j < pane->getLayerCount(); ++j) {
Layer *layer = pane->getLayer(j);
if (!layer) continue;
auto params = layer->getPlayParameters();
if (!params) continue;
connect(params.get(), SIGNAL(playAudibleChanged(bool)),
this, SLOT(playParametersAudibilityChanged(bool)));
}
}
}
LayerTreeModel::~LayerTreeModel()
{
}
void
LayerTreeModel::paneAdded()
{
emit layoutChanged();
}
void
LayerTreeModel::paneAboutToBeDeleted(Pane *pane)
{
SVCERR << "paneDeleted: " << pane << endl;
m_deletedPanes.insert(pane);
emit layoutChanged();
}
void
LayerTreeModel::propertyContainerAdded(PropertyContainer *)
{
emit layoutChanged();
}
void
LayerTreeModel::propertyContainerRemoved(PropertyContainer *)
{
emit layoutChanged();
}
void
LayerTreeModel::propertyContainerSelected(PropertyContainer *)
{
emit layoutChanged();
}
void
LayerTreeModel::paneLayerModelChanged()
{
emit layoutChanged();
}
void
LayerTreeModel::propertyContainerPropertyChanged(PropertyContainer *pc)
{
for (int i = 0; i < m_stack->getPaneCount(); ++i) {
Pane *pane = m_stack->getPane(i);
if (!pane) continue;
for (int j = 0; j < pane->getLayerCount(); ++j) {
if (pane->getLayer(j) == pc) {
emit dataChanged(createIndex(pane->getLayerCount() - j - 1,
m_layerNameColumn, pane),
createIndex(pane->getLayerCount() - j - 1,
m_modelNameColumn, pane));
}
}
}
}
void
LayerTreeModel::playParametersAudibilityChanged(bool a)
{
PlayParameters *params = dynamic_cast<PlayParameters *>(sender());
if (!params) return;
SVDEBUG << "LayerTreeModel::playParametersAudibilityChanged("
<< params << "," << a << ")" << endl;
for (int i = 0; i < m_stack->getPaneCount(); ++i) {
Pane *pane = m_stack->getPane(i);
if (!pane) continue;
for (int j = 0; j < pane->getLayerCount(); ++j) {
Layer *layer = pane->getLayer(j);
if (!layer) continue;
if (layer->getPlayParameters().get() == params) { // ugh
SVDEBUG << "LayerTreeModel::playParametersAudibilityChanged("
<< params << "," << a << "): row " << pane->getLayerCount() - j - 1 << ", col " << 2 << endl;
emit dataChanged(createIndex(pane->getLayerCount() - j - 1,
m_layerPlayedColumn, pane),
createIndex(pane->getLayerCount() - j - 1,
m_layerPlayedColumn, pane));
}
}
}
}
QVariant
LayerTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
QObject *obj = static_cast<QObject *>(index.internalPointer());
int row = index.row(), col = index.column();
Pane *pane = dynamic_cast<Pane *>(obj);
if (!pane) {
if (col == 0 && row < m_stack->getPaneCount()) {
switch (role) {
case Qt::DisplayRole:
return QVariant(QString("Pane %1").arg(row + 1));
case Qt::DecorationRole:
return QVariant(QIcon(QString(":/icons/pane.png")));
default: break;
}
}
}
if (pane && pane->getLayerCount() > row) {
Layer *layer = pane->getLayer(pane->getLayerCount() - row - 1);
if (layer) {
if (col == m_layerNameColumn) {
switch (role) {
case Qt::DisplayRole:
return QVariant(layer->objectName());
case Qt::DecorationRole:
return QVariant
(QIcon(QString(":/icons/%1.png")
.arg(layer->getPropertyContainerIconName())));
default: break;
}
} else if (col == m_layerVisibleColumn) {
if (role == Qt::CheckStateRole) {
return QVariant(layer->isLayerDormant(pane) ?
Qt::Unchecked : Qt::Checked);
} else if (role == Qt::TextAlignmentRole) {
return QVariant(Qt::AlignHCenter);
}
} else if (col == m_layerPlayedColumn) {
if (role == Qt::CheckStateRole) {
auto params = layer->getPlayParameters();
if (params) return QVariant(params->isPlayMuted() ?
Qt::Unchecked : Qt::Checked);
else return QVariant();
} else if (role == Qt::TextAlignmentRole) {
return QVariant(Qt::AlignHCenter);
}
} else if (col == m_modelNameColumn) {
auto model = ModelById::get(layer->getModel());
if (model && role == Qt::DisplayRole) {
return QVariant(model->objectName());
}
}
}
}
return QVariant();
}
bool
LayerTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid()) return false;
QObject *obj = static_cast<QObject *>(index.internalPointer());
int row = index.row(), col = index.column();
Pane *pane = dynamic_cast<Pane *>(obj);
if (!pane || pane->getLayerCount() <= row) return false;
Layer *layer = pane->getLayer(pane->getLayerCount() - row - 1);
if (!layer) return false;
if (col == m_layerVisibleColumn) {
if (role == Qt::CheckStateRole) {
layer->showLayer(pane, value.toInt() == Qt::Checked);
emit dataChanged(index, index);
return true;
}
} else if (col == m_layerPlayedColumn) {
if (role == Qt::CheckStateRole) {
auto params = layer->getPlayParameters();
if (params) {
params->setPlayMuted(value.toInt() == Qt::Unchecked);
emit dataChanged(index, index);
return true;
}
}
}
return false;
}
Qt::ItemFlags
LayerTreeModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = Qt::ItemIsEnabled;
if (!index.isValid()) return flags;
if (index.column() == m_layerVisibleColumn ||
index.column() == m_layerPlayedColumn) {
flags |= Qt::ItemIsUserCheckable;
} else if (index.column() == 0) {
flags |= Qt::ItemIsSelectable;
}
return flags;
}
QVariant
LayerTreeModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == m_layerNameColumn) return QVariant(tr("Layer"));
else if (section == m_layerVisibleColumn) return QVariant(tr("Shown"));
else if (section == m_layerPlayedColumn) return QVariant(tr("Played"));
else if (section == m_modelNameColumn) return QVariant(tr("Model"));
}
return QVariant();
}
QModelIndex
LayerTreeModel::index(int row, int column, const QModelIndex &parent) const
{
// cell for a pane contains row, column, pane stack
// -> its parent is the invalid cell
// cell for a layer contains row, column, pane
// -> its parent is row, column, pane stack (which identify the pane)
if (!parent.isValid()) {
if (row >= m_stack->getPaneCount() || column > 0) return QModelIndex();
return createIndex(row, column, m_stack);
}
QObject *obj = static_cast<QObject *>(parent.internalPointer());
if (obj == m_stack) {
Pane *pane = m_stack->getPane(parent.row());
if (!pane || parent.column() > 0) return QModelIndex();
return createIndex(row, column, pane);
}
return QModelIndex();
}
QModelIndex
LayerTreeModel::parent(const QModelIndex &index) const
{
QObject *obj = static_cast<QObject *>(index.internalPointer());
if (m_deletedPanes.find(obj) != m_deletedPanes.end()) {
// m_deletedPanes.erase(obj);
return QModelIndex();
}
Pane *pane = dynamic_cast<Pane *>(obj);
if (pane) {
int index = m_stack->getPaneIndex(pane);
if (index >= 0) return createIndex(index, 0, m_stack);
}
return QModelIndex();
}
int
LayerTreeModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid()) return m_stack->getPaneCount();
QObject *obj = static_cast<QObject *>(parent.internalPointer());
if (obj == m_stack) {
Pane *pane = m_stack->getPane(parent.row());
if (!pane || parent.column() > 0) return 0;
return pane->getLayerCount();
}
return 0;
}
int
LayerTreeModel::columnCount(const QModelIndex &parent) const
{
if (!parent.isValid()) return m_columnCount;
QObject *obj = static_cast<QObject *>(parent.internalPointer());
if (obj == m_stack) return m_columnCount; // row for a layer
return 1;
}
} // end namespace sv
|