File: dismag.cpp

package info (click to toggle)
munipack 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,104 kB
  • sloc: cpp: 29,677; sh: 4,909; f90: 2,872; makefile: 278; python: 140; xml: 72; awk: 12
file content (132 lines) | stat: -rw-r--r-- 3,676 bytes parent folder | download | duplicates (2)
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
/*

  xmunipack - magnifier

  Copyright © 2019-21 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Munipack 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.

  * There're unsolved mystery in sizer, layer algorithm which
    prevent setup of an initial size of this window to value
    of previously adjusted by user.

*/

#include "dismag.h"
#include "fits.h"
#include <wx/wx.h>
#include <wx/graphics.h>

MuniMagnifier::MuniMagnifier(wxWindow *w, MuniConfig *config):
  wxWindow(w,wxID_ANY), scale(config->magnifier_scale), zoom(1.0), inside(false)
{
  SetBackgroundStyle(wxBG_STYLE_PAINT);
  bgcolour = wxColour(config->display_bgcolour);

  Bind(wxEVT_PAINT,&MuniMagnifier::OnPaint,this);
  Bind(wxEVT_UPDATE_UI,&MuniMagnifier::OnUpdate,this);
  Bind(EVT_SLEW,&MuniMagnifier::OnMouseMotion,this);
}

void MuniMagnifier::SetImage(const wxImage& i)
{
  wxASSERT(i.IsOk());
  //  wxLogDebug("MuniMagnifierWindow::SetImage %d %d %d",i.IsOk(),i.GetWidth(),i.GetHeight());

  image = i;
}


void MuniMagnifier::OnUpdate(wxUpdateUIEvent& event)
{
  /*
  wxLogDebug("MuniMagnifierWindow::OnUpdate %d %d %d %d",crosshair.x,crosshair.y,
	     int(image.IsOk()),int(canvas.IsOk()));
  */

  event.SetUpdateInterval(40); // = 1/25 s
  Refresh();
}

void MuniMagnifier::OnPaint(wxPaintEvent& event)
{
  wxSize size(GetSize());
  int w = size.GetWidth() / scale;
  int h = size.GetHeight() / scale;
  int i = size.GetWidth() / (2*scale);
  int j = size.GetHeight() / (2*scale);
  int x1 = crosshair.x - i;
  int y1 = crosshair.y - j;


  wxBrush brush(bgcolour);
  wxPen pen(bgcolour);

  wxPaintDC dc(this);
  wxGraphicsContext *gc = wxGraphicsContext::Create(dc);
  if( gc ) {

    gc->SetBrush(brush);
    gc->SetPen(pen);
    gc->DrawRectangle(0,0,size.GetWidth(),size.GetHeight());

    if( image.IsOk() && inside) {

      wxRect rsub(x1,y1,w,h);
      wxRect rimg(0,0,image.GetWidth(),image.GetHeight());
      wxRect r = rimg.Intersect(rsub);

      if( ! r.IsEmpty() ) {
	wxImage sub(image.GetSubImage(r));
	sub.Rescale(sub.GetWidth()*scale,sub.GetHeight()*scale,
		    wxIMAGE_QUALITY_NEAREST);

	// offset
	int xoff = x1 < 0 ? wxMax(size.GetWidth() - sub.GetWidth(),0) : 0;
	int yoff = y1 < 0 ? wxMax(size.GetHeight()-sub.GetHeight(),0) : 0;

	wxGraphicsBitmap bmp = gc->CreateBitmapFromImage(sub);
	gc->DrawBitmap(bmp,xoff,yoff,sub.GetWidth(),sub.GetHeight());
      }
    }

    int alpha = 200;
    int s = scale * (zoom > 1.0 ? int(zoom + 0.5) : 1);
    int ss = wxMax(int(scale * zoom),1);
    int i1 = size.GetWidth() / 2 - ss / 2;
    int j1 = size.GetHeight()/ 2 - ss / 2;
    gc->SetBrush(*wxTRANSPARENT_BRUSH);

    wxColour whitea(255,255,255,alpha);
    gc->SetPen(wxPen(whitea));
    gc->DrawRectangle(i1,j1,s,s);

    wxColour blacka(0,0,0,alpha);
    gc->SetPen(wxPen(blacka));
    gc->DrawRectangle(i1-1,j1-1,s+2,s+2);

    delete gc;
  }
}


void MuniMagnifier::OnMouseMotion(MuniSlewEvent& event)
{
  //  wxLogDebug("MuniMagnifierWindow::OnMouseMotion %d %d",event.xsub,event.ysub);
  inside = event.inside;
  crosshair = wxPoint(event.xsub,event.ysub);
  zoom = event.zoom;
}