now uses the Qt native code for tray icons, the tray menu no longer hangs when the dialog is open, dialog updates as appropriate when tray is manipulated when the dialog is open, added in code for updating the dialog widgets when the backend changes

git-svn-id: svn://svn.code.sf.net/p/qjoypad/code/trunk@112 c05e91a0-76c8-4ec0-b377-ef19ce7cc080
This commit is contained in:
John Toman
2009-08-23 20:45:58 +00:00
committed by virtuoussin13
parent ea2be3b29b
commit 12cd906a46
5 changed files with 82 additions and 19 deletions

View File

@ -13,14 +13,16 @@ LayoutManager::LayoutManager( bool useTrayIcon ) {
//make a tray icon
if (useTrayIcon) {
TrayIcon* Tray = new TrayIcon(QPixmap(ICON24),NAME,Popup,0,"tray");
connect(Tray, SIGNAL( clicked(const QPoint&, int)), this, SLOT( trayClick()));
QSystemTrayIcon *Tray = new QSystemTrayIcon(this);
Tray->setContextMenu(Popup);
Tray->setIcon(QIcon(ICON24));
Tray->show();
connect(Tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayClick(QSystemTrayIcon::ActivationReason)));
}
//or make a floating icon
else {
FloatingIcon* Icon = new FloatingIcon(QPixmap(ICON64),Popup,0,"tray");
connect(Icon, SIGNAL( clicked()), this, SLOT( trayClick()));
connect(Icon, SIGNAL( clicked()), this, SLOT( iconClick()));
connect(Icon, SIGNAL( closed()), qApp, SLOT( quit()));
Icon->show();
}
@ -243,23 +245,24 @@ void LayoutManager::setLayoutName(QString name) {
}
}
void LayoutManager::trayClick() {
void LayoutManager::iconClick() {
//don't show the dialog if there aren't any joystick devices plugged in
if (available.count() == 0) {
error("No joystick devices available","No joystick devices are currently available to configure.\nPlease plug in a gaming device and select\n\"Update Joystick Devices\" from the popup menu.");
return;
}
if(le) {
return;
}
//otherwise, make a new LayoutEdit dialog and show it.
le = new LayoutEdit(this);
le->setLayout(CurrentLayout);
//note, this will cause the menu to hang. You cannot use the menu while the
//dialog is active. I'd rather it not work out that way, but this makes my
//code MUCH simpler for a small inconvenience that shouldn't matter. For
//instance, I don't have to worry about the current joysticks changing
//while there's a dialog and therefore adjusting the dialog to match.
le->exec();
delete le;
le = NULL;
}
void LayoutManager::trayClick(QSystemTrayIcon::ActivationReason reason) {
if(reason == QSystemTrayIcon::Trigger) {
iconClick();
}
}
void LayoutManager::trayMenu(QAction *menuItemAction) {
@ -375,6 +378,12 @@ void LayoutManager::updateJoyDevs() {
fillPopup();
//the actual update process is handled by main.cpp, we just need to signal
//ourselves to do it.
//raise(SIGUSR1);
if(le) {
le->updateJoypadWidgets();
}
DEBUG("done updating joydevs\n");
}
void LayoutManager::leWindowClosed() {
le=NULL;
}

View File

@ -14,6 +14,7 @@
#include <QApplication>
#include <QDialog>
#include <QInputDialog>
#include <QSystemTrayIcon>
#include <poll.h>
//a layout handles several joypads
@ -43,6 +44,7 @@ class LayoutManager : public QObject {
LayoutManager( bool useTrayIcon);
//produces a list of the names of all the available layout.
QStringList getLayoutNames();
void leWindowClosed();
public slots:
//load a layout with a given name
bool load(const QString& name);
@ -64,7 +66,8 @@ class LayoutManager : public QObject {
void remove();
//when the tray icon is clicked
void trayClick();
void iconClick();
void trayClick(QSystemTrayIcon::ActivationReason reason);
//when the user selects an item on the tray's popup menu
void trayMenu(QAction* menuItemAction);
//rebuild the popup menu with the current information

View File

@ -1,9 +1,9 @@
#include "layout_edit.h"
//build the dialog
LayoutEdit::LayoutEdit( LayoutManager* l ) {
LayoutEdit::LayoutEdit( LayoutManager* l ): QWidget(NULL) {
lm = l;
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle( NAME );
setWindowIcon(QPixmap(ICON24));
@ -92,11 +92,12 @@ LayoutEdit::LayoutEdit( LayoutManager* l ) {
connect( quit, SIGNAL( clicked() ), qApp, SLOT(quit()));
h->addWidget(quit);
LMain->addLayout(h);
this->show();
}
void LayoutEdit::setLayout(QString layout) {
//change the text,
CLayouts->setItemText(CLayouts->currentIndex(), layout);
CLayouts->setCurrentIndex(lm->getLayoutNames().indexOf(layout));
//update all the JoyPadWidgets.
for (uint i = 0; i < available.count(); i++) {
((JoyPadWidget*)PadStack->widget(i))->update();
@ -111,6 +112,50 @@ void LayoutEdit::updateLayoutList() {
CLayouts->setCurrentIndex(layouts.indexOf(lm->CurrentLayout));
}
void LayoutEdit::updateJoypadWidgets() {
int indexOfFlashRadio = LMain->indexOf(JoyButtons);
FlashRadioArray *newJoyButtons;
int padcount = available.count();
QString names[padcount];
int i = 0;
do
{
QHashIterator<int, JoyPad*> it( available );
while (it.hasNext())
{
it.next();
names[i] = it.value()->getName();
++i;
}
} while (0);
newJoyButtons = new FlashRadioArray(padcount, names, true, this );
LMain->insertWidget(indexOfFlashRadio, newJoyButtons);
LMain->removeWidget(JoyButtons);
FlashRadioArray* oldJoyButtons = JoyButtons;
JoyButtons = newJoyButtons;
connect( JoyButtons, SIGNAL( changed( int ) ), PadStack, SLOT( setCurrentIndex( int )));
oldJoyButtons->deleteLater();
int numberOfJoypads = PadStack->count();
for(int i = 0; i<numberOfJoypads; i++) {
PadStack->removeWidget(PadStack->widget(0));
}
i = 0;
do
{
QHashIterator<int, JoyPad*> it(available);
while (it.hasNext())
{
it.next();
//add a new JoyPadWidget to the stack
PadStack->insertWidget( i,it.value()->widget(PadStack,i) );
//every time it "flashes", flash the associated tab.
connect( PadStack->widget(i), SIGNAL( flashed( int ) ), JoyButtons, SLOT( flash( int )));
++i;
}
} while (0);
}
void LayoutEdit::windowActivationChange( bool oldActive ) {
if (oldActive) return;
//whenever the window becomes active, release all pressed buttons! This way
@ -124,3 +169,7 @@ void LayoutEdit::windowActivationChange( bool oldActive ) {
}
DEBUG("done releasing!\n");
}
void LayoutEdit::closeEvent(QCloseEvent *event) {
lm->leWindowClosed();
event->accept();
}

View File

@ -15,17 +15,18 @@ class LayoutEdit;
class LayoutManager;
class LayoutEdit : public QDialog {
class LayoutEdit : public QWidget {
public:
LayoutEdit( LayoutManager* l );
//swap to a new layout
void setLayout(QString layout);
//update the list of available layouts
void updateLayoutList();
void updateJoypadWidgets();
protected:
//the layout manager this represents
LayoutManager* lm;
virtual void closeEvent(QCloseEvent *event);
//find out when the window is activated.
virtual void windowActivationChange( bool oldActive );
//parts of the dialog:

View File

@ -66,6 +66,7 @@ int main( int argc, char **argv )
//create a new event loop. This will be captured by the QApplication
//when it gets created
QApplication a( argc, argv );
a.setQuitOnLastWindowClosed(false);
//where to look for settings. If it does not exist, it will be created