Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: animation-dci supports custom icon #247

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -43,9 +46,10 @@
class IconWidget : public QMainWindow
{
public:
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 @@
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 @@
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
2 changes: 1 addition & 1 deletion src/util/ddciiconplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ bool DDciIconPlayerPrivate::ensureHoverModeLastImage()
}
if (!image.atEnd())
return false;
Q_ASSERT(!player);
Q_ASSERT(player);
hoverModeLastImage = image.toImage(player->palette());
return !hoverModeLastImage.isNull();
}
Expand Down
Loading