This commit is contained in:
bubnikv 2018-12-12 10:02:10 +01:00
commit c6bdf45323
4 changed files with 51 additions and 33 deletions

View File

@ -78,6 +78,10 @@ ObjectList::ObjectList(wxWindow* parent) :
Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, &ObjectList::ItemValueChanged, this);
Bind(wxCUSTOMEVT_LAST_VOLUME_IS_DELETED, [this](wxCommandEvent& e) { last_volume_is_deleted(e.GetInt()); });
#ifdef __WXOSX__
Bind(wxEVT_KEY_DOWN, &ObjectList::OnChar, this);
#endif //__WXOSX__
}
ObjectList::~ObjectList()
@ -93,10 +97,6 @@ void ObjectList::create_objects_ctrl()
// 2. change it to the normal min value (200) after first whole App updating/layouting
SetMinSize(wxSize(-1, 1500)); // #ys_FIXME
#ifdef __WXOSX__
Connect(wxEVT_CHAR, wxKeyEventHandler(ObjectList::OnChar), NULL, this);
#endif //__WXOSX__
m_sizer = new wxBoxSizer(wxVERTICAL);
m_sizer->Add(this, 1, wxGROW | wxLEFT, 20);
@ -337,13 +337,14 @@ void ObjectList::selection_changed()
void ObjectList::OnChar(wxKeyEvent& event)
{
if (event.GetKeyCode() == WXK_DELETE && event.GetKeyCode() == WXK_BACK){
printf("KeyDown event\n");
if (event.GetKeyCode() == WXK_BACK){
printf("WXK_BACK\n");
remove();
}
else if (wxGetKeyState(wxKeyCode('A')) && wxGetKeyState(WXK_SHIFT))
select_item_all_children();
else
event.Skip();
}
@ -424,12 +425,6 @@ void ObjectList::key_event(wxKeyEvent& event)
event.Skip();
}
struct draging_item_data
{
int obj_idx;
int vol_idx;
};
void ObjectList::OnBeginDrag(wxDataViewEvent &event)
{
wxDataViewItem item(event.GetItem());
@ -449,10 +444,10 @@ void ObjectList::OnBeginDrag(wxDataViewEvent &event)
**/
m_prevent_list_events = true;//it's needed for GTK
wxTextDataObject *obj = new wxTextDataObject;
obj->SetText(wxString::Format("%d", m_objects_model->GetVolumeIdByItem(item)));
event.SetDataObject(obj);
event.SetDragFlags(/*wxDrag_AllowMove*/wxDrag_DefaultMove); // allows both copy and move;
m_dragged_data.init(m_objects_model->GetObjectIdByItem(item), m_objects_model->GetVolumeIdByItem(item));
event.SetDataObject(new wxTextDataObject);
event.SetDragFlags(wxDrag_DefaultMove); // allows both copy and move;
}
void ObjectList::OnDropPossible(wxDataViewEvent &event)
@ -460,8 +455,10 @@ void ObjectList::OnDropPossible(wxDataViewEvent &event)
wxDataViewItem item(event.GetItem());
// only allow drags for item or background, not containers
if (event.GetDataFormat() != wxDF_UNICODETEXT || item.IsOk() &&
(m_objects_model->GetParent(item) == wxDataViewItem(0) || m_objects_model->GetItemType(item) != itVolume))
if (item.IsOk() &&
(m_objects_model->GetParent(item) == wxDataViewItem(0) ||
m_objects_model->GetItemType(item) != itVolume ||
m_dragged_data.obj_idx() != m_objects_model->GetObjectIdByItem(item)))
event.Veto();
}
@ -469,20 +466,16 @@ void ObjectList::OnDrop(wxDataViewEvent &event)
{
wxDataViewItem item(event.GetItem());
if (m_selected_object_id < 0 || event.GetDataFormat() != wxDF_UNICODETEXT ||
item.IsOk() && ( m_objects_model->GetParent(item) == wxDataViewItem(0) ||
m_objects_model->GetItemType(item) != itVolume) ) {
if (item.IsOk() && ( m_objects_model->GetParent(item) == wxDataViewItem(0) ||
m_objects_model->GetItemType(item) != itVolume) ||
m_dragged_data.obj_idx() != m_objects_model->GetObjectIdByItem(item)) {
event.Veto();
m_dragged_data.clear();
return;
}
wxTextDataObject obj;
obj.SetData(wxDF_UNICODETEXT, event.GetDataSize(), event.GetDataBuffer());
printf("Drop\n");
int from_volume_id = std::stoi(obj.GetText().ToStdString());
const int from_volume_id = m_dragged_data.vol_idx();
int to_volume_id = m_objects_model->GetVolumeIdByItem(item);
printf("from %d to %d\n", from_volume_id, to_volume_id);
#ifdef __WXGTK__
/* Under GTK, DnD moves an item between another two items.
@ -497,16 +490,14 @@ void ObjectList::OnDrop(wxDataViewEvent &event)
int cnt = 0;
for (int id = from_volume_id; cnt < abs(from_volume_id - to_volume_id); id += delta, cnt++)
std::swap(volumes[id], volumes[id + delta]);
printf("Volumes are swapped\n");
select_item(m_objects_model->ReorganizeChildren(from_volume_id, to_volume_id,
m_objects_model->GetParent(item)));
printf("ItemChildren are Reorganized\n");
m_parts_changed = true;
parts_changed(m_selected_object_id);
printf("DropCompleted\n");
m_dragged_data.clear();
// m_prevent_list_events = false;
}

View File

@ -53,6 +53,27 @@ struct ItemForDelete
class ObjectList : public wxDataViewCtrl
{
struct dragged_item_data
{
void init(const int obj_idx, const int vol_idx) {
m_obj_idx = obj_idx;
m_vol_idx = vol_idx;
}
void clear() {
m_obj_idx = -1;
m_vol_idx = -1;
}
int obj_idx() const { return m_obj_idx; }
int vol_idx() const { return m_vol_idx; }
private:
int m_obj_idx = -1;
int m_vol_idx = -1;
} m_dragged_data;
wxBoxSizer *m_sizer {nullptr};
DynamicPrintConfig *m_default_config {nullptr};

View File

@ -891,7 +891,7 @@ wxDataViewItem PrusaObjectDataViewModel::GetItemByInstanceId(int obj_idx, int in
return wxDataViewItem(0);
}
int PrusaObjectDataViewModel::GetIdByItem(const wxDataViewItem& item)
int PrusaObjectDataViewModel::GetIdByItem(const wxDataViewItem& item) const
{
wxASSERT(item.IsOk());
@ -913,6 +913,11 @@ int PrusaObjectDataViewModel::GetIdByItemAndType(const wxDataViewItem& item, con
return node->GetIdx();
}
int PrusaObjectDataViewModel::GetObjectIdByItem(const wxDataViewItem& item) const
{
return GetIdByItem(GetTopParent(item));
}
int PrusaObjectDataViewModel::GetVolumeIdByItem(const wxDataViewItem& item) const
{
return GetIdByItemAndType(item, itVolume);

View File

@ -457,8 +457,9 @@ public:
wxDataViewItem GetItemById(int obj_idx);
wxDataViewItem GetItemByVolumeId(int obj_idx, int volume_idx);
wxDataViewItem GetItemByInstanceId(int obj_idx, int inst_idx);
int GetIdByItem(const wxDataViewItem& item);
int GetIdByItem(const wxDataViewItem& item) const;
int GetIdByItemAndType(const wxDataViewItem& item, const ItemType type) const;
int GetObjectIdByItem(const wxDataViewItem& item) const;
int GetVolumeIdByItem(const wxDataViewItem& item) const;
int GetInstanceIdByItem(const wxDataViewItem& item) const;
void GetItemInfo(const wxDataViewItem& item, ItemType& type, int& obj_idx, int& idx);