Skip to content

Commit

Permalink
fix: window title not updated in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
BLumia committed Jul 30, 2024
1 parent a6e31a2 commit 46b858a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 89 deletions.
60 changes: 2 additions & 58 deletions app/graphicsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ GraphicsView::GraphicsView(QWidget *parent)
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setStyleSheet("background-color: rgba(0, 0, 0, 220);"
"border-radius: 3px;");
setAcceptDrops(true);
setAcceptDrops(false);
setCheckerboardEnabled(false);

connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &GraphicsView::viewportRectChanged);
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &GraphicsView::viewportRectChanged);
}

void GraphicsView::showFileFromPath(const QString &filePath, bool doRequestGallery)
void GraphicsView::showFileFromPath(const QString &filePath)
{
emit navigatorViewRequired(false, transform());

Expand All @@ -48,28 +48,21 @@ void GraphicsView::showFileFromPath(const QString &filePath, bool doRequestGalle
// So we cannot use imageFormat() and check if it returns QImage::Format_Invalid to detect if we support the file.
// QImage::Format imageFormat = imageReader.imageFormat();
if (imageReader.format().isEmpty()) {
doRequestGallery = false;
showText(tr("File is not a valid image"));
} else if (imageReader.supportsAnimation() && imageReader.imageCount() > 1) {
showAnimated(filePath);
} else if (!imageReader.canRead()) {
doRequestGallery = false;
showText(tr("Image data is invalid or currently unsupported"));
} else {
QPixmap && pixmap = QPixmap::fromImageReader(&imageReader);
if (pixmap.isNull()) {
doRequestGallery = false;
showText(tr("Image data is invalid or currently unsupported"));
} else {
pixmap.setDevicePixelRatio(devicePixelRatioF());
showImage(pixmap);
}
}
}

if (doRequestGallery) {
emit requestGallery(filePath);
}
}

void GraphicsView::showImage(const QPixmap &pixmap)
Expand Down Expand Up @@ -318,55 +311,6 @@ void GraphicsView::resizeEvent(QResizeEvent *event)
return QGraphicsView::resizeEvent(event);
}

void GraphicsView::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls() || event->mimeData()->hasImage() || event->mimeData()->hasText()) {
event->acceptProposedAction();
} else {
event->ignore();
}
// qDebug() << event->mimeData() << "Drag Enter Event"
// << event->mimeData()->hasUrls() << event->mimeData()->hasImage()
// << event->mimeData()->formats() << event->mimeData()->hasFormat("text/uri-list");

return QGraphicsView::dragEnterEvent(event);
}

void GraphicsView::dragMoveEvent(QDragMoveEvent *event)
{
Q_UNUSED(event)
// by default, QGraphicsView/Scene will ignore the action if there are no QGraphicsItem under cursor.
// We actually doesn't care and would like to keep the drag event as-is, so just do nothing here.
}

void GraphicsView::dropEvent(QDropEvent *event)
{
event->acceptProposedAction();

const QMimeData * mimeData = event->mimeData();

if (mimeData->hasUrls()) {
const QList<QUrl> &urls = mimeData->urls();
if (urls.isEmpty()) {
showText(tr("File url list is empty"));
} else {
showFileFromPath(urls.first().toLocalFile(), true);
}
} else if (mimeData->hasImage()) {
QImage img = qvariant_cast<QImage>(mimeData->imageData());
QPixmap pixmap = QPixmap::fromImage(img);
if (pixmap.isNull()) {
showText(tr("Image data is invalid"));
} else {
showImage(pixmap);
}
} else if (mimeData->hasText()) {
showText(mimeData->text());
} else {
showText(tr("Not supported mimedata: %1").arg(mimeData->formats().first()));
}
}

bool GraphicsView::isThingSmallerThanWindowWith(const QTransform &transform) const
{
return rect().size().expandedTo(transform.mapRect(sceneRect()).size().toSize())
Expand Down
7 changes: 1 addition & 6 deletions app/graphicsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GraphicsView : public QGraphicsView
public:
GraphicsView(QWidget *parent = nullptr);

void showFileFromPath(const QString &filePath, bool requestGallery = false);
void showFileFromPath(const QString &filePath);

void showImage(const QPixmap &pixmap);
void showImage(const QImage &image);
Expand Down Expand Up @@ -48,7 +48,6 @@ class GraphicsView : public QGraphicsView
signals:
void navigatorViewRequired(bool required, QTransform transform);
void viewportRectChanged();
void requestGallery(const QString &filePath);

public slots:
void toggleCheckerboard(bool invertCheckerboardColor = false);
Expand All @@ -60,10 +59,6 @@ public slots:
void wheelEvent(QWheelEvent *event) override;
void resizeEvent(QResizeEvent *event) override;

void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
void dropEvent(QDropEvent *event) override;

bool isThingSmallerThanWindowWith(const QTransform &transform) const;
bool shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) const;
void setCheckerboardEnabled(bool enabled, bool invertColor = false);
Expand Down
80 changes: 58 additions & 22 deletions app/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ MainWindow::MainWindow(QWidget *parent)
this->setMinimumSize(350, 330);
this->setWindowIcon(QIcon(":/icons/app-icon.svg"));
this->setMouseTracking(true);
this->setAcceptDrops(true);

m_pm->setAutoLoadFilterSuffixes(supportedImageFormats());

Expand Down Expand Up @@ -95,9 +96,6 @@ MainWindow::MainWindow(QWidget *parent)
connect(m_graphicsView, &GraphicsView::viewportRectChanged,
m_gv, &NavigatorView::updateMainViewportRegion);

connect(m_graphicsView, &GraphicsView::requestGallery,
this, &MainWindow::loadGalleryBySingleLocalFile);

m_closeButton = new ToolButton(true, m_graphicsView);
m_closeButton->setIconSize(QSize(32, 32));
m_closeButton->setFixedSize(QSize(50, 50));
Expand Down Expand Up @@ -142,7 +140,8 @@ MainWindow::MainWindow(QWidget *parent)
m_nextButton->setVisible(galleryFileCount > 1);
});

connect(m_pm, &PlaylistManager::currentIndexChanged, this, &MainWindow::galleryCurrent);
connect(m_pm->model(), &PlaylistModel::modelReset, this, std::bind(&MainWindow::galleryCurrent, this, false));
connect(m_pm, &PlaylistManager::currentIndexChanged, this, std::bind(&MainWindow::galleryCurrent, this, true));

QShortcut * fullscreenShorucut = new QShortcut(QKeySequence(QKeySequence::FullScreen), this);
connect(fullscreenShorucut, &QShortcut::activated,
Expand Down Expand Up @@ -170,12 +169,8 @@ MainWindow::~MainWindow()
void MainWindow::showUrls(const QList<QUrl> &urls)
{
if (!urls.isEmpty()) {
if (urls.count() == 1) {
m_graphicsView->showFileFromPath(urls.first().toLocalFile(), true);
} else {
m_graphicsView->showFileFromPath(urls.first().toLocalFile(), false);
m_pm->setPlaylist(urls);
}
m_graphicsView->showFileFromPath(urls.first().toLocalFile());
m_pm->loadPlaylist(urls);
} else {
m_graphicsView->showText(tr("File url list is empty"));
return;
Expand Down Expand Up @@ -240,16 +235,12 @@ void MainWindow::clearGallery()
m_pm->setPlaylist({});
}

void MainWindow::loadGalleryBySingleLocalFile(const QString &path)
{
m_pm->loadPlaylist(QUrl::fromLocalFile(path));
}

void MainWindow::galleryPrev()
{
QModelIndex index = m_pm->previousIndex();
if (index.isValid()) {
m_pm->setCurrentIndex(index);
m_graphicsView->showFileFromPath(m_pm->localFileByIndex(index));
}
}

Expand All @@ -258,17 +249,18 @@ void MainWindow::galleryNext()
QModelIndex index = m_pm->nextIndex();
if (index.isValid()) {
m_pm->setCurrentIndex(index);
m_graphicsView->showFileFromPath(m_pm->localFileByIndex(index));
}
}

// If playlist (or its index) get changed, use this method to "reload" the current file.
void MainWindow::galleryCurrent()
// Only use this to update minor imformation. Do NOT use this to load image
// because it might cause an image gets loaded multiple times.
void MainWindow::galleryCurrent(bool showLoadImageHintWhenEmpty)
{
QModelIndex index = m_pm->curIndex();
if (index.isValid()) {
setWindowTitle(m_pm->urlByIndex(index).fileName());
m_graphicsView->showFileFromPath(m_pm->localFileByIndex(index), false);
} else {
} else if (showLoadImageHintWhenEmpty && m_pm->totalCount() <= 0) {
m_graphicsView->showText(QCoreApplication::translate("GraphicsScene", "Drag image here"));
}
}
Expand Down Expand Up @@ -511,6 +503,50 @@ void MainWindow::contextMenuEvent(QContextMenuEvent *event)
return FramelessWindow::contextMenuEvent(event);
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls() || event->mimeData()->hasImage() || event->mimeData()->hasText()) {
event->acceptProposedAction();
} else {
event->ignore();
}

return FramelessWindow::dragEnterEvent(event);
}

void MainWindow::dragMoveEvent(QDragMoveEvent *event)
{
Q_UNUSED(event)
}

void MainWindow::dropEvent(QDropEvent *event)
{
event->acceptProposedAction();

const QMimeData * mimeData = event->mimeData();

if (mimeData->hasUrls()) {
const QList<QUrl> &urls = mimeData->urls();
if (urls.isEmpty()) {
m_graphicsView->showText(tr("File url list is empty"));
} else {
showUrls(urls);
}
} else if (mimeData->hasImage()) {
QImage img = qvariant_cast<QImage>(mimeData->imageData());
QPixmap pixmap = QPixmap::fromImage(img);
if (pixmap.isNull()) {
m_graphicsView->showText(tr("Image data is invalid"));
} else {
m_graphicsView->showImage(pixmap);
}
} else if (mimeData->hasText()) {
m_graphicsView->showText(mimeData->text());
} else {
m_graphicsView->showText(tr("Not supported mimedata: %1").arg(mimeData->formats().first()));
}
}

void MainWindow::centerWindow()
{
this->setGeometry(
Expand Down Expand Up @@ -703,11 +739,11 @@ void MainWindow::on_actionPaste_triggered()
}

if (!clipboardImage.isNull()) {
setWindowTitle(tr("Image From Clipboard"));
m_graphicsView->showImage(clipboardImage);
clearGallery();
} else if (clipboardFileUrl.isValid()) {
QString localFile(clipboardFileUrl.toLocalFile());
m_graphicsView->showFileFromPath(localFile, true);
m_graphicsView->showFileFromPath(clipboardFileUrl.toLocalFile());
m_pm->loadPlaylist(clipboardFileUrl);
}
}
Expand All @@ -729,7 +765,7 @@ void MainWindow::on_actionTrash_triggered()
tr("Move to trash failed, it might caused by file permission issue, file system limitation, or platform limitation."));
} else {
m_pm->removeAt(index);
galleryCurrent();
galleryCurrent(true);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ class MainWindow : public FramelessWindow
QUrl currentImageFileUrl() const;

void clearGallery();
void loadGalleryBySingleLocalFile(const QString &path);
void galleryPrev();
void galleryNext();
void galleryCurrent();
void galleryCurrent(bool showLoadImageHintWhenEmpty);

static QStringList supportedImageFormats();

Expand All @@ -60,6 +59,9 @@ protected slots:
void wheelEvent(QWheelEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void contextMenuEvent(QContextMenuEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
void dropEvent(QDropEvent *event) override;

void centerWindow();
void closeWindow();
Expand Down
2 changes: 1 addition & 1 deletion app/playlistmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ class PlaylistManager : public QObject
void totalCountChanged(int count);

private:
int m_currentIndex;
int m_currentIndex = -1;
PlaylistModel m_model;
};

0 comments on commit 46b858a

Please sign in to comment.