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

kdeprint

  • kdeprint
posterpreview.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2001-2002 Michael Goffioul <kdeprint@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 <kprocess.h>
23 #include <tqpainter.h>
24 #include <tqsimplerichtext.h>
25 #include <tqtimer.h>
26 #include <tqpixmap.h>
27 #include <kprinter.h>
28 #include <klocale.h>
29 #include <kcursor.h>
30 #include <kglobalsettings.h>
31 
32 PosterPreview::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 
40 PosterPreview::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 
49 PosterPreview::~PosterPreview()
50 {
51  delete m_process;
52 }
53 
54 void PosterPreview::init()
55 {
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* ) ) );
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 
67 void 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 
81 void PosterPreview::setDirty()
82 {
83  if ( !m_dirty )
84  {
85  m_dirty = true;
86  TQTimer::singleShot( 1, this, TQT_SLOT( updatePoster() ) );
87  }
88 }
89 
90 void 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( KProcess::NotifyOnExit, KProcess::Stderr ) )
97  {
98  m_rows = m_cols = 0;
99  m_dirty = false;
100  update();
101  }
102 }
103 
104 void 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 "
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;
121  x = QMAX( x, 0 );
122  y = QMAX( y, 0 );
123  richtext.draw( p, x, y, TQRect( x, y, richtext.widthUsed(), richtext.height() ), colorGroup() );
124  m_boundingrect = TQRect();
125  }
126  else
127  {
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 ) );
135 
136  x = y = 0;
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 )
139  {
140  for ( int j=0; j<m_cols; j++, x+=m_pw )
141  {
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 );
151 
152  pw -= m_pw-2*m_mw-px;
153  px = 0;
154  }
155 
156  px = m_posterbb.x();
157  ph -= m_ph-2*m_mh-py;
158  py = 0;
159  pw = m_posterbb.width();
160  }
161  }
162  }
163 
164  delete p;
165  painter->drawPixmap( 0, 0, pix );
166 }
167 
168 void PosterPreview::mouseMoveEvent( TQMouseEvent *e )
169 {
170  if ( m_boundingrect.isValid() )
171  {
172  if ( m_boundingrect.contains( e->pos() ) )
173  setCursor( KCursor::handCursor() );
174  else
175  setCursor( KCursor::arrowCursor() );
176  }
177 }
178 
179 void PosterPreview::mousePressEvent( TQMouseEvent *e )
180 {
181  if ( e->button() == Qt::LeftButton && m_boundingrect.isValid() )
182  {
183  if ( m_boundingrect.contains( e->pos() ) )
184  {
185  int c, r;
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;
189 
190  if ( m_selectedpages.find( pagenum ) == m_selectedpages.end() ||
191  !( e->state() & TQt::ShiftButton ) )
192  {
193  if ( !( e->state() & TQt::ShiftButton ) )
194  m_selectedpages.clear();
195  m_selectedpages.append( pagenum );
196  update();
197  emitSelectedPages();
198  }
199  }
200  else if ( m_selectedpages.count() > 0 )
201  {
202  m_selectedpages.clear();
203  update();
204  emitSelectedPages();
205  }
206  }
207 }
208 
209 void PosterPreview::slotProcessStderr( KProcess*, char *buf, int len )
210 {
211  m_buffer.append( TQCString( buf, len ) );
212 }
213 
214 void PosterPreview::slotProcessExited( KProcess* )
215 {
216  if ( m_process->normalExit() && m_process->exitStatus() == 0 )
217  parseBuffer();
218  else
219  m_rows = m_cols = 0;
220 
221  m_dirty = false;
222  update();
223 }
224 
225 void PosterPreview::setPosterSize( int s )
226 {
227  setPosterSize( pageSizeToPageName( KPrinter::PageSize( s ) ) );
228 }
229 
230 void PosterPreview::setPosterSize( const TQString& s )
231 {
232  if ( m_postersize != s )
233  {
234  m_selectedpages.clear();
235  m_postersize = s;
236  setDirty();
237  emitSelectedPages();
238  }
239 }
240 
241 void PosterPreview::setMediaSize( int s )
242 {
243  setMediaSize( pageSizeToPageName( ( KPrinter::PageSize )s ) );
244 }
245 
246 void PosterPreview::setMediaSize( const TQString& s )
247 {
248  if ( m_mediasize != s )
249  {
250  m_selectedpages.clear();
251  m_mediasize = s;
252  setDirty();
253  emitSelectedPages();
254  }
255 }
256 
257 void PosterPreview::setCutMargin( int value )
258 {
259  m_cutmargin = value;
260  setDirty();
261 }
262 
263 void PosterPreview::setSelectedPages( const TQString& s )
264 {
265  TQStringList l = TQStringList::split( ",", s, false );
266  m_selectedpages.clear();
267  for ( TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it )
268  {
269  int p;
270  if ( ( p = ( *it ).find( '-' ) ) == -1 )
271  m_selectedpages.append( ( *it ).toInt() );
272  else
273  {
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 );
277  }
278  }
279  update();
280 }
281 
282 void PosterPreview::emitSelectedPages()
283 {
284  TQString s;
285  if ( m_selectedpages.count() > 0 )
286  {
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 );
290  }
291  emit selectionChanged( s );
292 }
293 
294 #include "posterpreview.moc"

kdeprint

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

kdeprint

Skip menu "kdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdeprint by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |