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 19, 2024
1 parent b23ef70 commit 8f03ae9
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/animation-dci/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
#include <QMenuBar>
#include <QPushButton>
#include <QHBoxLayout>
#include <QFileDialog>

#include <DDciIcon>
#include <DDciIconPlayer>
#include <QDropEvent>
#include <QMimeData>

DGUI_USE_NAMESPACE

Expand Down Expand Up @@ -46,6 +49,7 @@ class IconWidget : public QMainWindow
IconWidget(const QString &iconName)

Check warning on line 49 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());
Expand All @@ -66,12 +70,21 @@ class IconWidget : public QMainWindow
connect(disabled, &QPushButton::clicked, this, [this] {
player.setMode(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.setMode(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 +125,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.setMode(DDciIcon::Normal);
}
}
} else {
return false;
}
Expand Down

0 comments on commit 8f03ae9

Please sign in to comment.