LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2021, 08:04 PM   #1
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Rep: Reputation: 0
Question How to iterate over wxDataViewTreeCtrl items and serialize them to a file?


I have a application that I'm making with wxWidgets which plays audio samples and helps organize them. I have a wxDataViewListCtrl which has several column which holds information about the audio file like filename, path, length, sample rate and all, it also has a toggle column, that when clicked, should add that item to wxDataViewTreeCtrl, and remove when toggled off. I have the wxDataViewTreeCtrl setup as,
Code:
    CollectionView = new wxDataViewTreeCtrl(ViewChoice, BCID_CollectionView, wxDefaultPosition, wxDefaultSize, wxDV_NO_HEADER);
And I added a root node to begin with,
Code:
    wxDataViewItem RootNode = CollectionView->AppendContainer(wxDataViewItem(0), "ROOT", 0);
And this function should add that item to wxDataViewTreeCtrl, SampleListView is wxDataViewListCtrl here,
Code:
void Browser::OnCheckFavorite(wxDataViewEvent& event)
{
    // int selectedRow = SampleListView->GetSelectedRow();
    int selectedRow = SampleListView->ItemToRow(event.GetItem());

    if (selectedRow < 0) return;

    wxString Selection = SampleListView->GetTextValue(selectedRow, 1);

    std::deque<wxDataViewItem> nodes;
    nodes.push_back(RootNode);

    wxDataViewItem foundItem;

    if (SampleListView->GetToggleValue(selectedRow, 0))
    {
        wxLogDebug("Toggle on");

        while(!nodes.empty())
        {
            wxDataViewItem currentItem = nodes.front();
            nodes.pop_front();

            if ( CollectionView->GetItemText(currentItem) == Selection )
            {
                foundItem = currentItem;
                wxLogDebug(CollectionView->GetItemText(currentItem));
                wxLogDebug("Toggle selection: %s", CollectionView->GetItemText(CollectionView->GetSelection()));
                break;
            }

            int row = 0;
            wxDataViewItem child = CollectionView->GetNthChild(currentItem, row);

            while ( child.IsOk() )
            {
                nodes.push_back(child);
                child = CollectionView->GetNthChild(currentItem, row + 1);
                row ++;
            }
        }

        nodes.clear();

        if (foundItem.IsOk())
        {
            const wxString& msg = wxString::Format("%s already added as favorite. Skipping..", Selection);
            wxMessageDialog *msgDialog = new wxMessageDialog(NULL, msg, "Info", wxOK | wxICON_INFORMATION);
            msgDialog->ShowModal();
        }
        else
        {
            wxLogDebug("Sample not found adding as fav.");

            wxDataViewItem selected = CollectionView->GetSelection();
            wxString folder = CollectionView->GetItemText(selected);

            wxLogDebug("Selected: %s", folder);

            CollectionView->AppendItem(RootNode, Selection);

            std::string path = "/home/apoorv/repos/cpp-projects/wxWidgets/SampleBrowser/tree.yaml";

            serialize.SerializeDataViewTreeCtrlItems(path, *CollectionView, RootNode);

            db.UpdateFavoriteColumn(Selection.ToStdString(), 1);
            // db.UpdateFavoriteFolderDatabase(Selection.ToStdString(), folder.ToStdString());
        }
    }
And this code is responsible for serializing the tree items to a yaml file, so I save and load the data back to the application,
Code:
void Serializer::SerializeDataViewTreeCtrlItems(std::string filepath, wxDataViewTreeCtrl& tree, wxDataViewItem& item)
{
    std::ifstream ifin(filepath);

    YAML::Emitter out;

    out << YAML::BeginMap; // Container
    out << YAML::Key << "Container" << YAML::Value << "";

    if (!tree.IsContainer(item))
    {
        out << YAML::Key << "Child";
        out << YAML::BeginMap; // Child

        for ( int i = 0; i < tree.GetChildCount(item); i++ )
        {
            wxString child = tree.GetItemText(tree.GetNthChild(item, i));
            out << YAML::Key << "Item" << YAML::Value << child.ToStdString();
        }

        out << YAML::EndMap; // Child
    }

    out << YAML::EndMap; // Container

    std::ofstream ofout(filepath);
    ofout << out.c_str();
}
But running this code keeps giving me error about invalid index and nothing gets output to the yaml file,
Code:
./src/common/list.cpp(317): assert "Assert failure" failed in Item(): invalid index in wxListBase::Item
What am I doing wrong here?

Last edited by apoorv569; 02-14-2021 at 08:06 PM.
 
Old 02-15-2021, 02:24 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,276
Blog Entries: 24

Rep: Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224
Have you investigated why the assertion is failing? That is the most obvious place to start, but you have not included that important part in the code you have posted, so no one here can know what is happening.

What have you done to investigate why that index is invalid?

Based only on the description you have provided (not the code) I would ask:

* Why is the assertion failing? Did you try to find out?

Then in order until you find a problem...

* Is the file being correctly loaded by the application? How do you know?
* Is the exported file format valid? How do you know?
* Is the tree being properly serialized? How do you know?
* Is the item being added to the tree? How do you know?
* Is the root node being created as expected? How do you know?

Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!

Last edited by astrogeek; 02-15-2021 at 02:28 PM. Reason: tpoy
 
1 members found this post helpful.
Old 02-19-2021, 12:47 AM   #3
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by astrogeek View Post
Have you investigated why the assertion is failing? That is the most obvious place to start, but you have not included that important part in the code you have posted, so no one here can know what is happening.

What have you done to investigate why that index is invalid?

Based only on the description you have provided (not the code) I would ask:

* Why is the assertion failing? Did you try to find out?

Then in order until you find a problem...

* Is the file being correctly loaded by the application? How do you know?
* Is the exported file format valid? How do you know?
* Is the tree being properly serialized? How do you know?
* Is the item being added to the tree? How do you know?
* Is the root node being created as expected? How do you know?

Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!
If you can spot the mistake please point me towards it, you don't have to give me an exact fix for the problem.
 
Old 02-19-2021, 01:49 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,276
Blog Entries: 24

Rep: Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224
I have no idea as stated above. That failed assertion is the clue, but I cannot see it or test it, only the person sitting at the keyboard can do that.

Again, I suggest you start with the failed assertion and then try to answer the questions I posed in the order listed, that will lead you to the cause.

So try to answer that one: Why is that assertion failing? What condition does it assert? What is the state of the relevant variables immediately before the assertion? Add a temporary out << to test that state ahead of the assertion and investigate the thing that is failing. That will always lead you to the cause.
 
  


Reply

Tags
c++, serialization, tree, wxwidgets, yaml



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterate through all blocks in an ext4 fs and zero them out LinuxMinx Linux - Kernel 2 04-05-2019 06:26 AM
[SOLVED] How to make a WHILE loop iterate over script until user input stops hebruiser Programming 3 03-05-2018 08:31 PM
API to iterate over available network connections gr3ymatt3r Programming 1 03-21-2009 09:43 AM
Iterate over SQL results in bash script. Is there a better way to do this? word_virus Programming 4 11-09-2008 10:15 AM
how can I iterate over csv values using Ruby Joseph Schiller Programming 1 02-20-2006 08:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration