summaryrefslogtreecommitdiffstats
path: root/adept/libadept/quickfilter.h
blob: 4b7230043e5104ccb8aeb8d1254cdeac76556475 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/** -*- C++ -*-
	@file adept/quickfilter.h
	@author Peter Rockai <me@mornfall.net>
*/

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

#include <apt-front/cache/entity/entity.h>
#include <apt-front/cache/entity/package.h>
#include <apt-front/predicate/factory.h>
#include <adept/quickfilterui.h>
#include <adept/filterlist.h>
#include <adept/lister.h>
#include <adept/utils.h>

#ifndef EPT_TQUICKFILTER_H
#define EPT_TQUICKFILTER_H

class KLineEdit;

namespace adept {

template< typename T >
struct QuickFilter : predicate::Implementation< T, QuickFilter< T > >,
                     InterfacingPredicate
{
    enum Type { Regex, Substring, Exact };
    enum What { Name = 0x1, Description = 0x2, Maintainer = 0x4 };

    QuickFilter()
        : m_type( Substring ), m_match( "" ), m_what( Name | Description ) {
        setupPredicate();
    }

    void setupPredicate() {
        predicate::ArgumentList l;
        l.push_back( m_match );
        predicate::Predicate< T > a = not predicate::True< T >();
        if ( m_what & Name ) a = a or predicate::Factory< T >::name( m_match );
        if ( m_what & Description )
            a = a or predicate::Factory< T >::description( m_match );
        if ( m_what & Maintainer )
            a = a or predicate::Factory< T >::maintainer( m_match );
        m_op = a;
        /* m_op = predicate::map(
            predicate::predicate( predicate::Factory< T >::description( "" )
                                  or predicate::Factory< T >::name( "" )
                                  or predicate::Factory< T
        >::maintainer( "" ) ), l ); */
    }

    std::string summary() const {
        return u8( i18n( "Search: " ) ) + "\"" + m_match + "\"";
    }

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

    bool operator==( const QuickFilter &o ) const {
        return o.m_type == m_type && o.m_match == m_match;
    }

    std::string typeString() const {
        if (m_type == Regex) return "Regular Expression";
        if (m_type == Substring) return "Substring";
        if (m_type == Exact) return "Exact Match";
    }

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

    std::string match() const {
        return m_match;
    }

    void setMatch( const std::string &s ) {
        m_match = s;
        setupPredicate();
    }

    void setWhat( int w ) {
        m_what = w;
        setupPredicate();
    }

    int what() { return m_what; }

    virtual void reset() {
        m_match = "";
        setupPredicate();
    }

protected:
    Type m_type;
    std::string m_match;
    int m_what;
    predicate::Predicate< T > m_op;
};

class QuickFilterWidget : public QuickFilterUi
{
    Q_OBJECT
  TQ_OBJECT
public:
    QuickFilterWidget( TQWidget *parent, const char *name = 0 );
    virtual Predicate predicate();
public slots:
    void predicateChanged();
protected slots:
    void textChanged( const TQString & );
protected:
    void mouseReleaseEvent( TQMouseEvent *e );
    TQTimer timer;
};

}

#endif