posterpreview.cpp
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (c) 2001-2002 Michael Goffioul <tdeprint@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., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 **/ 00019 00020 #include "posterpreview.h" 00021 00022 #include <kprocess.h> 00023 #include <tqpainter.h> 00024 #include <tqsimplerichtext.h> 00025 #include <tqtimer.h> 00026 #include <tqpixmap.h> 00027 #include <kprinter.h> 00028 #include <tdelocale.h> 00029 #include <kcursor.h> 00030 #include <tdeglobalsettings.h> 00031 00032 PosterPreview::PosterPreview( TQWidget *parent, const char *name ) 00033 : TQFrame( parent, name ) 00034 { 00035 m_postersize = m_mediasize = "A4"; 00036 m_cutmargin = 5; 00037 init(); 00038 } 00039 00040 PosterPreview::PosterPreview( const TQString& postersize, const TQString& mediasize, TQWidget *parent, const char *name ) 00041 : TQFrame( 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 TDEProcess; 00057 connect( m_process, TQT_SIGNAL( receivedStderr( TDEProcess*, char*, int ) ), TQT_SLOT( slotProcessStderr( TDEProcess*, char*, int ) ) ); 00058 connect( m_process, TQT_SIGNAL( processExited( TDEProcess* ) ), TQT_SLOT( slotProcessExited( TDEProcess* ) ) ); 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( TQt::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 TQTimer::singleShot( 1, this, TQT_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" + TQString::number( m_cutmargin ) + "%"; 00096 if ( !m_process->start( TDEProcess::NotifyOnExit, TDEProcess::Stderr ) ) 00097 { 00098 m_rows = m_cols = 0; 00099 m_dirty = false; 00100 update(); 00101 } 00102 } 00103 00104 void PosterPreview::drawContents( TQPainter *painter ) 00105 { 00106 TQPixmap pix( width(), height() ); 00107 TQPainter *p = new TQPainter( &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 TQString txt = i18n( "Poster preview not available. Either the <b>poster</b> " 00116 "executable is not properly installed, or you don't have the required version" ); 00117 TQSimpleRichText richtext( ( m_buffer.isEmpty() ? txt : m_buffer.prepend( "<pre>" ).append( "</pre>" ) ), p->font() ); 00118 richtext.adjustSize(); 00119 int x = ( width()-richtext.widthUsed() )/2, y = ( height()-richtext.height() )/2; 00120 x = TQMAX( x, 0 ); 00121 y = TQMAX( y, 0 ); 00122 richtext.draw( p, x, y, TQRect( x, y, richtext.widthUsed(), richtext.height() ), colorGroup() ); 00123 m_boundingrect = TQRect(); 00124 } 00125 else 00126 { 00127 int totalx = m_cols*m_pw, totaly = m_rows*m_ph; 00128 float scale = TQMIN( float( width()-1 )/totalx, float( height()-1 )/totaly ); 00129 p->translate( 0, height()-1 ); 00130 p->scale( scale, -scale ); 00131 int x = ( int )( width()/scale-totalx )/2, y = ( int )( height()/scale-totaly )/2; 00132 p->translate( x, y ); 00133 m_boundingrect = p->xForm( TQRect( 0, 0, totalx, totaly ) ); 00134 00135 x = y = 0; 00136 int px = m_posterbb.x(), py = m_posterbb.y(), pw = m_posterbb.width(), ph = m_posterbb.height(); 00137 for ( int i=0; i<m_rows; i++, y+=m_ph, x=0 ) 00138 { 00139 for ( int j=0; j<m_cols; j++, x+=m_pw ) 00140 { 00141 bool selected = ( m_selectedpages.find( i*m_cols+j+1 ) != m_selectedpages.end() ); 00142 p->fillRect( x+1, y+1, m_pw-2, m_ph-2, ( selected ? TDEGlobalSettings::highlightColor() : white ) ); 00143 p->drawRect( x, y, m_pw, m_ph ); 00144 if ( pw > 0 && ph > 0 ) 00145 p->fillRect( x+m_mw+px, y+m_mh+py, TQMIN( pw, m_pw-2*m_mw-px ), TQMIN( ph, m_ph-2*m_mh-py ), 00146 ( selected ? TQColor(TDEGlobalSettings::highlightColor().dark( 160 )) : lightGray ) ); 00147 p->setPen( Qt::DotLine ); 00148 p->drawRect( x+m_mw, y+m_mh, m_pw-2*m_mw, m_ph-2*m_mh ); 00149 p->setPen( Qt::SolidLine ); 00150 00151 pw -= m_pw-2*m_mw-px; 00152 px = 0; 00153 } 00154 00155 px = m_posterbb.x(); 00156 ph -= m_ph-2*m_mh-py; 00157 py = 0; 00158 pw = m_posterbb.width(); 00159 } 00160 } 00161 } 00162 00163 delete p; 00164 painter->drawPixmap( 0, 0, pix ); 00165 } 00166 00167 void PosterPreview::mouseMoveEvent( TQMouseEvent *e ) 00168 { 00169 if ( m_boundingrect.isValid() ) 00170 { 00171 if ( m_boundingrect.contains( e->pos() ) ) 00172 setCursor( KCursor::handCursor() ); 00173 else 00174 setCursor( KCursor::arrowCursor() ); 00175 } 00176 } 00177 00178 void PosterPreview::mousePressEvent( TQMouseEvent *e ) 00179 { 00180 if ( e->button() == Qt::LeftButton && m_boundingrect.isValid() ) 00181 { 00182 if ( m_boundingrect.contains( e->pos() ) ) 00183 { 00184 int c, r; 00185 c = ( e->pos().x()-m_boundingrect.x() )/( m_boundingrect.width()/m_cols ) + 1; 00186 r = m_rows - ( e->pos().y()-m_boundingrect.y() )/( m_boundingrect.height()/m_rows ); 00187 int pagenum = ( r-1 )*m_cols+c; 00188 00189 if ( m_selectedpages.find( pagenum ) == m_selectedpages.end() || 00190 !( e->state() & TQt::ShiftButton ) ) 00191 { 00192 if ( !( e->state() & TQt::ShiftButton ) ) 00193 m_selectedpages.clear(); 00194 m_selectedpages.append( pagenum ); 00195 update(); 00196 emitSelectedPages(); 00197 } 00198 } 00199 else if ( m_selectedpages.count() > 0 ) 00200 { 00201 m_selectedpages.clear(); 00202 update(); 00203 emitSelectedPages(); 00204 } 00205 } 00206 } 00207 00208 void PosterPreview::slotProcessStderr( TDEProcess*, char *buf, int len ) 00209 { 00210 m_buffer.append( TQCString( buf, len ) ); 00211 } 00212 00213 void PosterPreview::slotProcessExited( TDEProcess* ) 00214 { 00215 if ( m_process->normalExit() && m_process->exitStatus() == 0 ) 00216 parseBuffer(); 00217 else 00218 m_rows = m_cols = 0; 00219 00220 m_dirty = false; 00221 update(); 00222 } 00223 00224 void PosterPreview::setPosterSize( int s ) 00225 { 00226 setPosterSize( pageSizeToPageName( KPrinter::PageSize( s ) ) ); 00227 } 00228 00229 void PosterPreview::setPosterSize( const TQString& s ) 00230 { 00231 if ( m_postersize != s ) 00232 { 00233 m_selectedpages.clear(); 00234 m_postersize = s; 00235 setDirty(); 00236 emitSelectedPages(); 00237 } 00238 } 00239 00240 void PosterPreview::setMediaSize( int s ) 00241 { 00242 setMediaSize( pageSizeToPageName( ( KPrinter::PageSize )s ) ); 00243 } 00244 00245 void PosterPreview::setMediaSize( const TQString& s ) 00246 { 00247 if ( m_mediasize != s ) 00248 { 00249 m_selectedpages.clear(); 00250 m_mediasize = s; 00251 setDirty(); 00252 emitSelectedPages(); 00253 } 00254 } 00255 00256 void PosterPreview::setCutMargin( int value ) 00257 { 00258 m_cutmargin = value; 00259 setDirty(); 00260 } 00261 00262 void PosterPreview::setSelectedPages( const TQString& s ) 00263 { 00264 TQStringList l = TQStringList::split( ",", s, false ); 00265 m_selectedpages.clear(); 00266 for ( TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it ) 00267 { 00268 int p; 00269 if ( ( p = ( *it ).find( '-' ) ) == -1 ) 00270 m_selectedpages.append( ( *it ).toInt() ); 00271 else 00272 { 00273 int p1 = ( *it ).left( p ).toInt(), p2 = ( *it ).mid( p+1 ).toInt(); 00274 for ( int i=p1; i<=p2; i++ ) 00275 m_selectedpages.append( i ); 00276 } 00277 } 00278 update(); 00279 } 00280 00281 void PosterPreview::emitSelectedPages() 00282 { 00283 TQString s; 00284 if ( m_selectedpages.count() > 0 ) 00285 { 00286 for ( TQValueList<int>::ConstIterator it=m_selectedpages.begin(); it!=m_selectedpages.end(); ++it ) 00287 s.append( TQString::number( *it ) + "," ); 00288 s.truncate( s.length()-1 ); 00289 } 00290 emit selectionChanged( s ); 00291 } 00292 00293 #include "posterpreview.moc"