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 if ( fs )
00173 return new KWViewModeText( doc, canvas, fs );
00174 return new KWViewModeNormal( doc, canvas, doc->viewFrameBorders() );
00175 }
00176 else
00177 {
00178 kdDebug() << viewModeType << " mode type is unknown\n";
00179 return 0;
00180 }
00181 }
00182
00184
00185 QSize KWViewModeNormal::contentsSize()
00186 {
00187 return QSize( m_doc->paperWidth(m_doc->startPage()),
00188 m_doc->zoomItY( m_doc->pageManager()->bottomOfPage(m_doc->lastPage()) ) );
00189 }
00190
00191 QRect KWViewModeNormal::viewPageRect( int pgNum )
00192 {
00193 KWPage* page = m_doc->pageManager()->page( pgNum );
00194 QRect r = page->zoomedRect( m_doc );
00195 r.moveBy( xOffset( page ), 0 );
00196 return r;
00197 }
00198
00199 QPoint KWViewModeNormal::normalToView( const QPoint & nPoint )
00200 {
00201 double unzoomedY = m_doc->unzoomItY( nPoint.y() );
00202 KWPage *page = m_doc->pageManager()->page(unzoomedY);
00203 if( !page) {
00204 kdWarning(31001) << "KWViewModeNormal::normalToView request for conversion out of the document! Check your input data.. ("<< nPoint << ")" << endl;
00205 return QPoint(0,0);
00206 }
00207 Q_ASSERT(canvas());
00208 return QPoint( xOffset(page) + nPoint.x(), nPoint.y() );
00209 }
00210
00211 QPoint KWViewModeNormal::viewToNormal( const QPoint & vPoint )
00212 {
00213
00214
00215 double unzoomedY = m_doc->unzoomItY( vPoint.y() );
00216 KWPage *page = m_doc->pageManager()->page(unzoomedY);
00217 if( !page) {
00218 kdWarning(31001) << "KWViewModeNormal::normalToView request for conversion out of the document! Check your input data.. ("<< vPoint << ")" << endl;
00219 return QPoint(-1,-1);
00220 }
00221 Q_ASSERT(canvas());
00222 return QPoint( vPoint.x() - xOffset(page), vPoint.y() );
00223 }
00224
00225 int KWViewModeNormal::xOffset(KWPage *page, int canvasWidth ) {
00226
00227 if(canvasWidth < 0)
00228 canvasWidth = canvas()->visibleWidth();
00229 return kMax( 0, ( canvasWidth - m_doc->zoomItX( page->width() ) ) / 2 );
00230 }
00231
00232 void KWViewModeNormal::drawPageBorders( QPainter * painter, const QRect & crect, const QRegion & emptySpaceRegion )
00233 {
00234 painter->save();
00235 painter->setPen( QApplication::palette().active().color( QColorGroup::Dark ) );
00236 painter->setBrush( Qt::NoBrush );
00237 QRect pageRect;
00238
00239 int lastPage = m_doc->lastPage();
00240 Q_ASSERT(canvas());
00241 const int canvasWidth = canvas()->visibleWidth();
00242 double pagePosPt = 0;
00243 int topOfPage = 0;
00244 for ( int pageNr = m_doc->startPage(); pageNr <= lastPage; pageNr++ )
00245 {
00246 KWPage *page = m_doc->pageManager()->page(pageNr);
00247
00248 int pageWidth = m_doc->zoomItX( page->width() );
00249 int pageHeight = m_doc->zoomItY( pagePosPt + page->height() ) - topOfPage;
00250 if ( crect.bottom() < topOfPage )
00251 break;
00252
00253 int x = xOffset(page, canvasWidth);
00254
00255 pageRect = QRect( x, topOfPage, pageWidth, pageHeight );
00256 drawOnePageBorder( painter, crect, pageRect, emptySpaceRegion );
00257
00258
00259 QRect leftArea( 0, topOfPage, x, pageHeight );
00260 leftArea &= crect;
00261 if ( !leftArea.isEmpty() ) {
00262 painter->fillRect( leftArea,
00263 QApplication::palette().active().brush( QColorGroup::Mid ) );
00264 }
00265
00266
00267 QRect rightArea( x + pageWidth, topOfPage, crect.right() - pageWidth + 1, pageHeight );
00268 rightArea &= crect;
00269 if ( !rightArea.isEmpty() )
00270 {
00271 painter->fillRect( rightArea,
00272 QApplication::palette().active().brush( QColorGroup::Mid ) );
00273
00274
00275 int topOffset = ( page==0 ) ? s_shadowOffset : 0;
00276 drawRightShadow( painter, crect, pageRect, topOffset );
00277 }
00278 pagePosPt += page->height();
00279 topOfPage += pageHeight;
00280 }
00281
00282 if ( crect.bottom() > topOfPage )
00283 {
00284 QRect bottomArea( 0, topOfPage, crect.right() + 1, crect.bottom() - topOfPage + 1 );
00285 QRect repaintRect = bottomArea.intersect( crect );
00286 if ( !repaintRect.isEmpty() )
00287 {
00288 painter->fillRect( repaintRect,
00289 QApplication::palette().active().brush( QColorGroup::Mid ) );
00290
00291 drawBottomShadow( painter, crect, pageRect, s_shadowOffset );
00292 }
00293 }
00294 painter->restore();
00295 }
00296
00298
00299 QRect KWViewModeEmbedded::viewPageRect( int pgNum )
00300 {
00301
00302 return m_doc->pageManager()->page( pgNum )->zoomedRect( m_doc );
00303 }
00304
00306
00307 KWViewModePreview::KWViewModePreview( KWDocument * doc, KWCanvas* canvas,
00308 bool drawFrameBorders, int _nbPagePerRow )
00309 : KWViewMode( doc, canvas, drawFrameBorders ),
00310 m_pagesPerRow(_nbPagePerRow),
00311 m_spacing(10)
00312 {}
00313
00314 int KWViewModePreview::leftSpacing()
00315 {
00316 if ( canvas() )
00317 {
00318 int pagesPerRow;
00319 if ( m_doc->pageCount() < m_pagesPerRow )
00320 pagesPerRow = m_doc->pageCount();
00321 else
00322 pagesPerRow = m_pagesPerRow;
00323
00324 int pagesWidth = ( m_spacing + ( m_doc->paperWidth(m_doc->startPage()) + m_spacing ) * pagesPerRow );
00325 if ( pagesWidth < canvas()->visibleWidth() )
00326 return ( m_spacing + ( canvas()->visibleWidth() / 2 ) - ( pagesWidth / 2 ) );
00327 }
00328 return m_spacing;
00329 }
00330
00331 int KWViewModePreview::topSpacing()
00332 {
00333 if ( canvas() )
00334 {
00335 int pagesHeight = ( m_spacing + ( m_doc->paperHeight(m_doc->startPage()) + m_spacing ) * numRows() );
00336 if ( pagesHeight < canvas()->visibleHeight() )
00337 return ( m_spacing + ( canvas()->visibleHeight() / 2 ) - ( pagesHeight / 2 ) );
00338 }
00339 return m_spacing;
00340 }
00341
00342 int KWViewModePreview::numRows() const
00343 {
00344 return ( m_doc->pageCount() ) / m_pagesPerRow + 1;
00345 }
00346
00347 QSize KWViewModePreview::contentsSize()
00348 {
00349 int pages = m_doc->pageCount();
00350 int rows = (pages-1) / m_pagesPerRow + 1;
00351 int hPages = rows > 1 ? m_pagesPerRow : pages;
00352 return QSize( m_spacing + hPages * ( m_doc->paperWidth(m_doc->startPage()) + m_spacing ),
00353 m_spacing + rows * ( m_doc->paperHeight(m_doc->startPage()) + m_spacing ) );
00354 }
00355
00356 QPoint KWViewModePreview::normalToView( const QPoint & nPoint )
00357 {
00358
00359 double unzoomedY = m_doc->unzoomItY( nPoint.y() );
00360 KWPage *page = m_doc->pageManager()->page(unzoomedY);
00361 if( !page) {
00362 kdWarning(31001) << "KWViewModePreview::normalToView request for conversion out of the document! Check your input data.. ("<< nPoint << ")" << endl;
00363 return QPoint(0,0);
00364 }
00365
00366 double yInPagePt = unzoomedY - page->offsetInDocument();
00367 int row = (page->pageNumber() - m_doc->startPage()) / m_pagesPerRow;
00368 int col = (page->pageNumber() - m_doc->startPage()) % m_pagesPerRow;
00369
00370
00371
00372
00373 return QPoint( leftSpacing() + col * ( m_doc->paperWidth(page->pageNumber()) +
00374 m_spacing ) + nPoint.x(),
00375 topSpacing() + row * ( m_doc->paperHeight(page->pageNumber()) +
00376 m_spacing ) + m_doc->zoomItY( yInPagePt ) );
00377 }
00378
00379 QRect KWViewModePreview::viewPageRect( int pgNum )
00380 {
00381 int row = (pgNum - m_doc->startPage()) / m_pagesPerRow;
00382 int col = (pgNum - m_doc->startPage()) % m_pagesPerRow;
00383 const int paperWidth = m_doc->paperWidth( pgNum );
00384 const int paperHeight = m_doc->paperHeight( pgNum );
00385 return QRect( leftSpacing() + col * ( paperWidth + m_spacing ),
00386 topSpacing() + row * ( paperHeight + m_spacing ),
00387 paperWidth,
00388 paperHeight );
00389 }
00390
00391 QPoint KWViewModePreview::viewToNormal( const QPoint & vPoint )
00392 {
00393
00394 int paperWidth = m_doc->paperWidth(m_doc->startPage());
00395 int paperHeight = m_doc->paperHeight(m_doc->startPage());
00396 QPoint p( vPoint.x() - leftSpacing(), vPoint.y() - topSpacing() );
00397 int col = static_cast<int>( p.x() / ( paperWidth + m_spacing ) );
00398 int xInPage = p.x() - col * ( paperWidth + m_spacing );
00399 int row = static_cast<int>( p.y() / ( paperHeight + m_spacing ) );
00400 int yInPage = p.y() - row * ( paperHeight + m_spacing );
00401 int page = row * m_pagesPerRow + col + m_doc->startPage();
00402 if ( page > m_doc->lastPage() )
00403 return QPoint( paperWidth, m_doc->pageTop( m_doc->lastPage() ) );
00404 else
00405 return QPoint( xInPage, yInPage + m_doc->pageTop( page ) );
00406 }
00407
00408 void KWViewModePreview::drawPageBorders( QPainter * painter, const QRect & crect, const QRegion & emptySpaceRegion )
00409 {
00410 painter->save();
00411 painter->setPen( QApplication::palette().active().color( QColorGroup::Dark ) );
00412 painter->setBrush( Qt::NoBrush );
00413
00414 QRegion grayRegion( crect );
00415 int pageCount = m_doc->pageCount();
00416 for ( int counter = 0; counter < pageCount; counter++ )
00417 {
00418 int row = counter / m_pagesPerRow;
00419 int col = counter % m_pagesPerRow;
00420 int page = m_doc->startPage() + counter;
00421 int paperWidth = m_doc->paperWidth(page);
00422 int paperHeight = m_doc->paperHeight(page);
00423 QRect pageRect( leftSpacing() + col * ( paperWidth + m_spacing ),
00424 topSpacing() + row * ( paperHeight + m_spacing ),
00425 paperWidth, paperHeight );
00426 drawOnePageBorder( painter, crect, pageRect, emptySpaceRegion );
00427 if( pageRect.top() > crect.bottom())
00428 break;
00429 if ( pageRect.intersects( crect ) )
00430 grayRegion -= pageRect;
00431 QRect rightShadow = drawRightShadow( painter, crect, pageRect, s_shadowOffset );
00432 if ( !rightShadow.isEmpty() )
00433 grayRegion -= rightShadow;
00434 QRect bottomShadow = drawBottomShadow( painter, crect, pageRect, s_shadowOffset );
00435 if ( !bottomShadow.isEmpty() )
00436 grayRegion -= bottomShadow;
00437
00438
00439
00440 }
00441 if ( !grayRegion.isEmpty() )
00442 {
00443
00444 m_doc->eraseEmptySpace( painter, grayRegion, QApplication::palette().active().brush( QColorGroup::Mid ) );
00445 }
00446 painter->restore();
00447 }
00448
00450
00451 KWViewModeText::KWViewModeText( KWDocument * doc, KWCanvas* canvas, KWTextFrameSet* fs )
00452 : KWViewMode( doc, canvas, false )
00453 {
00454 Q_ASSERT( fs );
00455 m_textFrameSet = fs;
00456 }
00457
00458 KWTextFrameSet * KWViewModeText::textFrameSet() const
00459 {
00460 return m_textFrameSet;
00461 }
00462
00463 KWTextFrameSet * KWViewModeText::determineTextFrameSet( KWDocument* doc )
00464 {
00465 KWTextFrameSet* fs = 0L;
00466
00467 if(!doc->getAllViews().empty()) {
00468 KWView *view = doc->getAllViews()[0];
00469 KWFrameView *fv = view->getGUI()->canvasWidget()->frameViewManager()->selectedFrame();
00470 KWFrame *f = fv == 0 ? 0 : fv->frame();
00471 if(f)
00472 fs = dynamic_cast<KWTextFrameSet *>( f->frameSet() );
00473
00474 if (!fs) {
00475 KWFrameSetEdit *fse = view->getGUI()->canvasWidget()->currentFrameSetEdit();
00476 if(fse)
00477 fs = dynamic_cast<KWTextFrameSet *>( fse->frameSet() );
00478 }
00479 }
00480
00481 if (!fs || fs->isHeaderOrFooter() || fs->isFootEndNote())
00482
00483 if ( doc->frameSetCount() > 0 && doc->frameSet( 0 )->isVisible() )
00484 fs = dynamic_cast<KWTextFrameSet *>( doc->frameSet( 0 ) );
00485
00486 return fs;
00487 }
00488
00489 QPoint KWViewModeText::normalToView( const QPoint & nPoint )
00490 {
00491 QPoint r (nPoint);
00492 r.setX(r.x() + OFFSET);
00493 return r;
00494 }
00495
00496 QPoint KWViewModeText::viewToNormal( const QPoint & vPoint )
00497 {
00498 QPoint r (vPoint);
00499 r.setX(r.x() - OFFSET);
00500 return r;
00501 }
00502
00503 QSize KWViewModeText::contentsSize()
00504 {
00505 textFrameSet();
00506
00507 if (!m_textFrameSet)
00508 return QSize();
00509
00510
00511
00512
00513
00514 int width = m_doc->layoutUnitToPixelX( m_textFrameSet->textDocument()->width() );
00515
00516 int height = QMAX((int)m_doc->paperHeight(m_doc->startPage()),
00517 m_doc->layoutUnitToPixelY( m_textFrameSet->textDocument()->height() ) );
00518
00519 return QSize( width, height );
00520 }
00521
00522 QSize KWViewModeText::availableSizeForText( KWTextFrameSet* )
00523 {
00524 return contentsSize();
00525 }
00526
00527 bool KWViewModeText::isFrameSetVisible( const KWFrameSet *fs )
00528 {
00529 if(fs==NULL) return false;
00530 if(fs==textFrameSet()) return true;
00531
00532 const KWFrameSet* parentFrameset = fs->groupmanager() ? fs->groupmanager() : fs;
00533 while ( parentFrameset->isFloating() ) {
00534 parentFrameset = parentFrameset->anchorFrameset();
00535 if ( parentFrameset == m_textFrameSet )
00536 return true;
00537 }
00538 return false;
00539 }
00540
00541 void KWViewModeText::drawPageBorders( QPainter * painter, const QRect & crect,
00542 const QRegion & )
00543 {
00544 painter->save();
00545 QRegion grayRegion( crect );
00546
00547 painter->setPen( QApplication::palette().active().color( QColorGroup::Dark ) );
00548 QSize cSize = contentsSize();
00549
00550
00551 QRect frameRect( OFFSET, 0, cSize.width() + 2, cSize.height() );
00552
00553 painter->drawLine( frameRect.topRight(), frameRect.bottomRight() );
00554 if ( frameRect.intersects( crect ) )
00555 grayRegion -= frameRect;
00556
00557
00558 if ( crect.bottom() >= cSize.height() )
00559 {
00560
00561 painter->drawLine( 0, cSize.height(),
00562 cSize.width(), cSize.height() );
00563 grayRegion -= QRect( 0, cSize.height(),
00564 cSize.width(), cSize.height() );
00565 }
00566
00567 if ( !grayRegion.isEmpty() )
00568 m_doc->eraseEmptySpace( painter, grayRegion, QApplication::palette().active().brush( QColorGroup::Mid ) );
00569 painter->restore();
00570 }
00571
00572 QRect KWViewModeText::rulerFrameRect()
00573 {
00574 return QRect( QPoint(OFFSET, 0), contentsSize() );
00575 }
00576
00577 void KWViewModeText::setPageLayout( KoRuler* hRuler, KoRuler* vRuler, const KoPageLayout& )
00578 {
00579
00580 KoPageLayout layout;
00581 layout.format = PG_CUSTOM;
00582 layout.orientation = PG_PORTRAIT;
00583 QSize cSize = contentsSize();
00584 layout.ptWidth = m_doc->unzoomItX( cSize.width() );
00585 layout.ptHeight = m_doc->unzoomItY( cSize.height() );
00586
00587 layout.ptLeft = OFFSET;
00588 layout.ptRight = 0;
00589 layout.ptTop = 0;
00590 layout.ptBottom = 0;
00591 layout.ptBindingSide = 0;
00592 layout.ptPageEdge = 0;
00593 hRuler->setPageLayout( layout );
00594 vRuler->setPageLayout( layout );
00595 }
00596
00597 bool KWViewModeText::isTextModeFrameset(KWFrameSet *fs) const {
00598 return fs==m_textFrameSet;
00599 }
00600
00601
00602 int KWViewModePrint::xOffset(KWPage *page, int canvasWidth) {
00603 Q_UNUSED(page);
00604 Q_UNUSED(canvasWidth);
00605 return 0;
00606 }