void ribi::cmap::QtExamplesItem::SetExamples(const boost::shared_ptr<const cmap::Examples>& examples) { std::vector<std::string> v; for (const boost::shared_ptr<const cmap::Example> example: examples->Get()) { const std::string s { example->GetText() }; const std::size_t wordwrap_length = 40; const std::vector<std::string> w { Wordwrap(s,wordwrap_length) }; std::copy(w.begin(),w.end(),std::back_inserter(v)); } this->SetText(v); }
void ribi::pvdb::TestHelperFunctions() { { static bool is_tested = false; if (is_tested) return; is_tested = true; } TRACE("Started TestHelperFunctions"); //GetRegexMatches { const std::string s = "In the Netherlands, 1234 AB and 2345 BC are valid zip codes"; std::vector<std::string> expected; expected.push_back("1234 AB"); expected.push_back("2345 BC"); { const std::string r = "(\\d{4} [A-Z]{2})"; assert(pvdb::GetRegexMatches(s,QRegExp(r.c_str())) == expected); } } { const std::string s = "<concept><name>Concept with examples</name><example>Example 1</example><example>Example 2</example><example>Example 3</example></concept>"; assert(std::count(s.begin(),s.end(),'\b') == 0); std::vector<std::string> expected; expected.push_back("<example>Example 1</example>"); expected.push_back("<example>Example 2</example>"); expected.push_back("<example>Example 3</example>"); { const std::string r = "(<example>.*</example>)"; assert(pvdb::GetRegexMatches(s,QRegExp(r.c_str())) == expected); } } //GetCombinations //Assume the number of elements is correct assert(GetCombinations(std::vector<int>( { } ) ).size() == 1); assert(GetCombinations(std::vector<int>( {1 } ) ).size() == 2); assert(GetCombinations(std::vector<int>( {1,2 } ) ).size() == 4); assert(GetCombinations(std::vector<int>( {1,2,3 } ) ).size() == 8); assert(GetCombinations(std::vector<int>( {1,2,3,4 } ) ).size() == 16); assert(GetCombinations(std::vector<int>( {1,2,3,4,5} ) ).size() == 32); //Assume the elements are correct { const std::vector<std::vector<int> > v = GetCombinations(std::vector<int>( { 1 } ) ); const std::vector<int> expected_0 = {}; const std::vector<int> expected_1 = {1}; assert(std::count(v.begin(),v.end(),expected_0)); assert(std::count(v.begin(),v.end(),expected_1)); } { const std::vector<std::vector<int> > v = GetCombinations(std::vector<int>( { 1,2 } ) ); const std::vector<int> expected_0 = {}; const std::vector<int> expected_1 = {1}; const std::vector<int> expected_2 = {2}; const std::vector<int> expected_3 = {1,2}; assert(std::count(v.begin(),v.end(),expected_0)); assert(std::count(v.begin(),v.end(),expected_1)); assert(std::count(v.begin(),v.end(),expected_2)); assert(std::count(v.begin(),v.end(),expected_3)); } { const std::vector<std::vector<int> > v = GetCombinations(std::vector<int>( { 1,2,3 } ) ); const std::vector<int> expected_0 = {}; const std::vector<int> expected_1 = {1}; const std::vector<int> expected_2 = {2}; const std::vector<int> expected_3 = {3}; const std::vector<int> expected_4 = {1,2}; const std::vector<int> expected_5 = {1,3}; const std::vector<int> expected_6 = {2,3}; const std::vector<int> expected_7 = {1,2,3}; assert(std::count(v.begin(),v.end(),expected_0)); assert(std::count(v.begin(),v.end(),expected_1)); assert(std::count(v.begin(),v.end(),expected_2)); assert(std::count(v.begin(),v.end(),expected_3)); assert(std::count(v.begin(),v.end(),expected_4)); assert(std::count(v.begin(),v.end(),expected_5)); assert(std::count(v.begin(),v.end(),expected_6)); assert(std::count(v.begin(),v.end(),expected_7)); } { const std::vector<std::vector<int> > v = GetCombinations(std::vector<int>( { 1,2,3,4 } ) ); const std::vector<int> expected_0 = {}; const std::vector<int> expected_1 = {1}; const std::vector<int> expected_2 = {2}; const std::vector<int> expected_3 = {3}; const std::vector<int> expected_4 = {4}; const std::vector<int> expected_5 = {1,2}; const std::vector<int> expected_6 = {1,3}; const std::vector<int> expected_7 = {1,4}; const std::vector<int> expected_8 = {2,3}; const std::vector<int> expected_9 = {2,4}; const std::vector<int> expected_10 = {3,4}; const std::vector<int> expected_11 = {1,2,3}; const std::vector<int> expected_12 = {1,2,4}; const std::vector<int> expected_13 = {1,3,4}; const std::vector<int> expected_14 = {2,3,4}; const std::vector<int> expected_15 = {1,2,3,4}; assert(std::count(v.begin(),v.end(),expected_0)); assert(std::count(v.begin(),v.end(),expected_1)); assert(std::count(v.begin(),v.end(),expected_2)); assert(std::count(v.begin(),v.end(),expected_3)); assert(std::count(v.begin(),v.end(),expected_4)); assert(std::count(v.begin(),v.end(),expected_5)); assert(std::count(v.begin(),v.end(),expected_6)); assert(std::count(v.begin(),v.end(),expected_7)); assert(std::count(v.begin(),v.end(),expected_8)); assert(std::count(v.begin(),v.end(),expected_9)); assert(std::count(v.begin(),v.end(),expected_10)); assert(std::count(v.begin(),v.end(),expected_11)); assert(std::count(v.begin(),v.end(),expected_12)); assert(std::count(v.begin(),v.end(),expected_13)); assert(std::count(v.begin(),v.end(),expected_14)); assert(std::count(v.begin(),v.end(),expected_15)); } //Wordwrap { const auto v { "", "1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "123456789", "1234567890", "12345678901", "123456789012", "1234567890123", "12345678901234", "123456789012345", "1234567890123456", "12345678901234567", "123456789012345678", "1234567890123456789", "12345678901234567890", "123456789012345678901", "1234567890123456789012", "12345678901234567890123", "123456789012345678901234", "1234567890123456789012345", "12345678901234567890123456", "123456789012345678901234567", "1234567890123456789012345678", "12345678901234567890123456789", "123456789012345678901234567890", "1234567890123456789012345678901", "12345678901234567890123456789012", "123456789012345678901234567890123", "1234567890123456789012345678901234", "12345678901234567890123456789012345", "123456789012345678901234567890123456", "1234567890123456789012345678901234567", "12345678901234567890123456789012345678", "123456789012345678901234567890123456789", "1234567890123456789012345678901234567890", "1 1", "12 12", "123 123", "1234 1234", "12345 12345", "123456 123456", "1234567 1234567", "12345678 8", "123456789 9", "1234567890 0", "1234567890 1234567890", "1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", " 1", " 1", " 1 ", " 1 ", " 1 2 ", " 1 23 ", " 12 34 ", " 12 34 ", " 12 34 ", " 12 34 5", " 12 34 5 ", " 12 34 5 6", "0 12 34 5 6", "0 12 34 5 6 ", " ", " ", " ", " ", " ", " ", " " }; for (int len=1; len!=1000; ++len) { for (const std::string& s: v) { //Wordwrap calls Unwordwrap Wordwrap(s,len); } } } TRACE("TestHelperFunctions finished successfully"); }
int main() { { const std::string s = "123"; const std::vector<std::string> expected = { "123" }; const std::vector<std::string> v = Wordwrap(s,11); assert(v == expected); assert(Unwordwrap(v) == s); } { const std::string s = "1234567890 123"; //Note that exected keeps trailing spaces at then end of a cut const std::vector<std::string> expected = { "1234567890 ", "123" }; const std::vector<std::string> v = Wordwrap(s,11); //if (v != expected) //{ // const std::size_t sz = v.size(); // for (std::size_t i=0; i!=sz; ++i) // { // std::cout << i << '/' << sz << ": '" << v[i] << '\'' << std::endl; // } //} assert(v == expected); assert(Unwordwrap(v) == s); } { const std::string s = "1234567890 123"; const std::vector<std::string> expected = { "1234567890 ", "123" }; const std::vector<std::string> v = Wordwrap(s,11); //if (v != expected) //{ // const std::size_t sz = v.size(); // for (std::size_t i=0; i!=sz; ++i) // { // std::cout << i << '/' << sz << ": '" << v[i] << '\'' << std::endl; // } //} assert(v == expected); assert(Unwordwrap(v) == s); } { const std::string s = "123 567890 123"; const std::vector<std::string> expected = { "123 567890 ", "123" }; const std::vector<std::string> v = Wordwrap(s,11); assert(v == expected); assert(Unwordwrap(v) == s); } { const auto v { "", "1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "123456789", "1234567890", "12345678901", "123456789012", "1234567890123", "12345678901234", "123456789012345", "1234567890123456", "12345678901234567", "123456789012345678", "1234567890123456789", "12345678901234567890", "123456789012345678901", "1234567890123456789012", "12345678901234567890123", "123456789012345678901234", "1234567890123456789012345", "12345678901234567890123456", "123456789012345678901234567", "1234567890123456789012345678", "12345678901234567890123456789", "123456789012345678901234567890", "1234567890123456789012345678901", "12345678901234567890123456789012", "123456789012345678901234567890123", "1234567890123456789012345678901234", "12345678901234567890123456789012345", "123456789012345678901234567890123456", "1234567890123456789012345678901234567", "12345678901234567890123456789012345678", "123456789012345678901234567890123456789", "1234567890123456789012345678901234567890", "1 1", "12 12", "123 123", "1234 1234", "12345 12345", "123456 123456", "1234567 1234567", "12345678 8", "123456789 9", "1234567890 0", "1234567890 1234567890", "1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890", " 1", " 1", " 1 ", " 1 ", " 1 2 ", " 1 23 ", " 12 34 ", " 12 34 ", " 12 34 ", " 12 34 5", " 12 34 5 ", " 12 34 5 6", "0 12 34 5 6", "0 12 34 5 6 ", " ", " ", " ", " ", " ", " ", " " }; for (int len=1; len!=1000; ++len) { for (const std::string& s: v) { //Wordwrap calls Unwordwrap Wordwrap(s,len); } } } }
void ribi::cmap::QtConceptMap::OnNodeKeyDownPressed(QtNode* const item, const int key) { //Note: item can also be the QtNode on a QtEdge assert(item); if (m_mode == Mode::edit && key == Qt::Key_F2) { //Edit concept QtScopedDisable<QtConceptMap> disable(this); QtConceptMapConceptEditDialog d(item->GetNode().GetConcept()); d.exec(); //Find the original Node or Edge if (::has_custom_vertex_with_my_vertex(item->GetNode(), m_conceptmap)) { assert(::has_custom_vertex_with_my_vertex(item->GetNode(), m_conceptmap)); const auto vd = ::find_first_custom_vertex_with_my_vertex(item->GetNode(), m_conceptmap); //Update the node here auto node = item->GetNode(); node.SetConcept(d.GetConcept()); //Update the node in the concept map set_my_custom_vertex(node, vd, m_conceptmap); } else { //It is a node on an edge //Find the first (and hopefully only) edge with the node on it Node node = item->GetNode(); const auto ed = ::find_first_custom_edge( [node](const Edge& e) { return e.GetNode() == node; }, m_conceptmap ); //Get hold of the Edge Edge edge = ::get_my_custom_edge(ed, m_conceptmap); //Update the Edge here node.SetConcept(d.GetConcept()); edge.SetNode(node); //Update the node in the concept map set_my_custom_edge(edge, ed, m_conceptmap); } //Update the QtNode item->GetNode().SetConcept(d.GetConcept()); //Set the word-wrapped text item->SetText(Wordwrap(d.GetConcept().GetName(), QtNode::GetWordWrapLength())); } else if (m_mode == Mode::rate && key == Qt::Key_F1) { //Rate concept QtScopedDisable<QtConceptMap> disable(this); const auto vd = ::find_first_custom_vertex_with_my_vertex(item->GetNode(), m_conceptmap); const auto subgraph = create_direct_neighbour_custom_and_selectable_edges_and_vertices_subgraph(vd, m_conceptmap); ribi::cmap::QtRateConceptDialog d(subgraph); d.exec(); if (d.GetOkClicked()) { //Find the original Node //const auto vd = FindNode(item->GetNode(), m_conceptmap); //Update the node here auto node = item->GetNode(); node.GetConcept().SetRatingComplexity(d.GetComplexity()); node.GetConcept().SetRatingConcreteness(d.GetConcreteness()); node.GetConcept().SetRatingSpecificity(d.GetSpecificity()); //Update the node in the concept map set_my_custom_vertex(node, vd, m_conceptmap); //Update the QtNode item->GetNode().SetConcept(node.GetConcept()); const int n_rated = (node.GetConcept().GetRatingComplexity() != -1 ? 1 : 0) + (node.GetConcept().GetRatingConcreteness() != -1 ? 1 : 0) + (node.GetConcept().GetRatingSpecificity() != -1 ? 1 : 0); switch (n_rated) { case 0: item->setBrush(QtBrushFactory().CreateRedGradientBrush()); break; case 1: case 2: item->setBrush(QtBrushFactory().CreateYellowGradientBrush()); break; case 3: item->setBrush(QtBrushFactory().CreateGreenGradientBrush()); break; default: assert(!"Should not get here"); } } } else if (m_mode == Mode::rate && key == Qt::Key_F2) { //Rate examples if (item->GetNode().GetConcept().GetExamples().Get().empty()) return; QtScopedDisable<QtConceptMap> disable(this); ribi::cmap::QtRateExamplesDialogNewName d(item->GetNode().GetConcept()); d.exec(); //Find the original Node const auto vd = ::find_first_custom_vertex_with_my_vertex(item->GetNode(), m_conceptmap); //Update the node here auto node = item->GetNode(); node.GetConcept().SetExamples(d.GetRatedExamples()); //Update the node in the concept map set_my_custom_vertex(node, vd, m_conceptmap); //Update the QtNode item->GetNode().GetConcept().SetExamples(d.GetRatedExamples()); } this->show(); this->setFocus(); this->scene()->setFocusItem(item); item->setSelected(true); this->scene()->update(); }