libkdenetwork Library API Documentation

kscoringeditor.cpp

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