added create cell menu item to regionmap

actorid
Marc Zinnschlag 11 years ago
parent 19b31c4146
commit fc4195a88f

@ -3,12 +3,19 @@
#include <algorithm>
#include <set>
#include <sstream>
#include <QHeaderView>
#include <QContextMenuEvent>
#include <QMenu>
#include "../../model/doc/document.hpp"
#include "../../model/world/regionmap.hpp"
#include "../../model/world/universalid.hpp"
#include "../../model/world/data.hpp"
#include "../../model/world/idtable.hpp"
#include "../../model/world/commands.hpp"
void CSVWorld::RegionMap::contextMenuEvent (QContextMenuEvent *event)
{
@ -23,12 +30,28 @@ void CSVWorld::RegionMap::contextMenuEvent (QContextMenuEvent *event)
if (getMissingRegionCells().size()>0)
menu.addAction (mSelectRegionsAction);
int selectedNonExistentCells = getSelectedCells (false, true).size();
if (selectedNonExistentCells>0)
{
if (selectedNonExistentCells==1)
mCreateCellsAction->setText ("Create one cell");
else
{
std::ostringstream stream;
stream << "Create " << selectedNonExistentCells << " cells";
mCreateCellsAction->setText (QString::fromUtf8 (stream.str().c_str()));
}
menu.addAction (mCreateCellsAction);
}
menu.exec (event->globalPos());
}
QModelIndexList CSVWorld::RegionMap::getUnselectedCells() const
{
const QAbstractItemModel *model = QTableView::model();
const QAbstractItemModel *model = QTableView::model();
int rows = model->rowCount();
int columns = model->columnCount();
@ -56,6 +79,25 @@ QModelIndexList CSVWorld::RegionMap::getUnselectedCells() const
return list;
}
QModelIndexList CSVWorld::RegionMap::getSelectedCells (bool existent, bool nonExistent) const
{
const QAbstractItemModel *model = QTableView::model();
QModelIndexList selected = selectionModel()->selectedIndexes();
QModelIndexList list;
for (QModelIndexList::const_iterator iter (selected.begin()); iter!=selected.end(); ++iter)
{
bool exists = model->data (*iter, Qt::BackgroundRole)!=QBrush (Qt::DiagCrossPattern);
if ((exists && existent) || (!exists && nonExistent))
list.push_back (*iter);
}
return list;
}
QModelIndexList CSVWorld::RegionMap::getMissingRegionCells() const
{
const QAbstractItemModel *model = QTableView::model();
@ -89,15 +131,16 @@ QModelIndexList CSVWorld::RegionMap::getMissingRegionCells() const
return list;
}
CSVWorld::RegionMap::RegionMap (QAbstractItemModel *model, QWidget *parent)
: QTableView (parent)
CSVWorld::RegionMap::RegionMap (const CSMWorld::UniversalId& universalId,
CSMDoc::Document& document, QWidget *parent)
: QTableView (parent), mEditLock (false), mDocument (document)
{
verticalHeader()->hide();
horizontalHeader()->hide();
setSelectionMode (QAbstractItemView::ExtendedSelection);
setModel (model);
setModel (document.getData().getTableModel (universalId));
resizeColumnsToContents();
resizeRowsToContents();
@ -113,11 +156,15 @@ CSVWorld::RegionMap::RegionMap (QAbstractItemModel *model, QWidget *parent)
mSelectRegionsAction = new QAction (tr ("Select Regions"), this);
connect (mSelectRegionsAction, SIGNAL (triggered()), this, SLOT (selectRegions()));
addAction (mSelectRegionsAction);
mCreateCellsAction = new QAction (tr ("Create Cells Action"), this);
connect (mCreateCellsAction, SIGNAL (triggered()), this, SLOT (createCells()));
addAction (mCreateCellsAction);
}
void CSVWorld::RegionMap::setEditLock (bool locked)
{
mEditLock = locked;
}
void CSVWorld::RegionMap::selectAll()
@ -139,4 +186,31 @@ void CSVWorld::RegionMap::selectRegions()
for (QModelIndexList::const_iterator iter (unselected.begin()); iter!=unselected.end(); ++iter)
selectionModel()->select (*iter, QItemSelectionModel::Select);
}
void CSVWorld::RegionMap::createCells()
{
if (mEditLock)
return;
QModelIndexList selected = getSelectedCells (false, true);
QAbstractItemModel *regionModel = model();
CSMWorld::IdTable *cellsModel = &dynamic_cast<CSMWorld::IdTable&> (*
mDocument.getData().getTableModel (CSMWorld::UniversalId::Type_Cells));
if (selected.size()>1)
mDocument.getUndoStack().beginMacro (tr ("Create cells"));
for (QModelIndexList::const_iterator iter (selected.begin()); iter!=selected.end(); ++iter)
{
std::string cellId = regionModel->data (*iter, CSMWorld::RegionMap::Role_CellId).
toString().toUtf8().constData();
mDocument.getUndoStack().push (new CSMWorld::CreateCommand (*cellsModel, cellId));
}
if (selected.size()>1)
mDocument.getUndoStack().endMacro();
}

@ -4,7 +4,16 @@
#include <QTableView>
class QAction;
class QAbstractItemModel;
namespace CSMDoc
{
class Document;
}
namespace CSMWorld
{
class UniversalId;
}
namespace CSVWorld
{
@ -15,20 +24,28 @@ namespace CSVWorld
QAction *mSelectAllAction;
QAction *mClearSelectionAction;
QAction *mSelectRegionsAction;
QAction *mCreateCellsAction;
bool mEditLock;
CSMDoc::Document& mDocument;
private:
void contextMenuEvent (QContextMenuEvent *event);
QModelIndexList getUnselectedCells() const;
///< Note non-existent cells are not listed.
///< \note Non-existent cells are not listed.
QModelIndexList getSelectedCells (bool existent = true, bool nonExistent = false) const;
///< \param existant Include existant cells.
/// \param nonExistant Include non-existant cells.
QModelIndexList getMissingRegionCells() const;
///< Unselected cells within all regions that have at least one selected cell.
public:
RegionMap (QAbstractItemModel *model, QWidget *parent = 0);
RegionMap (const CSMWorld::UniversalId& universalId, CSMDoc::Document& document,
QWidget *parent = 0);
void setEditLock (bool locked);
@ -39,6 +56,8 @@ namespace CSVWorld
void clearSelection();
void selectRegions();
void createCells();
};
}

@ -7,7 +7,7 @@ CSVWorld::RegionMapSubView::RegionMapSubView (CSMWorld::UniversalId universalId,
CSMDoc::Document& document)
: CSVDoc::SubView (universalId)
{
mRegionMap = new RegionMap (document.getData().getTableModel (universalId), this);
mRegionMap = new RegionMap (universalId, document, this);
setWidget (mRegionMap);
}

Loading…
Cancel
Save