libkdepim

kscoringeditor.cpp

00001 /*
00002     kscoringeditor.cpp
00003 
00004     Copyright (c) 2001 Mathias Waack
00005     Copyright (C) 2005 by Volker Krause <volker.krause@rwth-aachen.de>
00006 
00007     Author: Mathias Waack <mathias@atoll-net.de>
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013     You should have received a copy of the GNU General Public License
00014     along with this program; if not, write to the Free Software Foundation,
00015     Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
00016 */
00017 
00018 #undef QT_NO_COMPAT
00019 
00020 #include "kscoring.h"
00021 #include "kscoringeditor.h"
00022 
00023 #include <kdebug.h>
00024 #include <klocale.h>
00025 #include <kcombobox.h>
00026 #include <kcolorcombo.h>
00027 #include <kiconloader.h>
00028 #include <kregexpeditorinterface.h>
00029 #include <ktrader.h>
00030 #include <kparts/componentfactory.h>
00031 
00032 
00033 #include <qlabel.h>
00034 #include <qpushbutton.h>
00035 #include <qlayout.h>
00036 #include <qtooltip.h>
00037 #include <qcheckbox.h>
00038 #include <qbuttongroup.h>
00039 #include <qradiobutton.h>
00040 #include <qwidgetstack.h>
00041 #include <qapplication.h>
00042 #include <qtimer.h>
00043 #include <qhbox.h>
00044 
00045 // works for both ListBox and ComboBox
00046 template <class T> static int setCurrentItem(T *box, const QString& s)
00047 {
00048   int cnt = box->count();
00049   for (int i=0;i<cnt;++i) {
00050     if (box->text(i) == s) {
00051       box->setCurrentItem(i);
00052       return i;
00053     }
00054   }
00055   return -1;
00056 }
00057 
00058 
00059 //============================================================================
00060 //
00061 // class SingleConditionWidget (editor for one condition, used in ConditionEditWidget)
00062 //
00063 //============================================================================
00064 SingleConditionWidget::SingleConditionWidget(KScoringManager *m,QWidget *p, const char *n)
00065   : QFrame(p,n), manager(m)
00066 {
00067   QBoxLayout *topL = new QVBoxLayout(this,5);
00068   QBoxLayout *firstRow = new QHBoxLayout(topL);
00069   neg = new QCheckBox(i18n("Not"),this);
00070   QToolTip::add(neg,i18n("Negate this condition"));
00071   firstRow->addWidget(neg);
00072   headers = new KComboBox(this);
00073   headers->insertStringList(manager->getDefaultHeaders());
00074   headers->setEditable( true );
00075   QToolTip::add(headers,i18n("Select the header to match this condition against"));
00076   firstRow->addWidget(headers,1);
00077   matches = new KComboBox(this);
00078   matches->insertStringList(KScoringExpression::conditionNames());
00079   QToolTip::add(matches,i18n("Select the type of match"));
00080   firstRow->addWidget(matches,1);
00081   connect( matches, SIGNAL( activated( int ) ), SLOT( toggleRegExpButton( int ) ) );
00082   QHBoxLayout *secondRow = new QHBoxLayout( topL );
00083   secondRow->setSpacing( 1 );
00084   expr = new KLineEdit( this );
00085   QToolTip::add(expr,i18n("The condition for the match"));
00086   // reserve space for at least 20 characters
00087   expr->setMinimumWidth(fontMetrics().maxWidth()*20);
00088   secondRow->addWidget( expr );
00089   regExpButton = new QPushButton( i18n("Edit..."), this );
00090   secondRow->addWidget( regExpButton );
00091   connect( regExpButton, SIGNAL( clicked() ), SLOT( showRegExpDialog() ) );
00092 
00093   // occupy at much width as possible
00094   setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed));
00095   setFrameStyle(Box | Sunken);
00096   setLineWidth(1);
00097 }
00098 
00099 SingleConditionWidget::~SingleConditionWidget()
00100 {}
00101 
00102 void SingleConditionWidget::setCondition(KScoringExpression *e)
00103 {
00104   neg->setChecked(e->isNeg());
00105   headers->setCurrentText( e->getHeader() );
00106   setCurrentItem(matches,KScoringExpression::getNameForCondition(e->getCondition()));
00107   toggleRegExpButton( matches->currentItem() );
00108   expr->setText(e->getExpression());
00109 }
00110 
00111 KScoringExpression* SingleConditionWidget::createCondition() const
00112 {
00113   QString head = headers->currentText();
00114   QString match = matches->currentText();
00115   int condType = KScoringExpression::getConditionForName(match);
00116   match = KScoringExpression::getTypeString(condType);
00117   QString cond = expr->text();
00118   QString negs = (neg->isChecked())?"1":"0";
00119   return new KScoringExpression(head,match,cond,negs);
00120 }
00121 
00122 void SingleConditionWidget::clear()
00123 {
00124   neg->setChecked(false);
00125   expr->clear();
00126 }
00127 
00128 void SingleConditionWidget::toggleRegExpButton( int selected )
00129 {
00130   bool isRegExp = (KScoringExpression::MATCH == selected ||
00131       KScoringExpression::MATCHCS == selected) &&
00132       !KTrader::self()->query("KRegExpEditor/KRegExpEditor").isEmpty();
00133   regExpButton->setEnabled( isRegExp );
00134 }
00135 
00136 void SingleConditionWidget::showRegExpDialog()
00137 {
00138   QDialog *editorDialog = KParts::ComponentFactory::createInstanceFromQuery<QDialog>( "KRegExpEditor/KRegExpEditor" );
00139   if ( editorDialog ) {
00140     KRegExpEditorInterface *editor = static_cast<KRegExpEditorInterface *>( editorDialog->qt_cast( "KRegExpEditorInterface" ) );
00141     Q_ASSERT( editor ); // This should not fail!
00142     editor->setRegExp( expr->text() );
00143     editorDialog->exec();
00144     expr->setText( editor->regExp() );
00145   }
00146 }
00147 
00148 //============================================================================
00149 //
00150 // class ConditionEditWidget (the widget to edit the conditions of a rule)
00151 //
00152 //============================================================================
00153 ConditionEditWidget::ConditionEditWidget(KScoringManager *m, QWidget *p, const char *n)
00154   : KWidgetLister(1,8,p,n), manager(m)
00155 {
00156   // create one initial widget
00157   addWidgetAtEnd();
00158 }
00159 
00160 ConditionEditWidget::~ConditionEditWidget()
00161 {}
00162 
00163 QWidget* ConditionEditWidget::createWidget(QWidget *parent)
00164 {
00165   return new SingleConditionWidget(manager,parent);
00166 }
00167 
00168 void ConditionEditWidget::clearWidget(QWidget *w)
00169 {
00170   Q_ASSERT( w->isA("SingleConditionWidget") );
00171   SingleConditionWidget *sw = dynamic_cast<SingleConditionWidget*>(w);
00172   if (sw)
00173     sw->clear();
00174 }
00175 
00176 void ConditionEditWidget::slotEditRule(KScoringRule *rule)
00177 {
00178   KScoringRule::ScoreExprList l;
00179   if (rule) l = rule->getExpressions();
00180   if (!rule || l.count() == 0) {
00181     slotClear();
00182   } else {
00183     setNumberOfShownWidgetsTo(l.count());
00184     KScoringExpression *e = l.first();
00185     SingleConditionWidget *scw = static_cast<SingleConditionWidget*>(mWidgetList.first());
00186     while (e && scw) {
00187       scw->setCondition(e);
00188       e = l.next();
00189       scw = static_cast<SingleConditionWidget*>(mWidgetList.next());
00190     }
00191   }
00192 }
00193 
00194 void ConditionEditWidget::updateRule(KScoringRule *rule)
00195 {
00196   rule->cleanExpressions();
00197   for(QWidget *w = mWidgetList.first(); w; w = mWidgetList.next()) {
00198     if (! w->isA("SingleConditionWidget")) {
00199       kdWarning(5100) << "there is a widget in ConditionEditWidget "
00200                       << "which isn't a SingleConditionWidget" << endl;
00201     } else {
00202       SingleConditionWidget *saw = dynamic_cast<SingleConditionWidget*>(w);
00203       if (saw)
00204         rule->addExpression(saw->createCondition());
00205     }
00206   }
00207 }
00208 
00209 //============================================================================
00210 //
00211 // class SingleActionWidget (editor for one action, used in ActionEditWidget)
00212 //
00213 //============================================================================
00214 SingleActionWidget::SingleActionWidget(KScoringManager *m,QWidget *p, const char *n)
00215   : QWidget(p,n), notifyEditor(0), scoreEditor(0), colorEditor(0),manager(m)
00216 {
00217   QHBoxLayout *topL = new QHBoxLayout(this,0,5);
00218   types = new KComboBox(this);
00219   types->setEditable(false);
00220   topL->addWidget(types);
00221   stack = new QWidgetStack(this);
00222   topL->addWidget(stack);
00223 
00224   dummyLabel = new QLabel(i18n("Select an action."), stack);
00225   stack->addWidget(dummyLabel, 0);
00226 
00227   // init widget stack and the types combo box
00228   int index = 1;
00229   types->insertItem(QString::null);
00230   QStringList l = ActionBase::userNames();
00231   for ( QStringList::Iterator it = l.begin(); it != l.end(); ++it ) {
00232     QString name = *it;
00233     int feature = ActionBase::getTypeForUserName(name);
00234     if (manager->hasFeature(feature)) {
00235       types->insertItem(name);
00236       QWidget *w=0;
00237       switch (feature) {
00238         case ActionBase::SETSCORE:
00239           w = scoreEditor = new KIntSpinBox(-99999,99999,1,0,10, stack);
00240           break;
00241         case ActionBase::NOTIFY:
00242           w = notifyEditor = new KLineEdit(stack);
00243           break;
00244         case ActionBase::COLOR:
00245           w = colorEditor = new KColorCombo(stack);
00246           break;
00247         case ActionBase::MARKASREAD:
00248           w = new QLabel( stack ); // empty dummy
00249           break;
00250       }
00251       if ( w )
00252         stack->addWidget(w,index++);
00253     }
00254   }
00255 
00256   connect(types,SIGNAL(activated(int)),stack,SLOT(raiseWidget(int)));
00257 
00258   // raise the dummy label
00259   types->setCurrentItem(0);
00260   stack->raiseWidget(dummyLabel);
00261 }
00262 
00263 SingleActionWidget::~SingleActionWidget()
00264 {
00265 }
00266 
00267 void SingleActionWidget::setAction(ActionBase *act)
00268 {
00269   kdDebug(5100) << "SingleActionWidget::setAction()" << endl;
00270   setCurrentItem(types,ActionBase::userName(act->getType()));
00271   int index = types->currentItem();
00272   stack->raiseWidget(index);
00273   switch (act->getType()) {
00274     case ActionBase::SETSCORE:
00275       scoreEditor->setValue(act->getValueString().toInt());
00276       break;
00277     case ActionBase::NOTIFY:
00278       notifyEditor->setText(act->getValueString());
00279       break;
00280     case ActionBase::COLOR:
00281       colorEditor->setColor(QColor(act->getValueString()));
00282       break;
00283     case ActionBase::MARKASREAD:
00284       // nothing
00285       break;
00286     default:
00287       kdWarning(5100) << "unknown action type in SingleActionWidget::setAction()" << endl;
00288   }
00289 }
00290 
00291 ActionBase* SingleActionWidget::createAction() const
00292 {
00293   // no action selected...
00294   if (types->currentText().isEmpty())
00295     return 0;
00296 
00297   int type = ActionBase::getTypeForUserName(types->currentText());
00298   switch (type) {
00299     case ActionBase::SETSCORE:
00300       return new ActionSetScore(scoreEditor->value());
00301     case ActionBase::NOTIFY:
00302       return new ActionNotify(notifyEditor->text());
00303     case ActionBase::COLOR:
00304       return new ActionColor(colorEditor->color().name());
00305     case ActionBase::MARKASREAD:
00306       return new ActionMarkAsRead();
00307     default:
00308       kdWarning(5100) << "unknown action type in SingleActionWidget::getValue()" << endl;
00309       return 0;
00310   }
00311 }
00312 
00313 void SingleActionWidget::clear()
00314 {
00315   if (scoreEditor) scoreEditor->setValue(0);
00316   if (notifyEditor) notifyEditor->clear();
00317   if (colorEditor) colorEditor->setCurrentItem(0);
00318   types->setCurrentItem(0);
00319   stack->raiseWidget(dummyLabel);
00320 }
00321 
00322 //============================================================================
00323 //
00324 // class ActionEditWidget (the widget to edit the actions of a rule)
00325 //
00326 //============================================================================
00327 ActionEditWidget::ActionEditWidget(KScoringManager *m,QWidget *p, const char *n)
00328   : KWidgetLister(1,8,p,n), manager(m)
00329 {
00330   // create one initial widget
00331   addWidgetAtEnd();
00332 }
00333 
00334 ActionEditWidget::~ActionEditWidget()
00335 {}
00336 
00337 QWidget* ActionEditWidget::createWidget( QWidget *parent )
00338 {
00339   return new SingleActionWidget(manager,parent);
00340 }
00341 
00342 void ActionEditWidget::slotEditRule(KScoringRule *rule)
00343 {
00344   KScoringRule::ActionList l;
00345   if (rule) l = rule->getActions();
00346   if (!rule || l.count() == 0) {
00347     slotClear();
00348   } else {
00349     setNumberOfShownWidgetsTo(l.count());
00350     ActionBase *act = l.first();
00351     SingleActionWidget *saw = static_cast<SingleActionWidget*>(mWidgetList.first());
00352     while (act && saw) {
00353       saw->setAction(act);
00354       act = l.next();
00355       saw = static_cast<SingleActionWidget*>(mWidgetList.next());
00356     }
00357   }
00358 }
00359 
00360 void ActionEditWidget::updateRule(KScoringRule *rule)
00361 {
00362   rule->cleanActions();
00363   for(QWidget *w = mWidgetList.first(); w; w = mWidgetList.next()) {
00364     if (! w->isA("SingleActionWidget")) {
00365       kdWarning(5100) << "there is a widget in ActionEditWidget "
00366                       << "which isn't a SingleActionWidget" << endl;
00367     } else {
00368       SingleActionWidget *saw = dynamic_cast<SingleActionWidget*>(w);
00369       if (saw)
00370       {
00371         ActionBase *act = saw->createAction();
00372         if (act)
00373           rule->addAction(act);
00374       }
00375     }
00376   }
00377 }
00378 
00379 void ActionEditWidget::clearWidget(QWidget *w)
00380 {
00381   Q_ASSERT( w->isA("SingleActionWidget") );
00382   SingleActionWidget *sw = dynamic_cast<SingleActionWidget*>(w);
00383   if (sw)
00384     sw->clear();
00385 }
00386 
00387 //============================================================================
00388 //
00389 // class RuleEditWidget (the widget to edit one rule)
00390 //
00391 //============================================================================
00392 RuleEditWidget::RuleEditWidget(KScoringManager *m,QWidget *p, const char *n)
00393   : QWidget(p,n), dirty(false), manager(m), oldRuleName(QString::null)
00394 {
00395   kdDebug(5100) << "RuleEditWidget::RuleEditWidget()" << endl;
00396   if ( !n ) setName( "RuleEditWidget" );
00397   QVBoxLayout *topLayout = new QVBoxLayout( this, 5, KDialog::spacingHint() );
00398 
00399   //------------- Name, Servers, Groups ---------------------
00400   QGroupBox *groupB = new QGroupBox(i18n("Properties"),this);
00401   topLayout->addWidget(groupB);
00402   QGridLayout* groupL = new QGridLayout(groupB, 6,2, 8,5);
00403   groupL->addRowSpacing(0, fontMetrics().lineSpacing()-4);
00404 
00405   // name
00406   ruleNameEdit = new KLineEdit( groupB, "ruleNameEdit" );
00407   groupL->addWidget( ruleNameEdit, 1, 1 );
00408   QLabel *ruleNameLabel = new QLabel(ruleNameEdit, i18n("&Name:"), groupB, "ruleNameLabel");
00409   groupL->addWidget( ruleNameLabel, 1, 0 );
00410 
00411   // groups
00412   groupsEdit = new KLineEdit( groupB, "groupsEdit" );
00413   groupL->addWidget( groupsEdit, 2, 1 );
00414   QLabel *groupsLabel = new QLabel(groupsEdit, i18n("&Groups:"), groupB, "groupsLabel");
00415   groupL->addWidget( groupsLabel, 2, 0 );
00416 
00417   QPushButton *groupsBtn = new QPushButton(i18n("A&dd Group"), groupB);
00418   connect(groupsBtn,SIGNAL(clicked()),SLOT(slotAddGroup()));
00419   groupL->addWidget( groupsBtn, 3, 0 );
00420 
00421   groupsBox = new KComboBox( false, groupB, "groupsBox" );
00422   groupsBox->setDuplicatesEnabled(false);
00423   groupsBox->insertStringList(manager->getGroups());
00424   groupsBox->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
00425   groupL->addWidget( groupsBox, 3, 1 );
00426 
00427   // expires
00428   expireCheck = new QCheckBox(i18n("&Expire rule automatically"), groupB);
00429   groupL->addMultiCellWidget( expireCheck, 4,4, 0,1 );
00430   expireEdit = new KIntSpinBox(1,99999,1,30,10, groupB, "expireWidget");
00431   connect(expireEdit, SIGNAL(valueChanged(int)), SLOT(slotExpireEditChanged(int)));
00432   groupL->addWidget( expireEdit, 5, 1 );
00433   expireLabel = new QLabel(expireEdit, i18n("&Rule is valid for:"), groupB, "expireLabel");
00434   groupL->addWidget( expireLabel, 5, 0 );
00435   expireLabel->setEnabled(false);
00436   expireEdit->setEnabled(false);
00437 
00438   connect(expireCheck, SIGNAL(toggled(bool)), expireLabel, SLOT(setEnabled(bool)));
00439   connect(expireCheck, SIGNAL(toggled(bool)), expireEdit, SLOT(setEnabled(bool)));
00440 
00441   //------------- Conditions ---------------------
00442   QGroupBox *groupConds = new QGroupBox(i18n("Conditions"), this);
00443   topLayout->addWidget(groupConds);
00444   QGridLayout *condL = new QGridLayout(groupConds, 3,2, 8,5);
00445 
00446   condL->addRowSpacing(0, fontMetrics().lineSpacing()-4);
00447 
00448   QButtonGroup *buttonGroup = new QButtonGroup(groupConds);
00449   buttonGroup->hide();
00450   linkModeAnd = new QRadioButton(i18n("Match a&ll conditions"), groupConds);
00451   buttonGroup->insert(linkModeAnd);
00452   condL->addWidget(linkModeAnd, 1,0);
00453   linkModeOr = new QRadioButton(i18n("Matc&h any condition"), groupConds);
00454   buttonGroup->insert(linkModeOr);
00455   condL->addWidget(linkModeOr, 1,1);
00456   linkModeAnd->setChecked(true);
00457 
00458   condEditor = new ConditionEditWidget(manager,groupConds);
00459   condL->addMultiCellWidget(condEditor, 2,2, 0,1);
00460   connect(condEditor,SIGNAL(widgetRemoved()),this,SLOT(slotShrink()));
00461 
00462   //------------- Actions ---------------------
00463   QGroupBox *groupActions = new QGroupBox(i18n("Actions"), this);
00464   topLayout->addWidget(groupActions);
00465   QBoxLayout *actionL = new QVBoxLayout(groupActions,8,5);
00466   actionL->addSpacing(fontMetrics().lineSpacing()-4);
00467   actionEditor = new ActionEditWidget(manager,groupActions);
00468   actionL->addWidget(actionEditor);
00469   connect(actionEditor,SIGNAL(widgetRemoved()),this,SLOT(slotShrink()));
00470 
00471   topLayout->addStretch(1);
00472 
00473   kdDebug(5100) << "constructed RuleEditWidget" << endl;
00474 }
00475 
00476 RuleEditWidget::~RuleEditWidget()
00477 {
00478 }
00479 
00480 void RuleEditWidget::slotEditRule(const QString& ruleName)
00481 {
00482   kdDebug(5100) << "RuleEditWidget::slotEditRule(" << ruleName << ")" << endl;
00483 //   // first update the old rule if there is one
00484 //   kdDebug(5100) << "let see if we have a rule with name " << oldRuleName << endl;
00485 //   KScoringRule *rule;
00486 //   if (!oldRuleName.isNull() && oldRuleName != ruleName) {
00487 //     rule = manager->findRule(oldRuleName);
00488 //     if (rule) {
00489 //       kdDebug(5100) << "updating rule " << rule->getName() << endl;
00490 //       updateRule(rule);
00491 //     }
00492 //   }
00493 
00494   KScoringRule* rule = manager->findRule(ruleName);
00495   if (!rule) {
00496     kdDebug(5100) << "no rule for ruleName " << ruleName << endl;
00497     clearContents();
00498     return;
00499   }
00500   oldRuleName = rule->getName();
00501   ruleNameEdit->setText(rule->getName());
00502   groupsEdit->setText(rule->getGroups().join(";"));
00503 
00504   bool b = rule->getExpireDate().isValid();
00505   expireCheck->setChecked(b);
00506   expireEdit->setEnabled(b);
00507   expireLabel->setEnabled(b);
00508   if (b)
00509     expireEdit->setValue(QDate::currentDate().daysTo(rule->getExpireDate()));
00510   else
00511     expireEdit->setValue(30);
00512   if (rule->getLinkMode() == KScoringRule::AND) {
00513     linkModeAnd->setChecked(true);
00514   }
00515   else {
00516     linkModeOr->setChecked(true);
00517   }
00518 
00519   condEditor->slotEditRule(rule);
00520   actionEditor->slotEditRule(rule);
00521 
00522   kdDebug(5100) << "RuleEditWidget::slotEditRule() ready" << endl;
00523 }
00524 
00525 void RuleEditWidget::clearContents()
00526 {
00527   ruleNameEdit->setText("");
00528   groupsEdit->setText("");
00529   expireCheck->setChecked(false);
00530   expireEdit->setValue(30);
00531   expireEdit->setEnabled(false);
00532   condEditor->slotEditRule(0);
00533   actionEditor->slotEditRule(0);
00534   oldRuleName = QString::null;
00535 }
00536 
00537 void RuleEditWidget::updateRule(KScoringRule *rule)
00538 {
00539   oldRuleName = QString::null;
00540   QString groups = groupsEdit->text();
00541   if (groups.isEmpty())
00542     rule->setGroups(QStringList(".*"));
00543   else
00544     rule->setGroups(QStringList::split(";",groups));
00545   bool b = expireCheck->isChecked();
00546   if (b)
00547     rule->setExpireDate(QDate::currentDate().addDays(expireEdit->value()));
00548   else
00549     rule->setExpireDate(QDate());
00550   actionEditor->updateRule(rule);
00551   rule->setLinkMode(linkModeAnd->isChecked()?KScoringRule::AND:KScoringRule::OR);
00552   condEditor->updateRule(rule);
00553   if (rule->getName() != ruleNameEdit->text())
00554     manager->setRuleName(rule,ruleNameEdit->text());
00555 }
00556 
00557 void RuleEditWidget::updateRule()
00558 {
00559   KScoringRule *rule = manager->findRule(oldRuleName);
00560   if (rule) updateRule(rule);
00561 }
00562 
00563 void RuleEditWidget::slotAddGroup()
00564 {
00565   QString grp = groupsBox->currentText();
00566   if ( grp.isEmpty() )
00567       return;
00568   QString txt = groupsEdit->text().stripWhiteSpace();
00569   if ( txt == ".*" || txt.isEmpty() ) groupsEdit->setText(grp);
00570   else groupsEdit->setText(txt + ";" + grp);
00571 }
00572 
00573 void RuleEditWidget::setDirty()
00574 {
00575   kdDebug(5100) << "RuleEditWidget::setDirty()" << endl;
00576   if (dirty) return;
00577   dirty = true;
00578 }
00579 
00580 void RuleEditWidget::slotShrink()
00581 {
00582   emit(shrink());
00583 }
00584 
00585 void RuleEditWidget::slotExpireEditChanged(int value)
00586 {
00587   expireEdit->setSuffix(i18n(" day", " days", value));
00588 }
00589 
00590 //============================================================================
00591 //
00592 // class RuleListWidget (the widget for managing a list of rules)
00593 //
00594 //============================================================================
00595 RuleListWidget::RuleListWidget(KScoringManager *m, bool standalone, QWidget *p, const char *n)
00596   : QWidget(p,n), alone(standalone), manager(m)
00597 {
00598   kdDebug(5100) << "RuleListWidget::RuleListWidget()" << endl;
00599   if (!n) setName("RuleListWidget");
00600   QVBoxLayout *topL = new QVBoxLayout(this,standalone? 0:5,KDialog::spacingHint());
00601   ruleList = new KListBox(this);
00602   if (standalone) {
00603     connect(ruleList,SIGNAL(doubleClicked(QListBoxItem*)),
00604             this,SLOT(slotEditRule(QListBoxItem*)));
00605     connect(ruleList,SIGNAL(returnPressed(QListBoxItem*)),
00606             this,SLOT(slotEditRule(QListBoxItem*)));
00607   }
00608   connect(ruleList, SIGNAL(currentChanged(QListBoxItem*)),
00609           this, SLOT(slotRuleSelected(QListBoxItem*)));
00610   topL->addWidget(ruleList);
00611 
00612   QHBoxLayout *btnL = new QHBoxLayout( topL, KDialog::spacingHint() );
00613   mRuleUp = new QPushButton( this );
00614   mRuleUp->setPixmap( BarIcon( "up", KIcon::SizeSmall ) );
00615   QToolTip::add( mRuleUp, i18n("Move rule up") );
00616   btnL->addWidget( mRuleUp );
00617   connect( mRuleUp, SIGNAL( clicked() ), SLOT( slotRuleUp() ) );
00618   mRuleDown = new QPushButton( this );
00619   mRuleDown->setPixmap( BarIcon( "down", KIcon::SizeSmall ) );
00620   QToolTip::add( mRuleDown, i18n("Move rule down") );
00621   btnL->addWidget( mRuleDown );
00622   connect( mRuleDown, SIGNAL( clicked() ), SLOT( slotRuleDown() ) );
00623 
00624   btnL = new QHBoxLayout( topL, KDialog::spacingHint() );
00625   editRule=0L;
00626   newRule = new QPushButton(this);
00627   newRule->setPixmap( BarIcon( "filenew", KIcon::SizeSmall ) );
00628   QToolTip::add(newRule,i18n("New rule")),
00629   btnL->addWidget(newRule);
00630   connect(newRule, SIGNAL(clicked()), this, SLOT(slotNewRule()));
00631   // if we're standalone, we need an additional edit button
00632   if (standalone) {
00633     editRule = new QPushButton(this);
00634     editRule->setIconSet( BarIconSet("edit", KIcon::SizeSmall) );
00635     QToolTip::add(editRule,i18n("Edit rule"));
00636     btnL->addWidget(editRule);
00637     connect(editRule,SIGNAL(clicked()),this,SLOT(slotEditRule()));
00638   }
00639   delRule = new QPushButton(this);
00640   delRule->setIconSet( BarIconSet( "editdelete", KIcon::SizeSmall ) );
00641   QToolTip::add(delRule,i18n("Remove rule"));
00642   btnL->addWidget(delRule);
00643   connect(delRule, SIGNAL(clicked()), this, SLOT(slotDelRule()));
00644   copyRule = new QPushButton(this);
00645   copyRule->setIconSet(BarIconSet("editcopy", KIcon::SizeSmall));
00646   QToolTip::add(copyRule,i18n("Copy rule"));
00647   btnL->addWidget(copyRule);
00648   connect(copyRule, SIGNAL(clicked()), this, SLOT(slotCopyRule()));
00649 
00650   // the group filter
00651   QBoxLayout *filterL = new QVBoxLayout(topL,KDialog::spacingHint());
00652   KComboBox *filterBox = new KComboBox(this);
00653   QStringList l = m->getGroups();
00654   filterBox->insertItem(i18n("<all groups>"));
00655   filterBox->insertStringList(l);
00656   filterBox->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
00657   connect(filterBox,SIGNAL(activated(const QString&)),
00658           this,SLOT(slotGroupFilter(const QString&)));
00659   slotGroupFilter(i18n("<all groups>"));
00660   QLabel *lab = new QLabel(filterBox,i18n("Sho&w only rules for group:"),this);
00661   filterL->addWidget(lab);
00662   filterL->addWidget(filterBox);
00663 
00664   connect(manager,SIGNAL(changedRules()),
00665           this,SLOT(updateRuleList()));
00666   connect(manager,SIGNAL(changedRuleName(const QString&,const QString&)),
00667           this,SLOT(slotRuleNameChanged(const QString&,const QString&)));
00668 
00669   updateRuleList();
00670   updateButton();
00671 }
00672 
00673 RuleListWidget::~RuleListWidget()
00674 {
00675 }
00676 
00677 void RuleListWidget::updateButton()
00678 {
00679   bool state = ruleList->count() > 0;
00680   if(editRule)
00681     editRule->setEnabled(state);
00682   delRule->setEnabled(state);
00683   copyRule->setEnabled(state);
00684 
00685   QListBoxItem *item = ruleList->item( ruleList->currentItem() );
00686   if ( item ) {
00687     mRuleUp->setEnabled( item->prev() != 0 );
00688     mRuleDown->setEnabled( item->next() != 0 );
00689   }
00690 }
00691 
00692 void RuleListWidget::updateRuleList()
00693 {
00694   emit leavingRule();
00695   kdDebug(5100) << "RuleListWidget::updateRuleList()" << endl;
00696   QString curr = ruleList->currentText();
00697   ruleList->clear();
00698   if (group == i18n("<all groups>")) {
00699     QStringList l = manager->getRuleNames();
00700     ruleList->insertStringList(l);
00701   } else {
00702     KScoringManager::ScoringRuleList l = manager->getAllRules();
00703     for (KScoringRule* rule = l.first(); rule; rule = l.next() ) {
00704       if (rule->matchGroup(group)) ruleList->insertItem(rule->getName());
00705     }
00706   }
00707   int index = setCurrentItem(ruleList,curr);
00708   if (index <0) {
00709     ruleList->setCurrentItem(0);
00710     slotRuleSelected(ruleList->currentText());
00711   }
00712   else {
00713     slotRuleSelected(curr);
00714   }
00715 }
00716 
00717 void RuleListWidget::updateRuleList(const KScoringRule *rule)
00718 {
00719   kdDebug(5100) << "RuleListWidget::updateRuleList(" << rule->getName() << ")" << endl;
00720   QString name = rule->getName();
00721   updateRuleList();
00722   slotRuleSelected(name);
00723 }
00724 
00725 void RuleListWidget::slotRuleNameChanged(const QString& oldName, const QString& newName)
00726 {
00727   int ind = ruleList->currentItem();
00728   for (uint i=0;i<ruleList->count();++i)
00729     if (ruleList->text(i) == oldName) {
00730       ruleList->changeItem(newName,i);
00731       ruleList->setCurrentItem(ind);
00732       return;
00733     }
00734 }
00735 
00736 void RuleListWidget::slotEditRule(const QString& s)
00737 {
00738   emit ruleEdited(s);
00739 }
00740 
00741 void RuleListWidget::slotEditRule()
00742 {
00743   if (ruleList->currentItem() >= 0) {
00744     emit ruleEdited(ruleList->currentText());
00745   }
00746   else if (ruleList->count() == 0)
00747     emit ruleEdited(QString::null);
00748 }
00749 
00750 void RuleListWidget::slotEditRule(QListBoxItem* item)
00751 {
00752   slotEditRule(item->text());
00753 }
00754 
00755 void RuleListWidget::slotGroupFilter(const QString& s)
00756 {
00757   group = s;
00758   updateRuleList();
00759 }
00760 
00761 void RuleListWidget::slotRuleSelected(const QString& ruleName)
00762 {
00763   emit leavingRule();
00764   kdDebug(5100) << "RuleListWidget::slotRuleSelected(" << ruleName << ")" << endl;
00765   if (ruleName != ruleList->currentText()) {
00766     setCurrentItem(ruleList,ruleName);
00767   }
00768   updateButton();
00769   emit ruleSelected(ruleName);
00770 }
00771 
00772 void RuleListWidget::slotRuleSelected(QListBoxItem *item)
00773 {
00774   if (!item) return;
00775   QString ruleName = item->text();
00776   slotRuleSelected(ruleName);
00777 }
00778 
00779 void RuleListWidget::slotRuleSelected(int index)
00780 {
00781   uint idx = index;
00782   if (idx >= ruleList->count()) return;
00783   QString ruleName = ruleList->text(index);
00784   slotRuleSelected(ruleName);
00785 }
00786 
00787 void RuleListWidget::slotNewRule()
00788 {
00789   emit leavingRule();
00790   KScoringRule *rule = manager->addRule();
00791   updateRuleList(rule);
00792   if (alone) slotEditRule(rule->getName());
00793   updateButton();
00794 }
00795 
00796 void RuleListWidget::slotDelRule()
00797 {
00798   KScoringRule *rule = manager->findRule(ruleList->currentText());
00799   if (rule)
00800     manager->deleteRule(rule);
00801   // goto the next rule
00802   if (!alone) slotEditRule();
00803   updateButton();
00804 }
00805 
00806 void RuleListWidget::slotCopyRule()
00807 {
00808   emit leavingRule();
00809   QString ruleName = ruleList->currentText();
00810   KScoringRule *rule = manager->findRule(ruleName);
00811   if (rule) {
00812     KScoringRule *nrule = manager->copyRule(rule);
00813     updateRuleList(nrule);
00814     slotEditRule(nrule->getName());
00815   }
00816   updateButton();
00817 }
00818 
00819 void RuleListWidget::slotRuleUp()
00820 {
00821   KScoringRule *rule = 0, *below = 0;
00822   QListBoxItem *item = ruleList->item( ruleList->currentItem() );
00823   if ( item ) {
00824     rule = manager->findRule( item->text() );
00825     item = item->prev();
00826     if ( item )
00827       below = manager->findRule( item->text() );
00828   }
00829   if ( rule && below )
00830     manager->moveRuleAbove( rule, below );
00831   updateRuleList();
00832   updateButton();
00833 }
00834 
00835 void RuleListWidget::slotRuleDown()
00836 {
00837   KScoringRule *rule = 0, *above = 0;
00838   QListBoxItem *item = ruleList->item( ruleList->currentItem() );
00839   if ( item ) {
00840     rule = manager->findRule( item->text() );
00841     item = item->next();
00842     if ( item )
00843       above = manager->findRule( item->text() );
00844   }
00845   if ( rule && above )
00846     manager->moveRuleBelow( rule, above );
00847   updateRuleList();
00848   updateButton();
00849 }
00850 
00851 //============================================================================
00852 //
00853 // class KScoringEditor (the score edit dialog)
00854 //
00855 //============================================================================
00856 KScoringEditor* KScoringEditor::scoreEditor = 0;
00857 
00858 KScoringEditor::KScoringEditor(KScoringManager* m,
00859                                QWidget *parent, const char *name)
00860   : KDialogBase(parent,name,false,i18n("Rule Editor"),Ok|Apply|Cancel,Ok,true), manager(m)
00861 {
00862   manager->pushRuleList();
00863   if (!scoreEditor) scoreEditor = this;
00864   kdDebug(5100) << "KScoringEditor::KScoringEditor()" << endl;
00865   if (!name) setName("KScoringEditor");
00866   // the left side gives an overview about all rules, the right side
00867   // shows a detailed view of an selected rule
00868   QWidget *w = new QWidget(this);
00869   setMainWidget(w);
00870   QHBoxLayout *hbl = new QHBoxLayout(w,0,spacingHint());
00871   ruleLister = new RuleListWidget(manager,false,w);
00872   hbl->addWidget(ruleLister);
00873   ruleEditor = new RuleEditWidget(manager,w);
00874   hbl->addWidget(ruleEditor);
00875   connect(ruleLister,SIGNAL(ruleSelected(const QString&)),
00876           ruleEditor, SLOT(slotEditRule(const QString&)));
00877   connect(ruleLister, SIGNAL(leavingRule()),
00878           ruleEditor, SLOT(updateRule()));
00879   connect(ruleEditor, SIGNAL(shrink()), SLOT(slotShrink()));
00880   connect(this,SIGNAL(finished()),SLOT(slotFinished()));
00881   ruleLister->slotRuleSelected(0);
00882   resize(550, sizeHint().height());
00883 }
00884 
00885 void KScoringEditor::setDirty()
00886 {
00887   QPushButton *applyBtn = actionButton(Apply);
00888   applyBtn->setEnabled(true);
00889 }
00890 
00891 KScoringEditor::~KScoringEditor()
00892 {
00893   scoreEditor = 0;
00894 }
00895 
00896 KScoringEditor* KScoringEditor::createEditor(KScoringManager* m,
00897                                              QWidget *parent, const char *name)
00898 {
00899   if (scoreEditor) return scoreEditor;
00900   else return new KScoringEditor(m,parent,name);
00901 }
00902 
00903 void KScoringEditor::setRule(KScoringRule* r)
00904 {
00905   kdDebug(5100) << "KScoringEditor::setRule(" << r->getName() << ")" << endl;
00906   QString ruleName = r->getName();
00907   ruleLister->slotRuleSelected(ruleName);
00908 }
00909 
00910 void KScoringEditor::slotShrink()
00911 {
00912   QTimer::singleShot(5, this, SLOT(slotDoShrink()));
00913 }
00914 
00915 void KScoringEditor::slotDoShrink()
00916 {
00917   updateGeometry();
00918   QApplication::sendPostedEvents();
00919   resize(width(),sizeHint().height());
00920 }
00921 
00922 void KScoringEditor::slotApply()
00923 {
00924   QString ruleName = ruleLister->currentRule();
00925   KScoringRule *rule = manager->findRule(ruleName);
00926   if (rule) {
00927     ruleEditor->updateRule(rule);
00928     ruleLister->updateRuleList(rule);
00929   }
00930   manager->removeTOS();
00931   manager->pushRuleList();
00932 }
00933 
00934 void KScoringEditor::slotOk()
00935 {
00936   slotApply();
00937   manager->removeTOS();
00938   KDialogBase::slotOk();
00939   manager->editorReady();
00940 }
00941 
00942 void KScoringEditor::slotCancel()
00943 {
00944   manager->popRuleList();
00945   KDialogBase::slotCancel();
00946 }
00947 
00948 void KScoringEditor::slotFinished()
00949 {
00950   delayedDestruct();
00951 }
00952 
00953 //============================================================================
00954 //
00955 // class KScoringEditorWidgetDialog (a dialog for the KScoringEditorWidget)
00956 //
00957 //============================================================================
00958 KScoringEditorWidgetDialog::KScoringEditorWidgetDialog(KScoringManager *m, const QString& r, QWidget *p, const char *n)
00959   : KDialogBase(p,n,true,i18n("Edit Rule"),
00960                 KDialogBase::Ok|KDialogBase::Apply|KDialogBase::Close,
00961                 KDialogBase::Ok,true),
00962     manager(m), ruleName(r)
00963 {
00964   QFrame *f = makeMainWidget();
00965   QBoxLayout *topL = new QVBoxLayout(f);
00966   ruleEditor = new RuleEditWidget(manager,f);
00967   connect(ruleEditor, SIGNAL(shrink()), SLOT(slotShrink()));
00968   topL->addWidget(ruleEditor);
00969   ruleEditor->slotEditRule(ruleName);
00970   resize(0,0);
00971 }
00972 
00973 void KScoringEditorWidgetDialog::slotApply()
00974 {
00975   KScoringRule *rule = manager->findRule(ruleName);
00976   if (rule) {
00977     ruleEditor->updateRule(rule);
00978     ruleName = rule->getName();
00979   }
00980 }
00981 
00982 void KScoringEditorWidgetDialog::slotOk()
00983 {
00984   slotApply();
00985   KDialogBase::slotOk();
00986 }
00987 
00988 void KScoringEditorWidgetDialog::slotShrink()
00989 {
00990   QTimer::singleShot(5, this, SLOT(slotDoShrink()));
00991 }
00992 
00993 void KScoringEditorWidgetDialog::slotDoShrink()
00994 {
00995   updateGeometry();
00996   QApplication::sendPostedEvents();
00997   resize(width(),sizeHint().height());
00998 }
00999 
01000 //============================================================================
01001 //
01002 // class KScoringEditorWidget (a reusable widget for config dialog...)
01003 //
01004 //============================================================================
01005 KScoringEditorWidget::KScoringEditorWidget(KScoringManager *m,QWidget *p, const char *n)
01006   : QWidget(p,n), manager(m)
01007 {
01008   QBoxLayout *topL = new QVBoxLayout(this);
01009   ruleLister = new RuleListWidget(manager,true,this);
01010   topL->addWidget(ruleLister);
01011   connect(ruleLister,SIGNAL(ruleEdited(const QString&)),
01012           this,SLOT(slotRuleEdited(const QString &)));
01013 }
01014 
01015 KScoringEditorWidget::~KScoringEditorWidget()
01016 {
01017   manager->editorReady();
01018 }
01019 
01020 void KScoringEditorWidget::slotRuleEdited(const QString& ruleName)
01021 {
01022   KScoringEditorWidgetDialog dlg(manager,ruleName,this);
01023   dlg.exec();
01024   ruleLister->updateRuleList();
01025 }
01026 
01027 #include "kscoringeditor.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys