Back to index

scribus-ng  1.3.4.dfsg+svn20071115
tffilter.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "tffilter.h"
00008 #include "tffilter.moc"
00009 
00010 #include <qlayout.h>
00011 #include <qtooltip.h>
00012 #include "scribusapi.h"
00013 #include "scribuscore.h"
00014 #include "prefsmanager.h"
00015 #include "prefsfile.h"
00016 
00017 extern QPixmap SCRIBUS_API loadIcon(QString nam);
00018 
00019 tfFilter::tfFilter(QWidget *parent, const char *name,
00020                    int action, QString regExp, QString replace, QString pstyleName,
00021                    int less, int more, int style, bool match, bool enabled, bool regexp)
00022                    : QWidget(parent, name)
00023 {
00024        createWidget();
00025 
00026        firstChanged(action);
00027        currentAction = action;
00028        firstCombo->setCurrentItem(action);
00029        if (action == APPLY)
00030        {
00031               thirdCombo->setCurrentText(pstyleName);
00032               fourthChanged(style);
00033               fourthCombo->setCurrentItem(style);
00034               if (style == STARTS_WITH)
00035               {
00036                      fifthCombo->setCurrentText(regExp);
00037                      fifthRegexpCheck->setChecked(regexp);
00038                      if (match)
00039                             sixthCombo->setCurrentItem(0);
00040                      else
00041                             sixthCombo->setCurrentItem(1);
00042               }
00043               else if (style == LESS_THAN)
00044               {
00045                      if (less > 0)
00046                             fifthCombo->setCurrentText(QString("%1").arg(less));
00047               }
00048               else if (style == MORE_THAN)
00049               {
00050                      if (more > 0)
00051                             fifthCombo->setCurrentText(QString("%1").arg(more));
00052               }
00053        }
00054        else if (action == REMOVE) 
00055        {
00056               secondCombo->setCurrentText(regExp);
00057               secondRegexpCheck->setChecked(regexp);
00058        }
00059        else if (action == REPLACE)
00060        {
00061               secondCombo->setCurrentText(regExp);
00062               secondRegexpCheck->setChecked(regexp);
00063               thirdCombo->setCurrentText(replace);
00064        }
00065        enableCheck->setChecked(enabled);
00066        enableToggled(enabled);
00067 }
00068 
00069 tfFilter::tfFilter(QWidget *parent, const char *name) : QWidget(parent, name)
00070 {
00071        createWidget();
00072 }
00073 
00074 void tfFilter::createWidget()
00075 {
00076        firstCombo = NULL;
00077        firstLabel = NULL;
00078        secondCombo = NULL;
00079        secondLabel = NULL;
00080        thirdCombo = NULL;
00081        thirdLabel = NULL;
00082        fourthCombo = NULL;
00083        fourthLabel = NULL;
00084        fifthCombo = NULL;
00085        fifthLabel = NULL;
00086        sixthCombo = NULL;
00087        secondRegexpCheck = NULL;
00088 //     thirdRegexpCheck = NULL;
00089        fifthRegexpCheck = NULL;
00090 
00091        prefs = PrefsManager::instance()->prefsFile->getPluginContext("TextFilter");
00092        history = prefs->getTable("history");
00093 
00094        QHBoxLayout *layout = new QHBoxLayout(this);
00095        layout->setMargin(0);
00096 
00097        enableCheck = new QCheckBox(this, "enableCheck");
00098        enableCheck->setMaximumSize(QSize(25,25));
00099        enableCheck->setMinimumSize(QSize(25,25));
00100        enableCheck->setChecked(true);
00101        QToolTip::add(enableCheck, tr("Disable or enable this filter row"));
00102        layout->addWidget(enableCheck);
00103 
00104        actionFrame = new QFrame(this, "actionFrame");
00105        layout->addWidget(actionFrame);
00106 
00107        QBoxLayout* layout2 = new QVBoxLayout(actionFrame);
00108        alayout = new QHBoxLayout();
00109        alayout->setMargin(0);
00110        layout2->addLayout(alayout);
00111        layout2->addSpacing(4);
00112        blayout = new QHBoxLayout();
00113        blayout->setMargin(0);
00114        layout2->addLayout(blayout);
00115 
00116        currentAction = REMOVE;
00117        getFirstCombo();
00118 
00119 //     layout->addStretch(10);
00120 
00121        layout->addSpacing(20);
00122        removeButton = new QPushButton(loadIcon("22/list-remove.png"), 0, this, "removeButton");
00123        QToolTip::add(removeButton, tr("Remove this filter row"));
00124        removeButton->setMaximumSize(QSize(25,25));
00125        removeButton->setMinimumSize(QSize(25,25));
00126        layout->addWidget(removeButton);
00127        addButton = new QPushButton(loadIcon("22/list-add.png"), 0, this, "addButton");
00128        QToolTip::add(addButton, tr("Add a new filter row"));
00129        addButton->setMaximumSize(QSize(25,25));
00130        addButton->setMinimumSize(QSize(25,25));
00131        layout->addWidget(addButton);
00132 
00133        connect(enableCheck, SIGNAL(toggled(bool)), this, SLOT(enableToggled(bool)));
00134        connect(addButton, SIGNAL(clicked()), this, SLOT(addClick()));
00135        connect(removeButton, SIGNAL(clicked()), this, SLOT(removeClick()));
00136 }
00137 
00138 void tfFilter::setRemovable(bool b)
00139 {
00140        removeButton->setEnabled(b);
00141 }
00142 
00143 void tfFilter::enableToggled(bool on)
00144 {
00145        actionFrame->setEnabled(on);
00146 }
00147 
00148 void tfFilter::addClick()
00149 {
00150        emit addClicked(this);
00151 }
00152 
00153 void tfFilter::removeClick()
00154 {
00155        emit removeClicked(this);
00156 }
00157 
00158 void tfFilter::firstChanged(int index)
00159 {
00160        currentAction = index;
00161        getSecondCombo();
00162 }
00163 
00164 void tfFilter::secondChanged(int)
00165 {
00166 
00167 }
00168 
00169 void tfFilter::thirdChanged(int)
00170 {
00171 
00172 }
00173 
00174 void tfFilter::fourthChanged(int index)
00175 {
00176        switch (currentAction)
00177        {
00178               case APPLY:
00179                      thirdLabel->setText( tr("to"));
00180                      thirdLabel->show();
00181 //                   thirdRegexpCheck->hide();
00182                      switch (index)
00183                      {
00184                             case ALL_PARAGRAPHS:
00185                                    fourthLabel->hide();
00186                                    fifthCombo->hide();
00187                                    fifthLabel->hide();
00188                                    sixthCombo->hide();
00189                                    fifthRegexpCheck->hide();
00190                                    break;
00191                             case STARTS_WITH:
00192                                    fourthLabel->hide();
00193                                    fifthCombo->clear();
00194                                    fifthCombo->setEditable(true);
00195                                    fifthCombo->show();
00196                                    fifthRegexpCheck->show();
00197                                    fifthLabel->setText( tr("and"));
00198                                    fifthLabel->show();
00199                                    sixthCombo->clear();
00200                                    sixthCombo->setEditable(false);
00201                                    sixthCombo->insertItem( tr("remove match"));
00202                                    sixthCombo->insertItem( tr("do not remove match"));
00203                                    sixthCombo->show();
00204                                    break;
00205                             case LESS_THAN:
00206                             case MORE_THAN:
00207                                    fourthLabel->hide();
00208                                    fifthCombo->clear();
00209                                    fifthCombo->setEditable(true);
00210                                    fifthCombo->show();
00211                                    fifthRegexpCheck->hide();
00212                                    fifthLabel->setText( tr("words"));
00213                                    fifthLabel->show();
00214                                    sixthCombo->hide();
00215                                    break;
00216                      }
00217                      break;
00218        }
00219 }
00220 
00221 void tfFilter::fifthChanged(int)
00222 {
00223 
00224 }
00225 
00226 void tfFilter::sixthChanged(int)
00227 {
00228 
00229 }
00230 
00231 void tfFilter::getFirstCombo()
00232 {
00233        resetBRow();
00234        if (!firstCombo)
00235        {
00236               firstCombo = new QComboBox(0, actionFrame, "firstCombo");
00237               firstCombo->insertItem("");
00238               firstCombo->show();
00239               alayout->addWidget(firstCombo, -1);
00240               alayout->setSpacing(5);
00241               connect(firstCombo, SIGNAL(activated(int)), this, SLOT(firstChanged(int)));
00242        }
00243        if (!firstLabel)
00244        {
00245               firstLabel = new QLabel(actionFrame, "secondLabel");
00246               alayout->addWidget(firstLabel, -1);
00247               firstLabel->hide();
00248        }
00249        firstCombo->clear();
00250        firstCombo->setMinimumSize(QSize(120, 0));
00251        firstCombo->insertItem( tr("Remove"));
00252        firstCombo->insertItem( tr("Replace"));
00253        firstCombo->insertItem( tr("Apply"));
00254        firstCombo->show();
00255        getSecondCombo();
00256 }
00257 
00258 void tfFilter::getSecondCombo()
00259 {
00260        resetBRow();
00261        if (!secondCombo)
00262        {
00263               secondCombo = new QComboBox(0, actionFrame, "secondCombo");
00264               secondCombo->insertItem("");
00265               secondCombo->show();
00266               alayout->addWidget(secondCombo, 8);
00267               connect(secondCombo, SIGNAL(activated(int)), this, SLOT(secondChanged(int)));
00268        }
00269        if (!secondRegexpCheck)
00270        {
00271               secondRegexpCheck = new QCheckBox(actionFrame, "secondRegexpCheck");
00272               QToolTip::add(secondRegexpCheck, tr("Value at the left is a regular expression"));
00273               secondRegexpCheck->show();
00274               alayout->addWidget(secondRegexpCheck, -1);
00275        }
00276        if (!secondLabel)
00277        {
00278               secondLabel = new QLabel(actionFrame, "secondLabel");
00279               secondLabel->hide();
00280               alayout->addWidget(secondLabel, -1);
00281        }
00282        switch (currentAction)
00283        {
00284               case REPLACE:
00285                      firstLabel->hide();
00286                      secondLabel->setText( tr("with"));
00287                      secondLabel->show();
00288                      secondCombo->setEditable(true);
00289                      secondCombo->clear();
00290                      secondCombo->show();
00291                      secondRegexpCheck->show();
00292                      break;
00293               case APPLY:
00294                      firstLabel->hide();
00295                      secondLabel->hide();
00296                      secondCombo->setEditable(false);
00297                      secondCombo->clear();
00298                      secondCombo->insertItem( tr("paragraph style"));
00299                      secondRegexpCheck->hide();
00300                      break;
00301               case REMOVE:
00302                      firstLabel->setText( tr("all instances of"));
00303                      firstLabel->show();
00304                      secondCombo->clear();
00305                      secondCombo->setEditable(true);
00306                      secondCombo->show();
00307                      secondLabel->hide();
00308                      secondRegexpCheck->show();
00309                      break;
00310        }
00311        getThirdCombo(secondCombo->currentItem());
00312 }
00313 
00314 void tfFilter::getThirdCombo(int)
00315 {
00316        if (!thirdCombo)
00317        {
00318               thirdCombo = new QComboBox(0, actionFrame, "thirdCombo");
00319               thirdCombo->insertItem("");
00320               thirdCombo->hide();
00321               alayout->addWidget(thirdCombo, 8);
00322               connect(thirdCombo, SIGNAL(activated(int)), this, SLOT(thirdChanged(int)));
00323        }
00324 //     if (!thirdRegexpCheck)
00325 //     {
00326 //            thirdRegexpCheck = new QCheckBox(actionFrame, "secondRegexpCheck");
00327 //            QToolTip::add(thirdRegexpCheck, tr("Value is treated as a regular expression"));
00328 //            thirdRegexpCheck->hide();
00329 //            alayout->addWidget(thirdRegexpCheck, -1);
00330 //     }
00331        if (!thirdLabel)
00332        {
00333               thirdLabel = new QLabel(actionFrame, "thirdLabel");
00334               thirdLabel->hide();
00335               blayout->addWidget(thirdLabel, -1);
00336               blayout->addSpacing(5);
00337        }
00338        switch (currentAction)
00339        {
00340               case REPLACE:
00341                      thirdCombo->clear();
00342                      thirdCombo->setEditable(true);
00343                      thirdCombo->show();
00344 //                   thirdRegexpCheck->show();
00345                      break;
00346               case APPLY:
00347                      thirdCombo->clear();
00348                      getParagraphStyles();
00349                      thirdCombo->setEditable(true);
00350                      thirdCombo->show();
00351 //                   thirdRegexpCheck->hide();
00352                      getFourthCombo();
00353                      break;
00354               case REMOVE:
00355                      thirdCombo->hide();
00356 //                   thirdRegexpCheck->hide();
00357                      break;
00358        }
00359 }
00360 
00361 void tfFilter::getFourthCombo()
00362 {
00363        if (!fourthCombo)
00364        {
00365               fourthCombo = new QComboBox(0, actionFrame, "fourthCombo");
00366               fourthCombo->insertItem("");
00367               fourthCombo->hide();
00368               blayout->addWidget(fourthCombo, 8);
00369               connect(fourthCombo, SIGNAL(activated(int)), this, SLOT(fourthChanged(int)));
00370        }
00371        if (!fourthLabel)
00372        {
00373               fourthLabel = new QLabel(actionFrame, "fourthLabel");
00374               fourthLabel->hide();
00375               blayout->addWidget(fourthLabel, -1);
00376               blayout->addSpacing(5);
00377        }
00378        switch (currentAction)
00379        {
00380               case APPLY:
00381                      thirdLabel->setText( tr("to"));
00382                      thirdLabel->show();
00383                      fourthCombo->clear();
00384                      fourthCombo->insertItem( tr("all paragraphs"));
00385                      fourthCombo->insertItem( tr("paragraphs starting with"));
00386                      fourthCombo->insertItem( tr("paragraphs with less than"));
00387                      fourthCombo->insertItem( tr("paragraphs with more than"));
00388                      fourthCombo->setEditable(false);
00389                      fourthCombo->show();
00390                      fourthLabel->hide();
00391                      getFifthCombo();
00392                      break;
00393        }
00394 }
00395 
00396 void tfFilter::getFifthCombo()
00397 {
00398        if (!fifthCombo)
00399        {
00400               fifthCombo = new QComboBox(0, actionFrame, "fifthCombo");
00401               fifthCombo->insertItem("");
00402               fifthCombo->hide();
00403               blayout->addWidget(fifthCombo, 8);
00404               blayout->addSpacing(5);
00405               connect(fifthCombo, SIGNAL(activated(int)), this, SLOT(fifthChanged(int)));
00406        }
00407        if (!fifthRegexpCheck)
00408        {
00409               fifthRegexpCheck = new QCheckBox(actionFrame, "secondRegexpCheck");
00410               QToolTip::add(fifthRegexpCheck, tr("Value at the left is a regular expression"));
00411               fifthRegexpCheck->hide();
00412               blayout->addWidget(fifthRegexpCheck, -1);
00413               blayout->addSpacing(5);
00414        }
00415        if (!fifthLabel)
00416        {
00417               fifthLabel = new QLabel(actionFrame, "fifthLabel");
00418               fifthLabel->hide();
00419               blayout->addWidget(fifthLabel, -1);
00420               blayout->addSpacing(5);
00421        }
00422        getSixthCombo();
00423 }
00424 
00425 void tfFilter::getSixthCombo()
00426 {
00427        if (!sixthCombo)
00428        {
00429               sixthCombo = new QComboBox(0, actionFrame, "sixthCombo");
00430               sixthCombo->insertItem("");
00431               sixthCombo->hide();
00432               blayout->addWidget(sixthCombo, 7);
00433               connect(sixthCombo, SIGNAL(activated(int)), this, SLOT(sixthChanged(int)));
00434        }
00435 }
00436 
00437 void tfFilter::resetBRow()
00438 {
00439        if (thirdLabel)
00440        {
00441               thirdLabel->hide();
00442               thirdLabel->setText("");
00443        }
00444        if (fourthCombo)
00445        {
00446               fourthCombo->hide();
00447               fourthCombo->clear();
00448        }
00449        if (fourthLabel)
00450        {
00451               fourthLabel->hide();
00452               fourthLabel->setText("");
00453        }
00454        if (fifthCombo)
00455        {
00456               fifthCombo->hide();
00457               fifthCombo->clear();
00458        }
00459        if (fifthRegexpCheck)
00460        {
00461               fifthRegexpCheck->hide();
00462        }
00463        if (fifthLabel)
00464        {
00465               fifthLabel->hide();
00466               fifthLabel->setText("");
00467        }
00468        if (sixthCombo)
00469        {
00470               sixthCombo->hide();
00471               sixthCombo->clear();
00472        }
00473 }
00474 
00475 void tfFilter::getParagraphStyles()
00476 {
00477        thirdCombo->insertItem("");
00478        for (uint i = 0; i < ScCore->primaryMainWindow()->doc->paragraphStyles().count(); ++i)
00479        {
00480               thirdCombo->insertItem(ScCore->primaryMainWindow()->doc->paragraphStyles()[i].name());
00481        }
00482 }
00483 
00484 int tfFilter::getAction()
00485 {
00486        return currentAction;
00487 }
00488 
00489 QString tfFilter::regExp()
00490 {
00491        switch (currentAction)
00492        {
00493               case REMOVE:
00494               case REPLACE:
00495                      if (!secondCombo)
00496                             return "";
00497                      return QString(secondCombo->currentText());
00498                      break;
00499               case APPLY:
00500                      if (!fifthCombo)
00501                             return "";
00502                      return QString(fifthCombo->currentText());
00503                      break;
00504               default:
00505                      return "";
00506                      break;
00507        }
00508        return "";
00509 }
00510 
00511 QString tfFilter::replaceWith()
00512 {
00513        if (!thirdCombo)
00514               return "";
00515        return QString(thirdCombo->currentText());
00516 }
00517 
00518 QString tfFilter::getPStyleName()
00519 {
00520        if (!thirdCombo)
00521               return "";
00522        return QString(thirdCombo->currentText());
00523 }
00524 
00525 int tfFilter::getLessThan()
00526 {
00527        if (!fifthCombo)
00528               return -1;
00529        bool ok = false;
00530        QString text = QString(fifthCombo->currentText());
00531        int i = text.toInt(&ok);
00532        if (ok)
00533               return i;
00534        else
00535               return -1;
00536 }
00537 
00538 int tfFilter::getStyle()
00539 {
00540        if (!fourthCombo)
00541               return 0;
00542        return fourthCombo->currentItem();
00543 }
00544 
00545 bool tfFilter::removeMatch()
00546 {
00547        if (!sixthCombo)
00548               return false;
00549        return sixthCombo->currentItem() == 0;
00550 }
00551 
00552 int tfFilter::getMoreThan()
00553 {
00554        return getLessThan();
00555 }
00556 
00557 bool tfFilter::isEnabled()
00558 {
00559        return enableCheck->isChecked();
00560 }
00561 
00562 bool tfFilter::isRegExp()
00563 {
00564        bool checked = false;
00565        if ((currentAction == REPLACE) || (currentAction == REMOVE))
00566               checked = secondRegexpCheck->isChecked();
00567        else if (currentAction == APPLY)
00568               checked = fifthRegexpCheck->isChecked();
00569        return checked;
00570 }
00571 
00572 tfFilter::~tfFilter()
00573 {
00574 
00575 }