File: viewport.cpp

package info (click to toggle)
kdegraphics 2%3A980312-3
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 6,168 kB
  • ctags: 5,778
  • sloc: ansic: 26,607; cpp: 21,205; sh: 5,224; makefile: 1,651; perl: 108
file content (353 lines) | stat: -rw-r--r-- 7,124 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
////////////////
//
// viewport.cpp -- methods for ViewPort widget.
//
// Sirtaj Kang, Oct 1996.

// $Id: viewport.cpp,v 1.9 1998/03/09 20:26:02 kulow Exp $

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#include<qapp.h>
#include<qmsgbox.h>
#include<qcolor.h>

#include "viewport.h"
#include "viewport.moc"
#include "confighandler.h"

WViewPort::WViewPort(const char *file, QWidget *parent, 
		const char *name, WFlags f)
	: QLabel(parent, name, f)
{
	oldContext=0;
	registerFormats();

	setAutoResize(TRUE);
	
	QPopupMenu *ImageZoom = new QPopupMenu;
	ImageZoom->insertItem("+50%", this,
			      SLOT(doScalePlus()));
	ImageZoom->insertItem("+10%", this,
			      SLOT(doScalePlusSmall()));
	ImageZoom->insertItem("-10%", this,
			      SLOT(doScaleMinusSmall()));
	ImageZoom->insertItem("-50%", this,
			      SLOT(doScaleMinus()));

	QPopupMenu *ImageRotate = new QPopupMenu;
	ImageRotate->insertItem("Clockwise",this,
				SLOT(rotateClockwise()));
	ImageRotate->insertItem("Anti-clock", this, 
				SLOT(rotateAntiClockwise()));
	ImageRotate->insertItem("Mirror X", this, 
				SLOT(mirrorX()));
	ImageRotate->insertItem("Mirror Y", this, 
				SLOT(mirrorY()));
	
	QPopupMenu *ImageRoot = new QPopupMenu;
	ImageRoot->insertItem("Ti&le", this, SLOT(tileToDesktop()));
	ImageRoot->insertItem("Max Size", this, SLOT(maxToDesktop()));
	ImageRoot->insertItem("Maxpect", this, SLOT(maxpectToDesktop()));
	
	lb_popup = new QPopupMenu;
	lb_popup->insertItem("Zoom", ImageZoom);
	lb_popup->insertItem("Rotate",ImageRotate);
	lb_popup->insertItem("To Desktop",ImageRoot);
	lb_popup->insertItem("Fit window size to pixmap size", this,
			     SLOT(fitWindowToPixmap()));
	lb_popup->insertItem("Fit pixmap size to window size", this,
			     SLOT(fitPixmapToWindow()));

	QPixmapCache::setCacheLimit( KVConfigHandler::cacheSize );

	image = new QPixmap();
	imagefile="";
	
	load(file);
}

WViewPort::~WViewPort()
{
      if(oldContext)
            QColor::destroyAllocContext( oldContext );   
      delete image;
}

bool WViewPort::load(const char *filename)
{
	bool ret=0;
	QString save=imagefile;
	QPixmap *copyimage;

	if(filename){
	
		// Save image file name	

		imagefile= filename;

		// Try internal types, fail if not found.
		QApplication::setOverrideCursor(waitCursor);

		hide();
		
		if(oldContext)
			QColor::destroyAllocContext( oldContext );

		oldContext = QColor::enterAllocContext();
		
		copyimage = 0L;

		//query if the image "filename" is in cache 
		copyimage = QPixmapCache::find(filename);
		
		if (copyimage==0L)
		  { //image not cached 
		    
		    ret=image->load(filename);
		    setPixmap(*image);
		    if (ret==TRUE)
		      {
			// picture load was succesful.
			// let's put it in cache.

			// make a duplicate of image, because 
			// QPixmapCache doesn't allocate memory,
			// and put the duplicate in the cache.

		        // Stephan: I disabled the caching for now, 
			// because it has a big, fat color problem
			// under 256 color displays. The color context
			// is not saved. So, you always get the colors
			// or the latest loaded pixmap.
			// QPixmapCache::insert(filename,new QPixmap(*image));

			// Rem. On cache removal, the allocated 
			// memory is freed by QPixmapCache
		      }
		  }
		else
		  { //image cached 

		    *image = *copyimage; //no pionter copy, copy contructor!
		    setPixmap(*image);
		    ret = TRUE;
		  }

		QColor::leaveAllocContext();
		show();
		QApplication::restoreOverrideCursor();


		matrix.reset();

		fitToPixmap();	
	} else
		ret = 1;

	if(!ret)
		imagefile = save;

	return ret;
}


/*
bool WViewPort::load(const char *filename)
{
	bool ret=0;
	QString save=imagefile;

	if(filename){
	
		// Save image file name	

		imagefile= filename;

		// Try internal types, fail if not found.
		QApplication::setOverrideCursor(waitCursor);

		hide();
		
		if(oldContext)
			QColor::destroyAllocContext( oldContext );

		oldContext = QColor::enterAllocContext();

		ret=image->load(filename);
		setPixmap(*image);

		QColor::leaveAllocContext();
		show();
		QApplication::restoreOverrideCursor();


		matrix.reset();

		fitToPixmap();	
	} else
		ret = 1;

	if(!ret)
		imagefile = save;

	return ret;
}
*/
void WViewPort::mousePressEvent(QMouseEvent *e)
{
  static QPoint tmp_point;
  if(e->button() == RightButton) 
    {
      tmp_point = QCursor::pos();
      if(lb_popup)
	lb_popup->popup(tmp_point);
    }
  emit clicked();
}


void WViewPort::scale(float x, float y)
{
	QApplication::setOverrideCursor(waitCursor);

	matrix.scale(x,y);
	setPixmap(image->xForm(matrix));

	QApplication::restoreOverrideCursor();
	
	fitToPixmap();
}


void WViewPort::doScalePlus()
{
  QWMatrix S(1.5F, 0.0F, 0.0F, 1.5F, 0.0F, 0.0F);
  turnPixmap(S);
}

void WViewPort::doScalePlusSmall()
{
  QWMatrix S(1.1F, 0.0F, 0.0F, 1.1F, 0.0F, 0.0F);
  turnPixmap(S);
}

void WViewPort::doScaleMinusSmall()
{
  QWMatrix S(0.9090909090909F, 0.0F, 0.0F, 0.9090909090909F, 0.0F, 0.0F);
  turnPixmap(S);
}

void WViewPort::doScaleMinus()
{
  QWMatrix S(0.66666666666F, 0.0F, 0.0F, 0.66666666666F, 0.0F, 0.0F);
  turnPixmap(S);
}


void WViewPort::mirrorX()
{
  QWMatrix S(-1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F);
  turnPixmap(S);
}

void WViewPort::mirrorY()
{
  QWMatrix S(1.0F, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F);
  turnPixmap(S);
}

void WViewPort::rotateClockwise()
{
  QWMatrix S(0.0F, 1.0F, -1.0F, 0.0F, 0.0F, 0.0F);
  turnPixmap(S);
}

void WViewPort::rotateAntiClockwise()
{
  QWMatrix S(0.0F, -1.0F, 1.0F, 0.0F, 0.0F, 0.0F);
  turnPixmap(S);
}

void WViewPort::turnPixmap( QWMatrix S)
{
  matrix = matrix * S;

  QApplication::setOverrideCursor(waitCursor);
  setPixmap(image->xForm(matrix));
  QApplication::restoreOverrideCursor();
  fitToPixmap();

  emit doResize();
}

void WViewPort::fitToPixmap()
{
	resize( pixmap()->width(), pixmap()->height() );
	emit resized();
}


void WViewPort::tileToDesktop()
{
  qApp->desktop()->setBackgroundPixmap( *this->pixmap());
}

void WViewPort::maxToDesktop()
{ 
  QWMatrix mat;
  QPixmap image(*this->pixmap());
  QWidget *deskWidget = qApp->desktop();
  float swid, shei;
  
  shei= (float)deskWidget->height()/(float)image.height();
  swid= (float)deskWidget->width()/(float)image.width();
  
  mat.scale(swid, shei);
  
  deskWidget->setBackgroundPixmap(image.xForm(mat));
  
  repaint();
  
}

void WViewPort::maxpectToDesktop()
{
  
  float sc;
  QWidget *deskWidget = qApp->desktop();
  QPixmap *currPix = pixmap();

  float S = (float)deskWidget->height()/(float)deskWidget->width(),
    I=(float)currPix->height()/(float)currPix->width();

  if (S < I)
    sc= (float)deskWidget->height()/(float)currPix->height();
  else
    sc= (float)deskWidget->width()/(float)currPix->width();
  
  QWMatrix mat;
  QPixmap image(*this->pixmap());
  
  mat.scale(sc,sc);
  
  qApp->desktop()->setBackgroundPixmap(image.xForm(mat));
  
  repaint();
}

void WViewPort::fitWindowToPixmap()
{
  emit doResize();
}

void WViewPort::fitPixmapToWindow()
{
  float sx, sy;
  sx = (float) parwidth / (float) (width());
  sy = (float) parheight / (float) (height());
  QWMatrix S(sx, 0.0F, 0.0F, sy, 0.0F, 0.0F);
  turnPixmap(S);
}