Polling of available printers in now asynchronous (#264,#256)
- Increased polling interval from 1s to 10s - Use QtConcurrent::run to initiate a poll asynchronously to main event loop - Do not initiate a poll if the previous poll has not completed - Use copies instead of references to mCurrentAvailablePrinters in public API
This commit is contained in:
+1
-1
@@ -115,7 +115,7 @@ if (MINGW)
|
|||||||
set (CMAKE_PREFIX_PATH ${MINGW_BASE_DIR} )
|
set (CMAKE_PREFIX_PATH ${MINGW_BASE_DIR} )
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
find_package (Qt6 6.2 REQUIRED COMPONENTS Core Gui Widgets PrintSupport Xml Svg LinguistTools Test)
|
find_package (Qt6 6.2 REQUIRED COMPONENTS Concurrent Core Gui Widgets PrintSupport Xml Svg LinguistTools Test)
|
||||||
|
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
# Locate Qt directories
|
# Locate Qt directories
|
||||||
|
|||||||
@@ -138,6 +138,7 @@ target_include_directories (glabels-qt
|
|||||||
|
|
||||||
target_link_libraries (glabels-qt
|
target_link_libraries (glabels-qt
|
||||||
Model
|
Model
|
||||||
|
Qt6::Concurrent
|
||||||
Qt6::Widgets
|
Qt6::Widgets
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ namespace glabels
|
|||||||
|
|
||||||
auto* printerMonitor = PrinterMonitor::instance();
|
auto* printerMonitor = PrinterMonitor::instance();
|
||||||
loadDestinations( printerMonitor->availablePrinters() );
|
loadDestinations( printerMonitor->availablePrinters() );
|
||||||
connect( printerMonitor, SIGNAL(availablePrintersChanged(const QStringList&)),
|
connect( printerMonitor, SIGNAL(availablePrintersChanged(QStringList)),
|
||||||
this, SLOT(onAvailablePrintersChanged(const QStringList&)) );
|
this, SLOT(onAvailablePrintersChanged(QStringList)) );
|
||||||
|
|
||||||
setDestination( model::Settings::recentPrinter() );
|
setDestination( model::Settings::recentPrinter() );
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ namespace glabels
|
|||||||
///
|
///
|
||||||
/// Available printers changed handler
|
/// Available printers changed handler
|
||||||
///
|
///
|
||||||
void PrintView::onAvailablePrintersChanged( const QStringList& printers )
|
void PrintView::onAvailablePrintersChanged( QStringList printers )
|
||||||
{
|
{
|
||||||
auto savedSelection = destinationCombo->currentText();
|
auto savedSelection = destinationCombo->currentText();
|
||||||
loadDestinations( printers );
|
loadDestinations( printers );
|
||||||
|
|||||||
+1
-1
@@ -57,7 +57,7 @@ namespace glabels
|
|||||||
// Slots
|
// Slots
|
||||||
/////////////////////////////////
|
/////////////////////////////////
|
||||||
private slots:
|
private slots:
|
||||||
void onAvailablePrintersChanged( const QStringList& printers );
|
void onAvailablePrintersChanged( QStringList printers );
|
||||||
void onModelChanged();
|
void onModelChanged();
|
||||||
void updateView();
|
void updateView();
|
||||||
void onFormChanged();
|
void onFormChanged();
|
||||||
|
|||||||
@@ -21,8 +21,10 @@
|
|||||||
|
|
||||||
#include "PrinterMonitor.h"
|
#include "PrinterMonitor.h"
|
||||||
|
|
||||||
#include <QPrinterInfo>
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
#include <QMutexLocker>
|
||||||
|
#include <QPrinterInfo>
|
||||||
|
#include <QtConcurrentRun>
|
||||||
|
|
||||||
|
|
||||||
namespace glabels
|
namespace glabels
|
||||||
@@ -39,11 +41,13 @@ namespace glabels
|
|||||||
///
|
///
|
||||||
PrinterMonitor::PrinterMonitor()
|
PrinterMonitor::PrinterMonitor()
|
||||||
{
|
{
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
mCurrentAvailablePrinters = QPrinterInfo::availablePrinterNames();
|
mCurrentAvailablePrinters = QPrinterInfo::availablePrinterNames();
|
||||||
|
|
||||||
mTimer.reset( new QTimer( this ) );
|
mTimer.reset( new QTimer( this ) );
|
||||||
connect( mTimer.get(), SIGNAL(timeout()), this, SLOT(onTimerTimeout()) );
|
connect( mTimer.get(), SIGNAL(timeout()), this, SLOT(onTimerTimeout()) );
|
||||||
mTimer->start( 1000 );
|
mTimer->start( 10s );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -64,8 +68,9 @@ namespace glabels
|
|||||||
///
|
///
|
||||||
/// Get available printers
|
/// Get available printers
|
||||||
///
|
///
|
||||||
const QStringList& PrinterMonitor::availablePrinters() const
|
QStringList PrinterMonitor::availablePrinters()
|
||||||
{
|
{
|
||||||
|
QMutexLocker mutex( &mCurrentAvailablePrintersMutex );
|
||||||
return mCurrentAvailablePrinters;
|
return mCurrentAvailablePrinters;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,10 +79,25 @@ namespace glabels
|
|||||||
/// On timer timeout
|
/// On timer timeout
|
||||||
///
|
///
|
||||||
void PrinterMonitor::onTimerTimeout()
|
void PrinterMonitor::onTimerTimeout()
|
||||||
|
{
|
||||||
|
// Make sure previous poll is complete before starting a new one
|
||||||
|
if ( mPollStatus.isFinished() )
|
||||||
|
{
|
||||||
|
mPollStatus = QtConcurrent::run( &PrinterMonitor::asyncPoll, this );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Asynchronous poll
|
||||||
|
///
|
||||||
|
void PrinterMonitor::asyncPoll()
|
||||||
{
|
{
|
||||||
auto newAvailablePrinters = QPrinterInfo::availablePrinterNames();
|
auto newAvailablePrinters = QPrinterInfo::availablePrinterNames();
|
||||||
if ( newAvailablePrinters != mCurrentAvailablePrinters )
|
if ( newAvailablePrinters != mCurrentAvailablePrinters )
|
||||||
{
|
{
|
||||||
|
QMutexLocker mutex( &mCurrentAvailablePrintersMutex );
|
||||||
|
|
||||||
mCurrentAvailablePrinters = newAvailablePrinters;
|
mCurrentAvailablePrinters = newAvailablePrinters;
|
||||||
|
|
||||||
emit availablePrintersChanged( mCurrentAvailablePrinters );
|
emit availablePrintersChanged( mCurrentAvailablePrinters );
|
||||||
|
|||||||
@@ -22,6 +22,8 @@
|
|||||||
#define PrinterMonitor_h
|
#define PrinterMonitor_h
|
||||||
|
|
||||||
|
|
||||||
|
#include <QFuture>
|
||||||
|
#include <QMutex>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
@@ -53,7 +55,7 @@ namespace glabels
|
|||||||
// Public methods
|
// Public methods
|
||||||
/////////////////////////////////
|
/////////////////////////////////
|
||||||
public:
|
public:
|
||||||
const QStringList& availablePrinters() const;
|
QStringList availablePrinters();
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////
|
/////////////////////////////////
|
||||||
@@ -67,7 +69,14 @@ namespace glabels
|
|||||||
// Signals
|
// Signals
|
||||||
/////////////////////////////////
|
/////////////////////////////////
|
||||||
signals:
|
signals:
|
||||||
void availablePrintersChanged( const QStringList& availablePrinters );
|
void availablePrintersChanged( QStringList availablePrinters );
|
||||||
|
|
||||||
|
|
||||||
|
/////////////////////////////////
|
||||||
|
// Private methods
|
||||||
|
/////////////////////////////////
|
||||||
|
private:
|
||||||
|
void asyncPoll();
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////
|
/////////////////////////////////
|
||||||
@@ -77,8 +86,11 @@ namespace glabels
|
|||||||
static std::unique_ptr<PrinterMonitor> mInstance;
|
static std::unique_ptr<PrinterMonitor> mInstance;
|
||||||
|
|
||||||
std::unique_ptr<QTimer> mTimer;
|
std::unique_ptr<QTimer> mTimer;
|
||||||
|
|
||||||
QStringList mCurrentAvailablePrinters;
|
QStringList mCurrentAvailablePrinters;
|
||||||
|
QMutex mCurrentAvailablePrintersMutex;
|
||||||
|
|
||||||
|
QFuture<void> mPollStatus;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user