author | Oliver Maurhart <oliver.maurhart@ait.ac.at> |
Wed, 29 Oct 2014 15:18:22 +0100 | |
changeset 240 | d7ef04254e9c |
parent 236 | 8bfcfd4dd6ad |
permissions | -rwxr-xr-x |
oliver@101 | 1 |
#!/bin/env python |
oliver@101 | 2 |
# -*- coding: utf-8 -*- |
oliver@101 | 3 |
|
oliver@101 | 4 |
# ------------------------------------------------------------ |
oliver@101 | 5 |
# password_dialog.pyw |
oliver@101 | 6 |
# |
oliver@101 | 7 |
# the user should give us a password |
oliver@101 | 8 |
# |
oliver@101 | 9 |
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at> |
oliver@101 | 10 |
# |
oliver@240 | 11 |
# Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology |
oliver@101 | 12 |
# |
oliver@101 | 13 |
# |
oliver@240 | 14 |
# X-Net Services GmbH |
oliver@240 | 15 |
# Elisabethstrasse 1 |
oliver@240 | 16 |
# 4020 Linz |
oliver@240 | 17 |
# AUSTRIA |
oliver@240 | 18 |
# https://www.x-net.at |
oliver@240 | 19 |
# |
oliver@240 | 20 |
# AIT Austrian Institute of Technology |
oliver@240 | 21 |
# Donau City Strasse 1 |
oliver@240 | 22 |
# 1220 Wien |
oliver@240 | 23 |
# AUSTRIA |
oliver@240 | 24 |
# http://www.ait.ac.at |
oliver@240 | 25 |
# |
oliver@240 | 26 |
# |
oliver@240 | 27 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
oliver@240 | 28 |
# you may not use this file except in compliance with the License. |
oliver@240 | 29 |
# You may obtain a copy of the License at |
oliver@240 | 30 |
# |
oliver@240 | 31 |
# http://www.apache.org/licenses/LICENSE-2.0 |
oliver@240 | 32 |
# |
oliver@240 | 33 |
# Unless required by applicable law or agreed to in writing, software |
oliver@240 | 34 |
# distributed under the License is distributed on an "AS IS" BASIS, |
oliver@240 | 35 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
oliver@240 | 36 |
# See the License for the specific language governing permissions and |
oliver@240 | 37 |
# limitations under the License. |
oliver@101 | 38 |
# ------------------------------------------------------------ |
oliver@101 | 39 |
|
oliver@101 | 40 |
|
oliver@101 | 41 |
# ------------------------------------------------------------ |
oliver@101 | 42 |
# imports |
oliver@101 | 43 |
|
oliver@101 | 44 |
import sys |
oliver@101 | 45 |
|
oliver@232 | 46 |
if sys.platform == 'win32' or sys.platform == 'cygwin': |
oliver@232 | 47 |
import win32api |
oliver@232 | 48 |
import win32con |
oliver@232 | 49 |
import win32gui |
oliver@232 | 50 |
|
oliver@101 | 51 |
from PyQt4 import QtCore |
oliver@101 | 52 |
from PyQt4 import QtGui |
oliver@101 | 53 |
|
oliver@101 | 54 |
from ui_PasswordDialog import Ui_PasswordDialog |
oliver@101 | 55 |
from about_dialog import AboutDialog |
oliver@101 | 56 |
|
oliver@101 | 57 |
|
oliver@101 | 58 |
# ------------------------------------------------------------ |
oliver@101 | 59 |
# code |
oliver@101 | 60 |
|
oliver@101 | 61 |
|
oliver@101 | 62 |
class PasswordDialog(QtGui.QDialog): |
oliver@101 | 63 |
|
oliver@134 | 64 |
"""A dialog for letting the user type in a password""" |
oliver@101 | 65 |
|
oliver@101 | 66 |
def __init__(self, user_text = 'Please provide an approbitate password to proceed:'): |
oliver@101 | 67 |
|
oliver@101 | 68 |
QtGui.QDialog.__init__(self) |
oliver@101 | 69 |
|
oliver@101 | 70 |
# setup the user interface |
oliver@101 | 71 |
self.ui = Ui_PasswordDialog() |
oliver@101 | 72 |
self.ui.setupUi(self) |
oliver@101 | 73 |
|
oliver@101 | 74 |
# local members |
oliver@101 | 75 |
self._about_dialog = AboutDialog() |
oliver@101 | 76 |
|
oliver@101 | 77 |
# connectors |
oliver@101 | 78 |
self.ui.lblText.setText(user_text) |
oliver@101 | 79 |
self.ui.btnAbout.clicked.connect(self.clicked_about) |
oliver@101 | 80 |
self.ui.btnCancel.clicked.connect(self.reject) |
oliver@101 | 81 |
self.ui.btnOk.clicked.connect(self.clicked_ok) |
oliver@101 | 82 |
|
oliver@101 | 83 |
# positionate ourself central |
oliver@101 | 84 |
screen = QtGui.QDesktopWidget().screenGeometry() |
oliver@101 | 85 |
size = self.geometry() |
oliver@101 | 86 |
self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2) |
oliver@232 | 87 |
|
oliver@232 | 88 |
t = QtCore.QTimer(self) |
oliver@232 | 89 |
t.timeout.connect(self.force_foreground) |
oliver@232 | 90 |
t.setInterval(0) |
oliver@232 | 91 |
t.setSingleShot(True) |
oliver@232 | 92 |
t.start() |
oliver@101 | 93 |
|
oliver@101 | 94 |
|
oliver@101 | 95 |
def clicked_about(self): |
oliver@101 | 96 |
|
oliver@101 | 97 |
"""About button has been clicked.""" |
oliver@101 | 98 |
self._about_dialog.show() |
oliver@101 | 99 |
|
oliver@101 | 100 |
|
oliver@101 | 101 |
def clicked_ok(self): |
oliver@101 | 102 |
|
oliver@101 | 103 |
"""Ok button has been clicked.""" |
oliver@101 | 104 |
sys.stdout.write('{ ') |
oliver@134 | 105 |
sys.stdout.write('"password": "') |
oliver@101 | 106 |
sys.stdout.write(self.ui.edtPassword.text()) |
oliver@134 | 107 |
sys.stdout.write('" ') |
oliver@101 | 108 |
sys.stdout.write('}\n') |
oliver@101 | 109 |
self.accept() |
oliver@101 | 110 |
|
oliver@101 | 111 |
|
oliver@232 | 112 |
def force_foreground(self): |
oliver@232 | 113 |
|
oliver@232 | 114 |
"""Force ourselves into foreground""" |
oliver@232 | 115 |
if sys.platform == 'win32' or sys.platform == 'cygwin': |
oliver@232 | 116 |
w = self |
oliver@232 | 117 |
while not w.nativeParentWidget() is None: |
oliver@232 | 118 |
w = w.nativeParentWidget() |
oliver@232 | 119 |
win32gui.BringWindowToTop(int(w.effectiveWinId())) |
oliver@232 | 120 |
win32gui.SetForegroundWindow(int(w.effectiveWinId())) |
oliver@232 | 121 |
|
oliver@232 | 122 |
|
oliver@101 | 123 |
def set_user_text(user_text): |
oliver@101 | 124 |
|
oliver@101 | 125 |
"""Set a text to explain which password we need.""" |
oliver@101 | 126 |
self.ui.lblText.setText(user_text) |
oliver@101 | 127 |
|
oliver@101 | 128 |
|
oliver@101 | 129 |
if __name__ == "__main__": |
oliver@232 | 130 |
if sys.platform == 'win32' or sys.platform == 'cygwin': |
oliver@232 | 131 |
win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE) |
oliver@113 | 132 |
a = QtGui.QApplication(sys.argv) |
oliver@113 | 133 |
d = PasswordDialog() |
oliver@113 | 134 |
d.show() |
oliver@113 | 135 |
sys.exit(a.exec_()) |
oliver@101 | 136 |