List view

Started by CatalinNic, 04 September 2019, 20:13:07

CatalinNic

I want to create a list view and at this moment there are no tutorials covering TGUI and the question is: How I connect each individual items of a list view? For example I wrote this code:
Code (cpp) Select

void Editor::Open_dir(tgui::Gui& Editor_gui)
{
    tgui::ListView::Ptr file_listview = tgui::ListView::create();
    std::string path = "Maps";
    for(const std::filesystem::directory_entry& files : std::filesystem::directory_iterator(path))
    {
        file_listview->addItem(files.path().c_str());
        file_listview->connect("pressed", Open_file /*, ...*/);
    }     
}

or is this incorrect?

texus

The ListView has a "ItemSelected" signal that gets called when an item is highlighted and a "DoubleClicked" for when double clicking an item. If the Open_file function has an int parameter then it will contain the index of the selected item.

Kvaz1r

There is a documentation, so you can find there all available signals in section Public Attributes. 

CatalinNic

So, what are you saying is that the connect function will just do its thing with the item that was just added?

Kvaz1r

#4
Yes. Here simple example how you can get item that was clicked, my compiler still doesn't recognize that filesystem is part of std so sorry about that  :)

#include <TGUI/TGUI.hpp>

#include <string>
#include <iostream>
#include <filesystem>

int main()
{
sf::RenderWindow window(sf::VideoMode(400, 300), "TGUI window");
tgui::Gui gui(window);

auto panel = tgui::ScrollablePanel::create();
auto listView = tgui::ListView::create();
listView->setExpandLastColumn(true);
listView->connect("ItemSelected", [listView](int id) {
std::cout << "Click on: ";
for (const auto& el : listView->getItemRow(id))
{
std::cout << std::string(el) << ' ';
}
std::cout << "\n\n";
});

listView->setSize({ "100%,100%" });
listView->addColumn(L"   ID   ");
listView->addColumn(L"   File   ");

std::string path = ".";
auto i = 0;
for (const auto& entry : std::experimental::filesystem::directory_iterator(path))
{
listView->addItem({ std::to_string(++i), entry.path().generic_string() });
}
panel->add(listView);
gui.add(panel);

// Main loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// When the window is closed, the application ends
if (event.type == sf::Event::Closed)
window.close();

// When the window is resized, the view is changed
else if (event.type == sf::Event::Resized)
{
window.setView(sf::View(sf::FloatRect(0.f, 0.f, static_cast<float>(event.size.width), static_cast<float>(event.size.height))));
gui.setView(window.getView());
}

// Pass the event to all the widgets
gui.handleEvent(event);
}

window.clear();

// Draw all created widgets
gui.draw();

window.display();
}

return EXIT_SUCCESS;
}


CatalinNic

#5
You need to use std=c++17 flag in order to compile and thanks