20 #include "posterpreview.h"
23 #include <tqpainter.h>
24 #include <tqsimplerichtext.h>
28 #include <tdelocale.h>
30 #include <tdeglobalsettings.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 TDEProcess;
57 connect( m_process, TQ_SIGNAL( receivedStderr( TDEProcess*,
char*,
int ) ), TQ_SLOT( slotProcessStderr( TDEProcess*,
char*,
int ) ) );
58 connect( m_process, TQ_SIGNAL( processExited( TDEProcess* ) ), TQ_SLOT( slotProcessExited( TDEProcess* ) ) );
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, TQ_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( TDEProcess::NotifyOnExit, TDEProcess::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 the required version" );
117 TQSimpleRichText richtext( ( m_buffer.isEmpty() ? txt : m_buffer.prepend(
"<pre>" ).append(
"</pre>" ) ), p->font() );
118 richtext.adjustSize();
119 int x = ( width()-richtext.widthUsed() )/2, y = ( height()-richtext.height() )/2;
122 richtext.draw( p, x, y, TQRect( x, y, richtext.widthUsed(), richtext.height() ), colorGroup() );
123 m_boundingrect = TQRect();
127 int totalx = m_cols*m_pw, totaly = m_rows*m_ph;
128 float scale = TQMIN(
float( width()-1 )/totalx,
float( height()-1 )/totaly );
129 p->translate( 0, height()-1 );
130 p->scale( scale, -scale );
131 int x = ( int )( width()/scale-totalx )/2, y = (
int )( height()/scale-totaly )/2;
132 p->translate( x, y );
133 m_boundingrect = p->xForm( TQRect( 0, 0, totalx, totaly ) );
136 int px = m_posterbb.x(), py = m_posterbb.y(), pw = m_posterbb.width(), ph = m_posterbb.height();
137 for (
int i=0; i<m_rows; i++, y+=m_ph, x=0 )
139 for (
int j=0; j<m_cols; j++, x+=m_pw )
141 bool selected = ( m_selectedpages.find( i*m_cols+j+1 ) != m_selectedpages.end() );
142 p->fillRect( x+1, y+1, m_pw-2, m_ph-2, ( selected ? TDEGlobalSettings::highlightColor() : white ) );
143 p->drawRect( x, y, m_pw, m_ph );
144 if ( pw > 0 && ph > 0 )
145 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 ),
146 ( selected ? TQColor(TDEGlobalSettings::highlightColor().dark( 160 )) : lightGray ) );
147 p->setPen( TQt::DotLine );
148 p->drawRect( x+m_mw, y+m_mh, m_pw-2*m_mw, m_ph-2*m_mh );
149 p->setPen( TQt::SolidLine );
151 pw -= m_pw-2*m_mw-px;
156 ph -= m_ph-2*m_mh-py;
158 pw = m_posterbb.width();
164 painter->drawPixmap( 0, 0, pix );
167 void PosterPreview::mouseMoveEvent( TQMouseEvent *e )
169 if ( m_boundingrect.isValid() )
171 if ( m_boundingrect.contains( e->pos() ) )
172 setCursor( KCursor::handCursor() );
174 setCursor( KCursor::arrowCursor() );
178 void PosterPreview::mousePressEvent( TQMouseEvent *e )
180 if ( e->button() == TQt::LeftButton && m_boundingrect.isValid() )
182 if ( m_boundingrect.contains( e->pos() ) )
185 c = ( e->pos().x()-m_boundingrect.x() )/( m_boundingrect.width()/m_cols ) + 1;
186 r = m_rows - ( e->pos().y()-m_boundingrect.y() )/( m_boundingrect.height()/m_rows );
187 int pagenum = ( r-1 )*m_cols+c;
189 if ( m_selectedpages.find( pagenum ) == m_selectedpages.end() ||
190 !( e->state() & TQt::ShiftButton ) )
192 if ( !( e->state() & TQt::ShiftButton ) )
193 m_selectedpages.clear();
194 m_selectedpages.append( pagenum );
199 else if ( m_selectedpages.count() > 0 )
201 m_selectedpages.clear();
208 void PosterPreview::slotProcessStderr( TDEProcess*,
char *buf,
int len )
210 m_buffer.append( TQCString( buf, len ) );
213 void PosterPreview::slotProcessExited( TDEProcess* )
215 if ( m_process->normalExit() && m_process->exitStatus() == 0 )
224 void PosterPreview::setPosterSize(
int s )
229 void PosterPreview::setPosterSize(
const TQString& s )
231 if ( m_postersize != s )
233 m_selectedpages.clear();
240 void PosterPreview::setMediaSize(
int s )
245 void PosterPreview::setMediaSize(
const TQString& s )
247 if ( m_mediasize != s )
249 m_selectedpages.clear();
256 void PosterPreview::setCutMargin(
int value )
262 void PosterPreview::setSelectedPages(
const TQString& s )
264 TQStringList l = TQStringList::split(
",", s,
false );
265 m_selectedpages.clear();
266 for ( TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it )
269 if ( ( p = ( *it ).find(
'-' ) ) == -1 )
270 m_selectedpages.append( ( *it ).toInt() );
273 int p1 = ( *it ).left( p ).toInt(), p2 = ( *it ).mid( p+1 ).toInt();
274 for (
int i=p1; i<=p2; i++ )
275 m_selectedpages.append( i );
281 void PosterPreview::emitSelectedPages()
284 if ( m_selectedpages.count() > 0 )
286 for ( TQValueList<int>::ConstIterator it=m_selectedpages.begin(); it!=m_selectedpages.end(); ++it )
287 s.append( TQString::number( *it ) +
"," );
288 s.truncate( s.length()-1 );
290 emit selectionChanged( s );
293 #include "posterpreview.moc"
PageSize
Defines the paper size to use.