~marcustomlinson/unity-scopes-shell/lp-1552082

« back to all changes in this revision

Viewing changes to tests/data/mock-scope-filters/mock-scope-filters.cpp

  • Committer: Marcus Tomlinson
  • Date: 2016-03-29 06:38:28 UTC
  • mfrom: (295.1.5 unity-scopes-shell)
  • Revision ID: marcus.tomlinson@canonical.com-20160329063828-jt2mbzqdi1rj7g2t
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Pawel Stolowski <pawel.stolowski@canonical.com>
 
17
 */
 
18
 
 
19
#include <unity/scopes/CategorisedResult.h>
 
20
#include <unity/scopes/OptionSelectorFilter.h>
 
21
#include <unity/scopes/RangeInputFilter.h>
 
22
#include <unity/scopes/ValueSliderFilter.h>
 
23
#include <unity/scopes/ValueSliderLabels.h>
 
24
#include <unity/scopes/ScopeBase.h>
 
25
#include <unity/scopes/SearchReply.h>
 
26
 
 
27
#include <sstream>
 
28
 
 
29
#define EXPORT __attribute__ ((visibility ("default")))
 
30
 
 
31
using namespace std;
 
32
using namespace unity::scopes;
 
33
 
 
34
class MyQuery : public SearchQueryBase
 
35
{
 
36
public:
 
37
    MyQuery(CannedQuery const& query, SearchMetadata const& metadata) :
 
38
        SearchQueryBase(query, metadata)
 
39
    {
 
40
    }
 
41
 
 
42
    ~MyQuery()
 
43
    {
 
44
    }
 
45
 
 
46
    virtual void cancelled() override
 
47
    {
 
48
    }
 
49
 
 
50
    virtual void run(SearchReplyProxy const& reply) override
 
51
    {
 
52
        OptionSelectorFilter::UPtr filter1 = OptionSelectorFilter::create("f1", "Filter1");
 
53
        if (query().query_string() == "test_primary_filter") {
 
54
            filter1->set_display_hints(FilterBase::DisplayHints::Primary);
 
55
        }
 
56
 
 
57
        auto opt1 = filter1->add_option("o1", "Option1");
 
58
        filter1->add_option("o2", "Option2");
 
59
 
 
60
        RangeInputFilter::SPtr filter2 = RangeInputFilter::create("f2", Variant(2.0f), Variant::null(), "start", "", "", "end", "");
 
61
 
 
62
        auto cat1 = reply->register_category("cat1", "Category 1", "");
 
63
        CategorisedResult res1(cat1);
 
64
        res1.set_uri("test:uri");
 
65
 
 
66
        auto selected = filter1->active_options(query().filter_state());
 
67
        if (selected.size() == 1) {
 
68
            res1.set_title("result for option " + (*selected.begin())->id());
 
69
        } else {
 
70
            res1.set_title("result for: \"" + query().query_string() + "\"");
 
71
        }
 
72
 
 
73
        bool has_start_val = filter2->has_start_value(query().filter_state());
 
74
        bool has_end_val = filter2->has_end_value(query().filter_state());
 
75
 
 
76
        if (has_start_val || has_end_val)
 
77
        {
 
78
            res1.set_title("result for range: " +
 
79
                    (has_start_val ? std::to_string(filter2->start_value(query().filter_state())) : "***") + " - " +
 
80
                    (has_end_val ? std::to_string(filter2->end_value(query().filter_state())) : "***"));
 
81
        }
 
82
 
 
83
        ValueSliderFilter::SPtr filter3 = ValueSliderFilter::create("f3", 1, 99, 50, ValueSliderLabels("Min", "Max", {{33, "One third"}}));
 
84
 
 
85
        Filters filters;
 
86
        filters.push_back(std::move(filter1));
 
87
        filters.push_back(std::move(filter2));
 
88
        filters.push_back(std::move(filter3));
 
89
 
 
90
        reply->push(filters, query().filter_state());
 
91
        reply->push(res1);
 
92
    }
 
93
};
 
94
 
 
95
class MyScope : public ScopeBase
 
96
{
 
97
public:
 
98
    MyScope()
 
99
    {
 
100
    }
 
101
 
 
102
    virtual SearchQueryBase::UPtr search(CannedQuery const& q, SearchMetadata const& metadata) override
 
103
    {
 
104
        return SearchQueryBase::UPtr(new MyQuery(q, metadata));
 
105
    }
 
106
 
 
107
    virtual PreviewQueryBase::UPtr preview(Result const&, ActionMetadata const&) override
 
108
    {
 
109
        return nullptr;
 
110
    }
 
111
};
 
112
 
 
113
extern "C"
 
114
{
 
115
 
 
116
    EXPORT
 
117
    unity::scopes::ScopeBase*
 
118
    // cppcheck-suppress unusedFunction
 
119
    UNITY_SCOPE_CREATE_FUNCTION()
 
120
    {
 
121
        return new MyScope;
 
122
    }
 
123
 
 
124
    EXPORT
 
125
    void
 
126
    // cppcheck-suppress unusedFunction
 
127
    UNITY_SCOPE_DESTROY_FUNCTION(unity::scopes::ScopeBase* scope_base)
 
128
    {
 
129
        delete scope_base;
 
130
    }
 
131
 
 
132
}