Back to index

scribus-ng  1.3.4.dfsg+svn20071115
tfdia.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 "tfdia.h"
00008 #include "tfdia.moc"
00009 #include "prefsmanager.h"
00010 #include "prefsfile.h"
00011 #include <qpixmap.h>
00012 #include <qtooltip.h>
00013 #include <qlabel.h>
00014 #include <qframe.h>
00015 #include <qrect.h>
00016 #include <prefstable.h>
00017 #include <qsizepolicy.h>
00018 
00019 #include "commonstrings.h"
00020 
00021 extern QPixmap loadIcon(QString nam);
00022 
00023 tfDia::tfDia() : QDialog()
00024 {
00025        setCaption( tr("Create filter"));
00026        setIcon(loadIcon("AppIcon.png"));
00027        setMinimumWidth(524);
00028        prefs = PrefsManager::instance()->prefsFile->getPluginContext("TextFilter");
00029        setGeometry(prefs->getInt("x", 0),
00030                 prefs->getInt("y", 0),
00031                 prefs->getInt("width", 400),
00032                 prefs->getInt("height", 300));
00033        createLayout();
00034        resize(width() + 10, height() + 10);
00035 }
00036 
00037 void tfDia::createLayout()
00038 {
00039        currentFilter = "tf_lastUsed";
00040        currentIndex = 0;
00041        
00042        layout = new QVBoxLayout(this);
00043 
00044        QBoxLayout* layout1 = new QHBoxLayout(0, 5, 5, "layout1");
00045        clearButton = new QPushButton( tr("C&lear"), this, "clearButton");
00046        layout1->addWidget(clearButton);
00047        layout1->addStretch(10);
00048        deleteButton = new QPushButton( tr("&Delete"), this, "deleteButton");
00049        deleteButton->setEnabled(false);
00050        layout1->addWidget(deleteButton);
00051        filtersCombo = new QComboBox(0, this, "filtersCombo");
00052        filtersCombo->setMinimumSize(QSize(150, 0));
00053        filtersCombo->setEditable(false);
00054        filtersCombo->setDuplicatesEnabled(false);
00055        QToolTip::add(filtersCombo, tr("Choose a previously saved filter"));
00056        PrefsTable* filterNames = prefs->getTable("tf_Filters");
00057        filtersCombo->insertItem("");
00058        for (int i = 0; i < filterNames->height(); ++i)
00059        {
00060               QString fname = filterNames->get(i, 0, "");
00061               fname = fname.right(fname.length() - 3);
00062               filtersCombo->insertItem(fname);   
00063        }
00064        layout1->addWidget(filtersCombo);
00065        layout->addLayout(layout1);
00066 
00067        QBoxLayout* flayout = new QHBoxLayout(0,0,0, "flayout");
00068        QFrame* f = new QFrame(this, "f");
00069        f->setFrameStyle(QFrame::HLine | QFrame::Sunken);
00070        flayout->addWidget(f);
00071        layout->addLayout(flayout);
00072 
00073        
00074        qsv = new QScrollView(this, "qsv");
00075        QVBoxLayout *a1layout = new QVBoxLayout(0, 5, 12, "a1layout");
00076        vbox = new QFrame(this);
00077        vbox->setFixedWidth(qsv->viewport()->width());
00078        qsv->viewport()->resize(width() - 12, vbox->height());
00079        a1layout->addWidget(qsv);
00080        qsv->addChild(vbox);
00081        layout->addLayout(a1layout);
00082        
00083        alayout = new QVBoxLayout(vbox, 5,12, "alayout");
00084        
00085        createFilter(prefs->getTable("tf_lastUsed"));
00086        filters[0]->setRemovable((filters.size() >= 2));
00087        
00088        QBoxLayout* flayout2 = new QHBoxLayout(0,0,0, "flayout2");
00089        QFrame* f2 = new QFrame(this, "f2");
00090        f2->setFrameStyle(QFrame::HLine | QFrame::Sunken);
00091        flayout2->addWidget(f2);
00092        layout->addLayout(flayout2);
00093 
00094        QBoxLayout* layout2 = new QHBoxLayout(0, 5, 5, "layout2");
00095        saveEdit = new QLineEdit(this, "saveEdit");
00096        QToolTip::add(saveEdit, tr("Give a name to this filter for saving"));
00097        layout2->addWidget(saveEdit, 10);
00098        if (prefs->getBool("save_hint", true))
00099        {
00100               saveEdit->setText( tr("Give a name for saving"));
00101               prefs->set("save_hint", false);
00102        }
00103        layout2->addSpacing(20);
00104        okButton = new QPushButton(CommonStrings::tr_OK, this, "okButton");
00105        layout2->addWidget(okButton, 0);
00106        cancelButton = new QPushButton(CommonStrings::tr_Cancel, this, "cancelButton");
00107        layout2->addWidget(cancelButton, 0);
00108        layout->addLayout(layout2);
00109        
00110 
00111        connect(filtersCombo, SIGNAL(activated(const QString&)), this, SLOT(loadFilter(const QString&)));
00112        connect(clearButton, SIGNAL(clicked()), this, SLOT(clearClicked()));
00113        connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteClicked()));
00114        connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
00115        connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelClicked()));
00116        connect(saveEdit, SIGNAL(textChanged(const QString&)), this, SLOT(saveTextChanged(const QString&)));
00117 }
00118 
00119 void tfDia::createFilterRow(tfFilter* after)
00120 {
00121        tfFilter* tmp = new tfFilter(vbox, "tfFilter");
00122        if (after == NULL)
00123        {
00124               filters.push_back(tmp);
00125               alayout->addWidget(tmp);
00126        }
00127        else
00128        {
00129               std::vector<tfFilter*>::iterator it = filters.begin();
00130               uint i = 0;
00131               for (i= 0; i < filters.size(); ++i, ++it)
00132               {
00133                      if (filters[i] == after)
00134                      {
00135                             ++it;
00136                             ++i;
00137                             break;
00138                      }
00139               }
00140               filters.insert(it, tmp);
00141               alayout->insertWidget(static_cast<int>(i), tmp);
00142        }
00143        tmp->show();
00144        vbox->adjustSize();
00145        if (filters.size() == 2)
00146               filters[0]->setRemovable(true);
00147        else if (filters.size() == 1)
00148               filters[0]->setRemovable(false);
00149 
00150        connect(tmp, SIGNAL(addClicked(tfFilter*)), this, SLOT(createFilterRow(tfFilter*)));
00151        connect(tmp, SIGNAL(removeClicked(tfFilter*)), SLOT(removeRow(tfFilter*)));
00152 }
00153 
00154 void tfDia::createFilter(PrefsTable* table)
00155 {
00156        if (table->width() != 10)
00157               createFilterRow(NULL);
00158        else
00159        {
00160               for (uint i = 0; i < static_cast<uint>(table->height()); ++i)
00161               {
00162                      tfFilter* tmp = new tfFilter(vbox, "tfFilter",
00163                                                                        table->getInt(i, 0, 0),
00164                                                                        table->get(i, 1, ""),
00165                                                                        table->get(i, 2, ""),
00166                                                                        table->get(i, 3, ""),
00167                                                                        table->getInt(i, 4, 0),
00168                                                                        table->getInt(i, 5, 0),
00169                                                                        table->getInt(i, 6, 0),
00170                                                                        table->getBool(i, 7, true),
00171                                                                        table->getBool(i, 8, true),
00172                                                                        table->getBool(i, 9, false));
00173                      filters.push_back(tmp);
00174                      alayout->addWidget(tmp);
00175                      tmp->show();
00176                      vbox->adjustSize();
00177                      if (filters.size() == 2)
00178                             filters[0]->setRemovable(true);
00179                      connect(tmp, SIGNAL(addClicked(tfFilter*)), this, SLOT(createFilterRow(tfFilter*)));
00180                      connect(tmp, SIGNAL(removeClicked(tfFilter*)), this, SLOT(removeRow(tfFilter*)));
00181               }
00182        }
00183 }
00184 
00185 void tfDia::removeRow(tfFilter* tff)
00186 {
00187        std::vector<tfFilter*>::iterator it = filters.begin();
00188        for (uint i = 0; i < filters.size(); ++i, ++it)
00189        {
00190               if (filters[i] == tff)
00191               {
00192                      filters[i]->hide();
00193                      delete filters[i];
00194                      filters.erase(it);
00195               }
00196        }
00197        if (filters.size() == 1)
00198               filters[0]->setRemovable(false);
00199 }
00200 
00201 void tfDia::saveTextChanged(const QString& text)
00202 {
00203        filtersCombo->setCurrentItem(0);
00204        filtersCombo->setCurrentText(text);
00205 }
00206 
00207 void tfDia::clearClicked()
00208 {
00209        clear();
00210        createFilterRow(NULL);
00211 }
00212 
00213 void tfDia::clear()
00214 {
00215        for (uint i = 0; i < filters.size(); ++i)
00216        {
00217               filters[i]->hide();
00218               delete filters[i];
00219        }
00220        filters.clear();
00221 }
00222 
00223 void tfDia::okClicked()
00224 {
00225        storeLastFilter();
00226        if ((!saveEdit->text().isEmpty()) && (saveEdit->text() != tr("Give a name to this filter for saving")))
00227        {
00228               PrefsTable* savedFilters = prefs->getTable("tf_Filters");
00229               if (savedFilters->find(0, QString("tf_" + saveEdit->text())) == -1)
00230                      savedFilters->set(savedFilters->height(), 0, QString("tf_" + saveEdit->text()));
00231               prefs->removeTable("tf_" + saveEdit->text());
00232               PrefsTable* newFilter = prefs->getTable("tf_" + saveEdit->text());
00233               for (uint i = 0; i < filters.size(); ++i)
00234               {
00235                      writeFilterRow(newFilter, i, filters[i]);
00236               }
00237        }
00238        accept();
00239 }
00240 
00241 void tfDia::cancelClicked()
00242 {
00243        storeLastFilter();
00244        reject();
00245 }
00246 
00247 void tfDia::writeFilterRow(PrefsTable* table, int row, tfFilter* filter)
00248 {
00249        table->set(row, 0, filter->getAction());
00250        table->set(row, 1, filter->regExp());
00251        table->set(row, 2, filter->replaceWith());
00252        table->set(row, 3, filter->getPStyleName());
00253        table->set(row, 4, filter->getLessThan());
00254        table->set(row, 5, filter->getMoreThan());
00255        table->set(row, 6, filter->getStyle());
00256        table->set(row, 7, filter->removeMatch());
00257        table->set(row, 8, filter->isEnabled());
00258        table->set(row, 9, filter->isRegExp());
00259 }
00260 
00261 void tfDia::loadFilter(const QString& name)
00262 {
00263        if (currentFilter == "tf_lastUsed")
00264               storeLastFilter();
00265        if (filtersCombo->currentItem() == 0)
00266        {
00267               deleteButton->setEnabled(false);
00268               clear();
00269               createFilter(prefs->getTable("tf_lastUsed"));
00270               currentFilter = "tf_lastUsed";
00271               currentIndex = 0;
00272        }
00273        else if (prefs->containsTable("tf_" + name))
00274        {
00275               deleteButton->setEnabled(true);
00276               clear();
00277               createFilter(prefs->getTable("tf_"+name));
00278               currentFilter = "tf_" + name;
00279               currentIndex = filtersCombo->currentItem();
00280        }
00281        saveEdit->setText(name);
00282 }
00283 
00284 void tfDia::deleteClicked()
00285 {
00286        if (currentIndex != 0)
00287        {
00288               PrefsTable* t = prefs->getTable("tf_Filters");
00289               t->removeRow(0, currentFilter);
00290               prefs->removeTable(currentFilter);
00291               filtersCombo->removeItem(currentIndex);
00292               filtersCombo->setCurrentItem(0);
00293               clear();
00294               saveEdit->setText("");
00295               deleteButton->setEnabled(false);
00296               createFilter(prefs->getTable("tf_lastUsed"));
00297               currentIndex = 0;
00298               currentFilter = "tf_lastUsed";
00299        }
00300 }
00301 
00302 void tfDia::storeLastFilter()
00303 {
00304        prefs->removeTable("tf_lastUsed");
00305        PrefsTable* lastUsed = prefs->getTable("tf_lastUsed");
00306        for (uint i = 0; i < filters.size(); ++i)
00307        {
00308               writeFilterRow(lastUsed, i, filters[i]);
00309        }
00310 }
00311 
00312 void tfDia::resizeEvent(QResizeEvent*)
00313 {
00314        vbox->setFixedWidth(qsv->viewport()->width());
00315 }
00316 
00317 tfDia::~tfDia()
00318 {
00319        for (uint i = 0; i < filters.size(); ++i)
00320               delete filters[i];
00321        QRect r = geometry();
00322        prefs->set("x", r.x());
00323        prefs->set("y", r.y());
00324        prefs->set("width", width());
00325        prefs->set("height", height());
00326 }