The Battle for Wesnoth  1.15.0-dev
filter.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Define the common filters for the @ref gui2::pane class.
18  */
19 
20 #pragma once
21 
22 #include "gui/widgets/text_box.hpp"
23 #include "lexical_cast.hpp"
25 
26 namespace gui2
27 {
28 
29 template <class T>
30 inline bool sort(const pane::item& lhs,
31  const pane::item& rhs,
32  const std::string& tag,
33  const bool ascending)
34 {
35  if(ascending) {
36  return lexical_cast<T>(lhs.tags.at(tag))
37  < lexical_cast<T>(rhs.tags.at(tag));
38  } else {
39  return lexical_cast<T>(lhs.tags.at(tag))
40  > lexical_cast<T>(rhs.tags.at(tag));
41  }
42 }
43 
44 /**
45  * A filter testing whether a search string is used in a text.
46  *
47  * The comparison is a lower-case comparison.
48  *
49  * The function is expected to be used as a functor for
50  * @ref gui2::pane::filter().
51  *
52  * @param item The pane item to search in.
53  * @param tag The tag of the field containing the text to
54  * search in. @note This text field should
55  * already contain lower-cased text only.
56  * @param text_box A text box object whose text is the search
57  * string. The text in the text box will be
58  * converted to lower-cased text before the
59  * comparison is done.
60  *
61  * @returns Whether or not the comparison found a match.
62  */
63 inline bool contains(const pane::item& item,
64  const std::string& tag,
65  const text_box& text_box)
66 {
67  return item.tags.at(tag).find(utf8::lowercase(text_box.text()))
68  != std::string::npos;
69 }
70 
71 } // namespace gui2
std::map< std::string, std::string > tags
Definition: pane.hpp:46
bool contains(const pane::item &item, const std::string &tag, const text_box &text_box)
A filter testing whether a search string is used in a text.
Definition: filter.hpp:63
New lexcical_cast header.
To lexical_cast(From value)
Lexical cast converts one type to another.
Class for a single line text area.
Definition: text_box.hpp:121
Generic file dialog.
Definition: field-fwd.hpp:22
bool sort(const pane::item &lhs, const pane::item &rhs, const std::string &tag, const bool ascending)
Definition: filter.hpp:30
std::string lowercase(const std::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:51
const std::string & text() const