#!/usr/bin/env python
#-*-coding:utf-8-*-
import sys, os, tempfile, subprocess
from PyQt4 import QtGui,QtCore
class Window(QtGui.QWidget):
def __init__(self, parent=None):
self.config_read()
QtGui.QWidget.__init__(self, parent)
self.setWindowTitle('PySnap')
self.resize(428, 274)
self.center()
new_btn = QtGui.QPushButton('New snap')
save_btn = QtGui.QPushButton('Save as...')
self.open_combobox = QtGui.QComboBox()
self.get_apps()
copy_btn = QtGui.QPushButton('Copy')
help_combobox = QtGui.QComboBox()
exit_btn = QtGui.QPushButton('Выход')
area_label = QtGui.QLabel('Snapshot mode:')
area_combobox = QtGui.QComboBox()
delay_label = QtGui.QLabel('Delay')
delay_spin = QtGui.QSpinBox()
self.preview = QtGui.QLabel()
decor_chkbox = QtGui.QCheckBox()
line_hor_1 = QtGui.QFrame(self)
line_hor_1.setFrameShape(QtGui.QFrame.HLine)
line_hor_2 = QtGui.QFrame(self)
line_hor_2.setFrameShape(QtGui.QFrame.HLine)
first_hlayout = QtGui.QHBoxLayout()
first_hlayout.addWidget(area_label)
first_hlayout.addWidget(area_combobox)
second_hlayout = QtGui.QHBoxLayout()
second_hlayout.addWidget(delay_label)
second_hlayout.addWidget(delay_spin)
third_hlayout = QtGui.QHBoxLayout()
third_hlayout.addWidget(help_combobox)
third_hlayout.addWidget(exit_btn)
grid = QtGui.QGridLayout()
grid.addWidget(new_btn, 0, 1)
grid.addWidget(save_btn, 2, 1)
grid.addWidget(self.open_combobox, 3, 1)
grid.addWidget(copy_btn, 4, 1)
grid.addWidget(self.preview, 0, 0, 4, 1)
grid.setColumnMinimumWidth(0, 250)
grid.setColumnMinimumWidth(1, 150)
grid.setColumnStretch(0, 1)
grid.setRowMinimumHeight(1, 20)
grid.setRowStretch(1, 1)
grid.addWidget(line_hor_1, 5, 0, 1, 2)
grid.addLayout(first_hlayout, 6, 0, 1, 2)
grid.addLayout(second_hlayout, 7, 0, 1, 2)
grid.addWidget(decor_chkbox, 8, 0)
grid.addWidget(line_hor_2, 9, 0, 1, 2)
grid.addLayout(third_hlayout, 10, 0, 1, 2)
self.setLayout(grid)
self.connect(new_btn, QtCore.SIGNAL('clicked()'), self.get_win_snap)
self.connect(save_btn, QtCore.SIGNAL('clicked()'), self.save_image)
self.connect(exit_btn, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT('quit()'))
self.connect(self, QtCore.SIGNAL('closeEmitApp()'), QtCore.SLOT('close()'))
self.connect(self.open_combobox, QtCore.SIGNAL('activated(int)'), self.app_exec)
self.get_win_snap()
class WindowIdNotFound(Exception):
pass
def center(self):
"""
Center window position on screen
"""
screen = QtGui.QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width()-size.width())/2,
(screen.height()-size.height())/2-40+260)
def get_win_id(self):
"""
Get Window id by clicking using xwininfo tool
"""
wininfo = subprocess.Popen('xwininfo', shell=True, stdout=subprocess.PIPE).stdout
for line in wininfo:
if line.find('Window id: ') > -1:
return int(line[21:30],16)
raise WindowIdNotFound('Shto za nah...')
def get_win_snap(self):
"""
Get snapshot of window
to-do: preview;
"""
id = self.get_win_id()
self.snap = QtGui.QPixmap.grabWindow(id)
self.preview.setPixmap(self.snap.scaled(250,150,1))
#file_to_save = QtGui.QFileDialog.getSaveFileName(self,'Save snapshot as...')
#if self.snap.save(file_to_save,'PNG'):
# print 'Success detected'
def save_image(self):
"""
Save dialog
to-do: format choose; file filter; autoincrement; last folder; autoupload; user home;
"""
print "save button was pressed"
def get_apps(self, format = 'png'):
"""
Fill 'open' combobox with list of applications
to-do: move text in box to center;
try to make context menu as it was made in ksnapshot;
"""
mime_type = "image/" + format
f = open('/usr/share/applications/mimeinfo.cache', 'r')
for line in f:
if line.find(mime_type) > -1:
break
f.close()
apps = line[len(mime_type)+1:].split(';')
apps = apps[:len(apps)-1]
self.open_combobox.addItem('Open in...')
for app_desktop in apps:
f = open('/usr/share/applications/' + app_desktop.replace('-','/'))
for line in f:
if line.find('Exec=',0) > -1:
break
app = line[5:].split(' ',1)
self.open_combobox.addItem(app[0])
def app_exec(self):
"""
Open current snap (temporary file) in external application
"""
if self.open_combobox.currentIndex > 0:
tmp_dir = tempfile.mkdtemp(dir=tempfile.gettempdir())
tmp_file = "".join((tmp_dir, '/tmpimage.', self.format))
if self.snap.save(tmp_file,self.format):
a = self.open_combobox.currentText()
subprocess.Popen([a+" "+tmp_file], shell=True)
else: print 'G R A B L I!', tmp_file
self.open_combobox.setCurrentIndex(0)
def config_read(self):
"""
to-do:
"""
self.homedir = os.path.expanduser('~')
self.format = 'png'
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close()
app = QtGui.QApplication(sys.argv)
pysnap = Window()
pysnap.show()
sys.exit(app.exec_())