summaryrefslogtreecommitdiffstats
path: root/digikam/digikam/ratingfilter.cpp
blob: fea04682849b2c9445c57630d4486f6e497b86ab (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2007-10-09
 * Description : a widget to filter album contents by rating
 * 
 * Copyright (C) 2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * Copyright (C) 2007 by Arnd Baecker <arnd dot baecker at web dot de>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ============================================================ */

// TQt includes.

#include <tqpainter.h>
#include <tqpalette.h>
#include <tqpixmap.h>
#include <tqcursor.h>
#include <tqwhatsthis.h>

// KDE includes.

#include <kiconloader.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <kstandarddirs.h>
#include <tdepopupmenu.h>

// Local includes.

#include "constants.h"
#include "ddebug.h"
#include "dcursortracker.h"
#include "themeengine.h"
#include "ratingfilter.h"
#include "ratingfilter.moc"

namespace Digikam
{

class RatingFilterPriv
{
public:

    RatingFilterPriv()
    {
        dirty         = false;
        ratingTracker = 0;
        filterCond    = AlbumLister::GreaterEqualCondition;
    }
    
    bool                          dirty;

    DTipTracker                  *ratingTracker;

    AlbumLister::RatingCondition  filterCond;
};

RatingFilter::RatingFilter(TQWidget* parent)
            : RatingWidget(parent)
{
    d = new RatingFilterPriv;

    d->ratingTracker = new DTipTracker("", this);
    updateRatingTooltip();
    setMouseTracking(true);

    TQWhatsThis::add(this, i18n("Select the rating value used to filter "
                               "albums contents. Use contextual pop-up menu to "
                               "set rating filter condition."));

    // To dispatch signal from parent widget with filter condition.
    connect(this, TQT_SIGNAL(signalRatingChanged(int)),
            this, TQT_SLOT(slotRatingChanged()));
}

RatingFilter::~RatingFilter()
{    
    delete d->ratingTracker;
    delete d;
}

void RatingFilter::slotRatingChanged()
{
    emit signalRatingFilterChanged(rating(), d->filterCond);
}

void RatingFilter::setRatingFilterCondition(AlbumLister::RatingCondition cond)
{
    d->filterCond = cond;
    updateRatingTooltip();
    slotRatingChanged();
}

AlbumLister::RatingCondition RatingFilter::ratingFilterCondition()
{
    return d->filterCond;
}

void RatingFilter::mouseMoveEvent(TQMouseEvent* e)
{
    // This method have been re-implemented to display and update the famous TipTracker contents.

    if ( d->dirty )
    {
        int pos = e->x() / regPixmapWidth() +1;
    
        if (rating() != pos)
            setRating(pos);

        updateRatingTooltip();
    }
}

void RatingFilter::mousePressEvent(TQMouseEvent* e)
{
    // This method must be re-implemented to handle witch mousse button is pressed 
    // and show the rating filter settings pop-up menu with right mouse button.
    // NOTE: Left and Middle Mouse buttons continue to change rating filter value.

    d->dirty = false;

    if ( e->button() == TQt::LeftButton || e->button() == TQt::MidButton )
    {
        d->dirty   = true;
        int pos = e->x() / regPixmapWidth() +1;
    
        if (rating() == pos)
            setRating(rating()-1);
        else
            setRating(pos);
        updateRatingTooltip();
    }
    else if (e->button() == TQt::RightButton)
    {
        // Show pop-up menu about Rating Filter condition settings

        TDEPopupMenu popmenu(this);
        popmenu.insertTitle(SmallIcon("digikam"), i18n("Rating Filter"));
        popmenu.setCheckable(true);
        popmenu.insertItem(i18n("Greater Equal Condition"), AlbumLister::GreaterEqualCondition);
        popmenu.insertItem(i18n("Equal Condition"),         AlbumLister::EqualCondition);
        popmenu.insertItem(i18n("Less Equal Condition"),    AlbumLister::LessEqualCondition);
        popmenu.setItemChecked(d->filterCond, true);

        int choice = popmenu.exec((TQCursor::pos()));
        switch(choice)
        {
            case AlbumLister::GreaterEqualCondition:
            case AlbumLister::EqualCondition:
            case AlbumLister::LessEqualCondition:
            {
                setRatingFilterCondition((AlbumLister::RatingCondition)choice);
                break;
            }
            default:
                break;
        }
    }
}

void RatingFilter::mouseReleaseEvent(TQMouseEvent*)
{
    d->dirty = false;
}

void RatingFilter::updateRatingTooltip()
{
    // Adapt tip message with rating filter condition settings.

    switch(d->filterCond)
    {
        case AlbumLister::GreaterEqualCondition:
        {
            d->ratingTracker->setText(i18n("Rating >= %1").arg(rating()));
            break;
        }
        case AlbumLister::EqualCondition:
        {
            d->ratingTracker->setText(i18n("Rating = %1").arg(rating()));
            break;
        }
        case AlbumLister::LessEqualCondition:
        {
            d->ratingTracker->setText(i18n("Rating <= %1").arg(rating()));
            break;
        }
        default:
            break;
    }
}

}  // namespace Digikam