Fix typo found by Yuri Chornoivan
[kdepim.git] / knode / knrangefilter.cpp
blob26dd955939cb20be060f0ddcb054a29bc2883233
1 /*
2 KNode, the KDE newsreader
3 Copyright (c) 1999-2005 the KNode authors.
4 See file AUTHORS for details
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 You should have received a copy of the GNU General Public License
11 along with this program; if not, write to the Free Software Foundation,
12 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
15 #include <QHBoxLayout>
16 #include <QLabel>
17 #include <QCheckBox>
18 #include <QComboBox>
20 #include <knuminput.h>
21 #include <kconfig.h>
22 #include <kconfiggroup.h>
24 #include "knrangefilter.h"
26 using namespace KNode;
28 bool KNode::RangeFilter::doFilter( int a )
30 bool ret=true;
31 if(enabled) {
32 switch (op1) {
33 case gt:
34 case gtoeq:
35 if (op2 != dis)
36 ret=( matchesOp(val1,op1,a) && matchesOp(a,op2,val2) );
37 else
38 ret = matchesOp(val1,op1,a);
39 break;
40 case eq:
41 case lt:
42 case ltoeq:
43 ret = matchesOp(val1,op1,a);
44 break;
45 default:
46 ret = false;
50 return ret;
55 bool KNode::RangeFilter::matchesOp( int v1, Op o, int v2 )
57 bool ret=false;
59 switch(o) {
60 case eq: ret=(v1==v2); break;
61 case gt: ret=(v1<v2); break;
62 case gtoeq: ret=(v1<=v2); break;
63 case ltoeq: ret=(v1>=v2); break;
64 case lt: ret=(v1>v2); break;
65 default: ret=false; break;
68 return ret;
73 void KNode::RangeFilter::load( const KConfigGroup &group )
75 enabled=group.readEntry("enabled", false);
76 val1=group.readEntry("val1",0);
77 op1=(Op) group.readEntry("op1",0);
78 val2=group.readEntry("val2",0);
79 op2=(Op) group.readEntry("op2",0);
84 void KNode::RangeFilter::save( KConfigGroup &group )
86 group.writeEntry("enabled", enabled);
87 group.writeEntry("val1", val1);
88 group.writeEntry("op1", (int)op1);
89 group.writeEntry("op2", (int)op2);
90 group.writeEntry("val2", val2);
96 //=====================================================================================
98 KNode::RangeFilterWidget::RangeFilterWidget( const QString& value, int min, int max,
99 QWidget* parent, const QString &unit )
100 : QGroupBox( value, parent )
102 QHBoxLayout *layout = new QHBoxLayout( this );
104 enabled = new QCheckBox( this );
105 layout->addWidget( enabled );
107 val1 = new KIntSpinBox( min, max, 1, min, this );
108 val1->setSuffix( unit );
109 layout->addWidget( val1 );
111 op1 = new QComboBox( this );
112 op1->addItem("<");
113 op1->addItem("<=");
114 op1->addItem("=");
115 op1->addItem(">=");
116 op1->addItem(">");
117 layout->addWidget( op1 );
119 des = new QLabel( value, this );
120 des->setAlignment( Qt::AlignCenter );
121 layout->addWidget( des );
123 op2 = new QComboBox( this );
124 op2->addItem("");
125 op2->addItem("<");
126 op2->addItem("<=");
127 layout->addWidget( op2 );
129 val2 = new KIntSpinBox( min, max, 1, min, this );
130 val2->setSuffix( unit );
131 layout->addWidget( val2 );
133 connect(op1, SIGNAL(activated(int)), SLOT(slotOp1Changed(int)));
134 connect(op2, SIGNAL(activated(int)), SLOT(slotOp2Changed(int)));
135 connect(enabled, SIGNAL(toggled(bool)), SLOT(slotEnabled(bool)));
137 slotEnabled(false);
142 KNode::RangeFilterWidget::~RangeFilterWidget()
148 RangeFilter KNode::RangeFilterWidget::filter()
150 RangeFilter r;
151 r.val1=val1->value();
152 r.val2=val2->value();
154 r.op1=(RangeFilter::Op) op1->currentIndex();
155 if (op2->currentText().isEmpty())
156 r.op2 = RangeFilter::dis;
157 else if (op2->currentText()=="<")
158 r.op2 = RangeFilter::gt;
159 else if (op2->currentText()=="<=")
160 r.op2 = RangeFilter::gtoeq;
162 r.enabled=enabled->isChecked();
164 return r;
169 void KNode::RangeFilterWidget::setFilter( RangeFilter &f )
171 val1->setValue(f.val1);
172 val2->setValue(f.val2);
174 op1->setCurrentIndex((int)f.op1);
175 if ( f.op2 == RangeFilter::dis )
176 op2->setCurrentIndex(0);
177 else if ( f.op2 == RangeFilter::gt )
178 op2->setCurrentIndex(1);
179 else if ( f.op2 == RangeFilter::gtoeq )
180 op2->setCurrentIndex(2);
182 enabled->setChecked(f.enabled);
187 void KNode::RangeFilterWidget::clear()
189 val1->setValue(val1->minimum());
190 val2->setValue(val2->minimum());
191 enabled->setChecked(false);
196 void KNode::RangeFilterWidget::slotOp1Changed( int id )
198 bool state = op1->isEnabled() && id < 2;
199 op2->setEnabled(state);
200 des->setEnabled(state);
201 slotOp2Changed(op2->currentIndex());
206 void KNode::RangeFilterWidget::slotOp2Changed( int id )
208 val2->setEnabled(op1->isEnabled() && (op1->currentIndex()<2) && (id>0));
213 void KNode::RangeFilterWidget::slotEnabled( bool e )
215 op1->setEnabled(e);
216 val1->setEnabled(e);
217 des->setEnabled(e);
218 slotOp1Changed(op1->currentIndex());
221 // -----------------------------------------------------------------------------
223 #include "knrangefilter.moc"