Skip to content

Commit

Permalink
refactor: remove DataCommitter interface from QHotkeyApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
shenlebantongying authored Nov 26, 2024
1 parent f526585 commit a1b3d76
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 82 deletions.
53 changes: 0 additions & 53 deletions src/hotkeywrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,15 @@

//////////////////////////////////////////////////////////////////////////

QHotkeyApplication::QHotkeyApplication( int & argc, char ** argv ):
QtSingleApplication( argc, argv )
{
connect( this,
&QGuiApplication::commitDataRequest,
this,
&QHotkeyApplication::hotkeyAppCommitData,
Qt::DirectConnection );

connect( this,
&QGuiApplication::saveStateRequest,
this,
&QHotkeyApplication::hotkeyAppSaveState,
Qt::DirectConnection );

#if defined( Q_OS_WIN )
installNativeEventFilter( this );
#endif
}

QHotkeyApplication::QHotkeyApplication( QString const & id, int & argc, char ** argv ):
QtSingleApplication( id, argc, argv )
{
connect( this,
&QGuiApplication::commitDataRequest,
this,
&QHotkeyApplication::hotkeyAppCommitData,
Qt::DirectConnection );

connect( this,
&QGuiApplication::saveStateRequest,
this,
&QHotkeyApplication::hotkeyAppSaveState,
Qt::DirectConnection );

#if defined( Q_OS_WIN )
installNativeEventFilter( this );
#endif
}

void QHotkeyApplication::addDataCommiter( DataCommitter & d )
{
dataCommitters.append( &d );
}

void QHotkeyApplication::removeDataCommiter( DataCommitter & d )
{
dataCommitters.removeAll( &d );
}

void QHotkeyApplication::hotkeyAppCommitData( QSessionManager & mgr )
{
for ( int x = 0; x < dataCommitters.size(); ++x ) {
dataCommitters[ x ]->commitData( mgr );
}
}

void QHotkeyApplication::hotkeyAppSaveState( QSessionManager & mgr )
{
mgr.setRestartHint( QSessionManager::RestartNever );
}

#ifdef Q_OS_WIN
void QHotkeyApplication::registerWrapper( HotkeyWrapper * wrapper )
{
Expand Down
18 changes: 0 additions & 18 deletions src/hotkeywrapper.hh
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,6 @@ signals:

//////////////////////////////////////////////////////////////////////////

class DataCommitter
{
public:

virtual void commitData( QSessionManager & ) = 0;
virtual ~DataCommitter() {}
};

class QHotkeyApplication: public QtSingleApplication
#if defined( Q_OS_WIN )
Expand All @@ -184,20 +177,9 @@ class QHotkeyApplication: public QtSingleApplication

friend class HotkeyWrapper;

QList< DataCommitter * > dataCommitters;

public:
QHotkeyApplication( int & argc, char ** argv );
QHotkeyApplication( QString const & id, int & argc, char ** argv );

void addDataCommiter( DataCommitter & );
void removeDataCommiter( DataCommitter & );

private slots:
/// This calls all data committers.
void hotkeyAppCommitData( QSessionManager & );

void hotkeyAppSaveState( QSessionManager & );
#ifdef Q_OS_WIN

protected:
Expand Down
24 changes: 21 additions & 3 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QIcon>
#include <QMessageBox>
#include <QMutex>
#include <QSessionManager>
#include <QString>
#include <QStringBuilder>
#include <QtWebEngineCore/QWebEngineUrlScheme>
Expand Down Expand Up @@ -584,7 +585,26 @@ int main( int argc, char ** argv )

MainWindow m( cfg );

app.addDataCommiter( m );
/// Session manager things.
// Redirect commit data request to Mainwindow's handler.
QObject::connect(
&app,
&QGuiApplication::commitDataRequest,
&m,
[ &m ]( QSessionManager & ) {
m.commitData();
},
Qt::DirectConnection );

// Just don't restart. This probably isn't really needed.
QObject::connect(
&app,
&QGuiApplication::saveStateRequest,
&app,
[]( QSessionManager & mgr ) {
mgr.setRestartHint( QSessionManager::RestartNever );
},
Qt::DirectConnection );

QObject::connect( &app, &QtSingleApplication::messageReceived, &m, &MainWindow::messageFromAnotherInstanceReceived );

Expand All @@ -608,8 +628,6 @@ int main( int argc, char ** argv )
#endif
int r = app.exec();

app.removeDataCommiter( m );

if ( logFilePtr->isOpen() ) {
logFilePtr->close();
}
Expand Down
5 changes: 0 additions & 5 deletions src/ui/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1213,11 +1213,6 @@ void MainWindow::removeGroupComboBoxActionsFromDialog( QDialog * dialog, GroupCo
}
}

void MainWindow::commitData( QSessionManager & )
{
commitData();
}

void MainWindow::commitData()
{
if ( cfg.preferences.clearNetworkCacheOnExit ) {
Expand Down
6 changes: 3 additions & 3 deletions src/ui/mainwindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
using std::string;
using std::vector;

class MainWindow: public QMainWindow, public DataCommitter
class MainWindow: public QMainWindow
{
Q_OBJECT

Expand All @@ -53,7 +53,6 @@ public:
MainWindow( Config::Class & cfg );
~MainWindow();

virtual void commitData( QSessionManager & );

/// Set group for main/popup window
void setGroupByName( QString const & name, bool main_window );
Expand All @@ -68,6 +67,8 @@ public slots:
void showStatusBarMessage( QString const &, int, QPixmap const & );
void wordReceived( QString const & );
void headwordFromFavorites( QString const &, QString const & );
/// Save config and states...
void commitData();
void quitApp();

private:
Expand All @@ -76,7 +77,6 @@ private:
void addGroupComboBoxActionsToDialog( QDialog * dialog, GroupComboBox * pGroupComboBox );
void removeGroupComboBoxActionsFromDialog( QDialog * dialog, GroupComboBox * pGroupComboBox );

void commitData();

QSystemTrayIcon * trayIcon;

Expand Down

0 comments on commit a1b3d76

Please sign in to comment.