summaryrefslogtreecommitdiffstats
path: root/filters/kword/pdf/dialog.cpp
blob: 2020baea9b8cc285d0a23d8ca5aaacca3bc5270b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (c) 2002-2003 Nicolas HADACEK (hadacek@kde.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "dialog.h"
#include "dialog.moc"

#include <tqhbox.h>
#include <tqvgroupbox.h>
#include <tqregexp.h>
#include <tqapplication.h>
#include <tqgrid.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqbuttongroup.h>
#include <tqradiobutton.h>
#include <tqwhatsthis.h>
#include <tqcheckbox.h>

#include <tdelocale.h>
#include <kdebug.h>
#include <klineedit.h>


//-----------------------------------------------------------------------------
SelectionRange::SelectionRange(const TQString &s)
{
    // fill
    TQValueVector<TQPair<uint, uint> > r;
    TQStringList list = TQStringList::split(',', s);
    TQRegExp range("^([0-9]+)\\-([0-9]+)$");
    TQRegExp one("^[0-9]+$");
    for (TQStringList::iterator it = list.begin(); it!=list.end(); ++it) {
        if ( one.exactMatch(*it) ) {
            uint p = (*it).toUInt();
            r.push_back( qMakePair(p, p) );
        } else if ( range.exactMatch(*it) ) {
            uint p1 = range.cap(1).toUInt();
            uint p2 = range.cap(2).toUInt();
            if ( p1>p2 ) continue;
            r.push_back( qMakePair(p1, p2) );
        }
    }

    // order
    TQPair<uint, uint> tmp;
    for (uint i=1; i<r.size(); i++)
        if ( r[i].first<r[i-1].first )
            tqSwap(r[i-1], r[i]);

    // coalesce
    for (uint i=0; i<r.size(); i++)
        if ( i!=0 && r[i].first<=tmp.second )
            tmp.second = kMax(tmp.second, r[i].second);
        else {
            _ranges.push_back(r[i]);
            tmp = r[i];
            kdDebug(30516) << "selection range: (" << tmp.first << ","
                           << tmp.second << ") " << endl;
        }
}

uint SelectionRange::nbPages() const
{
    uint nb = 0;
    for (uint i=0; i<_ranges.size(); i++)
        nb += _ranges[i].second - _ranges[i].first + 1;
    return nb;
}

SelectionRangeIterator::SelectionRangeIterator(const SelectionRange &range)
    : _ranges(range._ranges)
{
    toFirst();
}

int SelectionRangeIterator::toFirst()
{
    if ( _ranges.size()==0 ) _current = -1;
    else {
        _index = 0;
        _current = _ranges[0].first;
    }
    return _current;
}

int SelectionRangeIterator::next()
{
    if ( _current==-1 ) return -1;
    if ( _current==int(_ranges[_index].second) ) {
        _index++;
        _current = (_index==_ranges.size() ? -1
                    : int(_ranges[_index].first));
    } else _current++;
    return _current;
}

//-----------------------------------------------------------------------------
namespace PDFImport
{

Dialog::Dialog(uint nbPages, bool isEncrypted, TQWidget *widget)
    : KDialogBase(Plain, i18n("KWord's PDF Import Filter"), Ok|Cancel, Ok,
                  widget, "pdf_import_dialog"), _nbPages(nbPages)
{
    TQApplication::restoreOverrideCursor();

    TQVBoxLayout *top = new TQVBoxLayout(plainPage(), KDialogBase::marginHint(),
                                       KDialogBase::spacingHint());

    // page selection
    TQVGroupBox *gbox = new TQVGroupBox(i18n("Page Selection"), plainPage());
    gbox->setInsideSpacing(KDialogBase::spacingHint());
    top->addWidget(gbox);
    _group = new TQButtonGroup;
    _allButton = new TQRadioButton(i18n("All (%1 pages)").arg(nbPages), gbox);
    _allButton->setChecked(true);
    _group->insert(_allButton);
    TQHBox *hbox = new TQHBox(gbox);
    _rangeButton = new TQRadioButton(i18n("Range:"), hbox);
    _group->insert(_rangeButton);
    _range = new KLineEdit(hbox);
    _range->setFocus();
    connect(_range, TQ_SIGNAL(textChanged(const TQString &)),
            TQ_SLOT(rangeChanged(const TQString &)));

    // options
    _images = new TQCheckBox(i18n("Import images"), plainPage());
    _images->setChecked(true);
    top->addWidget(_images);
    _smart = new TQCheckBox(i18n("\"Smart\" mode"), plainPage());
    _smart->setChecked(true);
    TQWhatsThis::add(_smart,
                    i18n("Removes returns and hyphens at end of line. "
                         "Also tries to compute the paragraph alignment. "
                         "Note that the layout of some pages can "
                         "get messed up."));
    top->addWidget(_smart);

    // passwords
    gbox = new TQVGroupBox(i18n("Passwords"), plainPage());
    top->addWidget(gbox);
    TQGrid *grid = new TQGrid(2, gbox);
    grid->setSpacing(KDialogBase::spacingHint());
    (void)new TQLabel(i18n("Owner:"), grid);
    _owner = new KLineEdit(grid);
    _owner->setEchoMode(TQLineEdit::Password);
    (void)new TQLabel(i18n("User:"), grid);
    _user = new KLineEdit(grid);
    _user->setEchoMode(TQLineEdit::Password);
    grid->setEnabled(isEncrypted);
}

Dialog::~Dialog()
{
    delete _group;
    TQApplication::setOverrideCursor(TQt::waitCursor);
}

void Dialog::rangeChanged(const TQString &)
{
    _rangeButton->setChecked(true);
}

Options Dialog::options() const
{
    Options o;
    o.range = SelectionRange( (_allButton->isChecked() ?
                             TQString("1-%1").arg(_nbPages) : _range->text()) );
    o.ownerPassword = _owner->text();
    o.userPassword = _user->text();
    o.importImages = _images->isChecked();
    o.smart = _smart->isChecked();
    return o;
}

} // namespace