filters

dialog.cpp

00001 /*
00002  * Copyright (c) 2002-2003 Nicolas HADACEK (hadacek@kde.org)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "dialog.h"
00021 #include "dialog.moc"
00022 
00023 #include <qhbox.h>
00024 #include <qvgroupbox.h>
00025 #include <qregexp.h>
00026 #include <qapplication.h>
00027 #include <qgrid.h>
00028 #include <qlabel.h>
00029 #include <qlayout.h>
00030 #include <qbuttongroup.h>
00031 #include <qradiobutton.h>
00032 #include <qwhatsthis.h>
00033 #include <qcheckbox.h>
00034 
00035 #include <klocale.h>
00036 #include <kdebug.h>
00037 #include <klineedit.h>
00038 
00039 
00040 //-----------------------------------------------------------------------------
00041 SelectionRange::SelectionRange(const QString &s)
00042 {
00043     // fill
00044     QValueVector<QPair<uint, uint> > r;
00045     QStringList list = QStringList::split(',', s);
00046     QRegExp range("^([0-9]+)\\-([0-9]+)$");
00047     QRegExp one("^[0-9]+$");
00048     for (QStringList::iterator it = list.begin(); it!=list.end(); ++it) {
00049         if ( one.exactMatch(*it) ) {
00050             uint p = (*it).toUInt();
00051             r.push_back( qMakePair(p, p) );
00052         } else if ( range.exactMatch(*it) ) {
00053             uint p1 = range.cap(1).toUInt();
00054             uint p2 = range.cap(2).toUInt();
00055             if ( p1>p2 ) continue;
00056             r.push_back( qMakePair(p1, p2) );
00057         }
00058     }
00059 
00060     // order
00061     QPair<uint, uint> tmp;
00062     for (uint i=1; i<r.size(); i++)
00063         if ( r[i].first<r[i-1].first )
00064             qSwap(r[i-1], r[i]);
00065 
00066     // coalesce
00067     for (uint i=0; i<r.size(); i++)
00068         if ( i!=0 && r[i].first<=tmp.second )
00069             tmp.second = kMax(tmp.second, r[i].second);
00070         else {
00071             _ranges.push_back(r[i]);
00072             tmp = r[i];
00073             kdDebug(30516) << "selection range: (" << tmp.first << ","
00074                            << tmp.second << ") " << endl;
00075         }
00076 }
00077 
00078 uint SelectionRange::nbPages() const
00079 {
00080     uint nb = 0;
00081     for (uint i=0; i<_ranges.size(); i++)
00082         nb += _ranges[i].second - _ranges[i].first + 1;
00083     return nb;
00084 }
00085 
00086 SelectionRangeIterator::SelectionRangeIterator(const SelectionRange &range)
00087     : _ranges(range._ranges)
00088 {
00089     toFirst();
00090 }
00091 
00092 int SelectionRangeIterator::toFirst()
00093 {
00094     if ( _ranges.size()==0 ) _current = -1;
00095     else {
00096         _index = 0;
00097         _current = _ranges[0].first;
00098     }
00099     return _current;
00100 }
00101 
00102 int SelectionRangeIterator::next()
00103 {
00104     if ( _current==-1 ) return -1;
00105     if ( _current==int(_ranges[_index].second) ) {
00106         _index++;
00107         _current = (_index==_ranges.size() ? -1
00108                     : int(_ranges[_index].first));
00109     } else _current++;
00110     return _current;
00111 }
00112 
00113 //-----------------------------------------------------------------------------
00114 namespace PDFImport
00115 {
00116 
00117 Dialog::Dialog(uint nbPages, bool isEncrypted, QWidget *widget)
00118     : KDialogBase(Plain, i18n("KWord's PDF Import Filter"), Ok|Cancel, Ok,
00119                   widget, "pdf_import_dialog"), _nbPages(nbPages)
00120 {
00121     QApplication::restoreOverrideCursor();
00122 
00123     QVBoxLayout *top = new QVBoxLayout(plainPage(), KDialogBase::marginHint(),
00124                                        KDialogBase::spacingHint());
00125 
00126     // page selection
00127     QVGroupBox *gbox = new QVGroupBox(i18n("Page Selection"), plainPage());
00128     gbox->setInsideSpacing(KDialogBase::spacingHint());
00129     top->addWidget(gbox);
00130     _group = new QButtonGroup;
00131     _allButton = new QRadioButton(i18n("All (%1 pages)").arg(nbPages), gbox);
00132     _allButton->setChecked(true);
00133     _group->insert(_allButton);
00134     QHBox *hbox = new QHBox(gbox);
00135     _rangeButton = new QRadioButton(i18n("Range:"), hbox);
00136     _group->insert(_rangeButton);
00137     _range = new KLineEdit(hbox);
00138     _range->setFocus();
00139     connect(_range, SIGNAL(textChanged(const QString &)),
00140             SLOT(rangeChanged(const QString &)));
00141 
00142     // options
00143     _images = new QCheckBox(i18n("Import images"), plainPage());
00144     _images->setChecked(true);
00145     top->addWidget(_images);
00146     _smart = new QCheckBox(i18n("\"Smart\" mode"), plainPage());
00147     _smart->setChecked(true);
00148     QWhatsThis::add(_smart,
00149                     i18n("Removes returns and hyphens at end of line. "
00150                          "Also tries to compute the paragraph alignment. "
00151                          "Note that the layout of some pages can "
00152                          "get messed up."));
00153     top->addWidget(_smart);
00154 
00155     // passwords
00156     gbox = new QVGroupBox(i18n("Passwords"), plainPage());
00157     top->addWidget(gbox);
00158     QGrid *grid = new QGrid(2, gbox);
00159     grid->setSpacing(KDialogBase::spacingHint());
00160     (void)new QLabel(i18n("Owner:"), grid);
00161     _owner = new KLineEdit(grid);
00162     _owner->setEchoMode(QLineEdit::Password);
00163     (void)new QLabel(i18n("User:"), grid);
00164     _user = new KLineEdit(grid);
00165     _user->setEchoMode(QLineEdit::Password);
00166     grid->setEnabled(isEncrypted);
00167 }
00168 
00169 Dialog::~Dialog()
00170 {
00171     delete _group;
00172     QApplication::setOverrideCursor(Qt::waitCursor);
00173 }
00174 
00175 void Dialog::rangeChanged(const QString &)
00176 {
00177     _rangeButton->setChecked(true);
00178 }
00179 
00180 Options Dialog::options() const
00181 {
00182     Options o;
00183     o.range = SelectionRange( (_allButton->isChecked() ?
00184                              QString("1-%1").arg(_nbPages) : _range->text()) );
00185     o.ownerPassword = _owner->text();
00186     o.userPassword = _user->text();
00187     o.importImages = _images->isChecked();
00188     o.smart = _smart->isChecked();
00189     return o;
00190 }
00191 
00192 } // namespace
KDE Home | KDE Accessibility Home | Description of Access Keys