summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/GraphicWin.cpp
blob: df069c4cdced55d845e18208db2b70f80161f2dd (plain)
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

#include "GraphicWin.h"

GraphicWin::GraphicWin( QWidget *parent, const char *name, WFlags f)
  : QWidget(parent, name, f), m_isFitted(false), m_isRotated(false)
{
  QVBoxLayout* grid = new QVBoxLayout(this);
  m_scroll = new GraphicScroll(this);
  grid->addWidget(m_scroll,1);
  QHBoxLayout* b = new QHBoxLayout(grid);

  QPushButton* fitButton = new QPushButton("Rotated", this);
  connect(fitButton, SIGNAL( clicked() ), this, SLOT( slotRotate() ) );
  b->addWidget(fitButton);

  fitButton = new QPushButton("Fitted", this);
  connect(fitButton, SIGNAL( clicked() ), this, SLOT( slotFit() ) );
  b->addWidget(fitButton);

  fitButton = new QPushButton("Close", this);
  connect(fitButton, SIGNAL( clicked() ), this, SLOT( slotClosed() ) );
  b->addWidget(fitButton);
}

void GraphicWin::setImage(QImage& im)
{
  m_isRotated = m_isFitted = false;
  m_im = im;
  resetpm();
}

QImage GraphicWin::resizeimage(int w, int h)
{
  if (w*m_im.height() < h*m_im.width())
    {
      h = (m_im.height()*w + m_im.width()/2)/m_im.width();
    }
  else
    {
      w = (m_im.width()*h + m_im.height()/2)/m_im.height();
    }
  return m_im.smoothScale(w,h);
}

void GraphicWin::slotFit()
{
  m_isFitted = !m_isFitted;
  resetpm();
}

void GraphicWin::slotRotate()
{
  m_isRotated = !m_isRotated;
  resetpm();
}

void GraphicWin::resetpm()
{
  QImage im;
  if (m_isFitted)
    {
      int w, h;
      if (m_isRotated)
	{
	  w = m_scroll->height()-5;
	  h = m_scroll->width()-5;
	}
      else
	{
	  w = m_scroll->width()-5;
	  h = m_scroll->height()-5;
	}
      im = resizeimage(w, h);
    }
  else
    {
      im = m_im;
    }
  QPixmap pc;
  if (m_isRotated)
    {
      QWMatrix m;
      m.rotate(90);
      QPixmap pc1;
      pc1.convertFromImage(im);
      pc = pc1.xForm(m);
    }
  else
    {
      pc.convertFromImage(im);
    }
  m_scroll->setPixmap(pc);
}

/*
 Something like this should work...but doesn't quite
 smoothscale is better anyway.

void GraphicWin::resetpm()
{
  QWMatrix m;
  if (m_isRotated)
    {
      m.rotate(90);
    }
  if (m_isFitted)
    {
      double sw = (width()-5.0)/(m_isRotated ? m_im.height() : m_im.width());
      double sh = (height()-5.0)/(m_isRotated ? m_im.width() : m_im.height());
      if (sw < sh)
	{
	  m.scale(sw, sw);
	}
      else
	{
	  m.scale(sh, sh);
	}
    }
  QPixmap pc = m_im.xForm(m);
  m_scroll->setPixmap(pc);
}
*/