-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPreviewView.cpp
153 lines (122 loc) · 3.08 KB
/
PreviewView.cpp
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
/*
* Copyright 2013-2023, Stefano Ceccherini <[email protected]>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "PreviewView.h"
#include <Bitmap.h>
#include <Catalog.h>
#include <GroupLayout.h>
#include <LayoutBuilder.h>
#include <Screen.h>
#include <String.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "PreviewView"
class BitmapView : public BView {
public:
BitmapView();
virtual ~BitmapView();
virtual void Draw(BRect rect);
};
PreviewView::PreviewView()
:
BView("rect_view", B_WILL_DRAW),
fBitmapView(NULL),
fCoordRect(BScreen().Frame()),
fTimeStamp(0),
fChanged(true),
fLeftTop(NULL),
fRightBottom(NULL)
{
BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
.Add(fBitmapView = new BitmapView());
}
void
PreviewView::AttachedToWindow()
{
BView::AttachedToWindow();
SetViewColor(Parent()->ViewColor());
Update();
}
void
PreviewView::_SetRect(const BRect& rect)
{
fCoordRect = rect;
BString text;
text << B_TRANSLATE("Capture area:\n");
text << B_TRANSLATE("left:") << " " << (int)rect.left << ", ";
text << B_TRANSLATE("top:") << " " << (int)rect.top << ", ";
text << B_TRANSLATE("right:") << " " << (int)rect.right << ", ";
text << B_TRANSLATE("bottom:") << " " << (int)rect.bottom;
SetToolTip(text.String());
}
static float
BRectRatio(const BRect& rect)
{
return rect.Width() / rect.Height();
}
static float
BRectHorizontalOverlap(const BRect& original, const BRect& resized)
{
return ((original.Height() / resized.Height() * resized.Width()) - original.Width()) / 2;
}
static float
BRectVerticalOverlap(const BRect& original, const BRect& resized)
{
return ((original.Width() / resized.Width() * resized.Height()) - original.Height()) / 2;
}
void
PreviewView::Update(const BRect* rect, BBitmap* bitmap)
{
if (Window() == NULL || Window()->IsHidden())
return;
if (rect != NULL)
_SetRect(*rect);
bigtime_t now = system_time();
if (bitmap == NULL) {
// Avoid updating preview too often
if (fTimeStamp + 50000 >= now)
return;
BScreen screen(Window());
screen.GetBitmap(&bitmap, false, &fCoordRect);
}
if (bitmap != NULL) {
BRect destRect;
BRect bitmapBounds = bitmap->Bounds();
BRect viewBounds = fBitmapView->Bounds();
if (BRectRatio(viewBounds) >= BRectRatio(bitmapBounds)) {
float overlap = BRectHorizontalOverlap(viewBounds, bitmapBounds);
destRect.Set(-overlap, 0, viewBounds.Width() + overlap,
viewBounds.Height());
} else {
float overlap = BRectVerticalOverlap(viewBounds, bitmapBounds);
destRect.Set(0, -overlap, viewBounds.Width(), viewBounds.Height() + overlap);
}
fTimeStamp = now;
fBitmapView->SetViewBitmap(bitmap,
bitmap->Bounds().OffsetToCopy(B_ORIGIN),
destRect,
B_FOLLOW_TOP|B_FOLLOW_LEFT, B_FILTER_BITMAP_BILINEAR);
Invalidate();
}
}
BRect
PreviewView::Rect() const
{
return fCoordRect;
}
// BitmapView
BitmapView::BitmapView()
:
BView("bitmap_view", B_WILL_DRAW|B_FULL_UPDATE_ON_RESIZE)
{
}
BitmapView::~BitmapView()
{
}
void
BitmapView::Draw(BRect rect)
{
BView::Draw(rect);
SetHighColor(0, 0, 0);
StrokeRect(Bounds());
}