wxDataViewTreeCtrl - How to check if a container has child items? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by apoorv569 »

doublemax wrote: Tue Jun 01, 2021 7:09 am If this is a wxEVT_DATAVIEW_ITEM_BEGIN_DRAG handler, it should be fine.
Yes it is,

Code: Select all

    Bind(wxEVT_DATAVIEW_ITEM_BEGIN_DRAG, &MainFrame::OnDragFromSampleView, this);
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by doublemax »

Do you handle any other events from the wxDVC?
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by apoorv569 »

doublemax wrote: Tue Jun 01, 2021 8:05 am Do you handle any other events from the wxDVC?
I have as of now 5 event handlers for wxDataViewListCtrl,

Code: Select all

    Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &MainFrame::OnClickSampleView, this, BC_SampleListView);
    Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, &MainFrame::OnCheckFavorite, this, BC_SampleListView);
    Bind(wxEVT_DATAVIEW_ITEM_BEGIN_DRAG, &MainFrame::OnDragFromSampleView, this);
    m_SampleListView->Connect(wxEVT_DROP_FILES, wxDropFilesEventHandler(MainFrame::OnDragAndDropToSampleListView), NULL, this);
    Bind(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, &MainFrame::OnShowSampleListViewContextMenu, this, BC_SampleListView);
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by doublemax »

I don't think any of those can cause this issue. I'm out of ideas, i'd need some running code to play around with.
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by apoorv569 »

doublemax wrote: Tue Jun 01, 2021 8:37 am I don't think any of those can cause this issue. I'm out of ideas, i'd need some running code to play around with.
I made this small demo program for what I was trying to do, It is doing the exact thing that I mentioned in the previous post.

I made a small change in this demo program to make it work. In my original program, in the OnDragAndDropToCollectionView() I had this check,

Code: Select all

void MainFrame::OnDragAndDropToCollectionView(wxDropFilesEvent& event)
{
    if (event.GetNumberOfFiles() > 0)                 //    <========
    {
 .
 .
 .
 }
I just changed it to,

Code: Select all

void MainFrame::OnDragAndDropToCollectionView(wxDropFilesEvent& event)
{
    if (event.GetNumberOfFiles() <= 0)                 //    <========
    {
 .
 .
 .
 }
As for some reason it was always showing 0 files.
Attachments
App.hpp
(987 Bytes) Downloaded 59 times
App.cpp
(5.64 KiB) Downloaded 69 times
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by doublemax »

Especially the drop handler looked a little strange. It shouldn't care from where the files are dropped, so it should not use m_SampleListView.

Apart from that, everything seems to work fine, i could not reproduce the issue regarding the selection.
Attachments
App.hpp
(972 Bytes) Downloaded 63 times
App.cpp
(5.24 KiB) Downloaded 62 times
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by apoorv569 »

doublemax wrote: Tue Jun 01, 2021 4:37 pm Especially the drop handler looked a little strange. It shouldn't care from where the files are dropped, so it should not use m_SampleListView.

Apart from that, everything seems to work fine, i could not reproduce the issue regarding the selection.
The files you sent back, acts the same way for me as before. Also if I don't use m_SampleListView to get the files, it won't detect that I'm dragging more than 1 file, it always says I dragged 1 file. This is why I used this. I copied the exact code you sent me, it says 0 files dragged. For some reason.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by doublemax »

I guess the native control under Linux behaves differently than the generic one under Windows. Unfortunately i can only test under Windows.

At least the fact that it's impossible to drag multiple items because a single click clears the selection looks like a bug to me.
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by apoorv569 »

doublemax wrote: Tue Jun 01, 2021 8:35 pm I guess the native control under Linux behaves differently than the generic one under Windows. Unfortunately i can only test under Windows.

At least the fact that it's impossible to drag multiple items because a single click clears the selection looks like a bug to me.
What do I need to test the demo program I shared above on windows. I mean how do I set up the environment on windows for building? Will my same meson file work? I have a separate laptop that I can use to test on windows. I eventually want to make my app available on windows as well so I have to do this someday anyway. Maybe then I'll test if it indeed behaves different than what I'm seeing on Linux.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by doublemax »

There are many ways to build under Windows, try following this guide:
viewtopic.php?f=19&t=47231

If you don't want to use CodeBlocks, you can stop after chapter 3.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by ONEEYEMAN »

Hi,
You can install MSVC Community Edition for free and build everything.

1. Download the IDE from MS site and install it.
2. Start the IDE and open wxWidgets-3.1.4\build\msw\wx.sln, the latest ione available).
3. Go to "Build -> Batch Build...", click "Select ALL", then "Build". Wait for the library to build.
4. Open wxWidgets-3.1.4\samples\minimal\minimal.sln (latest one).
5. Open minimal.cpp and replace its code with the one you want to use.
6. Compile and run.

Thank you.

You can also try to compile and run the "datraview" sample on both Windows and Linux and see if the DnD operation works there. If you do - make sure you select "Multiple selection" menu.

Thank you.
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by apoorv569 »

doublemax wrote: Tue Jun 01, 2021 4:37 pm Especially the drop handler looked a little strange. It shouldn't care from where the files are dropped, so it should not use m_SampleListView.

Apart from that, everything seems to work fine, i could not reproduce the issue regarding the selection.
I have a small problem, where I check for, if the item on which the file is being dropped is a container or not, I'm calling SetToggleValue(), to toggle the column to true in wxDataViewListCtrl, but it triggers the other handler function that I have for when the column is toggled named OnCheckFavorite(), and results in duplicating the files being dropped. Here is the section from drag and drop handler,

Code: Select all

            if(drop_target.IsOk() && m_CollectionView->IsContainer(drop_target))
            {
                m_SampleListView->SetToggleValue(true, row, 0);                                 // <-----------

                m_CollectionView->AppendItem(drop_target, files[i]);

                db.UpdateFavoriteColumn(files[i].ToStdString(), 1);
                db.UpdateFavoriteFolder(files[i].ToStdString(), folder_name.ToStdString());
            }
And here is the section that adds files when the toggle column is toggled,

Code: Select all

            wxDataViewItem selected = m_CollectionView->GetSelection();
            wxString folder;

            if(selected.IsOk() && m_CollectionView->IsContainer(selected))
            {
                folder = m_CollectionView->GetItemText(selected);
                m_CollectionView->AppendItem(selected, selection);
            }
            else
            {
                folder = m_CollectionView->GetItemText(favorites_folder);
                m_CollectionView->AppendItem(favorites_folder, selection);
            }

            db.UpdateFavoriteColumn(selection.ToStdString(), 1);
            db.UpdateFavoriteFolder(selection.ToStdString(), folder.ToStdString());
As you can see, they both handle adding the file to a folder(container) differently, when toggle coumn is toggled, it checks for the currently selected item in wxDataViewTreeCtrl if nothing is selected they go in a default folder I made, and the drag and drop handler adds by checking position and what container the files are dropped on. Is there a way I can stop triggering the handler for when the toggle column value is changed in my drag and drop handler, i.e when I call SetToggleValue()?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by doublemax »

Does it even make sense in your application to have duplicate entries in the list? If not, you should just check for that, and it will solve this problem.
Use the source, Luke!
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by apoorv569 »

doublemax wrote: Sat Jun 05, 2021 10:18 pm Does it even make sense in your application to have duplicate entries in the list? If not, you should just check for that, and it will solve this problem.
I am already checking for duplicates before adding a item, what is happening here, is say I have 3 folders, A, B and C, A is default folder that is there and cannot be deleted, B and C are user created folders. I have set it up as such if user drags and drops a file on empty area in wxDataViewTreeCtrl, it won't let the user add, and if the user toggles the toggle column if no folder is selected, it will go to folder A. But if user drags and drops on folder B or C it also gets added to B/C and A, because it triggers the other method and that folder does not have that file. I cannot add the same file to either A or B after this. This gets fixed when the app is closed and reopened again, as I am storing which folder the file goes to in database so it loads back in that folder when the app is opened again.

Code: Select all

        if (found_item.IsOk())
        {
            wxString msg = wxString::Format("%s already added as favorite.", selection);
            wxMessageDialog msgDialog(NULL, msg, "Info", wxOK | wxICON_INFORMATION);
            msgDialog.ShowModal();
        }
        else
        {
            wxLogDebug("Sample not found adding as fav.");

            wxDataViewItem selected = m_CollectionView->GetSelection();
            wxString folder;

            if(selected.IsOk() && m_CollectionView->IsContainer(selected))
            {
                folder = m_CollectionView->GetItemText(selected);
                m_CollectionView->AppendItem(selected, selection);
            }
            else
            {
                folder = m_CollectionView->GetItemText(favorites_folder);
                m_CollectionView->AppendItem(favorites_folder, selection);
            }

            db.UpdateFavoriteColumn(selection.ToStdString(), 1);
            db.UpdateFavoriteFolder(selection.ToStdString(), folder.ToStdString());
Here is a GIF as a example,
Image

When I drag the file to the folder Bass, it gets added to folder Synth also, because it was selected the and SetToggleValue() triggered the OnCheckFavorite(), and it adds to whatever item is selected. But after closing and reopening the app, it is fixed, and when I drag a file to folder Bass, it gets added to Favorites also, because of that when no folder is selected add to the default folder.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewTreeCtrl - How to check if a container has child items?

Post by doublemax »

What exactly is the meaning of the toggle column? If it just means "this file is in any favorite folder", then it should not be toggleable. It should just be an indicator. IMHO

(Going offline now, no more answers today ;) )
Use the source, Luke!
Post Reply