| # kdeprint Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# posterpreview.cpp

    00001 /*
    00002  *  This file is part of the KDE libraries
    00003  *  Copyright (c) 2001-2002 Michael Goffioul <kdeprint@swing.be>
    00004  *
    00005  *  This library is free software; you can redistribute it and/or
    00006  *  modify it under the terms of the GNU Library General Public
    00007  *  License version 2 as published by the Free Software Foundation.
    00008  *
    00009  *  This library is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012  *  Library General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU Library General Public License
    00015  *  along with this library; see the file COPYING.LIB.  If not, write to
    00016  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00017  *  Boston, MA 02111-1307, USA.
    00018  **/
    00019
    00020 #include "posterpreview.h"
    00021
    00022 #include <kprocess.h>
    00023 #include <qpainter.h>
    00024 #include <qsimplerichtext.h>
    00025 #include <qtimer.h>
    00026 #include <qpixmap.h>
    00027 #include <kprinter.h>
    00028 #include <klocale.h>
    00029 #include <kcursor.h>
    00030 #include <kglobalsettings.h>
    00031
    00032 PosterPreview::PosterPreview( QWidget *parent, const char *name )
    00033     : QFrame( parent, name )
    00034 {
    00035     m_postersize = m_mediasize = "A4";
    00036     m_cutmargin = 5;
    00037     init();
    00038 }
    00039
    00040 PosterPreview::PosterPreview( const QString& postersize, const QString& mediasize, QWidget *parent, const char *name )
    00041     : QFrame( parent, name )
    00042 {
    00043     m_postersize = postersize;
    00044     m_mediasize = mediasize;
    00045     m_cutmargin = 5;
    00046     init();
    00047 }
    00048
    00049 PosterPreview::~PosterPreview()
    00050 {
    00051     delete m_process;
    00052 }
    00053
    00054 void PosterPreview::init()
    00055 {
    00056     m_process = new KProcess;
    00057     connect( m_process, SIGNAL( receivedStderr( KProcess*, char*, int ) ), SLOT( slotProcessStderr( KProcess*, char*, int ) ) );
    00058     connect( m_process, SIGNAL( processExited( KProcess* ) ), SLOT( slotProcessExited( KProcess* ) ) );
    00059
    00060     m_cols = m_rows = m_pw = m_ph = m_mw = m_mh = 0;
    00061     m_dirty = false;
    00062     setDirty();
    00063     setMouseTracking( true );
    00064     setBackgroundMode( Qt::NoBackground );
    00065 }
    00066
    00067 void PosterPreview::parseBuffer()
    00068 {
    00069     int rotate;
    00070     float pw, ph, mw, mh;
    00071     float x1, x2, y1, y2;
    00072     sscanf( m_buffer.ascii(), "%d %d %d %g %g %g %g %g %g %g %g", &m_rows, &m_cols, &rotate,
    00073             &pw, &ph, &mw, &mh, &x1, &y1, &x2, &y2 );
    00074     m_pw = ( int )( rotate ? ph : pw );
    00075     m_ph = ( int )( rotate ? pw : ph );
    00076     m_mw = ( int )( rotate ? mh : mw );
    00077     m_mh = ( int )( rotate ? mw : mh );
    00078     m_posterbb.setCoords( ( int )x1, ( int )y1, ( int )x2, ( int )y2 );
    00079 }
    00080
    00081 void PosterPreview::setDirty()
    00082 {
    00083     if ( !m_dirty )
    00084     {
    00085         m_dirty = true;
    00086         QTimer::singleShot( 1, this, SLOT( updatePoster() ) );
    00087     }
    00088 }
    00089
    00090 void PosterPreview::updatePoster()
    00091 {
    00092     m_buffer = "";
    00093     m_process->clearArguments();
    00094     *m_process << "poster" << "-F" << "-m" + m_mediasize << "-p" + m_postersize
    00095         << "-c" + QString::number( m_cutmargin ) + "%";
    00096     if ( !m_process->start( KProcess::NotifyOnExit, KProcess::Stderr ) )
    00097     {
    00098         m_rows = m_cols = 0;
    00099         m_dirty = false;
    00100         update();
    00101     }
    00102 }
    00103
    00104 void PosterPreview::drawContents( QPainter *painter )
    00105 {
    00106     QPixmap pix( width(), height() );
    00107     QPainter *p = new QPainter( &pix );
    00108
    00109     p->fillRect( 0, 0, width(), height(), colorGroup().background() );
    00110
    00111     if ( isEnabled() )
    00112     {
    00113         if ( m_rows <= 0 || m_cols <= 0 || m_pw <= 0 || m_ph <= 0 )
    00114         {
    00115             QString txt = i18n( "Poster preview not available. Either the <b>poster</b> "
    00116                           "executable is not properly installed, or you don't have "
    00117                           "the required version; available at http://printing.kde.org/downloads/." );
    00118             QSimpleRichText richtext( ( m_buffer.isEmpty() ? txt : m_buffer.prepend( "<pre>" ).append( "</pre>" ) ), p->font() );
    00119             richtext.adjustSize();
    00120             int x = ( width()-richtext.widthUsed() )/2, y = ( height()-richtext.height() )/2;
    00121             x = QMAX( x, 0 );
    00122             y = QMAX( y, 0 );
    00123             richtext.draw( p, x, y, QRect( x, y, richtext.widthUsed(), richtext.height() ), colorGroup() );
    00124             m_boundingrect = QRect();
    00125         }
    00126         else
    00127         {
    00128             int totalx = m_cols*m_pw, totaly = m_rows*m_ph;
    00129             float scale = QMIN( float( width()-1 )/totalx, float( height()-1 )/totaly );
    00130             p->translate( 0, height()-1 );
    00131             p->scale( scale, -scale );
    00132             int x = ( int )( width()/scale-totalx )/2, y = ( int )( height()/scale-totaly )/2;
    00133             p->translate( x, y );
    00134             m_boundingrect = p->xForm( QRect( 0, 0, totalx, totaly ) );
    00135
    00136             x = y = 0;
    00137             int px = m_posterbb.x(), py = m_posterbb.y(), pw = m_posterbb.width(), ph = m_posterbb.height();
    00138             for ( int i=0; i<m_rows; i++, y+=m_ph, x=0 )
    00139             {
    00140                 for ( int j=0; j<m_cols; j++, x+=m_pw )
    00141                 {
    00142                     bool selected = ( m_selectedpages.find( i*m_cols+j+1 ) != m_selectedpages.end() );
    00143                     p->fillRect( x+1, y+1, m_pw-2, m_ph-2, ( selected ? KGlobalSettings::highlightColor() : white ) );
    00144                     p->drawRect( x, y, m_pw, m_ph );
    00145                     if ( pw > 0 && ph > 0 )
    00146                         p->fillRect( x+m_mw+px, y+m_mh+py, QMIN( pw, m_pw-2*m_mw-px ), QMIN( ph, m_ph-2*m_mh-py ),
    00147                                 ( selected ? KGlobalSettings::highlightColor().dark( 160 ) : lightGray ) );
    00148                     p->setPen( Qt::DotLine );
    00149                     p->drawRect( x+m_mw, y+m_mh, m_pw-2*m_mw, m_ph-2*m_mh );
    00150                     p->setPen( Qt::SolidLine );
    00151
    00152                     pw -= m_pw-2*m_mw-px;
    00153                     px = 0;
    00154                 }
    00155
    00156                 px = m_posterbb.x();
    00157                 ph -= m_ph-2*m_mh-py;
    00158                 py = 0;
    00159                 pw = m_posterbb.width();
    00160             }
    00161         }
    00162     }
    00163
    00164     delete p;
    00165     painter->drawPixmap( 0, 0, pix );
    00166 }
    00167
    00168 void PosterPreview::mouseMoveEvent( QMouseEvent *e )
    00169 {
    00170     if ( m_boundingrect.isValid() )
    00171     {
    00172         if ( m_boundingrect.contains( e->pos() ) )
    00173             setCursor( KCursor::handCursor() );
    00174         else
    00175             setCursor( KCursor::arrowCursor() );
    00176     }
    00177 }
    00178
    00179 void PosterPreview::mousePressEvent( QMouseEvent *e )
    00180 {
    00181     if ( e->button() == Qt::LeftButton && m_boundingrect.isValid() )
    00182     {
    00183         if ( m_boundingrect.contains( e->pos() ) )
    00184         {
    00185             int c, r;
    00186             c = ( e->pos().x()-m_boundingrect.x() )/( m_boundingrect.width()/m_cols ) + 1;
    00187             r = m_rows - ( e->pos().y()-m_boundingrect.y() )/( m_boundingrect.height()/m_rows );
    00188             int pagenum = ( r-1 )*m_cols+c;
    00189
    00190             if ( m_selectedpages.find( pagenum ) == m_selectedpages.end() ||
    00191                     !( e->state() & Qt::ShiftButton ) )
    00192             {
    00193                 if ( !( e->state() & Qt::ShiftButton ) )
    00194                     m_selectedpages.clear();
    00195                 m_selectedpages.append( pagenum );
    00196                 update();
    00197                 emitSelectedPages();
    00198             }
    00199         }
    00200         else if ( m_selectedpages.count() > 0 )
    00201         {
    00202             m_selectedpages.clear();
    00203             update();
    00204             emitSelectedPages();
    00205         }
    00206     }
    00207 }
    00208
    00209 void PosterPreview::slotProcessStderr( KProcess*, char *buf, int len )
    00210 {
    00211     m_buffer.append( QCString( buf, len ) );
    00212 }
    00213
    00214 void PosterPreview::slotProcessExited( KProcess* )
    00215 {
    00216     if ( m_process->normalExit() && m_process->exitStatus() == 0 )
    00217         parseBuffer();
    00218     else
    00219         m_rows = m_cols = 0;
    00220
    00221     m_dirty = false;
    00222     update();
    00223 }
    00224
    00225 void PosterPreview::setPosterSize( int s )
    00226 {
    00227     setPosterSize( pageSizeToPageName( KPrinter::PageSize( s ) ) );
    00228 }
    00229
    00230 void PosterPreview::setPosterSize( const QString& s )
    00231 {
    00232     if ( m_postersize != s )
    00233     {
    00234         m_selectedpages.clear();
    00235         m_postersize = s;
    00236         setDirty();
    00237         emitSelectedPages();
    00238     }
    00239 }
    00240
    00241 void PosterPreview::setMediaSize( int s )
    00242 {
    00243     setMediaSize( pageSizeToPageName( ( KPrinter::PageSize )s ) );
    00244 }
    00245
    00246 void PosterPreview::setMediaSize( const QString& s )
    00247 {
    00248     if ( m_mediasize != s )
    00249     {
    00250         m_selectedpages.clear();
    00251         m_mediasize = s;
    00252         setDirty();
    00253         emitSelectedPages();
    00254     }
    00255 }
    00256
    00257 void PosterPreview::setCutMargin( int value )
    00258 {
    00259     m_cutmargin = value;
    00260     setDirty();
    00261 }
    00262
    00263 void PosterPreview::setSelectedPages( const QString& s )
    00264 {
    00265     QStringList l = QStringList::split( ",", s, false );
    00266     m_selectedpages.clear();
    00267     for ( QStringList::ConstIterator it=l.begin(); it!=l.end(); ++it )
    00268     {
    00269         int p;
    00270         if ( ( p = ( *it ).find( '-' ) ) == -1 )
    00271             m_selectedpages.append( ( *it ).toInt() );
    00272         else
    00273         {
    00274             int p1 = ( *it ).left( p ).toInt(), p2 = ( *it ).mid( p+1 ).toInt();
    00275             for ( int i=p1; i<=p2; i++ )
    00276                 m_selectedpages.append( i );
    00277         }
    00278     }
    00279     update();
    00280 }
    00281
    00282 void PosterPreview::emitSelectedPages()
    00283 {
    00284     QString s;
    00285     if ( m_selectedpages.count() > 0 )
    00286     {
    00287         for ( QValueList<int>::ConstIterator it=m_selectedpages.begin(); it!=m_selectedpages.end(); ++it )
    00288             s.append( QString::number( *it ) + "," );
    00289         s.truncate( s.length()-1 );
    00290     }
    00291     emit selectionChanged( s );
    00292 }
    00293
    00294 #include "posterpreview.moc"

This file is part of the documentation for kdeprint Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:31:05 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003