Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • app
  • src
buttonappearancewatcher.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17#include "buttonappearancewatcher.h"
18
19#include <QAbstractButton>
20#include <QEvent>
21
22#include "platformhandler.h"
23
24ButtonAppearanceWatcher::ButtonAppearanceWatcher(IconResource normalIconResource,
25 IconResource hoverIconResource,
26 QObject* parent) :
27 QObject(parent),
28 mNormalIconResource(normalIconResource),
29 mHoverIconResource(hoverIconResource)
30{}
31
32bool ButtonAppearanceWatcher::eventFilter(QObject* watched, QEvent* event)
33{
34 QAbstractButton* button = qobject_cast<QAbstractButton*>(watched);
35 if (!button) {
36 return false;
37 }
38
39 IconResource res = mNormalIconResource;
40 AppearanceEventType apType = determineAppearanceEvent(event);
41
42 if (shouldUpdateResource(event, apType)) {
43 if (event->type() == QEvent::ApplicationPaletteChange) {
44 res = mNormalIconResource;
45 }
46 else if (event->type() == QEvent::Enter) {
47 res = mHoverIconResource;
48 }
49 else if (event->type() == QEvent::Leave) {
50 res = mNormalIconResource;
51 }
52 mOldAppearanceType = apType;
53
54 bool isDarkmode = PlatformHandler::isDarkMode();
55 button->setIcon(res.iconForMode(isDarkmode));
56 return true;
57 }
58
59 return false;
60}
61
62AppearanceEventType ButtonAppearanceWatcher::determineAppearanceEvent(QEvent *event) const
63{
64 if (event->type() == QEvent::ApplicationPaletteChange) {
65 bool isDarkmode = PlatformHandler::isDarkMode();
66 if (isDarkmode) {
67 return AppearanceEventType::DARK_APPEARANCE;
68 } else {
69 return AppearanceEventType::LIGHT_APPEARANCE;
70 }
71 } else if (event->type() == QEvent::Enter) {
72 return AppearanceEventType::ICON_ACTIVE;
73 } else if (event->type() == QEvent::Leave) {
74 return AppearanceEventType::ICON_NORMAL;
75 }
76
77 return AppearanceEventType::NONE;
78}
79
80bool ButtonAppearanceWatcher::shouldUpdateResource(QEvent* event, AppearanceEventType appearanceType) const
81{
82 if (appearanceType == mOldAppearanceType) { return false; }
83
84 return determineAppearanceEvent(event) != AppearanceEventType::NONE;
85}
QAbstractButton
QAbstractButton::setIcon
void setIcon(const QIcon &icon)
QEvent
QEvent::ApplicationPaletteChange
ApplicationPaletteChange
QObject
QObject::event
virtual bool event(QEvent *e)
IconResource
Definition: appearance.h:24
Generated on Thu Jun 5 2025 14:06:43 for Pencil2D by doxygen 1.9.6 based on revision 4c63407997b2c03e5048716586dec6fbbb755173