20 #include "posterpreview.h"
23 #include <tqpainter.h>
24 #include <tqsimplerichtext.h>
30 #include <kglobalsettings.h>
32 PosterPreview::PosterPreview( TQWidget *parent,
const char *name )
33 : TQFrame( parent, name )
35 m_postersize = m_mediasize =
"A4";
40 PosterPreview::PosterPreview(
const TQString& postersize,
const TQString& mediasize, TQWidget *parent,
const char *name )
41 : TQFrame( parent, name )
43 m_postersize = postersize;
44 m_mediasize = mediasize;
49 PosterPreview::~PosterPreview()
54 void PosterPreview::init()
56 m_process =
new KProcess;
57 connect( m_process, TQT_SIGNAL( receivedStderr( KProcess*,
char*,
int ) ), TQT_SLOT( slotProcessStderr( KProcess*,
char*,
int ) ) );
58 connect( m_process, TQT_SIGNAL( processExited( KProcess* ) ), TQT_SLOT( slotProcessExited( KProcess* ) ) );
60 m_cols = m_rows = m_pw = m_ph = m_mw = m_mh = 0;
63 setMouseTracking(
true );
64 setBackgroundMode( TQt::NoBackground );
67 void PosterPreview::parseBuffer()
72 sscanf( m_buffer.ascii(),
"%d %d %d %g %g %g %g %g %g %g %g", &m_rows, &m_cols, &rotate,
73 &pw, &ph, &mw, &mh, &x1, &y1, &x2, &y2 );
74 m_pw = ( int )( rotate ? ph : pw );
75 m_ph = ( int )( rotate ? pw : ph );
76 m_mw = ( int )( rotate ? mh : mw );
77 m_mh = ( int )( rotate ? mw : mh );
78 m_posterbb.setCoords( (
int )x1, (
int )y1, (
int )x2, (
int )y2 );
81 void PosterPreview::setDirty()
86 TQTimer::singleShot( 1,
this, TQT_SLOT( updatePoster() ) );
90 void PosterPreview::updatePoster()
93 m_process->clearArguments();
94 *m_process <<
"poster" <<
"-F" <<
"-m" + m_mediasize <<
"-p" + m_postersize
95 <<
"-c" + TQString::number( m_cutmargin ) +
"%";
96 if ( !m_process->start( KProcess::NotifyOnExit, KProcess::Stderr ) )
104 void PosterPreview::drawContents( TQPainter *painter )
106 TQPixmap pix( width(), height() );
107 TQPainter *p =
new TQPainter( &pix );
109 p->fillRect( 0, 0, width(), height(), colorGroup().background() );
113 if ( m_rows <= 0 || m_cols <= 0 || m_pw <= 0 || m_ph <= 0 )
115 TQString txt = i18n(
"Poster preview not available. Either the <b>poster</b> "
116 "executable is not properly installed, or you don't have "
117 "the required version; available at http://printing.kde.org/downloads/." );
118 TQSimpleRichText richtext( ( m_buffer.isEmpty() ? txt : m_buffer.prepend(
"<pre>" ).append(
"</pre>" ) ), p->font() );
119 richtext.adjustSize();
120 int x = ( width()-richtext.widthUsed() )/2, y = ( height()-richtext.height() )/2;
123 richtext.draw( p, x, y, TQRect( x, y, richtext.widthUsed(), richtext.height() ), colorGroup() );
124 m_boundingrect = TQRect();
128 int totalx = m_cols*m_pw, totaly = m_rows*m_ph;
129 float scale = QMIN(
float( width()-1 )/totalx,
float( height()-1 )/totaly );
130 p->translate( 0, height()-1 );
131 p->scale( scale, -scale );
132 int x = ( int )( width()/scale-totalx )/2, y = (
int )( height()/scale-totaly )/2;
133 p->translate( x, y );
134 m_boundingrect = p->xForm( TQRect( 0, 0, totalx, totaly ) );
137 int px = m_posterbb.x(), py = m_posterbb.y(), pw = m_posterbb.width(), ph = m_posterbb.height();
138 for (
int i=0; i<m_rows; i++, y+=m_ph, x=0 )
140 for (
int j=0; j<m_cols; j++, x+=m_pw )
142 bool selected = ( m_selectedpages.find( i*m_cols+j+1 ) != m_selectedpages.end() );
143 p->fillRect( x+1, y+1, m_pw-2, m_ph-2, ( selected ? KGlobalSettings::highlightColor() : white ) );
144 p->drawRect( x, y, m_pw, m_ph );
145 if ( pw > 0 && ph > 0 )
146 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 ),
147 ( selected ? TQColor(KGlobalSettings::highlightColor().dark( 160 )) : lightGray ) );
148 p->setPen( Qt::DotLine );
149 p->drawRect( x+m_mw, y+m_mh, m_pw-2*m_mw, m_ph-2*m_mh );
150 p->setPen( Qt::SolidLine );
152 pw -= m_pw-2*m_mw-px;
157 ph -= m_ph-2*m_mh-py;
159 pw = m_posterbb.width();
165 painter->drawPixmap( 0, 0, pix );
168 void PosterPreview::mouseMoveEvent( TQMouseEvent *e )
170 if ( m_boundingrect.isValid() )
172 if ( m_boundingrect.contains( e->pos() ) )
173 setCursor( KCursor::handCursor() );
175 setCursor( KCursor::arrowCursor() );
179 void PosterPreview::mousePressEvent( TQMouseEvent *e )
181 if ( e->button() == Qt::LeftButton && m_boundingrect.isValid() )
183 if ( m_boundingrect.contains( e->pos() ) )
186 c = ( e->pos().x()-m_boundingrect.x() )/( m_boundingrect.width()/m_cols ) + 1;
187 r = m_rows - ( e->pos().y()-m_boundingrect.y() )/( m_boundingrect.height()/m_rows );
188 int pagenum = ( r-1 )*m_cols+c;
190 if ( m_selectedpages.find( pagenum ) == m_selectedpages.end() ||
191 !( e->state() & TQt::ShiftButton ) )
193 if ( !( e->state() & TQt::ShiftButton ) )
194 m_selectedpages.clear();
195 m_selectedpages.append( pagenum );
200 else if ( m_selectedpages.count() > 0 )
202 m_selectedpages.clear();
209 void PosterPreview::slotProcessStderr( KProcess*,
char *buf,
int len )
211 m_buffer.append( TQCString( buf, len ) );
214 void PosterPreview::slotProcessExited( KProcess* )
216 if ( m_process->normalExit() && m_process->exitStatus() == 0 )
225 void PosterPreview::setPosterSize(
int s )
230 void PosterPreview::setPosterSize(
const TQString& s )
232 if ( m_postersize != s )
234 m_selectedpages.clear();
241 void PosterPreview::setMediaSize(
int s )
246 void PosterPreview::setMediaSize(
const TQString& s )
248 if ( m_mediasize != s )
250 m_selectedpages.clear();
257 void PosterPreview::setCutMargin(
int value )
263 void PosterPreview::setSelectedPages(
const TQString& s )
265 TQStringList l = TQStringList::split(
",", s,
false );
266 m_selectedpages.clear();
267 for ( TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it )
270 if ( ( p = ( *it ).find(
'-' ) ) == -1 )
271 m_selectedpages.append( ( *it ).toInt() );
274 int p1 = ( *it ).left( p ).toInt(), p2 = ( *it ).mid( p+1 ).toInt();
275 for (
int i=p1; i<=p2; i++ )
276 m_selectedpages.append( i );
282 void PosterPreview::emitSelectedPages()
285 if ( m_selectedpages.count() > 0 )
287 for ( TQValueList<int>::ConstIterator it=m_selectedpages.begin(); it!=m_selectedpages.end(); ++it )
288 s.append( TQString::number( *it ) +
"," );
289 s.truncate( s.length()-1 );
291 emit selectionChanged( s );
294 #include "posterpreview.moc"