Skip to content

Commit

Permalink
chore: animation-dci supports custom icon
Browse files Browse the repository at this point in the history
for test dci animation icon
  • Loading branch information
kegechen committed Jul 18, 2024
1 parent e1131de commit 66b7919
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions examples/animation-dci/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
#include <QMenuBar>
#include <QPushButton>
#include <QHBoxLayout>

#include <qfiledialog.h>
#include <DDciIcon>
#include <DDciIconPlayer>
#include <QDropEvent>
#include <QMimeData>

DGUI_USE_NAMESPACE

Expand Down Expand Up @@ -46,32 +48,42 @@ class IconWidget : public QMainWindow
IconWidget(const QString &iconName)

Check warning on line 48 in examples/animation-dci/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Class 'IconWidget' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.
{
setCentralWidget(label = new QLabel(this));
label->setAcceptDrops(true);
centralWidget()->setAttribute(Qt::WA_MouseTracking);
centralWidget()->installEventFilter(this);
statusBar()->addWidget(message = new QLabel());

auto normal = new QPushButton("Normal");
connect(normal, &QPushButton::clicked, this, [this] {
player.setMode(DDciIcon::Normal);
player.play(DDciIcon::Normal);
});
auto hover = new QPushButton("Hover");
connect(hover, &QPushButton::clicked, this, [this] {
player.setMode(DDciIcon::Hover);
player.play(DDciIcon::Hover);
});
auto pressed = new QPushButton("Pressed");
connect(pressed, &QPushButton::clicked, this, [this] {
player.setMode(DDciIcon::Pressed);
player.play(DDciIcon::Pressed);
});
auto disabled = new QPushButton("Disabled");
connect(disabled, &QPushButton::clicked, this, [this] {
player.setMode(DDciIcon::Disabled);
player.play(DDciIcon::Disabled);
});

auto open = new QPushButton("...");
connect(open, &QPushButton::clicked, this, [this] {
QString dciFile = QFileDialog::getOpenFileName(this, "select a dci icon file", "", "*.dci");
player.setIcon(DDciIcon(dciFile));
player.play(DDciIcon::Normal);
});

QWidget *menuWidget = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout(menuWidget);
layout->addWidget(normal);
layout->addWidget(hover);
layout->addWidget(pressed);
layout->addWidget(disabled);
layout->addWidget(open);
setMenuWidget(menuWidget);

icon = DDciIcon::fromTheme(iconName);
Expand Down Expand Up @@ -112,6 +124,20 @@ class IconWidget : public QMainWindow
player.setMode(DDciIcon::Pressed);
} else if (event->type() == QEvent::MouseButtonRelease) {
player.setMode(label->hasMouseTracking() ? DDciIcon::Hover : DDciIcon::Normal);
} else if (event->type() == QEvent::DragEnter) {
event->accept();
} else if (event->type() == QEvent::Drop) {
auto de = static_cast<QDropEvent *>(event);
if (de->mimeData()->hasFormat("text/uri-list")) {
QString uris = de->mimeData()->data("text/uri-list");
QUrl url(uris.split("\r\n").value(0));

DDciIcon icon(url.path());
if (!icon.isNull()) {
player.setIcon(icon);
player.play(DDciIcon::Normal);
}
}
} else {
return false;
}
Expand Down

0 comments on commit 66b7919

Please sign in to comment.