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