Skip to content
Commits on Source (2)
......@@ -100,6 +100,8 @@ class OE_controller
bool clearSelection();
BoundingBox getSelectionBoundingBox();
bool moveInListSelectedStitches(int posPrevious);
OE_actions* getLastAction();
bool editActionMovePointCurve(vector_2d offset);
bool editActionMovePointLinkStitch(vector_2d offset);
......
......@@ -152,4 +152,21 @@ class OE_actionSetSelectedStitchPattern : public OE_actions
OE_actionSetSelectedFillStitchPattern* actionSetSelectedFillStitchPattern;
};
class OE_actionMoveInListSelectedStitches : public OE_actions
{
public:
OE_actionMoveInListSelectedStitches(OE_document* doc, int posInsertion);
virtual ~OE_actionMoveInListSelectedStitches();
virtual void undo();
virtual void redo();
protected:
std::list<OE_stitchs*> actionStitches;
std::list<OE_linkstitch*> newLinkStitches;
std::list<OE_linkstitch*> oldLinkStitches;
void startNewBlock(std::list<OE_stitchs*> &list, OE_stitchs* stitch);
};
......@@ -458,6 +458,16 @@ BoundingBox OE_controller::getSelectionBoundingBox()
return boundingBox;
}
bool OE_controller::moveInListSelectedStitches(int posPrevious)
{
if (document)
{
addAction(new OE_actionMoveInListSelectedStitches(document, posPrevious));
return true;
}
return false;
}
OE_actions* OE_controller::getLastAction()
{
if (document && document->activeActionsStack.size())
......
......@@ -357,7 +357,7 @@ void OE_interfaceDisplay::key(QKeyEvent* event)
else if (event->key() == Qt::Key_Delete)
{
//TODO create an action to delete all selected stitches
std::list<OE_stitchs*>::iterator it=selectedStitches.begin();
std::list<OE_stitchs*>::iterator it=document->selectedStitchs.begin();
controller->addAction(new OE_actionDelStitch(document, *it));
update();
emit refreshStitchList();
......
......@@ -521,3 +521,162 @@ void OE_actionSetSelectedStitchPattern::redo()
active = true;
}
}
///////// OE_actionShiftSelectedStitches ///////////////
OE_actionMoveInListSelectedStitches::OE_actionMoveInListSelectedStitches(OE_document* doc, int posInsertion) : OE_actions(doc)
{
if (document)
{
actionStitches = document->stitchs;
std::list<OE_stitchs*> tmpBeforeStitchList;
std::list<OE_stitchs*> tmpAfterStitchList;
std::list<OE_stitchs*> tmpSelectedStitchList;
bool selected = false;
int curIndex = 0;
// to always get good insertion point (just after a linkstitches)
posInsertion += posInsertion & 1;
//loop on every stitches, split before and after insertion and make a lists with selected ones
for (std::list<OE_stitchs*>::iterator stitchIt=document->stitchs.begin(); stitchIt!=document->stitchs.end(); ++stitchIt)
{
curIndex++;
OE_linkstitch* linkStitch = dynamic_cast<OE_linkstitch*>(*stitchIt);
if (!linkStitch)
{
//check if the stitch is selected
auto it = std::find(document->selectedStitchs.begin(), document->selectedStitchs.end(), *stitchIt);
if(it != document->selectedStitchs.end())
{
//if we start a new selected block
if (selected == false)
startNewBlock(tmpSelectedStitchList, *stitchIt);
selected = true;
}
else
{
//if we start a new unselected block
if (selected == true)
{
if (curIndex <= posInsertion)
startNewBlock(tmpBeforeStitchList, *stitchIt);
else
startNewBlock(tmpAfterStitchList, *stitchIt);
}
selected = false;
}
}
//add the stitch to the corresponding list
if (selected)
tmpSelectedStitchList.push_back(*stitchIt);
else
{
if (curIndex <= posInsertion) //if we're before the insertion point
tmpBeforeStitchList.push_back(*stitchIt);
else
tmpAfterStitchList.push_back(*stitchIt);
}
}
//remove last linkstitch in each lists if needed
OE_linkstitch* linkStitch = dynamic_cast<OE_linkstitch*>(tmpBeforeStitchList.back());
if (linkStitch)
{
document->selectedStitchs.remove(linkStitch);
oldLinkStitches.push_back(linkStitch);
tmpBeforeStitchList.pop_back();
}
linkStitch = dynamic_cast<OE_linkstitch*>(tmpAfterStitchList.back());
if (linkStitch)
{
document->selectedStitchs.remove(linkStitch);
oldLinkStitches.push_back(linkStitch);
tmpAfterStitchList.pop_back();
}
linkStitch = dynamic_cast<OE_linkstitch*>(tmpSelectedStitchList.back());
if (linkStitch)
{
document->selectedStitchs.remove(linkStitch);
oldLinkStitches.push_back(linkStitch);
tmpSelectedStitchList.pop_back();
}
//concatenate the lists
if (tmpSelectedStitchList.size()>0)
{
if (tmpBeforeStitchList.size()>0)
{
OE_linkstitch* link = new OE_linkstitch(tmpBeforeStitchList.back(),tmpSelectedStitchList.front());
newLinkStitches.push_back(link);
tmpBeforeStitchList.push_back(link);
}
tmpBeforeStitchList.insert(tmpBeforeStitchList.end(),tmpSelectedStitchList.begin(),tmpSelectedStitchList.end());
if (tmpAfterStitchList.size()>0)
{
OE_linkstitch* link = new OE_linkstitch(tmpBeforeStitchList.back(),tmpAfterStitchList.front());
newLinkStitches.push_back(link);
tmpBeforeStitchList.push_back(link);
tmpBeforeStitchList.insert(tmpBeforeStitchList.end(),tmpAfterStitchList.begin(),tmpAfterStitchList.end());
}
//then replace stitchlist in the document
document->stitchs = tmpBeforeStitchList;
}
}
}
void OE_actionMoveInListSelectedStitches::startNewBlock(std::list<OE_stitchs*> &list, OE_stitchs* stitch)
{
if (list.size()!=0)
{
//remove previous linkstitch
OE_linkstitch* linkStitch = dynamic_cast<OE_linkstitch*>(list.back());
document->selectedStitchs.remove(linkStitch);
oldLinkStitches.push_back(linkStitch);
list.pop_back();
//and make a new one
OE_linkstitch* link = new OE_linkstitch(list.back(),stitch);
newLinkStitches.push_back(link);
list.push_back(link);
}
}
OE_actionMoveInListSelectedStitches::~OE_actionMoveInListSelectedStitches()
{
if (active)
{
for(auto&& stitch : oldLinkStitches) {
delete stitch;
}
}else
{
for(auto&& stitch : newLinkStitches) {
delete stitch;
}
}
}
void OE_actionMoveInListSelectedStitches::undo()
{
if (active)
{
document->stitchs = switchVal(document->stitchs, &actionStitches);
active = false;
}
}
void OE_actionMoveInListSelectedStitches::redo()
{
if (!active)
{
document->stitchs = switchVal(document->stitchs, &actionStitches);
active = true;
}
}
......@@ -152,6 +152,19 @@ void OE_ui_stitchList::handleSelectionChanged()
getInterfaceDisplay()->update();
}
void OE_ui_stitchList::dropEvent(QDropEvent *event)
{
QModelIndex index = indexAt(event->pos());
DropIndicatorPosition pos = this->dropIndicatorPosition();
if (pos == DropIndicatorPosition::BelowItem)
getController()->moveInListSelectedStitches(index.row()+1);
else
getController()->moveInListSelectedStitches(index.row());
getInterfaceDisplay()->update();
refreshStitches();
}
void OE_ui_stitchList::keyPressEvent(QKeyEvent *event)
{
if(event->key()==Qt::Key_Right) {
......
......@@ -29,6 +29,8 @@ private:
void addItem(QColor color, QIcon icon, QIcon type, QIcon pattern, int stitchCount);
void addItem(QColor color, int stitchCount);
void keyPressEvent(QKeyEvent *event);
protected:
void dropEvent(QDropEvent *event);
private slots:
void on_refreshStitchList();
void on_refreshStitch(OE_stitchs* stitch);
......