Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/control/McpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,9 @@ QJsonObject McpServer::handleToolsCall(const QJsonObject& params)
{
const int id = static_cast<int>(args.value("id").toInteger(0));
if (mm.getLayerById(id).isNull()) return textResult(QString("No layer with id %1.").arg(id), true);
const int index = static_cast<int>(args.value("index").toInteger(0));
_mainWindow->moveLayer(id, index);
const qint64 index = args.value("index").toInteger(-1);
if (index < 0 || index >= mm.nLayers() || !_mainWindow->moveLayer(id, int(index)))
return textResult(QString("Expected a layer index between 0 and %1.").arg(mm.nLayers() - 1), true);
return textResult(QString("Moved layer %1 to index %2.").arg(id).arg(index));
}
if (name == "set_layer_visible" || name == "set_layer_solo" || name == "set_layer_locked")
Expand Down Expand Up @@ -694,7 +695,7 @@ QJsonArray McpServer::toolDefinitions() const
tools.append(tool("move_layer", "Move a layer to a new stacking index.",
QJsonObject{
{"id", prop("integer", "Layer id.")},
{"index", prop("integer", "Target index (0 = bottom).")}
{"index", prop("integer", "Target index (0 = top).")}
},
QJsonArray{"id", "index"}));
tools.append(tool("set_layer_visible", "Show or hide a layer. Returns the layer.",
Expand Down
43 changes: 0 additions & 43 deletions src/core/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,49 +324,6 @@ void DeleteLayerCommand::redo()
_mainWindow->deleteLayer(_layerId);
}

MoveLayerCommand::MoveLayerCommand(MainWindow *mainWindow, uid layerId, MM::MoveElement moveType, QUndoCommand *parent) :
QUndoCommand(parent),
_mainWindow(mainWindow),
_layerId(layerId),
_moveType(moveType)
{
switch (moveType) {
case MM::Raise: setText(QObject::tr("Raise layer")); break;
case MM::Lower: setText(QObject::tr("Lower layer")); break;
case MM::Top: setText(QObject::tr("Raise layer to top")); break;
case MM::Bottom: setText(QObject::tr("Lower layer to bottom")); break;
default:; // should not happen
}
}

void MoveLayerCommand::undo()
{
if (!_layer.isNull())
{
// Do the inverse move.
_mainWindow->moveLayer(_layerId, _fromIdx);
}
}

void MoveLayerCommand::redo()
{
// Store mapping pointer before delete it
_layer = _mainWindow->getMappingManager().getLayerById(_layerId);
_fromIdx = _mainWindow->getMappingManager().getLayerIndex(_layer);
int maxLayerIdx = _mainWindow->getMappingManager().nLayers()-1;

switch (_moveType) {
case MM::Raise: _toIdx = qMax(_fromIdx - 1, 0); break;
case MM::Lower: _toIdx = qMin(_fromIdx + 1, maxLayerIdx); break;
case MM::Top: _toIdx = 0; break;
case MM::Bottom: _toIdx = maxLayerIdx; break;
default:; // should not happen
}

// Do the move.
_mainWindow->moveLayer(_layerId, _toIdx);
}

FlipShapeCommand::FlipShapeCommand(MapperGLCanvas *canvas, MShape::FlipDirection direction, QUndoCommand *parent)
: TransformShapeCommand (canvas, STEP, parent)
{
Expand Down
17 changes: 10 additions & 7 deletions src/core/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum CommandId {
};

class MainWindow;
class MappingManager;
class MapperGLCanvas;

class AddSourceCommand : public QUndoCommand
Expand Down Expand Up @@ -243,21 +244,23 @@ class DeleteLayerCommand : public QUndoCommand
uid _layerId;
};

/// Moves a layer within its project and remembers the row to restore on undo.
class MoveLayerCommand : public QUndoCommand
{
public:
explicit MoveLayerCommand(MainWindow *mainWindow, uid layerId, MM::MoveElement moveType, QUndoCommand *parent = 0);
/// Creates a command for the given layer and direction.
MoveLayerCommand(MappingManager& manager, uid layerId, MM::MoveElement moveType);

void undo() Q_DECL_OVERRIDE;
void redo() Q_DECL_OVERRIDE;
/// Restores the row the layer occupied before the move.
void undo() override;
/// Moves the layer in the requested direction.
void redo() override;

private:
MainWindow *_mainWindow;
MappingManager& _manager;
Layer::ptr _layer;
uid _layerId;
MM::MoveElement _moveType;
int _fromIdx;
int _toIdx;
int _fromIdx = -1;
};

/**
Expand Down
41 changes: 22 additions & 19 deletions src/core/MappingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/

#include "MappingManager.h"

#include <QSet>
#include <iostream>

namespace mmp {
Expand Down Expand Up @@ -203,15 +205,18 @@ bool MappingManager::removeLayer(uid mappingId)
}
}

/// Moves a mapping of given uid by a certain number of steps up or down.
/// Moves a layer to a zero-based row. Returns false for an unknown id or invalid row.
bool MappingManager::moveLayer(uid mappingId, int toIndex)
{
// Make sure the source to which this mapping refers to exists in the manager.
int idx = getLayerIndex(mappingId);
if (idx >= 0)
if (idx >= 0 && toIndex >= 0 && toIndex < layerVector.size())
{
if (idx == toIndex)
return true;
layerVector.move(idx, toIndex);
updateLayerDepths();
emit layerMoved(mappingId, idx, toIndex);
return true;
}
else
Expand Down Expand Up @@ -299,26 +304,24 @@ QVector<Source::ptr> MappingManager::getVisibleSources() const
return visibleSources;
}

void MappingManager::reorderLayers(QVector<uid> mappingIds)
bool MappingManager::reorderLayers(const QVector<uid>& layerIds)
{
// Both vector needs to have the same size.
Q_ASSERT( mappingIds.size() == layerVector.size() );
layerVector.clear();
int depth = 0;
for (QVector<uid>::iterator it = mappingIds.begin();
it != mappingIds.end(); ++it)
{
// Uid should be a key of the layerMap.
Q_ASSERT( layerMap.contains(*it) );
// Makes sure the uids are not repeated.
Q_ASSERT( ! layerVector.contains(layerMap[*it]) );
// Adds the mapping at the right place in the vector.
Layer::ptr layer = layerMap[*it];
layer->setDepth(depth);
layerVector.push_back( layer );
if (layerIds.size() != layerVector.size())
return false;

depth++;
QVector<Layer::ptr> ordered;
ordered.reserve(layerIds.size());
QSet<uid> seen;
for (uid id : layerIds) {
const Layer::ptr layer = layerMap.value(id);
if (!layer || seen.contains(id))
return false;
seen.insert(id);
ordered.append(layer);
}
layerVector.swap(ordered);
updateLayerDepths();
return true;
}

void MappingManager::updateLayerDepths()
Expand Down
18 changes: 11 additions & 7 deletions src/core/MappingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ namespace mmp {
* This is a container class for all the sources and mappings ie. the main model object that allows
* CRUD over sources and mappings.
*/
class MappingManager
class MappingManager : public QObject
{
Q_OBJECT
public:
/// Constructor.
/// Constructs an empty project.
MappingManager();

signals:
/// Reports the old and new rows after a layer changes position in the project.
void layerMoved(uid id, int from, int to);

private:
// Model elements.
/// Container for all sources.
Expand All @@ -63,7 +68,6 @@ class MappingManager
QSize _outputResolution;

/// What the triggers of the project (keys of the computer keyboard) do to its settings.
/// Shared by the copies of the manager, which the canvases make to draw.
QSharedPointer<LinkTable> _links;
QSharedPointer<MediaCues> _cues;

Expand Down Expand Up @@ -193,7 +197,7 @@ class MappingManager
/// Removes a mapping of given uid.
bool removeLayer(uid mappingId);

/// Moves a mapping of given uid by a certain number of steps up or down.
/// Moves a layer to a zero-based row. Returns false for an unknown id or invalid row.
bool moveLayer(uid mappingId, int toIndex);

/// Returns the number of mappings.
Expand All @@ -203,7 +207,7 @@ class MappingManager
* Returns the i-th mapping in the vector. Good for iterating over all mappings. Vector is
* ordered from bottom (deepest) to top (shallowest) layer.
*/
Layer::ptr getLayer(int i) { return layerVector[i]; }
Layer::ptr getLayer(int i) const { return layerVector[i]; }

/// Returns mapping with given uid.
Layer::ptr getLayerById(uid id) const { return layerMap[id]; }
Expand All @@ -220,8 +224,8 @@ class MappingManager

int getLayerDepth(Layer::ptr mapping) const { return -getLayerIndex(mapping); }

/// Reorders the mappings according to given list of uids. QVector needs to
void reorderLayers(QVector<uid> mappingIds);
/// Applies a complete permutation of layer ids. An invalid order leaves the project intact.
bool reorderLayers(const QVector<uid>& layerIds);

/// Update mapping depths after a move.
void updateLayerDepths();
Expand Down
63 changes: 63 additions & 0 deletions src/core/MoveLayerCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* MoveLayerCommand.cpp
*
* (c) 2014 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2014 Alexandre Quessy -- alexandre(@)quessy(.)net
* (c) 2014 Dame Diongue -- baydamd(@)gmail(.)com
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "MappingManager.h"
#include "Commands.h"

namespace mmp {

MoveLayerCommand::MoveLayerCommand(MappingManager& manager, uid layerId, MM::MoveElement moveType)
: _manager(manager), _layer(manager.getLayerById(layerId)), _moveType(moveType)
{
switch (_moveType) {
case MM::Raise: setText(QObject::tr("Raise layer")); break;
case MM::Lower: setText(QObject::tr("Lower layer")); break;
case MM::Top: setText(QObject::tr("Raise layer to top")); break;
case MM::Bottom: setText(QObject::tr("Lower layer to bottom")); break;
}
}

void MoveLayerCommand::undo()
{
if (!_manager.moveLayer(_layer->getId(), _fromIdx))
setObsolete(true);
}

void MoveLayerCommand::redo()
{
_fromIdx = _manager.getLayerIndex(_layer);
if (_fromIdx < 0) {
setObsolete(true);
return;
}
const int last = _manager.nLayers() - 1;
int to = _fromIdx;
switch (_moveType) {
case MM::Raise: to = qMax(_fromIdx - 1, 0); break;
case MM::Lower: to = qMin(_fromIdx + 1, last); break;
case MM::Top: to = 0; break;
case MM::Bottom: to = last; break;
}
if (to == _fromIdx || !_manager.moveLayer(_layer->getId(), to))
setObsolete(true);
}

} // namespace mmp
20 changes: 10 additions & 10 deletions src/core/Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,18 @@ void Texture::bind()
void Texture::uploadBits()
{
lockMutex();
_uploadBitsLocked();
if (bitsHaveChanged())
_uploadBitsLocked();
unlockMutex();
}

void Texture::_uploadBitsLocked()
{
if (bitsHaveChanged())
{
int width = getWidth();
int height = getHeight();
const uchar* bits = getBits(); // Resets bitsHaveChanged().
Util::uploadTextureBits(width, height, getBitsFormat(), getBitsType(), bits,
_uploadedWidth, _uploadedHeight);
}
const int width = getWidth();
const int height = getHeight();
const uchar* bits = getBits(); // Resets bitsHaveChanged() and may convert a YUV frame.
Util::uploadTextureBits(width, height, getBitsFormat(), getBitsType(), bits,
_uploadedWidth, _uploadedHeight);
}

Texture::Texture(uid id) :
Expand Down Expand Up @@ -719,7 +717,9 @@ void Video::bind()
else
{
glBindTexture(GL_TEXTURE_2D, textureId);
_uploadBitsLocked();
// Taking the YUV frame cleared its change flag, even if the upload failed.
if (uploading)
_uploadBitsLocked();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Expand Down
2 changes: 1 addition & 1 deletion src/core/Source.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class Texture : public Source
int effectRenderCount() const;

protected:
/// uploadBits() with the mutex already locked.
/// Uploads the current RGB pixels. The caller holds the mutex and detected a new frame.
void _uploadBitsLocked();

public:
Expand Down
1 change: 1 addition & 0 deletions src/core/core.pri
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ SOURCES += $$PWD/Commands.cpp \
$$PWD/IntermediateImage.cpp \
$$PWD/Layer.cpp \
$$PWD/Link.cpp \
$$PWD/MoveLayerCommand.cpp \
$$PWD/MappingManager.cpp \
$$PWD/MetaObjectRegistry.cpp \
$$PWD/MidiMessage.cpp \
Expand Down
8 changes: 4 additions & 4 deletions src/gui/LayerItemDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ void LayerItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt

if (index.column() == MM::GroupButtonColum && isLayerRow(index))
{
bool isSolo = index.model()->data(index, Qt::CheckStateRole + 1).toBool();
bool isLocked = index.model()->data(index, Qt::CheckStateRole + 2).toBool();
bool isSolo = index.model()->data(index, LayerTreeModel::SoloRole).toBool();
bool isLocked = index.model()->data(index, LayerTreeModel::LockedRole).toBool();
// Create all mappings buttons:
// solo button
QStyleOptionToolButton mappingSoloButton;
Expand Down Expand Up @@ -456,10 +456,10 @@ bool LayerItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
model->setData(index, !(index.data(Qt::CheckStateRole).toBool()), Qt::CheckStateRole);
break;
case LayerRowButton::Solo:
model->setData(index, !(index.data(Qt::CheckStateRole + 1).toBool()), Qt::CheckStateRole + 1);
model->setData(index, !(index.data(LayerTreeModel::SoloRole).toBool()), LayerTreeModel::SoloRole);
break;
case LayerRowButton::Lock:
model->setData(index, !(index.data(Qt::CheckStateRole + 2).toBool()), Qt::CheckStateRole + 2);
model->setData(index, !(index.data(LayerTreeModel::LockedRole).toBool()), LayerTreeModel::LockedRole);
break;
case LayerRowButton::Duplicate:
emit itemDuplicated(index.data(Qt::UserRole).toInt());
Expand Down
Loading
Loading