listview and add item

Started by billarhos, 24 February 2019, 16:03:52

billarhos

Check this out



#if defined NDEBUG
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#endif

#define USE_TGUI
#include "SFML_libs.hpp"

#include <TGUI/TGUI.hpp>

using namespace std;

bool running = true;

//https://scitechdaily.com/chandra-solves-the-universes-missing-mass-problem/
const std::string dataStr =
{
"New results from NASA’s Chandra X - ray Observatory may have helped solve the Universe’s “missing mass” problem. Astronomers cannot account for about a third of the normal matter â€" that is, hydrogen, helium, and other elements â€" that were created in the first billion years or so after the Big Bang."
"Scientists have proposed that the missing mass could be hidden in gigantic strands or filaments of warm(temperature less than 100,000 Kelvin) and hot(temperature greater than 100,000 K) gas in intergalactic space.These filaments are known by astronomers as the “warm - hot intergalactic medium” or WHIM."
"They are invisible to optical light telescopes, but some of the warm gas in filaments has been detected in ultraviolet light.The main part of this graphic is from the Millennium simulation, which uses supercomputers to formulate how the key components of the Universe, including the WHIM, would have evolved over cosmic time."
"If these filaments exist, they could absorb certain types of light such as X - rays that pass through them.The inset in this graphic represents some of the X - ray data collected by Chandra from a distant, rapidly - growing supermassive black hole known as a quasar.The plot is a spectrum â€" the amount of X - rays over a range"
"of wavelengths â€" from a new study of the quasar H1821 + 643 that is located about 3.4 billion light years from Earth."
"The latest result uses a new technique that both hones the search for the WHIM carefully and boosts the relatively weak absorption signature by combining different parts of the spectrum to find a valid signal.With this technique, researchers identified 17 possible filaments lying between the quasar and Earth, and obtained their distances."
};

bool matchCase(false);
bool matchWholeWord(false);

std::string::size_type  search_Index = std::string::npos;

std::string lower_string(const std::string& str)
{
string lower;
if (matchCase)
lower = str;
else
transform(str.begin(), str.end(), std::back_inserter(lower), tolower);
return lower;
}

std::string::size_type find_str_ci(const std::string& str, const std::string& substr)
{
if (matchWholeWord)
{
string findStr = " " + substr + " ";
return lower_string(str).find(lower_string(findStr));
}
return lower_string(str).find(lower_string(substr));
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "TGUI window");
window.setFramerateLimit(60);

tgui::Gui gui(window);

auto pTextBox = tgui::TextBox::create();
pTextBox->setPosition(50, 100);
pTextBox->setSize(700, 470);
pTextBox->setTextSize(16);
pTextBox->setText(dataStr);
gui.add(pTextBox);

auto pSearchTextBox = tgui::TextBox::create();
pSearchTextBox->setPosition(50, 50);
pSearchTextBox->setSize(200, 30);
pSearchTextBox->setTextSize(16);

pSearchTextBox->connect("TextChanged", [&]()
{
search_Index = find_str_ci(dataStr.c_str(), pSearchTextBox->getText().toAnsiString().c_str());

if (search_Index != std::string::npos)
{
pTextBox->setSelectedText(matchWholeWord ? search_Index + 1: search_Index, matchWholeWord ? search_Index + pSearchTextBox->getText().toAnsiString().size() + 1 : search_Index + pSearchTextBox->getText().toAnsiString().size());
}
else
{
pTextBox->setSelectedText(0, 0);
}
});
gui.add(pSearchTextBox);

auto pCheckBoxCase = tgui::CheckBox::create();
pCheckBoxCase->setPosition(300, 50);
pCheckBoxCase->setText("Match case");
pCheckBoxCase->setSize(25, 25);
pCheckBoxCase->connect("Changed", [&]()
{
matchCase = !matchCase;
pTextBox->setSelectedText(0, 0);
pSearchTextBox->setText("");
});
gui.add(pCheckBoxCase);

auto pCheckBoxWord = tgui::CheckBox::create();
pCheckBoxWord->setPosition(470, 50);
pCheckBoxWord->setText("Match whole word");
pCheckBoxWord->setSize(25, 25);
pCheckBoxWord->connect("Changed", [&]()
{
matchWholeWord = !matchWholeWord;
pTextBox->setSelectedText(0, 0);
pSearchTextBox->setText("");
});

gui.add(pCheckBoxWord);

while (running)
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
{
running = false;
}
break;
case sf::Event::MouseButtonPressed:
{

}
break;
case sf::Event::KeyPressed:
{
if (event.key.code == sf::Keyboard::Escape)
running = false;
break;
}
}

gui.handleEvent(event);
}

window.clear(sf::Color(200, 200, 200, 255));
gui.draw();
window.display();
}

return EXIT_SUCCESS;
}

texus

Looks good. Seems like setSelectedText is indeed enough to implement searching and basic highlighting in your own code.

I fixed the issue with right click (which was actually just undefined behavior when removing an item while the mouse hovered over the last item in the list).