You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
295 lines
7.9 KiB
295 lines
7.9 KiB
15 years ago
|
/*
|
||
|
* This file is part of the KDE libraries
|
||
13 years ago
|
* Copyright (c) 2001-2002 Michael Goffioul <tdeprint@swing.be>
|
||
15 years ago
|
*
|
||
|
* This library is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU Library General Public
|
||
|
* License version 2 as published by the Free Software Foundation.
|
||
|
*
|
||
|
* This library is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Library General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Library General Public License
|
||
|
* along with this library; see the file COPYING.LIB. If not, write to
|
||
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
**/
|
||
|
|
||
|
#include "posterpreview.h"
|
||
|
|
||
|
#include <kprocess.h>
|
||
15 years ago
|
#include <tqpainter.h>
|
||
|
#include <tqsimplerichtext.h>
|
||
|
#include <tqtimer.h>
|
||
|
#include <tqpixmap.h>
|
||
15 years ago
|
#include <kprinter.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kcursor.h>
|
||
|
#include <kglobalsettings.h>
|
||
|
|
||
15 years ago
|
PosterPreview::PosterPreview( TQWidget *parent, const char *name )
|
||
|
: TQFrame( parent, name )
|
||
15 years ago
|
{
|
||
|
m_postersize = m_mediasize = "A4";
|
||
|
m_cutmargin = 5;
|
||
|
init();
|
||
|
}
|
||
|
|
||
15 years ago
|
PosterPreview::PosterPreview( const TQString& postersize, const TQString& mediasize, TQWidget *parent, const char *name )
|
||
|
: TQFrame( parent, name )
|
||
15 years ago
|
{
|
||
|
m_postersize = postersize;
|
||
|
m_mediasize = mediasize;
|
||
|
m_cutmargin = 5;
|
||
|
init();
|
||
|
}
|
||
|
|
||
|
PosterPreview::~PosterPreview()
|
||
|
{
|
||
|
delete m_process;
|
||
|
}
|
||
|
|
||
|
void PosterPreview::init()
|
||
|
{
|
||
|
m_process = new KProcess;
|
||
15 years ago
|
connect( m_process, TQT_SIGNAL( receivedStderr( KProcess*, char*, int ) ), TQT_SLOT( slotProcessStderr( KProcess*, char*, int ) ) );
|
||
|
connect( m_process, TQT_SIGNAL( processExited( KProcess* ) ), TQT_SLOT( slotProcessExited( KProcess* ) ) );
|
||
15 years ago
|
|
||
|
m_cols = m_rows = m_pw = m_ph = m_mw = m_mh = 0;
|
||
|
m_dirty = false;
|
||
|
setDirty();
|
||
|
setMouseTracking( true );
|
||
14 years ago
|
setBackgroundMode( TQt::NoBackground );
|
||
15 years ago
|
}
|
||
|
|
||
|
void PosterPreview::parseBuffer()
|
||
|
{
|
||
|
int rotate;
|
||
|
float pw, ph, mw, mh;
|
||
|
float x1, x2, y1, y2;
|
||
|
sscanf( m_buffer.ascii(), "%d %d %d %g %g %g %g %g %g %g %g", &m_rows, &m_cols, &rotate,
|
||
|
&pw, &ph, &mw, &mh, &x1, &y1, &x2, &y2 );
|
||
|
m_pw = ( int )( rotate ? ph : pw );
|
||
|
m_ph = ( int )( rotate ? pw : ph );
|
||
|
m_mw = ( int )( rotate ? mh : mw );
|
||
|
m_mh = ( int )( rotate ? mw : mh );
|
||
|
m_posterbb.setCoords( ( int )x1, ( int )y1, ( int )x2, ( int )y2 );
|
||
|
}
|
||
|
|
||
|
void PosterPreview::setDirty()
|
||
|
{
|
||
|
if ( !m_dirty )
|
||
|
{
|
||
|
m_dirty = true;
|
||
15 years ago
|
TQTimer::singleShot( 1, this, TQT_SLOT( updatePoster() ) );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void PosterPreview::updatePoster()
|
||
|
{
|
||
|
m_buffer = "";
|
||
|
m_process->clearArguments();
|
||
|
*m_process << "poster" << "-F" << "-m" + m_mediasize << "-p" + m_postersize
|
||
15 years ago
|
<< "-c" + TQString::number( m_cutmargin ) + "%";
|
||
15 years ago
|
if ( !m_process->start( KProcess::NotifyOnExit, KProcess::Stderr ) )
|
||
|
{
|
||
|
m_rows = m_cols = 0;
|
||
|
m_dirty = false;
|
||
|
update();
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void PosterPreview::drawContents( TQPainter *painter )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQPixmap pix( width(), height() );
|
||
|
TQPainter *p = new TQPainter( &pix );
|
||
15 years ago
|
|
||
14 years ago
|
p->fillRect( 0, 0, width(), height(), tqcolorGroup().background() );
|
||
15 years ago
|
|
||
|
if ( isEnabled() )
|
||
|
{
|
||
|
if ( m_rows <= 0 || m_cols <= 0 || m_pw <= 0 || m_ph <= 0 )
|
||
|
{
|
||
15 years ago
|
TQString txt = i18n( "Poster preview not available. Either the <b>poster</b> "
|
||
15 years ago
|
"executable is not properly installed, or you don't have "
|
||
|
"the required version; available at http://printing.kde.org/downloads/." );
|
||
15 years ago
|
TQSimpleRichText richtext( ( m_buffer.isEmpty() ? txt : m_buffer.prepend( "<pre>" ).append( "</pre>" ) ), p->font() );
|
||
15 years ago
|
richtext.adjustSize();
|
||
|
int x = ( width()-richtext.widthUsed() )/2, y = ( height()-richtext.height() )/2;
|
||
|
x = QMAX( x, 0 );
|
||
|
y = QMAX( y, 0 );
|
||
14 years ago
|
richtext.draw( p, x, y, TQRect( x, y, richtext.widthUsed(), richtext.height() ), tqcolorGroup() );
|
||
15 years ago
|
m_boundingrect = TQRect();
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
int totalx = m_cols*m_pw, totaly = m_rows*m_ph;
|
||
|
float scale = QMIN( float( width()-1 )/totalx, float( height()-1 )/totaly );
|
||
|
p->translate( 0, height()-1 );
|
||
|
p->scale( scale, -scale );
|
||
|
int x = ( int )( width()/scale-totalx )/2, y = ( int )( height()/scale-totaly )/2;
|
||
|
p->translate( x, y );
|
||
15 years ago
|
m_boundingrect = p->xForm( TQRect( 0, 0, totalx, totaly ) );
|
||
15 years ago
|
|
||
|
x = y = 0;
|
||
|
int px = m_posterbb.x(), py = m_posterbb.y(), pw = m_posterbb.width(), ph = m_posterbb.height();
|
||
|
for ( int i=0; i<m_rows; i++, y+=m_ph, x=0 )
|
||
|
{
|
||
|
for ( int j=0; j<m_cols; j++, x+=m_pw )
|
||
|
{
|
||
13 years ago
|
bool selected = ( m_selectedpages.find( i*m_cols+j+1 ) != m_selectedpages.end() );
|
||
15 years ago
|
p->fillRect( x+1, y+1, m_pw-2, m_ph-2, ( selected ? KGlobalSettings::highlightColor() : white ) );
|
||
|
p->drawRect( x, y, m_pw, m_ph );
|
||
|
if ( pw > 0 && ph > 0 )
|
||
|
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 ),
|
||
14 years ago
|
( selected ? TQColor(KGlobalSettings::highlightColor().dark( 160 )) : lightGray ) );
|
||
15 years ago
|
p->setPen( Qt::DotLine );
|
||
|
p->drawRect( x+m_mw, y+m_mh, m_pw-2*m_mw, m_ph-2*m_mh );
|
||
|
p->setPen( Qt::SolidLine );
|
||
|
|
||
|
pw -= m_pw-2*m_mw-px;
|
||
|
px = 0;
|
||
|
}
|
||
|
|
||
|
px = m_posterbb.x();
|
||
|
ph -= m_ph-2*m_mh-py;
|
||
|
py = 0;
|
||
|
pw = m_posterbb.width();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
delete p;
|
||
|
painter->drawPixmap( 0, 0, pix );
|
||
|
}
|
||
|
|
||
15 years ago
|
void PosterPreview::mouseMoveEvent( TQMouseEvent *e )
|
||
15 years ago
|
{
|
||
|
if ( m_boundingrect.isValid() )
|
||
|
{
|
||
14 years ago
|
if ( m_boundingrect.contains( e->pos() ) )
|
||
15 years ago
|
setCursor( KCursor::handCursor() );
|
||
|
else
|
||
|
setCursor( KCursor::arrowCursor() );
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void PosterPreview::mousePressEvent( TQMouseEvent *e )
|
||
15 years ago
|
{
|
||
|
if ( e->button() == Qt::LeftButton && m_boundingrect.isValid() )
|
||
|
{
|
||
14 years ago
|
if ( m_boundingrect.contains( e->pos() ) )
|
||
15 years ago
|
{
|
||
|
int c, r;
|
||
|
c = ( e->pos().x()-m_boundingrect.x() )/( m_boundingrect.width()/m_cols ) + 1;
|
||
|
r = m_rows - ( e->pos().y()-m_boundingrect.y() )/( m_boundingrect.height()/m_rows );
|
||
|
int pagenum = ( r-1 )*m_cols+c;
|
||
|
|
||
13 years ago
|
if ( m_selectedpages.find( pagenum ) == m_selectedpages.end() ||
|
||
14 years ago
|
!( e->state() & TQt::ShiftButton ) )
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( !( e->state() & TQt::ShiftButton ) )
|
||
15 years ago
|
m_selectedpages.clear();
|
||
|
m_selectedpages.append( pagenum );
|
||
|
update();
|
||
|
emitSelectedPages();
|
||
|
}
|
||
|
}
|
||
|
else if ( m_selectedpages.count() > 0 )
|
||
|
{
|
||
|
m_selectedpages.clear();
|
||
|
update();
|
||
|
emitSelectedPages();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PosterPreview::slotProcessStderr( KProcess*, char *buf, int len )
|
||
|
{
|
||
15 years ago
|
m_buffer.append( TQCString( buf, len ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
void PosterPreview::slotProcessExited( KProcess* )
|
||
|
{
|
||
|
if ( m_process->normalExit() && m_process->exitStatus() == 0 )
|
||
|
parseBuffer();
|
||
|
else
|
||
|
m_rows = m_cols = 0;
|
||
|
|
||
|
m_dirty = false;
|
||
|
update();
|
||
|
}
|
||
|
|
||
|
void PosterPreview::setPosterSize( int s )
|
||
|
{
|
||
|
setPosterSize( pageSizeToPageName( KPrinter::PageSize( s ) ) );
|
||
|
}
|
||
|
|
||
15 years ago
|
void PosterPreview::setPosterSize( const TQString& s )
|
||
15 years ago
|
{
|
||
|
if ( m_postersize != s )
|
||
|
{
|
||
|
m_selectedpages.clear();
|
||
|
m_postersize = s;
|
||
|
setDirty();
|
||
|
emitSelectedPages();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PosterPreview::setMediaSize( int s )
|
||
|
{
|
||
|
setMediaSize( pageSizeToPageName( ( KPrinter::PageSize )s ) );
|
||
|
}
|
||
|
|
||
15 years ago
|
void PosterPreview::setMediaSize( const TQString& s )
|
||
15 years ago
|
{
|
||
|
if ( m_mediasize != s )
|
||
|
{
|
||
|
m_selectedpages.clear();
|
||
|
m_mediasize = s;
|
||
|
setDirty();
|
||
|
emitSelectedPages();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PosterPreview::setCutMargin( int value )
|
||
|
{
|
||
|
m_cutmargin = value;
|
||
|
setDirty();
|
||
|
}
|
||
|
|
||
15 years ago
|
void PosterPreview::setSelectedPages( const TQString& s )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQStringList l = TQStringList::split( ",", s, false );
|
||
15 years ago
|
m_selectedpages.clear();
|
||
15 years ago
|
for ( TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it )
|
||
15 years ago
|
{
|
||
|
int p;
|
||
13 years ago
|
if ( ( p = ( *it ).find( '-' ) ) == -1 )
|
||
15 years ago
|
m_selectedpages.append( ( *it ).toInt() );
|
||
|
else
|
||
|
{
|
||
|
int p1 = ( *it ).left( p ).toInt(), p2 = ( *it ).mid( p+1 ).toInt();
|
||
|
for ( int i=p1; i<=p2; i++ )
|
||
|
m_selectedpages.append( i );
|
||
|
}
|
||
|
}
|
||
|
update();
|
||
|
}
|
||
|
|
||
|
void PosterPreview::emitSelectedPages()
|
||
|
{
|
||
15 years ago
|
TQString s;
|
||
15 years ago
|
if ( m_selectedpages.count() > 0 )
|
||
|
{
|
||
15 years ago
|
for ( TQValueList<int>::ConstIterator it=m_selectedpages.begin(); it!=m_selectedpages.end(); ++it )
|
||
|
s.append( TQString::number( *it ) + "," );
|
||
15 years ago
|
s.truncate( s.length()-1 );
|
||
|
}
|
||
|
emit selectionChanged( s );
|
||
|
}
|
||
|
|
||
|
#include "posterpreview.moc"
|