Debian Codegraph @

examples/animation-dci/main.cpp

raw
1 // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include <QApplication>
6 #include <QWidget>
7 #include <QImage>
8 #include <QPainter>
9 #include <QPainterPath>
10 #include <QLabel>
11 #include <QEvent>
12 #include <QDebug>
13 #include <QDateTime>
14 #include <QMainWindow>
15 #include <QStatusBar>
16 #include <QMenuBar>
17 #include <QPushButton>
18 #include <QHBoxLayout>
19 #include <QFileDialog>
20
21 #include <DDciIcon>
22 #include <DDciIconPlayer>
23 #include <QDropEvent>
24 #include <QMimeData>
25
26 DGUI_USE_NAMESPACE
27
28 static QString iconModeToString(DDciIcon::Mode mode)
29 {
30 switch (mode) {
31 case DDciIcon::Normal:
32 return "Normal";
33 case DDciIcon::Hover:
34 return "Hover";
35 case DDciIcon::Pressed:
36 return "Pressed";
37 case DDciIcon::Disabled:
38 return "Disabled";
39 default:
40 break;
41 }
42
43 return nullptr;
44 }
45
46 class IconWidget : public QMainWindow
47 {
48 public:
49 IconWidget(const QString &iconName)
50 {
51 setCentralWidget(label = new QLabel(this));
52 label->setAcceptDrops(true);
53 centralWidget()->setAttribute(Qt::WA_MouseTracking);
54 centralWidget()->installEventFilter(this);
55 statusBar()->addWidget(message = new QLabel());
56
57 auto normal = new QPushButton("Normal");
58 connect(normal, &QPushButton::clicked, this, [this] {
59 player.setMode(DDciIcon::Normal);
60 });
61 auto hover = new QPushButton("Hover");
62 connect(hover, &QPushButton::clicked, this, [this] {
63 player.setMode(DDciIcon::Hover);
64 });
65 auto pressed = new QPushButton("Pressed");
66 connect(pressed, &QPushButton::clicked, this, [this] {
67 player.setMode(DDciIcon::Pressed);
68 });
69 auto disabled = new QPushButton("Disabled");
70 connect(disabled, &QPushButton::clicked, this, [this] {
71 player.setMode(DDciIcon::Disabled);
72 });
73
74 auto open = new QPushButton("...");
75 connect(open, &QPushButton::clicked, this, [this] {
76 QString dciFile = QFileDialog::getOpenFileName(this, "select a dci icon file", "", "*.dci");
77 player.setIcon(DDciIcon(dciFile));
78 player.setMode(DDciIcon::Normal);
79 });
80
81 QWidget *menuWidget = new QWidget(this);
82 QHBoxLayout *layout = new QHBoxLayout(menuWidget);
83 layout->addWidget(normal);
84 layout->addWidget(hover);
85 layout->addWidget(pressed);
86 layout->addWidget(disabled);
87 layout->addWidget(open);
88 setMenuWidget(menuWidget);
89
90 icon = DDciIcon::fromTheme(iconName);
91
92 connect(&player, &DDciIconPlayer::updated, this, [this] {
93 auto image = player.currentImage();
94 label->setPixmap(QPixmap::fromImage(image));
95 });
96 connect(&player, &DDciIconPlayer::modeChanged, this, [this] (DDciIcon::Mode old, DDciIcon::Mode newMode) {
97 message->setText(QString("Old State: %1, Current State: %2")
98 .arg(iconModeToString(old))
99 .arg(iconModeToString(newMode)));
100 });
101
102 DDciIconPalette p("pink");
103 player.setPalette(p);
104 player.setDevicePixelRatio(devicePixelRatioF());
105 player.setIcon(icon);
106 }
107
108 private:
109 bool event(QEvent *event) override {
110 if (event->type() == QEvent::WindowActivate) {
111 player.play(DDciIcon::Normal);
112 }
113 return QMainWindow::event(event);
114 }
115 bool eventFilter(QObject *watched, QEvent *event) {
116 if (watched != label)
117 return QMainWindow::eventFilter(watched, event);
118
119 if (event->type() == QEvent::Enter) {
120 player.setMode(DDciIcon::Hover);
121 } else if (event->type() == QEvent::Leave) {
122 if (player.mode() == DDciIcon::Hover)
123 player.setMode(DDciIcon::Normal);
124 } else if (event->type() == QEvent::MouseButtonPress) {
125 player.setMode(DDciIcon::Pressed);
126 } else if (event->type() == QEvent::MouseButtonRelease) {
127 player.setMode(label->hasMouseTracking() ? DDciIcon::Hover : DDciIcon::Normal);
128 } else if (event->type() == QEvent::DragEnter) {
129 event->accept();
130 } else if (event->type() == QEvent::Drop) {
131 auto de = static_cast<QDropEvent *>(event);
132 if (de->mimeData()->hasFormat("text/uri-list")) {
133 QString uris = de->mimeData()->data("text/uri-list");
134 QUrl url(uris.split("\r\n").value(0));
135
136 DDciIcon icon(url.path());
137 if (!icon.isNull()) {
138 player.setIcon(icon);
139 player.setMode(DDciIcon::Normal);
140 }
141 }
142 } else {
143 return false;
144 }
145
146 return true;
147 }
148
149 DDciIcon icon;
150 DDciIconPlayer player;
151
152 QLabel *message = nullptr;
153 QLabel *label = nullptr;
154 };
155
156 int main(int argc, char *argv[])
157 {
158 QApplication app(argc, argv);
159
160 IconWidget *window = new IconWidget(QStringLiteral("test_heart"));
161
162 window->setMinimumSize(300, 300);
163 window->show();
164
165 return app.exec();
166 }