-
Notifications
You must be signed in to change notification settings - Fork 22
/
YTableActionHandler.cc
111 lines (95 loc) · 4.37 KB
/
YTableActionHandler.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Copyright (C) 2020 SUSE LLC
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) version 3.0 of the License. This library
is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details. You should have received a copy of the GNU
Lesser General Public License along with this library; if not, write
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
Floor, Boston, MA 02110-1301 USA
*/
#include <vector>
#include <boost/algorithm/string.hpp>
#define YUILogComponent "rest-api"
#include <yui/YUILog.h>
#include <yui/YTable.h>
#include <yui/YTableItem.h>
#include "YTableActionHandler.h"
std::function<void (YTable*)> YTableActionHandler::get_handler( YTable * widget,
const std::string &value,
const int &column_id,
const int &row_id )
{
// Select a row in the table by the row number
if ( row_id != -1 )
{
return [&] (YTable *tb) {
if( row_id >= tb->itemsCount() || row_id < 0 )
throw YUIException( "Table: '" + tb->label() + "' does NOT contain row #" + std::to_string( row_id ) );
if ( YItem * item = tb->itemAt(row_id) )
{
yuiMilestone() << "Activating Table \"" << tb->label() << '"' << std::endl;
tb->setKeyboardFocus();
tb->selectItem( item );
activate_widget(tb, item);
}
else
{
throw YUIException( "Row: '" + std::to_string( row_id ) + "' cannot be found in the table" );
}
};
}
// Do the search of the value in the given column
return [&] (YTable *tb) {
std::vector<std::string> path;
boost::split( path, value, boost::is_any_of( TablePathDelimiter ) );
auto * item = YTableActionHandler::table_findItem( path.begin(),
path.end(),
tb->itemsBegin(),
tb->itemsEnd(),
column_id );
if ( item )
{
yuiMilestone() << "Activating Table \"" << tb->label() << "\" Item: \"" << item->label( column_id ) << "\"" << std::endl;
tb->setKeyboardFocus();
tb->selectItem( item );
activate_widget(tb, item);
}
else
{
throw YUIException( "Item: '" + value + "' cannot be found in the table" );
}
};
}
YTableItem *
YTableActionHandler::table_findItem( std::vector<std::string>::const_iterator path_begin,
std::vector<std::string>::const_iterator path_end,
YItemConstIterator begin,
YItemConstIterator end,
const int &column_id )
{
for ( YItemConstIterator it = begin; it != end; ++it )
{
YTableItem * item = dynamic_cast<YTableItem *>( *it );
if ( ! item )
return nullptr;
if ( normalize_label ( item->label(column_id) ) == *path_begin )
{
if ( std::next( path_begin ) == path_end )
return item;
// Recursively search child items
YTableItem * result = YTableActionHandler::table_findItem( ++path_begin,
path_end,
item->childrenBegin(),
item->childrenEnd(),
column_id );
if ( result )
return result;
}
}
return nullptr;
}