File: UserImage.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (104 lines) | stat: -rw-r--r-- 2,611 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
/*  SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************

  Audacity: A Digital Audio Editor

  UserImage.cpp

  Dmitry Vedenko

**********************************************************************/
#include "UserImage.h"

#include <memory>

#include <wx/dcbuffer.h>
#include <wx/graphics.h>

namespace audacity::cloud::audiocom
{

UserImage::UserImage(wxWindow* parent, const wxSize& size)
    : wxPanelWrapper(parent, wxID_ANY, wxDefaultPosition, size, wxBORDER_NONE)
{
   SetBackgroundStyle(wxBG_STYLE_PAINT);

   SetMinSize(size);
   SetMaxSize(size);

   Bind(wxEVT_PAINT, [this](auto&) { OnPaint(); });
}

void UserImage::SetBitmap(const wxBitmap& bitmap)
{
   mBitmap = bitmap;
   Refresh(true);
}

void UserImage::SetBitmap(const wxString& path)
{
   wxImage image(path);

   if (image.IsOk())
   {
      mBitmap = wxBitmap(image);
      Refresh(true);
   }
}

void UserImage::OnPaint()
{
   wxAutoBufferedPaintDC dc(this);
   std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));

   gc->SetInterpolationQuality(wxINTERPOLATION_BEST);

   const wxSize size = GetSize();

   gc->SetBrush(wxBrush(GetBackgroundColour()));
   gc->DrawRectangle(0, 0, size.x, size.y);

   if (!mBitmap.IsOk())
   {
      gc->SetBrush(wxBrush(wxColour(255, 255, 255, 255)));
      gc->DrawEllipse(0, 0, size.x, size.y);
   }
   else
   {
      const wxSize resultingImageSize { std::min(size.x, mBitmap.GetWidth()),
                                        std::min(size.y, mBitmap.GetHeight()) };

      gc->DrawBitmap(
         mBitmap,
         (size.x - resultingImageSize.x) / 2,
         (size.y - resultingImageSize.y) / 2,
         resultingImageSize.x,
         resultingImageSize.y);

      auto path = gc->CreatePath();

      path.MoveToPoint(0, 0);
      path.AddLineToPoint(size.x / 2, 0);
      path.AddArc(size.x / 2, size.y / 2, size.x / 2, 3 * M_PI_2, M_PI, false);
      path.CloseSubpath();

      path.MoveToPoint(size.x, 0);
      path.AddLineToPoint(size.x, size.y / 2);
      path.AddArc(size.x / 2, size.y / 2, size.x / 2, 0, 3 * M_PI_2, false);
      path.CloseSubpath();

      path.MoveToPoint(size.x, size.y);
      path.AddLineToPoint(size.x / 2, size.y);
      path.AddArc(size.x / 2, size.y / 2, size.x / 2, M_PI_2, 0, false);
      path.CloseSubpath();

      path.MoveToPoint(0, size.y);
      path.AddLineToPoint(0, size.y / 2);
      path.AddArc(size.x / 2, size.y / 2, size.x / 2, M_PI, M_PI_2, false);
      path.CloseSubpath();

      gc->DrawPath(path);
   }
}

} // namespace audacity::cloud::audiocom