2014-06-06 10:43:21 +00:00
|
|
|
|
|
|
|
#include "commanddispatcher.hpp"
|
|
|
|
|
|
|
|
#include "../doc/document.hpp"
|
|
|
|
|
|
|
|
#include "idtable.hpp"
|
|
|
|
#include "record.hpp"
|
|
|
|
#include "commands.hpp"
|
|
|
|
|
2014-06-07 11:02:45 +00:00
|
|
|
std::vector<std::string> CSMWorld::CommandDispatcher::getDeletableRecords() const
|
2014-06-06 10:43:21 +00:00
|
|
|
{
|
2014-06-07 11:02:45 +00:00
|
|
|
std::vector<std::string> result;
|
2014-06-06 10:43:21 +00:00
|
|
|
|
|
|
|
IdTable& model = dynamic_cast<IdTable&> (*mDocument.getData().getTableModel (mId));
|
|
|
|
|
2014-06-07 11:06:19 +00:00
|
|
|
int stateColumnIndex = model.findColumnIndex (Columns::ColumnId_Modification);
|
|
|
|
|
2014-06-07 11:02:45 +00:00
|
|
|
for (std::vector<std::string>::const_iterator iter (mSelection.begin());
|
2014-06-06 10:43:21 +00:00
|
|
|
iter!=mSelection.end(); ++iter)
|
|
|
|
{
|
2014-06-07 11:02:45 +00:00
|
|
|
int row = model.getModelIndex (*iter, 0).row();
|
|
|
|
|
2014-06-06 10:43:21 +00:00
|
|
|
// check record state
|
2014-06-07 11:06:19 +00:00
|
|
|
RecordBase::State state = static_cast<RecordBase::State> (
|
|
|
|
model.data (model.index (row, stateColumnIndex)).toInt());
|
2014-06-06 10:43:21 +00:00
|
|
|
|
|
|
|
if (state==RecordBase::State_Deleted)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// check other columns (only relevant for a subset of the tables)
|
|
|
|
int dialogueTypeIndex = model.searchColumnIndex (Columns::ColumnId_DialogueType);
|
|
|
|
|
|
|
|
if (dialogueTypeIndex!=-1)
|
|
|
|
{
|
2014-06-07 11:02:45 +00:00
|
|
|
int type = model.data (model.index (row, dialogueTypeIndex)).toInt();
|
2014-06-06 10:43:21 +00:00
|
|
|
|
|
|
|
if (type!=ESM::Dialogue::Topic && type!=ESM::Dialogue::Journal)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push_back (*iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-06-07 11:02:45 +00:00
|
|
|
std::vector<std::string> CSMWorld::CommandDispatcher::getRevertableRecords() const
|
2014-06-06 10:43:21 +00:00
|
|
|
{
|
2014-06-07 11:02:45 +00:00
|
|
|
std::vector<std::string> result;
|
2014-06-06 10:43:21 +00:00
|
|
|
|
|
|
|
IdTable& model = dynamic_cast<IdTable&> (*mDocument.getData().getTableModel (mId));
|
|
|
|
|
|
|
|
/// \todo Reverting temporarily disabled on tables that support reordering, because
|
|
|
|
/// revert logic currently can not handle reordering.
|
|
|
|
if (model.getFeatures() & IdTable::Feature_ReorderWithinTopic)
|
|
|
|
return result;
|
|
|
|
|
2014-06-07 11:06:19 +00:00
|
|
|
int stateColumnIndex = model.findColumnIndex (Columns::ColumnId_Modification);
|
|
|
|
|
2014-06-07 11:02:45 +00:00
|
|
|
for (std::vector<std::string>::const_iterator iter (mSelection.begin());
|
2014-06-06 10:43:21 +00:00
|
|
|
iter!=mSelection.end(); ++iter)
|
|
|
|
{
|
2014-06-07 11:02:45 +00:00
|
|
|
int row = model.getModelIndex (*iter, 0).row();
|
|
|
|
|
2014-06-06 10:43:21 +00:00
|
|
|
// check record state
|
2014-06-07 11:06:19 +00:00
|
|
|
RecordBase::State state = static_cast<RecordBase::State> (
|
|
|
|
model.data (model.index (row, stateColumnIndex)).toInt());
|
2014-06-06 10:43:21 +00:00
|
|
|
|
|
|
|
if (state==RecordBase::State_BaseOnly)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result.push_back (*iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSMWorld::CommandDispatcher::CommandDispatcher (CSMDoc::Document& document,
|
|
|
|
const CSMWorld::UniversalId& id, QObject *parent)
|
|
|
|
: QObject (parent), mDocument (document), mId (id), mLocked (false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void CSMWorld::CommandDispatcher::setEditLock (bool locked)
|
|
|
|
{
|
|
|
|
mLocked = locked;
|
|
|
|
}
|
|
|
|
|
2014-06-07 11:02:45 +00:00
|
|
|
void CSMWorld::CommandDispatcher::setSelection (const std::vector<std::string>& selection)
|
2014-06-06 10:43:21 +00:00
|
|
|
{
|
|
|
|
mSelection = selection;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSMWorld::CommandDispatcher::canDelete() const
|
|
|
|
{
|
|
|
|
if (mLocked)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return getDeletableRecords().size()!=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSMWorld::CommandDispatcher::canRevert() const
|
|
|
|
{
|
|
|
|
if (mLocked)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return getRevertableRecords().size()!=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::CommandDispatcher::executeDelete()
|
|
|
|
{
|
|
|
|
if (mLocked)
|
|
|
|
return;
|
|
|
|
|
2014-06-07 11:02:45 +00:00
|
|
|
std::vector<std::string> rows = getDeletableRecords();
|
2014-06-06 10:43:21 +00:00
|
|
|
|
|
|
|
if (rows.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
IdTable& model = dynamic_cast<IdTable&> (*mDocument.getData().getTableModel (mId));
|
|
|
|
|
|
|
|
int columnIndex = model.findColumnIndex (Columns::ColumnId_Id);
|
|
|
|
|
|
|
|
if (rows.size()>1)
|
|
|
|
mDocument.getUndoStack().beginMacro (tr ("Delete multiple records"));
|
|
|
|
|
2014-06-07 11:02:45 +00:00
|
|
|
for (std::vector<std::string>::const_iterator iter (rows.begin()); iter!=rows.end(); ++iter)
|
2014-06-06 10:43:21 +00:00
|
|
|
{
|
2014-06-07 11:02:45 +00:00
|
|
|
std::string id = model.data (model.getModelIndex (*iter, columnIndex)).
|
2014-06-06 10:43:21 +00:00
|
|
|
toString().toUtf8().constData();
|
|
|
|
|
|
|
|
mDocument.getUndoStack().push (new CSMWorld::DeleteCommand (model, id));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rows.size()>1)
|
|
|
|
mDocument.getUndoStack().endMacro();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::CommandDispatcher::executeRevert()
|
|
|
|
{
|
|
|
|
if (mLocked)
|
|
|
|
return;
|
|
|
|
|
2014-06-07 11:02:45 +00:00
|
|
|
std::vector<std::string> rows = getRevertableRecords();
|
2014-06-06 10:43:21 +00:00
|
|
|
|
|
|
|
if (rows.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
IdTable& model = dynamic_cast<IdTable&> (*mDocument.getData().getTableModel (mId));
|
|
|
|
|
|
|
|
int columnIndex = model.findColumnIndex (Columns::ColumnId_Id);
|
|
|
|
|
|
|
|
if (rows.size()>1)
|
|
|
|
mDocument.getUndoStack().beginMacro (tr ("Revert multiple records"));
|
|
|
|
|
2014-06-07 11:02:45 +00:00
|
|
|
for (std::vector<std::string>::const_iterator iter (rows.begin()); iter!=rows.end(); ++iter)
|
2014-06-06 10:43:21 +00:00
|
|
|
{
|
2014-06-07 11:02:45 +00:00
|
|
|
std::string id = model.data (model.getModelIndex (*iter, columnIndex)).
|
2014-06-06 10:43:21 +00:00
|
|
|
toString().toUtf8().constData();
|
|
|
|
|
|
|
|
mDocument.getUndoStack().push (new CSMWorld::RevertCommand (model, id));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rows.size()>1)
|
|
|
|
mDocument.getUndoStack().endMacro();
|
|
|
|
}
|