Issue #25: Windows task button progress bar

This commit is contained in:
Jakub Melka 2022-09-09 19:45:03 +02:00
parent 72ad433467
commit 825fe59420
7 changed files with 298 additions and 53 deletions

View File

@ -33,6 +33,7 @@ add_library(Pdf4QtViewer SHARED
pdfviewermainwindowlite.cpp
pdfviewersettings.cpp
pdfviewersettingsdialog.cpp
pdfwintaskbarprogress.cpp
pdfaboutdialog.ui
pdfadvancedfindwidget.ui
pdfdocumentpropertiesdialog.ui

View File

@ -90,10 +90,7 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) :
m_pageZoomSpinBox(nullptr),
m_isLoadingUI(false),
m_progress(new pdf::PDFProgress(this)),
#ifdef WIN_TASKBAR_BUTTON
m_taskbarButton(new QWinTaskbarButton(this)),
m_progressTaskbarIndicator(nullptr),
#endif
m_progressTaskbarIndicator(new PDFWinTaskBarProgress(this)),
m_progressDialog(nullptr),
m_isChangingProgressStep(false)
{
@ -105,11 +102,6 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) :
adjustToolbar(ui->mainToolBar);
ui->mainToolBar->setWindowTitle(tr("Standard"));
// Initialize task bar progress
#ifdef WIN_TASKBAR_BUTTON
m_progressTaskbarIndicator = m_taskbarButton->progress();
#endif
// Initialize actions
m_actionManager->setAction(PDFActionManager::Open, ui->actionOpen);
m_actionManager->setAction(PDFActionManager::Close, ui->actionClose);
@ -355,11 +347,9 @@ void PDFViewerMainWindow::onProgressStarted(pdf::ProgressStartupInfo info)
m_progressDialog->setCancelButton(nullptr);
}
#ifdef WIN_TASKBAR_BUTTON
m_progressTaskbarIndicator->setRange(0, 100);
m_progressTaskbarIndicator->reset();
m_progressTaskbarIndicator->show();
#endif
m_programController->setIsBusy(true);
m_programController->updateActionsAvailability();
@ -379,9 +369,7 @@ void PDFViewerMainWindow::onProgressStep(int percentage)
m_progressDialog->setValue(percentage);
}
#ifdef WIN_TASKBAR_BUTTON
m_progressTaskbarIndicator->setValue(percentage);
#endif
}
void PDFViewerMainWindow::onProgressFinished()
@ -392,9 +380,8 @@ void PDFViewerMainWindow::onProgressFinished()
m_progressDialog->deleteLater();
m_progressDialog = nullptr;
}
#ifdef WIN_TASKBAR_BUTTON
m_progressTaskbarIndicator->hide();
#endif
m_programController->setIsBusy(false);
m_programController->updateActionsAvailability();
@ -521,9 +508,7 @@ void PDFViewerMainWindow::closeEvent(QCloseEvent* event)
void PDFViewerMainWindow::showEvent(QShowEvent* event)
{
Q_UNUSED(event);
#ifdef WIN_TASKBAR_BUTTON
m_taskbarButton->setWindow(windowHandle());
#endif
m_progressTaskbarIndicator->setWindow(windowHandle());
}
void PDFViewerMainWindow::dragEnterEvent(QDragEnterEvent* event)

View File

@ -1,4 +1,4 @@
// Copyright (C) 2019-2021 Jakub Melka
// Copyright (C) 2019-2022 Jakub Melka
//
// This file is part of PDF4QT.
//
@ -34,14 +34,11 @@
#include "pdfundoredomanager.h"
#include "pdfplugin.h"
#include "pdfprogramcontroller.h"
#include "pdfwintaskbarprogress.h"
#include <QFuture>
#include <QTreeView>
#include <QMainWindow>
#ifdef WIN_TASKBAR_BUTTON
#include <QWinTaskbarButton>
#include <QWinTaskbarProgress>
#endif
#include <QFutureWatcher>
#include <QProgressDialog>
@ -120,10 +117,7 @@ private:
QDoubleSpinBox* m_pageZoomSpinBox;
bool m_isLoadingUI;
pdf::PDFProgress* m_progress;
#ifdef WIN_TASKBAR_BUTTON
QWinTaskbarButton* m_taskbarButton;
QWinTaskbarProgress* m_progressTaskbarIndicator;
#endif
PDFWinTaskBarProgress* m_progressTaskbarIndicator;
QProgressDialog* m_progressDialog;
bool m_isChangingProgressStep;

View File

@ -88,10 +88,7 @@ PDFViewerMainWindowLite::PDFViewerMainWindowLite(QWidget* parent) :
m_pageZoomSpinBox(nullptr),
m_isLoadingUI(false),
m_progress(new pdf::PDFProgress(this)),
#ifdef WIN_TASKBAR_BUTTON
m_taskbarButton(new QWinTaskbarButton(this)),
m_progressTaskbarIndicator(nullptr),
#endif
m_progressTaskbarIndicator(new PDFWinTaskBarProgress(this)),
m_progressDialog(nullptr),
m_isChangingProgressStep(false)
{
@ -103,11 +100,6 @@ PDFViewerMainWindowLite::PDFViewerMainWindowLite(QWidget* parent) :
adjustToolbar(ui->mainToolBar);
ui->mainToolBar->setWindowTitle(tr("Standard"));
// Initialize task bar progress
#ifdef WIN_TASKBAR_BUTTON
m_progressTaskbarIndicator = m_taskbarButton->progress();
#endif
// Initialize actions
m_actionManager->setAction(PDFActionManager::Open, ui->actionOpen);
m_actionManager->setAction(PDFActionManager::Close, ui->actionClose);
@ -277,11 +269,9 @@ void PDFViewerMainWindowLite::onProgressStarted(pdf::ProgressStartupInfo info)
m_progressDialog->setCancelButton(nullptr);
}
#ifdef WIN_TASKBAR_BUTTON
m_progressTaskbarIndicator->setRange(0, 100);
m_progressTaskbarIndicator->reset();
m_progressTaskbarIndicator->show();
#endif
m_programController->setIsBusy(true);
m_programController->updateActionsAvailability();
@ -301,9 +291,7 @@ void PDFViewerMainWindowLite::onProgressStep(int percentage)
m_progressDialog->setValue(percentage);
}
#ifdef WIN_TASKBAR_BUTTON
m_progressTaskbarIndicator->setValue(percentage);
#endif
}
void PDFViewerMainWindowLite::onProgressFinished()
@ -314,9 +302,8 @@ void PDFViewerMainWindowLite::onProgressFinished()
m_progressDialog->deleteLater();
m_progressDialog = nullptr;
}
#ifdef WIN_TASKBAR_BUTTON
m_progressTaskbarIndicator->hide();
#endif
m_programController->setIsBusy(false);
m_programController->updateActionsAvailability();
@ -422,9 +409,7 @@ void PDFViewerMainWindowLite::closeEvent(QCloseEvent* event)
void PDFViewerMainWindowLite::showEvent(QShowEvent* event)
{
Q_UNUSED(event);
#ifdef WIN_TASKBAR_BUTTON
m_taskbarButton->setWindow(windowHandle());
#endif
m_progressTaskbarIndicator->setWindow(windowHandle());
}
void PDFViewerMainWindowLite::dragEnterEvent(QDragEnterEvent* event)

View File

@ -34,14 +34,11 @@
#include "pdfundoredomanager.h"
#include "pdfplugin.h"
#include "pdfprogramcontroller.h"
#include "pdfwintaskbarprogress.h"
#include <QFuture>
#include <QTreeView>
#include <QMainWindow>
#ifdef WIN_TASKBAR_BUTTON
#include <QWinTaskbarButton>
#include <QWinTaskbarProgress>
#endif
#include <QFutureWatcher>
#include <QProgressDialog>
@ -116,10 +113,7 @@ private:
QDoubleSpinBox* m_pageZoomSpinBox;
bool m_isLoadingUI;
pdf::PDFProgress* m_progress;
#ifdef WIN_TASKBAR_BUTTON
QWinTaskbarButton* m_taskbarButton;
QWinTaskbarProgress* m_progressTaskbarIndicator;
#endif
PDFWinTaskBarProgress* m_progressTaskbarIndicator;
QProgressDialog* m_progressDialog;
bool m_isChangingProgressStep;

View File

@ -0,0 +1,228 @@
// Copyright (C) 2022 Jakub Melka
//
// This file is part of PDF4QT.
//
// PDF4QT is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// with the written consent of the copyright owner, any later version.
//
// PDF4QT is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
#include "pdfwintaskbarprogress.h"
#include <QWindow>
#if defined(Q_OS_WIN) && !defined(PDF4QT_NO_TASKBAR_PROGRESS)
#define PDF4QT_TASKBAR_PROGRESS
#endif
#ifdef PDF4QT_TASKBAR_PROGRESS
#include <ShObjIdl.h>
#endif
namespace pdfviewer
{
#ifdef PDF4QT_TASKBAR_PROGRESS
class PDFWinTaskBarProgressImpl
{
public:
PDFWinTaskBarProgressImpl()
{
HRESULT result = CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_INPROC_SERVER, __uuidof(ITaskbarList3), reinterpret_cast<void**>(&m_taskBarList));
if (FAILED(result))
{
qWarning("Failed to create task bar progress");
m_taskBarList = nullptr;
}
else
{
HRESULT taskBarInitResult = m_taskBarList->HrInit();
if (FAILED(taskBarInitResult))
{
qWarning("Failed to initialize task bar progress");
m_taskBarList->Release();
m_taskBarList = nullptr;
}
}
}
~PDFWinTaskBarProgressImpl()
{
if (m_taskBarList)
{
m_taskBarList->Release();
m_taskBarList = nullptr;
}
}
inline HWND getHandle() const
{
return m_window ? reinterpret_cast<HWND>(m_window->winId()) : HWND();
}
inline void updateProgress() const
{
if (m_window && m_taskBarList)
{
const pdf::PDFReal range = m_progressMax - m_progressMin;
const pdf::PDFReal offset = m_value - m_progressMin;
const ULONGLONG max = 1000;
if (!qFuzzyIsNull(range))
{
const ULONGLONG progress = max * offset / range;
m_taskBarList->SetProgressValue(getHandle(), progress, max);
}
else
{
m_taskBarList->SetProgressValue(getHandle(), 0, max);
}
TBPFLAG stateFlag = TBPF_NOPROGRESS;
if (m_progressBarVisible)
{
stateFlag = TBPF_NORMAL;
}
m_taskBarList->SetProgressState(getHandle(), stateFlag);
}
}
inline void setWindow(QWindow* window)
{
if (m_window = window)
{
m_window = window;
updateProgress();
}
}
inline void reset()
{
setValue(m_progressMin);
}
inline void show()
{
if (!m_progressBarVisible)
{
m_progressBarVisible = true;
updateProgress();
}
}
inline void hide()
{
if (m_progressBarVisible)
{
m_progressBarVisible = false;
updateProgress();
}
}
inline void setValue(pdf::PDFInteger value)
{
if (m_value != value)
{
m_value = qBound(m_progressMin, value, m_progressMax);
updateProgress();
}
}
inline void setRange(pdf::PDFInteger min, pdf::PDFInteger max)
{
Q_ASSERT(min < max);
if (m_progressMin != min || m_progressMax != max)
{
m_progressMin = min;
m_progressMax = max;
m_value = qBound(min, m_value, max);
updateProgress();
}
}
private:
QWindow* m_window = nullptr;
ITaskbarList3* m_taskBarList = nullptr;
// Progress bar
bool m_progressBarVisible = false;
pdf::PDFInteger m_progressMin = 0;
pdf::PDFInteger m_progressMax = 100;
pdf::PDFInteger m_value = 0;
};
#else
class PDFWinTaskBarProgressImpl
{
public:
constexpr PDFWinTaskBarProgressImpl() = default;
constexpr ~PDFWinTaskBarProgressImpl() = default;
constexpr void setWindow(QWindow* window) { Q_UNUSED(window); }
constexpr void reset() { }
constexpr void show() { }
constexpr void hide() { }
constexpr void setValue(pdf::PDFInteger value) { Q_UNUSED(value); }
constexpr void setRange(pdf::PDFInteger min, pdf::PDFInteger max) { Q_UNUSED(min); Q_UNUSED(max); }
};
#endif
PDFWinTaskBarProgress::PDFWinTaskBarProgress(QObject* parent) :
QObject(parent),
m_impl(new PDFWinTaskBarProgressImpl())
{
}
PDFWinTaskBarProgress::~PDFWinTaskBarProgress()
{
delete m_impl;
}
void PDFWinTaskBarProgress::setWindow(QWindow* window)
{
m_impl->setWindow(window);
}
void PDFWinTaskBarProgress::reset()
{
m_impl->reset();
}
void PDFWinTaskBarProgress::show()
{
m_impl->show();
}
void PDFWinTaskBarProgress::hide()
{
m_impl->hide();
}
void PDFWinTaskBarProgress::setValue(pdf::PDFInteger value)
{
m_impl->setValue(value);
}
void PDFWinTaskBarProgress::setRange(pdf::PDFInteger min, pdf::PDFInteger max)
{
m_impl->setRange(min, max);
}
} // namespace pdfviewer

View File

@ -0,0 +1,58 @@
// Copyright (C) 2022 Jakub Melka
//
// This file is part of PDF4QT.
//
// PDF4QT is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// with the written consent of the copyright owner, any later version.
//
// PDF4QT is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
#ifndef PDFWINTASKBARPROGRESS_H
#define PDFWINTASKBARPROGRESS_H
#include "pdfviewerglobal.h"
#include "pdfglobal.h"
#include <QObject>
class QWindow;
namespace pdfviewer
{
class PDFWinTaskBarProgressImpl;
/// Windows task bar progress. Displays progress
/// in the task bar icon in window's bottom side.
class PDFWinTaskBarProgress : public QObject
{
Q_OBJECT
public:
PDFWinTaskBarProgress(QObject* parent);
virtual ~PDFWinTaskBarProgress() override;
void setWindow(QWindow* window);
void reset();
void show();
void hide();
void setValue(pdf::PDFInteger value);
void setRange(pdf::PDFInteger min, pdf::PDFInteger max);
private:
PDFWinTaskBarProgressImpl* m_impl;
};
} // namespace pdfviewer
#endif // PDFWINTASKBARPROGRESS_H