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

Add "Reload File" feature #625

Merged
merged 2 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions src/actionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ QMenuBar *ActionManager::buildMenuBar(QWidget *parent)
addCloneOfAction(fileMenu, "open");
addCloneOfAction(fileMenu, "openurl");
fileMenu->addMenu(buildRecentsMenu(true, fileMenu));
addCloneOfAction(fileMenu, "reloadfile");
fileMenu->addSeparator();
#ifdef Q_OS_MACOS
fileMenu->addSeparator();
Expand Down Expand Up @@ -585,6 +586,8 @@ void ActionManager::actionTriggered(QAction *triggeredAction, MainWindow *releva
relevantWindow->openWith(openWithItem);
} else if (key == "openurl") {
relevantWindow->pickUrl();
} else if (key == "reloadfile") {
relevantWindow->reloadFile();
} else if (key == "opencontainingfolder") {
relevantWindow->openContainingFolder();
} else if (key == "showfileinfo") {
Expand Down Expand Up @@ -660,6 +663,10 @@ void ActionManager::initializeActionLibrary()
auto *openUrlAction = new QAction(QIcon::fromTheme("document-open-remote", QIcon::fromTheme("folder-remote")), tr("Open &URL..."));
actionLibrary.insert("openurl", openUrlAction);

auto *reloadFileAction = new QAction(QIcon::fromTheme("view-refresh"), tr("Re&load File"));
reloadFileAction->setData({"disable"});
actionLibrary.insert("reloadfile", reloadFileAction);

auto *closeWindowAction = new QAction(QIcon::fromTheme("window-close"), tr("Close Window"));
actionLibrary.insert("closewindow", closeWindowAction);

Expand Down
5 changes: 5 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,11 @@ void MainWindow::pickUrl()
inputDialog->open();
}

void MainWindow::reloadFile()
{
graphicsView->reloadFile();
}

void MainWindow::openWith(const OpenWith::OpenWithItem &openWithItem)
{
OpenWith::openWith(getCurrentFileDetails().fileInfo.absoluteFilePath(), openWithItem);
Expand Down
2 changes: 2 additions & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class MainWindow : public QMainWindow

void pickUrl();

void reloadFile();

void openContainingFolder();

void openWith(const OpenWith::OpenWithItem &exec);
Expand Down
8 changes: 8 additions & 0 deletions src/qvgraphicsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ void QVGraphicsView::loadFile(const QString &fileName)
imageCore.loadFile(fileName);
}

void QVGraphicsView::reloadFile()
{
if (!getCurrentFileDetails().isPixmapLoaded)
return;

imageCore.loadFile(getCurrentFileDetails().fileInfo.absoluteFilePath(), true);
}

void QVGraphicsView::postLoad()
{
updateLoadedPixmapItem();
Expand Down
2 changes: 2 additions & 0 deletions src/qvgraphicsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class QVGraphicsView : public QGraphicsView
void loadMimeData(const QMimeData *mimeData);
void loadFile(const QString &fileName);

void reloadFile();

void zoomIn(const QPoint &pos = QPoint(-1, -1));

void zoomOut(const QPoint &pos = QPoint(-1, -1));
Expand Down
4 changes: 2 additions & 2 deletions src/qvimagecore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ QVImageCore::QVImageCore(QObject *parent) : QObject(parent)
settingsUpdated();
}

void QVImageCore::loadFile(const QString &fileName)
void QVImageCore::loadFile(const QString &fileName, bool isReloading)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cache key includes the file size, but this ensures it handles situations in which the size doesn't change (e.g. an uncompressed BMP was re-saved).

{
if (waitingOnLoad)
{
Expand Down Expand Up @@ -105,7 +105,7 @@ void QVImageCore::loadFile(const QString &fileName)
QString cacheKey = getPixmapCacheKey(sanitaryFileName, fileInfo.size(), targetColorSpace);

//check if cached already before loading the long way
auto *cachedData = QVImageCore::pixmapCache.take(cacheKey);
auto *cachedData = isReloading ? nullptr : QVImageCore::pixmapCache.take(cacheKey);
if (cachedData != nullptr)
{
ReadData readData = *cachedData;
Expand Down
2 changes: 1 addition & 1 deletion src/qvimagecore.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class QVImageCore : public QObject

explicit QVImageCore(QObject *parent = nullptr);

void loadFile(const QString &fileName);
void loadFile(const QString &fileName, bool isReloaing = false);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

ReadData readFile(const QString &fileName, const QColorSpace &targetColorSpace, bool forCache);
void loadPixmap(const ReadData &readData);
void closeImage();
Expand Down
1 change: 1 addition & 0 deletions src/shortcutmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void ShortcutManager::initializeShortcutsList()
{
shortcutsList.append({tr("Open"), "open", keyBindingsToStringList(QKeySequence::Open), {}});
shortcutsList.append({tr("Open URL"), "openurl", QStringList(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_O).toString()), {}});
shortcutsList.append({tr("Reload File"), "reloadfile", keyBindingsToStringList(QKeySequence::Refresh), {}});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed this is cmd+r on macOS, but this conflicts with rename. I don't know which should be prioritized--maybe refresh is more important.

Make sure this doesn't conflict on other platforms too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, you can take a look at my latest commit and see if you like that idea or have a different suggestion perhaps.

shortcutsList.append({tr("Open Containing Folder"), "opencontainingfolder", {}, {}});
//Sets open containing folder action name to platform-appropriate alternative
#ifdef Q_OS_WIN
Expand Down