00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "KWViewMode.h"
00022 #include "KWCanvas.h"
00023 #include "KWView.h"
00024 #include "KWDocument.h"
00025 #include "KWTextFrameSet.h"
00026 #include "KWFrameSet.h"
00027 #include "KWTableFrameSet.h"
00028 #include "KWPageManager.h"
00029 #include "KWPage.h"
00030 #include "KWFrameViewManager.h"
00031 #include "KWFrameView.h"
00032 #include <qapplication.h>
00033 #include <kdebug.h>
00034
00035 const unsigned short KWViewMode::s_shadowOffset = 3;
00036
00037 QSize KWViewMode::availableSizeForText( KWTextFrameSet* textfs )
00038 {
00039 KWFrame* frame = textfs->frameIterator().getLast();
00040 return m_doc->zoomSize( KoSize( frame->innerWidth(), frame->internalY() + frame->innerHeight() ) );
00041
00042 }
00043
00044 void KWViewMode::drawOnePageBorder( QPainter * painter, const QRect & crect, const QRect & _pageRect,
00045 const QRegion & emptySpaceRegion )
00046 {
00047 if ( !crect.intersects( _pageRect ) )
00048 return;
00049
00050 QRect pageRect( _pageRect );
00051
00052 painter->drawRect( pageRect );
00053
00054 pageRect.rLeft() += 1;
00055 pageRect.rTop() += 1;
00056 pageRect.rRight() -= 1;
00057 pageRect.rBottom() -= 1;
00058
00059 QRect pagecrect = pageRect.intersect( crect );
00060 if ( !pagecrect.isEmpty() )
00061 {
00062
00063 QRegion pageEmptyRegion = emptySpaceRegion.intersect( pagecrect );
00064
00065 if ( !pageEmptyRegion.isEmpty() )
00066 m_doc->eraseEmptySpace( painter, pageEmptyRegion, QApplication::palette().active().brush( QColorGroup::Base ) );
00067 }
00068 }
00069
00070 QRect KWViewMode::drawRightShadow( QPainter * painter, const QRect & crect, const QRect & pageRect, int topOffset )
00071 {
00072 QRect shadowRect( pageRect.right() + 1, pageRect.top() + topOffset, s_shadowOffset, pageRect.height() - topOffset );
00073 shadowRect &= crect;
00074 if ( !shadowRect.isEmpty() )
00075 {
00076 painter->fillRect( shadowRect,
00077 QApplication::palette().active().brush( QColorGroup::Shadow ) );
00078 }
00079 return shadowRect;
00080 }
00081
00082 QRect KWViewMode::drawBottomShadow( QPainter * painter, const QRect & crect, const QRect & pageRect, int leftOffset )
00083 {
00084 QRect shadowRect( pageRect.left() + leftOffset, pageRect.bottom() + 1, pageRect.width(), s_shadowOffset );
00085 shadowRect &= crect;
00086 if ( !shadowRect.isEmpty() )
00087 painter->fillRect( shadowRect,
00088 QApplication::palette().active().brush( QColorGroup::Shadow ) );
00089 return shadowRect;
00090 }
00091
00092 QPoint KWViewMode::pageCorner()
00093 {
00094
00095 KWFrame * frame = 0L;
00096
00097 if( m_canvas->currentFrameSetEdit() && m_canvas->currentFrameSetEdit()->currentFrame() )
00098 frame = m_canvas->currentFrameSetEdit()->currentFrame();
00099 else {
00100 KWFrameView *view = m_canvas->frameViewManager()->selectedFrame();
00101 frame = view == 0 ? 0 : view->frame();
00102 }
00103
00104 int pageNum = 0;
00105 if ( frame )
00106 pageNum = frame->pageNumber();
00107 QPoint nPoint( 0, m_doc->pageTop(pageNum) + 1 );
00108 QPoint cPoint( normalToView( nPoint ) );
00109
00110
00111
00112 return cPoint;
00113 }
00114
00115 QRect KWViewMode::rulerFrameRect()
00116 {
00117
00118 KWFrameSetEdit * edit = m_canvas->currentFrameSetEdit();
00119 KWFrame * frame = 0L;
00120
00121 if ( edit && edit->currentFrame() )
00122 frame = edit->currentFrame();
00123 else {
00124 KWFrameView *view = m_canvas->frameViewManager()->selectedFrame();
00125 frame = view == 0 ? 0 : view->frame();
00126 }
00127 if( !frame) {
00128 KWFrameSet *fs= m_doc->frameSet(0);
00129 if(fs) frame=fs->frame(0);
00130 }
00131 if ( frame )
00132 {
00133 QRect r = normalToView( m_doc->zoomRect( frame->innerRect() ) );
00134
00135
00136 int pageNum = frame->pageNumber();
00137 QPoint nPoint( 0, m_doc->pageTop(pageNum) + 1 );
00138 QPoint cPoint( normalToView( nPoint ) );
00139
00140
00141 r.moveBy( -cPoint.x(), -cPoint.y() );
00142 return r;
00143 }
00144 return QRect();
00145 }
00146
00147 void KWViewMode::setPageLayout( KoRuler* hRuler, KoRuler* vRuler, const KoPageLayout& layout )
00148 {
00149 hRuler->setPageLayout( layout );
00150 vRuler->setPageLayout( layout );
00151 }
00152
00153
00154 KWViewMode * KWViewMode::create( const QString & viewModeType, KWDocument *doc, KWCanvas* canvas )
00155 {
00156 Q_ASSERT(doc);
00157 if(viewModeType=="ModeNormal")
00158 {
00159 return new KWViewModeNormal( doc, canvas, doc->viewFrameBorders() );
00160 }
00161 else if(viewModeType=="ModeEmbedded")
00162 {
00163 return new KWViewModeEmbedded( doc, canvas );
00164 }
00165 else if(viewModeType=="ModePreview")
00166 {
00167 return new KWViewModePreview( doc, canvas, doc->viewFrameBorders(), doc->nbPagePerRow() );
00168 }
00169 else if(viewModeType=="ModeText")
00170 {
00171 KWTextFrameSet* fs = KWViewModeText::determineTextFrameSet( doc );
00172 return new KWViewModeText( doc, canvas, fs );
00173 }
00174 else
00175 {
00176 kdDebug() << viewModeType << " mode type is unknown\n";
00177 return 0;
00178 }
00179 }
00180
00182
00183 QSize KWViewModeNormal::contentsSize()
00184 {
00185 return QSize( m_doc->paperWidth(m_doc->startPage()),
00186 m_doc->zoomItY( m_doc->pageManager()->bottomOfPage(m_doc->lastPage()) ) );
00187 }
00188
00189 QRect KWViewModeNormal::viewPageRect( int pgNum )
00190 {
00191 KWPage* page = m_doc->pageManager()->page( pgNum );
00192 QRect r = page->zoomedRect( m_doc );
00193 r.moveBy( xOffset( page ), 0 );
00194 return r;
00195 }
00196
00197 QPoint KWViewModeNormal::normalToView( const QPoint & nPoint )
00198 {
00199 double unzoomedY = m_doc->unzoomItY( nPoint.y() );
00200 KWPage *page = m_doc->pageManager()->page(unzoomedY);
00201 if( !page) {
00202 kdWarning(31001) << "KWViewModeNormal::normalToView request for conversion out of the document! Check your input data.. ("<< nPoint << ")" << endl;
00203 return QPoint(0,0);
00204 }
00205 Q_ASSERT(canvas());
00206 return QPoint( xOffset(page) + nPoint.x(), nPoint.y() );
00207 }
00208
00209 QPoint KWViewModeNormal::viewToNormal( const QPoint & vPoint )
00210 {
00211
00212
00213 double unzoomedY = m_doc->unzoomItY( vPoint.y() );
00214 KWPage *page = m_doc->pageManager()->page(unzoomedY);
00215 if( !page) {
00216 kdWarning(31001) << "KWViewModeNormal::normalToView request for conversion out of the document! Check your input data.. ("<< vPoint << ")" << endl;
00217 return QPoint(-1,-1);
00218 }
00219 Q_ASSERT(canvas());
00220 return QPoint( vPoint.x() - xOffset(page), vPoint.y() );
00221 }
00222
00223 int KWViewModeNormal::xOffset(KWPage *page, int canvasWidth ) {
00224
00225 if(canvasWidth < 0)
00226 canvasWidth = canvas()->visibleWidth();
00227 return kMax( 0, ( canvasWidth - m_doc->zoomItX( page->width() ) ) / 2 );
00228 }
00229
00230 void KWViewModeNormal::drawPageBorders( QPainter * painter, const QRect & crect, const QRegion & emptySpaceRegion )
00231 {
00232 painter->save();
00233 painter->setPen( QApplication::palette().active().color( QColorGroup::Dark ) );
00234 painter->setBrush( Qt::NoBrush );
00235 QRect pageRect;
00236
00237 int lastPage = m_doc->lastPage();
00238 Q_ASSERT(canvas());
00239 const int canvasWidth = canvas()->visibleWidth();
00240 double pagePosPt = 0;
00241 int topOfPage = 0;
00242 for ( int pageNr = m_doc->startPage(); pageNr <= lastPage; pageNr++ )
00243 {
00244 KWPage *page = m_doc->pageManager()->page(pageNr);
00245
00246 int pageWidth = m_doc->zoomItX( page->width() );
00247 int pageHeight = m_doc->zoomItY( pagePosPt + page->height() ) - topOfPage;
00248 if ( crect.bottom() < topOfPage )
00249 break;
00250
00251 int x = xOffset(page, canvasWidth);
00252
00253 pageRect = QRect( x, topOfPage, pageWidth, pageHeight );
00254 drawOnePageBorder( painter, crect, pageRect, emptySpaceRegion );
00255
00256
00257 QRect leftArea( 0, topOfPage, x, pageHeight );
00258 leftArea &= crect;
00259 if ( !leftArea.isEmpty() ) {
00260 painter->fillRect( leftArea,
00261 QApplication::palette().active().brush( QColorGroup::Mid ) );
00262 }
00263
00264
00265 QRect rightArea( x + pageWidth, topOfPage, crect.right() - pageWidth + 1, pageHeight );
00266 rightArea &= crect;
00267 if ( !rightArea.isEmpty() )
00268 {
00269 painter->fillRect( rightArea,
00270 QApplication::palette().active().brush( QColorGroup::Mid ) );
00271
00272
00273 int topOffset = ( page==0 ) ? s_shadowOffset : 0;
00274 drawRightShadow( painter, crect, pageRect, topOffset );
00275 }
00276 pagePosPt += page->height();
00277 topOfPage += pageHeight;
00278 }
00279
00280 if ( crect.bottom() > topOfPage )
00281 {
00282 QRect bottomArea( 0, topOfPage, crect.right() + 1, crect.bottom() - topOfPage + 1 );
00283 QRect repaintRect = bottomArea.intersect( crect );
00284 if ( !repaintRect.isEmpty() )
00285 {
00286 painter->fillRect( repaintRect,
00287 QApplication::palette().active().brush( QColorGroup::Mid ) );
00288
00289 drawBottomShadow( painter, crect, pageRect, s_shadowOffset );
00290 }
00291 }
00292 painter->restore();
00293 }
00294
00296
00297 QRect KWViewModeEmbedded::viewPageRect( int pgNum )
00298 {
00299
00300 return m_doc->pageManager()->page( pgNum )->zoomedRect( m_doc );
00301 }
00302
00304
00305 KWViewModePreview::KWViewModePreview( KWDocument * doc, KWCanvas* canvas,
00306 bool drawFrameBorders, int _nbPagePerRow )
00307 : KWViewMode( doc, canvas, drawFrameBorders ),
00308 m_pagesPerRow(_nbPagePerRow),
00309 m_spacing(10)
00310 {}
00311
00312 int KWViewModePreview::leftSpacing()
00313 {
00314 if ( canvas() )
00315 {
00316 int pagesPerRow;
00317 if ( m_doc->pageCount() < m_pagesPerRow )
00318 pagesPerRow = m_doc->pageCount();
00319 else
00320 pagesPerRow = m_pagesPerRow;
00321
00322 int pagesWidth = ( m_spacing + ( m_doc->paperWidth(m_doc->startPage()) + m_spacing ) * pagesPerRow );
00323 if ( pagesWidth < canvas()->visibleWidth() )
00324 return ( m_spacing + ( canvas()->visibleWidth() / 2 ) - ( pagesWidth / 2 ) );
00325 }
00326 return m_spacing;
00327 }
00328
00329 int KWViewModePreview::topSpacing()
00330 {
00331 if ( canvas() )
00332 {
00333 int pagesHeight = ( m_spacing + ( m_doc->paperHeight(m_doc->startPage()) + m_spacing ) * numRows() );
00334 if ( pagesHeight < canvas()->visibleHeight() )
00335 return ( m_spacing + ( canvas()->visibleHeight() / 2 ) - ( pagesHeight / 2 ) );
00336 }
00337 return m_spacing;
00338 }
00339
00340 int KWViewModePreview::numRows() const
00341 {
00342 return ( m_doc->pageCount() ) / m_pagesPerRow + 1;
00343 }
00344
00345 QSize KWViewModePreview::contentsSize()
00346 {
00347 int pages = m_doc->pageCount();
00348 int rows = (pages-1) / m_pagesPerRow + 1;
00349 int hPages = rows > 1 ? m_pagesPerRow : pages;
00350 return QSize( m_spacing + hPages * ( m_doc->paperWidth(m_doc->startPage()) + m_spacing ),
00351 m_spacing + rows * ( m_doc->paperHeight(m_doc->startPage()) + m_spacing ) );
00352 }
00353
00354 QPoint KWViewModePreview::normalToView( const QPoint & nPoint )
00355 {
00356
00357 double unzoomedY = m_doc->unzoomItY( nPoint.y() );
00358 KWPage *page = m_doc->pageManager()->page(unzoomedY);
00359 if( !page) {
00360 kdWarning(31001) << "KWViewModePreview::normalToView request for conversion out of the document! Check your input data.. ("<< nPoint << ")" << endl;
00361 return QPoint(0,0);
00362 }
00363
00364 double yInPagePt = unzoomedY - page->offsetInDocument();
00365 int row = (page->pageNumber() - m_doc->startPage()) / m_pagesPerRow;
00366 int col = (page->pageNumber() - m_doc->startPage()) % m_pagesPerRow;
00367
00368
00369
00370
00371 return QPoint( leftSpacing() + col * ( m_doc->paperWidth(page->pageNumber()) +
00372 m_spacing ) + nPoint.x(),
00373 topSpacing() + row * ( m_doc->paperHeight(page->pageNumber()) +
00374 m_spacing ) + m_doc->zoomItY( yInPagePt ) );
00375 }
00376
00377 QRect KWViewModePreview::viewPageRect( int pgNum )
00378 {
00379 int row = (pgNum - m_doc->startPage()) / m_pagesPerRow;
00380 int col = (pgNum - m_doc->startPage()) % m_pagesPerRow;
00381 const int paperWidth = m_doc->paperWidth( pgNum );
00382 const int paperHeight = m_doc->paperHeight( pgNum );
00383 return QRect( leftSpacing() + col * ( paperWidth + m_spacing ),
00384 topSpacing() + row * ( paperHeight + m_spacing ),
00385 paperWidth,
00386 paperHeight );
00387 }
00388
00389 QPoint KWViewModePreview::viewToNormal( const QPoint & vPoint )
00390 {
00391
00392 int paperWidth = m_doc->paperWidth(m_doc->startPage());
00393 int paperHeight = m_doc->paperHeight(m_doc->startPage());
00394 QPoint p( vPoint.x() - leftSpacing(), vPoint.y() - topSpacing() );
00395 int col = static_cast<int>( p.x() / ( paperWidth + m_spacing ) );
00396 int xInPage = p.x() - col * ( paperWidth + m_spacing );
00397 int row = static_cast<int>( p.y() / ( paperHeight + m_spacing ) );
00398 int yInPage = p.y() - row * ( paperHeight + m_spacing );
00399 int page = row * m_pagesPerRow + col + m_doc->startPage();
00400 if ( page > m_doc->lastPage() )
00401 return QPoint( paperWidth, m_doc->pageTop( m_doc->lastPage() ) );
00402 else
00403 return QPoint( xInPage, yInPage + m_doc->pageTop( page ) );
00404 }
00405
00406 void KWViewModePreview::drawPageBorders( QPainter * painter, const QRect & crect, const QRegion & emptySpaceRegion )
00407 {
00408 painter->save();
00409 painter->setPen( QApplication::palette().active().color( QColorGroup::Dark ) );
00410 painter->setBrush( Qt::NoBrush );
00411
00412 QRegion grayRegion( crect );
00413 int pageCount = m_doc->pageCount();
00414 for ( int counter = 0; counter < pageCount; counter++ )
00415 {
00416 int row = counter / m_pagesPerRow;
00417 int col = counter % m_pagesPerRow;
00418 int page = m_doc->startPage() + counter;
00419 int paperWidth = m_doc->paperWidth(page);
00420 int paperHeight = m_doc->paperHeight(page);
00421 QRect pageRect( leftSpacing() + col * ( paperWidth + m_spacing ),
00422 topSpacing() + row * ( paperHeight + m_spacing ),
00423 paperWidth, paperHeight );
00424 drawOnePageBorder( painter, crect, pageRect, emptySpaceRegion );
00425 if( pageRect.top() > crect.bottom())
00426 break;
00427 if ( pageRect.intersects( crect ) )
00428 grayRegion -= pageRect;
00429 QRect rightShadow = drawRightShadow( painter, crect, pageRect, s_shadowOffset );
00430 if ( !rightShadow.isEmpty() )
00431 grayRegion -= rightShadow;
00432 QRect bottomShadow = drawBottomShadow( painter, crect, pageRect, s_shadowOffset );
00433 if ( !bottomShadow.isEmpty() )
00434 grayRegion -= bottomShadow;
00435
00436
00437
00438 }
00439 if ( !grayRegion.isEmpty() )
00440 {
00441
00442 m_doc->eraseEmptySpace( painter, grayRegion, QApplication::palette().active().brush( QColorGroup::Mid ) );
00443 }
00444 painter->restore();
00445 }
00446
00448
00449 KWViewModeText::KWViewModeText( KWDocument * doc, KWCanvas* canvas, KWTextFrameSet* fs )
00450 : KWViewMode( doc, canvas, false )
00451 {
00452 m_textFrameSet = fs;
00453 }
00454
00455 KWTextFrameSet * KWViewModeText::textFrameSet() const
00456 {
00457 if ( !m_textFrameSet )
00458 m_textFrameSet = determineTextFrameSet( m_doc );
00459 return m_textFrameSet;
00460 }
00461
00462 KWTextFrameSet * KWViewModeText::determineTextFrameSet( KWDocument* doc )
00463 {
00464 KWTextFrameSet* fs = 0L;
00465
00466 if(!doc->getAllViews().empty()) {
00467 KWView *view = doc->getAllViews()[0];
00468 KWFrameView *fv = view->getGUI()->canvasWidget()->frameViewManager()->selectedFrame();
00469 KWFrame *f = fv == 0 ? 0 : fv->frame();
00470 if(f)
00471 fs = dynamic_cast<KWTextFrameSet *>( f->frameSet() );
00472
00473 if (!fs) {
00474 KWFrameSetEdit *fse = view->getGUI()->canvasWidget()->currentFrameSetEdit();
00475 if(fse)
00476 fs = dynamic_cast<KWTextFrameSet *>( fse->frameSet() );
00477 }
00478 }
00479
00480 if (!fs || fs->isHeaderOrFooter() || fs->isFootEndNote())
00481
00482 if ( doc->frameSetCount() > 0 && doc->frameSet( 0 )->isVisible() )
00483 fs = dynamic_cast<KWTextFrameSet *>( doc->frameSet( 0 ) );
00484
00485 return fs;
00486 }
00487
00488 QPoint KWViewModeText::normalToView( const QPoint & nPoint )
00489 {
00490 QPoint r (nPoint);
00491 r.setX(r.x() + OFFSET);
00492 return r;
00493 }
00494
00495 QPoint KWViewModeText::viewToNormal( const QPoint & vPoint )
00496 {
00497 QPoint r (vPoint);
00498 r.setX(r.x() - OFFSET);
00499 return r;
00500 }
00501
00502 QSize KWViewModeText::contentsSize()
00503 {
00504 if (!textFrameSet())
00505 return QSize();
00506
00507
00508
00509
00510
00511 int width = m_doc->layoutUnitToPixelX( m_textFrameSet->textDocument()->width() );
00512
00513 int height = QMAX((int)m_doc->paperHeight(m_doc->startPage()),
00514 m_doc->layoutUnitToPixelY( m_textFrameSet->textDocument()->height() ) );
00515
00516 return QSize( width, height );
00517 }
00518
00519 QSize KWViewModeText::availableSizeForText( KWTextFrameSet* )
00520 {
00521 return contentsSize();
00522 }
00523
00524 bool KWViewModeText::isFrameSetVisible( const KWFrameSet *fs )
00525 {
00526 if(fs==NULL) return false;
00527 if(fs==textFrameSet()) return true;
00528
00529 const KWFrameSet* parentFrameset = fs->groupmanager() ? fs->groupmanager() : fs;
00530 while ( parentFrameset->isFloating() ) {
00531 parentFrameset = parentFrameset->anchorFrameset();
00532 if ( parentFrameset == m_textFrameSet )
00533 return true;
00534 }
00535 return false;
00536 }
00537
00538 void KWViewModeText::drawPageBorders( QPainter * painter, const QRect & crect,
00539 const QRegion & )
00540 {
00541 painter->save();
00542 QRegion grayRegion( crect );
00543
00544 painter->setPen( QApplication::palette().active().color( QColorGroup::Dark ) );
00545 QSize cSize = contentsSize();
00546
00547
00548 QRect frameRect( OFFSET, 0, cSize.width() + 2, cSize.height() );
00549
00550 painter->drawLine( frameRect.topRight(), frameRect.bottomRight() );
00551 if ( frameRect.intersects( crect ) )
00552 grayRegion -= frameRect;
00553
00554
00555 if ( crect.bottom() >= cSize.height() )
00556 {
00557
00558 painter->drawLine( 0, cSize.height(),
00559 cSize.width(), cSize.height() );
00560 grayRegion -= QRect( 0, cSize.height(),
00561 cSize.width(), cSize.height() );
00562 }
00563
00564 if ( !grayRegion.isEmpty() )
00565 m_doc->eraseEmptySpace( painter, grayRegion, QApplication::palette().active().brush( QColorGroup::Mid ) );
00566 painter->restore();
00567 }
00568
00569 QRect KWViewModeText::rulerFrameRect()
00570 {
00571 return QRect( QPoint(OFFSET, 0), contentsSize() );
00572 }
00573
00574 void KWViewModeText::setPageLayout( KoRuler* hRuler, KoRuler* vRuler, const KoPageLayout& )
00575 {
00576
00577 KoPageLayout layout;
00578 layout.format = PG_CUSTOM;
00579 layout.orientation = PG_PORTRAIT;
00580 QSize cSize = contentsSize();
00581 layout.ptWidth = m_doc->unzoomItX( cSize.width() );
00582 layout.ptHeight = m_doc->unzoomItY( cSize.height() );
00583
00584 layout.ptLeft = OFFSET;
00585 layout.ptRight = 0;
00586 layout.ptTop = 0;
00587 layout.ptBottom = 0;
00588 layout.ptBindingSide = 0;
00589 layout.ptPageEdge = 0;
00590 hRuler->setPageLayout( layout );
00591 vRuler->setPageLayout( layout );
00592 }
00593
00594 bool KWViewModeText::isTextModeFrameset(KWFrameSet *fs) const {
00595 return fs == textFrameSet();
00596 }
00597
00598
00599 int KWViewModePrint::xOffset(KWPage *page, int canvasWidth) {
00600 Q_UNUSED(page);
00601 Q_UNUSED(canvasWidth);
00602 return 0;
00603 }