summaryrefslogtreecommitdiffstats
path: root/kword/KWPageManager.cpp
blob: e13d409994f02d373f65221b184ac5c865062553 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* This file is part of the KOffice project
 * Copyright (C) 2005 Thomas Zander <zander@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; version 2.

 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "KWPageManager.h"
#include "KWPage.h"
#include "KoRect.h"

//#define DEBUG_PAGES

KWPageManager::KWPageManager() {
    m_firstPage = 1;
    m_onlyAllowAppend = false;
    m_pageList.setAutoDelete(true);
    m_defaultPageLayout = KoPageLayout::standardLayout();
}

int KWPageManager::pageNumber(const KoRect &frame) const {
    int page=m_firstPage;
    double startOfpage = 0.0;
    TQPtrListIterator<KWPage> pages(m_pageList);
    while(pages.current() && startOfpage < frame.top()) {
        startOfpage += pages.current()->height();
        if(startOfpage >= frame.top())
            break;
        page++;
        ++pages;
    }
    // Disable strict checks. Testcase: F10, drag rectangle, move the mouse under the bottom of the last page.
    // Instead of a bunch of warnings and making the insertion-rectangle disappear, we simply want to bound
    // the mouse position to the last page.
    if ( !pages.current() )
        page = m_firstPage + m_pageList.count() - 1;
#if 0
    if(!pages.current() || frame.right() > pages.current()->width() ||
            frame.top() > pages.current()->height() + startOfpage) {
#ifdef DEBUG_PAGES
    if(!pages.current())
        kdDebug(31001) << "  KWPageManager::pageNumber of "<< frame << " is too high, no page there\n";
    else if(frame.right() > pages.current()->width())
        kdDebug(31001) << "  KWPageManager::pageNumber right of "<< frame << " is out of bounds\n";
    else if(frame.top() > pages.current()->height() + startOfpage)
        kdDebug(31001) << "  KWPageManager::pageNumber "<< frame << " spans multiple pages\n";

kdDebug(31001) << kdBacktrace() << endl;
#endif
        return -1; // not inside the page...
    }
#endif
    return page;
}
int KWPageManager::pageNumber(const KoPoint &point) const {
    return pageNumber(KoRect(point, point));
}
int KWPageManager::pageNumber(const double ptY) const {
    return pageNumber(KoRect(0, ptY, 0, 0));
}
int KWPageManager::pageNumber(const KoRect *rect) const {
    KoRect tmp(*rect);
    return pageNumber(tmp);
}

int KWPageManager::pageCount() const {
    return m_pageList.count();
}

KWPage* KWPageManager::page(int pageNum) const {
    TQPtrListIterator<KWPage> pages(m_pageList);
    while(pages.current()) {
        if(pages.current()->pageNumber() == pageNum)
            return pages.current();
        ++pages;
    }
    kdWarning(31001) << "KWPageManager::page(" << pageNum << ") failed; Requested page does not exist ["<< m_firstPage << "-"<< lastPageNumber() << "]"<< endl;
#ifdef DEBUG_PAGES
    kdDebug(31001) << kdBacktrace();
#endif
    return 0;
}
KWPage* KWPageManager::page(const KoRect &frame) const {
    return page(pageNumber(frame));
}
KWPage* KWPageManager::page(const KoPoint &point) const {
    return page(pageNumber(point));
}
KWPage* KWPageManager::page(double ptY) const {
    return page(pageNumber(ptY));
}
KWPage* KWPageManager::page(const KoRect *rect) const {
    KoRect tmp(*rect);
    return page(tmp);
}

void KWPageManager::setStartPage(int startPage) {
    int offset = startPage - m_firstPage;
    bool switchSides = startPage % 2 != m_firstPage % 2;
    for(TQPtrListIterator<KWPage> pages(m_pageList); pages.current(); ++pages) {
        KWPage *page = pages.current();
        page->m_pageNum = page->m_pageNum + offset;
        if(switchSides)
            page->m_pageSide = page->m_pageSide == KWPage::Left ? KWPage::Right : KWPage::Left;
    }
    m_firstPage = startPage;
}

int KWPageManager::lastPageNumber() const {
    return pageCount() + m_firstPage - 1;
}

KWPage* KWPageManager::insertPage(int index) {
    if(m_onlyAllowAppend)
        return appendPage();
    KWPage *page = new KWPage(this, TQMIN( TQMAX(index, m_firstPage), lastPageNumber()+1 ));
    TQPtrListIterator<KWPage> pages(m_pageList);
    while(pages.current() && pages.current()->pageNumber() < index)
        ++pages;
    while(pages.current()) {
        pages.current()->m_pageNum++;
        ++pages;
    }
    m_pageList.inSort(page);
    return page;
}

KWPage* KWPageManager::appendPage() {
    KWPage *page = new KWPage(this, lastPageNumber() + 1);
    m_pageList.append(page);
    return page;
}

const KoPageLayout KWPageManager::pageLayout(int pageNumber) const {
    KoPageLayout lay = m_defaultPageLayout;
    if(pageNumber >= m_firstPage && pageNumber <= lastPageNumber()) {
        KWPage *page = this->page(pageNumber);
        lay.ptHeight = page->height();
        lay.ptWidth = page->width();
        lay.ptTop = page->topMargin();
        lay.ptLeft = page->leftMargin();
        lay.ptBottom = page->bottomMargin();
        lay.ptRight = page->rightMargin();
    }
    return lay;
}

double KWPageManager::topOfPage(int pageNum) const {
    return pageOffset(pageNum, false);
}
double KWPageManager::bottomOfPage(int pageNum) const {
    return pageOffset(pageNum, true);
}

double KWPageManager::pageOffset(int pageNum, bool bottom) const {
    if(pageNum < m_firstPage)
        return 0;
    TQPtrListIterator<KWPage> pages(m_pageList);
    double offset = 0.0;
    while(pages.current()) {
        if(pages.current()->pageNumber() == pageNum) {
            if(bottom)
                offset += pages.current()->height();
            break;
        }
        offset += pages.current()->height();
        ++pages;
    }
    return offset;
}

void KWPageManager::removePage(int pageNumber) {
    removePage(page(pageNumber));
}
void KWPageManager::removePage(KWPage *page) {
    if(!page)
        return;
    TQPtrListIterator<KWPage> pages(m_pageList);
    while(pages.current() && pages.current()->pageNumber() <= page->pageNumber())
        ++pages;
    while(pages.current()) {
        pages.current()->m_pageNum--;
        ++pages;
    }
    // TODO make the deletion of object page occur in a single shot
    m_pageList.remove(page);
}

void KWPageManager::setDefaultPage(const KoPageLayout &layout) {
    m_defaultPageLayout = layout;
    // make sure we have 1 default, either pageBound or left/right bound.
    if(m_defaultPageLayout.ptLeft < 0 || m_defaultPageLayout.ptRight < 0) {
        m_defaultPageLayout.ptLeft = -1;
        m_defaultPageLayout.ptRight = -1;
    } else {
        m_defaultPageLayout.ptPageEdge = -1;
        m_defaultPageLayout.ptBindingSide = -1;
        m_defaultPageLayout.ptLeft = TQMAX(m_defaultPageLayout.ptLeft, 0);
        m_defaultPageLayout.ptRight = TQMAX(m_defaultPageLayout.ptRight, 0);
    }
    //kdDebug() << "setDefaultPage l:" << m_defaultPageLayout.ptLeft << ", r: " << m_defaultPageLayout.ptRight << ", a: " << m_defaultPageLayout.ptPageEdge << ", b: " << m_defaultPageLayout.ptBindingSide << endl;
}

KoPoint KWPageManager::clipToDocument(const KoPoint &point) {
    int page=m_firstPage;
    double startOfpage = 0.0;
    TQPtrListIterator<KWPage> pages(m_pageList);
    while(pages.current()) {
        startOfpage += pages.current()->height();
        if(startOfpage >= point.y())
            break;
        page++;
        ++pages;
    }
    page = TQMIN(page, lastPageNumber());
    KoRect rect = this->page(page)->rect();
    if(rect.contains(point))
        return point;

    KoPoint rc(point);
    if(rect.top() > rc.y())
        rc.setY(rect.top());
    else if(rect.bottom() < rc.y())
        rc.setY(rect.bottom());

    if(rect.left() > rc.x())
        rc.setX(rect.left());
    else if(rect.right() < rc.x())
        rc.setX(rect.right());
    return rc;
}

// **** PageList ****
int KWPageManager::PageList::compareItems(TQPtrCollection::Item a, TQPtrCollection::Item b)
{
    int pa = ((KWPage *)a)->pageNumber();
    int pb = ((KWPage *)b)->pageNumber();
    if (pa == pb) return 0;
    if (pa < pb) return -1;
    return 1;
}