Skip to content

Commit

Permalink
replace default show_message by a popup gui, samish appearance, close…
Browse files Browse the repository at this point in the history
…d by user, text copyable.
  • Loading branch information
troubadoour committed Sep 2, 2015
1 parent 8118e5f commit d682e62
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 42 deletions.
80 changes: 38 additions & 42 deletions usr/lib/python2.7/dist-packages/sdwdate_gui/sdwdate_gui.py
Expand Up @@ -7,6 +7,7 @@
from subprocess import check_output, call
import pickle
import os
import signal
import time


Expand Down Expand Up @@ -49,22 +50,6 @@ class Update(QtCore.QObject):
update_tip = QtCore.pyqtSignal()


## Started by left click action.
## Set message_showing for the default time
## the balloon is displayed (10 seconds).
class MessageStatus(QThread):
message_status = QtCore.pyqtSignal(bool)
showing = False

def run(self):
self.showing = True
self.message_status.emit(True)
while self.showing:
time.sleep(10)
self.message_status.emit(False)
self.showing = False


class SdwdateTrayIcon(QtGui.QSystemTrayIcon):

def __init__(self, parent=None):
Expand All @@ -76,17 +61,20 @@ def __init__(self, parent=None):

self.path = '/var/run/sdwdate'
self.status_path = '/var/run/sdwdate/status'
self.popup_path = '/usr/lib/sdwdate-gui/show_message'
self.message = ''
self.pop = ''

self.update = Update(self)
self.update.update_tip.connect(self.update_tip)

self.message_status = MessageStatus(self)
self.message_status.message_status.connect(self.update_message_status)
self.message_showing = False

self.activated.connect(self.show_message)
self.messageClicked.connect(self.message_clicked)
self.activated.connect(self.mouse_event)

self.clicked_once = False
self.pos_x = 0
self.pos_y = 0

if os.path.exists(self.status_path):
## Read status when GUI is loaded.
Expand All @@ -96,38 +84,46 @@ def __init__(self, parent=None):
self.watcher.fileChanged.connect(self.status_changed)
else:
self.setIcon(QtGui.QIcon('/usr/share/icons/oxygen/16x16/status/dialog-error.png'))
msg = ('%s\n' %(self.title) +
'sdwdate not running\n' +
'Try to restart it: Right click -> Restart sdwdate\n' +
'If the icon stays red, please report this bug.')
msg = '''sdwdate is not running.
Try to restart it: Right click -> Restart sdwdate
If the icon stays red, please report this bug.'''
self.message = msg
self.setToolTip(msg)
self.watcher_2 = watcher([self.path])
self.watcher_2.directoryChanged.connect(self.watch_folder)

def show_message(self, reason):
if reason == self.Trigger: # left click
self.message_status.start()
self.showMessage(self.title, self.message)

## The balloon is closed on left click.
## Forbid showing again.
def message_clicked(self):
self.message_status.quit()
self.message_showing = False

## Signal generated in MessageStatus.
def update_message_status(self, is_showing):
self.message_showing = is_showing
def show_message(self):
## Store own positon.
if not self.clicked_once:
self.pos_x = QtGui.QCursor.pos().x() - 50
self.pos_y = QtGui.QCursor.pos().y() - 50
self.clicked_once = True

try:
## Terminate popup if showing.
is_popup_running = ['pgrep', '-f', self.popup_path]
popup_pid = check_output(is_popup_running)
os.kill(int(popup_pid), signal.SIGTERM)
except subprocess.CalledProcessError:
pass
## Show popup.
run_popup = ('%s "%s" %s %s &'
% (self.popup_path, self.message, self.pos_x, self.pos_y))
call(run_popup, shell=True)

def mouse_event(self, reason):
## Left click.
if reason == self.Trigger:
self.show_message()

def update_tip(self):
## Update tooltip if mouse on icon.
if self.geometry().contains(QtGui.QCursor.pos()):
QtGui.QToolTip.showText(QtGui.QCursor.pos(),
'%s\n%s' %(self.title, self.message))
## Update balloon message if it's already shown.
if self.message_showing:
self.showMessage(self.title, self.message)
'%s\n%s' %(self.title, self.message))
## Do not show message on loading.
if self.clicked_once:
self.show_message()

def status_changed(self):
## Prevent race condition.
Expand Down
95 changes: 95 additions & 0 deletions usr/lib/sdwdate-gui/show_message
@@ -0,0 +1,95 @@
#!/usr/bin/env python

import sys
import signal
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtCore import Qt

class TitleBar(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setWindowFlags(Qt.FramelessWindowHint)
self.setStyle(QtGui.QStyleFactory.create('CleanLooks'))
css = """
QWidget
{
Background:lightyellow;
color:black;
font:12px bold;
font-weight:bold;
border-radius: 5px;
height: 11px;
}
QToolButton
{
font:12px
Background:lightyellow;
}
QToolButton:hover
{
font:12px
Background:yellow;
}
"""
self.setStyleSheet(css)

close=QtGui.QToolButton(self);
close.setIcon(QtGui.QIcon('/usr/share/icons/sdwdate-gui/1441238560_close.png'))
close.clicked.connect(self.close);

title = QtGui.QLabel(self);
title.setText("Time Synchronisation Monitor ");

title_layout=QtGui.QHBoxLayout(self);
title_layout.addWidget(title);
title_layout.addWidget(close);

def close(self):
popup.close()


class Frame(QtGui.QFrame):
def __init__(self, parent=None):
QtGui.QFrame.__init__(self, parent)
css = """
QFrame
{
Background:lightyellow;
color:black;
font:12px ;
border-radius: 5px;
}
"""
self.setStyleSheet(css)
self.setWindowFlags(Qt.FramelessWindowHint);
self.m_content= QtGui.QWidget(self);
self.m_titleBar= TitleBar(self);

frame_layout = QtGui.QVBoxLayout(self);
frame_layout.addWidget(self.m_titleBar);
frame_layout.addWidget(self.m_content);
frame_layout.setMargin(5);

# Allows you to access the content area of the frame
# where widgets and layouts can be added
def contentWidget(self):
return self.m_content

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv);
msg = sys.argv[1]
popup = Frame()

content = QtGui.QVBoxLayout(popup.contentWidget());
content.setMargin(0);

text = QtGui.QLabel()
text.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByMouse |
QtCore.Qt.TextSelectableByMouse)
text.setText(msg)

content.addWidget(text)
popup.move(int(sys.argv[2]), int(sys.argv[3]));
popup.show()
app.exec_()
Binary file added usr/share/icons/1441238560_close.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d682e62

Please sign in to comment.