summaryrefslogtreecommitdiffstats
path: root/adept/libadept/easytagfilter.h
blob: e2f6165f5c49c5c529740f202d5c5d00ecefd4a9 (plain)
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
/** -*- C++ -*-
	@file adept/quickfilter.h
	@author Peter Rockai <me@mornfall.net>
*/

#include <klocale.h>
#include <tqlayout.h>
#include <kdebug.h>

#include <apt-front/cache/entity/entity.h>
#include <apt-front/cache/entity/package.h>
#include <apt-front/cache/component/tags.h>
#include <apt-front/predicate/factory.h>

#include <adept/easytagfilterui.h>
#include <adept/filterlist.h>
#include <adept/lister.h>

#ifndef EPT_EASYTAGFILTER_H
#define EPT_EASYTAGFILTER_H

namespace adept {

template< typename T >
struct EasyTagFilter : predicate::Implementation< T, EasyTagFilter< T > >,
    InterfacingPredicate
{
    typedef std::map< std::string, std::string > Tags;

    EasyTagFilter() {
        setupPredicate();
    }

    void setupPredicate() {
        Cache &cache = cache::Global::get(); // FIXME?
        m_op = predicate::True< T >();
        for (Tags::iterator i = m_tags.begin(); i != m_tags.end(); ++i ) {
            m_op = m_op and predicate::Factory< T >::tag( 
                cache.tags().tagByName( i->first + "::" + i->second ) );
            // kdDebug() << t.summary() << endl;
        }
    }

    std::string summary() const {
        std::string r( i18n( "EasyTag filter: " ).local8Bit() );
        for (Tags::const_iterator j, i = m_tags.begin();
             i != m_tags.end(); ++i ) {
            j = i; ++j;
            r += i->first + ": " + i->second;
            if (j != m_tags.end())
                r += ", ";
        }
        return r;
    }

    void parseArguments( const predicate::ArgumentList & ) {}

    bool operator==( const EasyTagFilter &o ) const {
        return o.m_tags == m_tags;
    }

    bool operator()( const T &p ) {
        return m_op( p );
    }

    void set( const std::string &f, const std::string &t ) {
        m_tags[f] = t;
        setupPredicate();
    }

    std::string get( const std::string &f ) {
        return m_tags[ f ];
    }

protected:
    Tags m_tags;
    predicate::Predicate< T > m_op;
};

class EasyTagFilterWidget : public EasyTagFilterUi
{
    Q_OBJECT
  TQ_OBJECT
public:
    EasyTagFilterWidget( TQWidget *parent, const char *name = 0 );
    virtual Predicate predicate();
public slots:
    void predicateChanged();
    void reset();
};

}

#endif