aboutsummaryrefslogtreecommitdiff
path: root/src/aobutton.cpp
blob: 07ea649a35e7900cb39af2eda5a8d51a562ea357 (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
#include "aobutton.h"

#include "options.h"

AOButton::AOButton(AOApplication *ao_app, QWidget *parent)
    : QPushButton(parent)
    , ao_app(ao_app)
{
  m_movie = new QMovie(this);

  connect(m_movie, &QMovie::frameChanged, this, [this] {
    this->setIcon(m_movie->currentPixmap().scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
    this->setIconSize(QSize(this->width(), this->height()));
  });
}

AOButton::~AOButton()
{
  deleteMovie();
}

void AOButton::setImage(QString image_name)
{
  deleteMovie();

  QString file_path = ao_app->get_image(image_name, Options::getInstance().theme(), Options::getInstance().subTheme(), ao_app->default_theme, QString(), QString(), QString(), !Options::getInstance().animatedThemeEnabled());
  if (file_path.isEmpty())
  {
    setStyleSheet(QString());
    setIcon(QIcon());
  }
  else
  {
    setText(QString());
    setStyleSheet("QPushButton { background-color: transparent; border: 0px }");

    if (Options::getInstance().animatedThemeEnabled())
    {
      m_movie = new QMovie;
      m_movie->setFileName(file_path);

      connect(m_movie, &QMovie::frameChanged, this, &AOButton::handleNextFrame);

      m_movie->start();
    }
    else
    {
      updateIcon(QPixmap(file_path));
    }
  }
}

void AOButton::deleteMovie()
{
  if (m_movie)
  {
    disconnect(m_movie, &QMovie::frameChanged, this, &AOButton::handleNextFrame);

    m_movie->stop();
    m_movie->deleteLater();
    m_movie = nullptr;
  }
}

void AOButton::handleNextFrame()
{
  updateIcon(m_movie->currentPixmap());
}

void AOButton::updateIcon(QPixmap icon)
{
  const QSize current_size = size();
  setIcon(icon.scaled(current_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  setIconSize(current_size);
}