• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeprint
 

tdeprint

  • tdeprint
posterpreview.cpp
1/*
2 * This file is part of the KDE libraries
3 * Copyright (c) 2001-2002 Michael Goffioul <tdeprint@swing.be>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 **/
19
20#include "posterpreview.h"
21
22#include <tdeprocess.h>
23#include <tqpainter.h>
24#include <tqsimplerichtext.h>
25#include <tqtimer.h>
26#include <tqpixmap.h>
27#include <kprinter.h>
28#include <tdelocale.h>
29#include <kcursor.h>
30#include <tdeglobalsettings.h>
31
32PosterPreview::PosterPreview( TQWidget *parent, const char *name )
33 : TQFrame( parent, name )
34{
35 m_postersize = m_mediasize = "A4";
36 m_cutmargin = 5;
37 init();
38}
39
40PosterPreview::PosterPreview( const TQString& postersize, const TQString& mediasize, TQWidget *parent, const char *name )
41 : TQFrame( parent, name )
42{
43 m_postersize = postersize;
44 m_mediasize = mediasize;
45 m_cutmargin = 5;
46 init();
47}
48
49PosterPreview::~PosterPreview()
50{
51 delete m_process;
52}
53
54void PosterPreview::init()
55{
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* ) ) );
59
60 m_cols = m_rows = m_pw = m_ph = m_mw = m_mh = 0;
61 m_dirty = false;
62 setDirty();
63 setMouseTracking( true );
64 setBackgroundMode( TQt::NoBackground );
65}
66
67void PosterPreview::parseBuffer()
68{
69 int rotate;
70 float pw, ph, mw, mh;
71 float x1, x2, y1, y2;
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 );
79}
80
81void PosterPreview::setDirty()
82{
83 if ( !m_dirty )
84 {
85 m_dirty = true;
86 TQTimer::singleShot( 1, this, TQ_SLOT( updatePoster() ) );
87 }
88}
89
90void PosterPreview::updatePoster()
91{
92 m_buffer = "";
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 ) )
97 {
98 m_rows = m_cols = 0;
99 m_dirty = false;
100 update();
101 }
102}
103
104void PosterPreview::drawContents( TQPainter *painter )
105{
106 TQPixmap pix( width(), height() );
107 TQPainter *p = new TQPainter( &pix );
108
109 p->fillRect( 0, 0, width(), height(), colorGroup().background() );
110
111 if ( isEnabled() )
112 {
113 if ( m_rows <= 0 || m_cols <= 0 || m_pw <= 0 || m_ph <= 0 )
114 {
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;
120 x = TQMAX( x, 0 );
121 y = TQMAX( y, 0 );
122 richtext.draw( p, x, y, TQRect( x, y, richtext.widthUsed(), richtext.height() ), colorGroup() );
123 m_boundingrect = TQRect();
124 }
125 else
126 {
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 ) );
134
135 x = y = 0;
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 )
138 {
139 for ( int j=0; j<m_cols; j++, x+=m_pw )
140 {
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 );
150
151 pw -= m_pw-2*m_mw-px;
152 px = 0;
153 }
154
155 px = m_posterbb.x();
156 ph -= m_ph-2*m_mh-py;
157 py = 0;
158 pw = m_posterbb.width();
159 }
160 }
161 }
162
163 delete p;
164 painter->drawPixmap( 0, 0, pix );
165}
166
167void PosterPreview::mouseMoveEvent( TQMouseEvent *e )
168{
169 if ( m_boundingrect.isValid() )
170 {
171 if ( m_boundingrect.contains( e->pos() ) )
172 setCursor( KCursor::handCursor() );
173 else
174 setCursor( KCursor::arrowCursor() );
175 }
176}
177
178void PosterPreview::mousePressEvent( TQMouseEvent *e )
179{
180 if ( e->button() == TQt::LeftButton && m_boundingrect.isValid() )
181 {
182 if ( m_boundingrect.contains( e->pos() ) )
183 {
184 int c, r;
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;
188
189 if ( m_selectedpages.find( pagenum ) == m_selectedpages.end() ||
190 !( e->state() & TQt::ShiftButton ) )
191 {
192 if ( !( e->state() & TQt::ShiftButton ) )
193 m_selectedpages.clear();
194 m_selectedpages.append( pagenum );
195 update();
196 emitSelectedPages();
197 }
198 }
199 else if ( m_selectedpages.count() > 0 )
200 {
201 m_selectedpages.clear();
202 update();
203 emitSelectedPages();
204 }
205 }
206}
207
208void PosterPreview::slotProcessStderr( TDEProcess*, char *buf, int len )
209{
210 m_buffer.append( TQCString( buf, len ) );
211}
212
213void PosterPreview::slotProcessExited( TDEProcess* )
214{
215 if ( m_process->normalExit() && m_process->exitStatus() == 0 )
216 parseBuffer();
217 else
218 m_rows = m_cols = 0;
219
220 m_dirty = false;
221 update();
222}
223
224void PosterPreview::setPosterSize( int s )
225{
226 setPosterSize( pageSizeToPageName( KPrinter::PageSize( s ) ) );
227}
228
229void PosterPreview::setPosterSize( const TQString& s )
230{
231 if ( m_postersize != s )
232 {
233 m_selectedpages.clear();
234 m_postersize = s;
235 setDirty();
236 emitSelectedPages();
237 }
238}
239
240void PosterPreview::setMediaSize( int s )
241{
242 setMediaSize( pageSizeToPageName( ( KPrinter::PageSize )s ) );
243}
244
245void PosterPreview::setMediaSize( const TQString& s )
246{
247 if ( m_mediasize != s )
248 {
249 m_selectedpages.clear();
250 m_mediasize = s;
251 setDirty();
252 emitSelectedPages();
253 }
254}
255
256void PosterPreview::setCutMargin( int value )
257{
258 m_cutmargin = value;
259 setDirty();
260}
261
262void PosterPreview::setSelectedPages( const TQString& s )
263{
264 TQStringList l = TQStringList::split( ",", s, false );
265 m_selectedpages.clear();
266 for ( TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it )
267 {
268 int p;
269 if ( ( p = ( *it ).find( '-' ) ) == -1 )
270 m_selectedpages.append( ( *it ).toInt() );
271 else
272 {
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 );
276 }
277 }
278 update();
279}
280
281void PosterPreview::emitSelectedPages()
282{
283 TQString s;
284 if ( m_selectedpages.count() > 0 )
285 {
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 );
289 }
290 emit selectionChanged( s );
291}
292
293#include "posterpreview.moc"
KPrinter::PageSize
PageSize
Defines the paper size to use.
Definition: kprinter.h:168

tdeprint

Skip menu "tdeprint"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeprint

Skip menu "tdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.9.4
This website is maintained by Timothy Pearson.