filters

dialog.cpp
1/*
2 * Copyright (c) 2002-2003 Nicolas HADACEK (hadacek@kde.org)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "dialog.h"
21#include "dialog.moc"
22
23#include <tqhbox.h>
24#include <tqvgroupbox.h>
25#include <tqregexp.h>
26#include <tqapplication.h>
27#include <tqgrid.h>
28#include <tqlabel.h>
29#include <tqlayout.h>
30#include <tqbuttongroup.h>
31#include <tqradiobutton.h>
32#include <tqwhatsthis.h>
33#include <tqcheckbox.h>
34
35#include <tdelocale.h>
36#include <kdebug.h>
37#include <klineedit.h>
38
39
40//-----------------------------------------------------------------------------
41SelectionRange::SelectionRange(const TQString &s)
42{
43 // fill
44 TQValueVector<TQPair<uint, uint> > r;
45 TQStringList list = TQStringList::split(',', s);
46 TQRegExp range("^([0-9]+)\\-([0-9]+)$");
47 TQRegExp one("^[0-9]+$");
48 for (TQStringList::iterator it = list.begin(); it!=list.end(); ++it) {
49 if ( one.exactMatch(*it) ) {
50 uint p = (*it).toUInt();
51 r.push_back( qMakePair(p, p) );
52 } else if ( range.exactMatch(*it) ) {
53 uint p1 = range.cap(1).toUInt();
54 uint p2 = range.cap(2).toUInt();
55 if ( p1>p2 ) continue;
56 r.push_back( qMakePair(p1, p2) );
57 }
58 }
59
60 // order
61 TQPair<uint, uint> tmp;
62 for (uint i=1; i<r.size(); i++)
63 if ( r[i].first<r[i-1].first )
64 tqSwap(r[i-1], r[i]);
65
66 // coalesce
67 for (uint i=0; i<r.size(); i++)
68 if ( i!=0 && r[i].first<=tmp.second )
69 tmp.second = kMax(tmp.second, r[i].second);
70 else {
71 _ranges.push_back(r[i]);
72 tmp = r[i];
73 kdDebug(30516) << "selection range: (" << tmp.first << ","
74 << tmp.second << ") " << endl;
75 }
76}
77
78uint SelectionRange::nbPages() const
79{
80 uint nb = 0;
81 for (uint i=0; i<_ranges.size(); i++)
82 nb += _ranges[i].second - _ranges[i].first + 1;
83 return nb;
84}
85
86SelectionRangeIterator::SelectionRangeIterator(const SelectionRange &range)
87 : _ranges(range._ranges)
88{
89 toFirst();
90}
91
92int SelectionRangeIterator::toFirst()
93{
94 if ( _ranges.size()==0 ) _current = -1;
95 else {
96 _index = 0;
97 _current = _ranges[0].first;
98 }
99 return _current;
100}
101
102int SelectionRangeIterator::next()
103{
104 if ( _current==-1 ) return -1;
105 if ( _current==int(_ranges[_index].second) ) {
106 _index++;
107 _current = (_index==_ranges.size() ? -1
108 : int(_ranges[_index].first));
109 } else _current++;
110 return _current;
111}
112
113//-----------------------------------------------------------------------------
114namespace PDFImport
115{
116
117Dialog::Dialog(uint nbPages, bool isEncrypted, TQWidget *widget)
118 : KDialogBase(Plain, i18n("KWord's PDF Import Filter"), Ok|Cancel, Ok,
119 widget, "pdf_import_dialog"), _nbPages(nbPages)
120{
121 TQApplication::restoreOverrideCursor();
122
123 TQVBoxLayout *top = new TQVBoxLayout(plainPage(), KDialogBase::marginHint(),
124 KDialogBase::spacingHint());
125
126 // page selection
127 TQVGroupBox *gbox = new TQVGroupBox(i18n("Page Selection"), plainPage());
128 gbox->setInsideSpacing(KDialogBase::spacingHint());
129 top->addWidget(gbox);
130 _group = new TQButtonGroup;
131 _allButton = new TQRadioButton(i18n("All (%1 pages)").arg(nbPages), gbox);
132 _allButton->setChecked(true);
133 _group->insert(_allButton);
134 TQHBox *hbox = new TQHBox(gbox);
135 _rangeButton = new TQRadioButton(i18n("Range:"), hbox);
136 _group->insert(_rangeButton);
137 _range = new KLineEdit(hbox);
138 _range->setFocus();
139 connect(_range, TQ_SIGNAL(textChanged(const TQString &)),
140 TQ_SLOT(rangeChanged(const TQString &)));
141
142 // options
143 _images = new TQCheckBox(i18n("Import images"), plainPage());
144 _images->setChecked(true);
145 top->addWidget(_images);
146 _smart = new TQCheckBox(i18n("\"Smart\" mode"), plainPage());
147 _smart->setChecked(true);
148 TQWhatsThis::add(_smart,
149 i18n("Removes returns and hyphens at end of line. "
150 "Also tries to compute the paragraph alignment. "
151 "Note that the layout of some pages can "
152 "get messed up."));
153 top->addWidget(_smart);
154
155 // passwords
156 gbox = new TQVGroupBox(i18n("Passwords"), plainPage());
157 top->addWidget(gbox);
158 TQGrid *grid = new TQGrid(2, gbox);
159 grid->setSpacing(KDialogBase::spacingHint());
160 (void)new TQLabel(i18n("Owner:"), grid);
161 _owner = new KLineEdit(grid);
162 _owner->setEchoMode(TQLineEdit::Password);
163 (void)new TQLabel(i18n("User:"), grid);
164 _user = new KLineEdit(grid);
165 _user->setEchoMode(TQLineEdit::Password);
166 grid->setEnabled(isEncrypted);
167}
168
169Dialog::~Dialog()
170{
171 delete _group;
172 TQApplication::setOverrideCursor(TQt::waitCursor);
173}
174
175void Dialog::rangeChanged(const TQString &)
176{
177 _rangeButton->setChecked(true);
178}
179
180Options Dialog::options() const
181{
182 Options o;
183 o.range = SelectionRange( (_allButton->isChecked() ?
184 TQString("1-%1").arg(_nbPages) : _range->text()) );
185 o.ownerPassword = _owner->text();
186 o.userPassword = _user->text();
187 o.importImages = _images->isChecked();
188 o.smart = _smart->isChecked();
189 return o;
190}
191
192} // namespace