hydrus/hydrus/client/gui/ClientGUIFrames.py

84 lines
2.5 KiB
Python
Raw Normal View History

2016-11-02 21:09:14 +00:00
import os
2020-04-22 21:00:35 +00:00
2019-11-14 03:56:30 +00:00
from qtpy import QtWidgets as QW
2020-04-22 21:00:35 +00:00
from hydrus.core import HydrusGlobals as HG
2020-07-29 20:52:44 +00:00
2020-04-22 21:00:35 +00:00
from hydrus.client import ClientConstants as CC
from hydrus.client.gui import ClientGUITopLevelWindows
from hydrus.client.gui import QtPorting as QP
2016-11-02 21:09:14 +00:00
class ShowKeys( ClientGUITopLevelWindows.Frame ):
def __init__( self, key_type, keys ):
2021-04-28 21:43:16 +00:00
if key_type == 'registration': title = 'Registration Tokens'
2016-11-02 21:09:14 +00:00
elif key_type == 'access': title = 'Access Keys'
2020-07-15 20:52:09 +00:00
tlw = HG.client_controller.GetMainTLW()
ClientGUITopLevelWindows.Frame.__init__( self, tlw, HG.client_controller.PrepStringForDisplay( title ) )
2016-11-02 21:09:14 +00:00
self._key_type = key_type
self._keys = keys
#
2019-11-14 03:56:30 +00:00
self._text_ctrl = QW.QPlainTextEdit( self )
self._text_ctrl.setLineWrapMode( QW.QPlainTextEdit.NoWrap )
self._text_ctrl.setReadOnly( True )
2016-11-02 21:09:14 +00:00
2019-11-14 03:56:30 +00:00
self._save_to_file = QW.QPushButton( 'save to file', self )
self._save_to_file.clicked.connect( self.EventSaveToFile )
2016-11-02 21:09:14 +00:00
2019-11-14 03:56:30 +00:00
self._done = QW.QPushButton( 'done', self )
self._done.clicked.connect( self.close )
2016-11-02 21:09:14 +00:00
#
if key_type == 'registration': prepend = 'r'
else: prepend = ''
2019-01-09 22:59:03 +00:00
self._text = os.linesep.join( [ prepend + key.hex() for key in self._keys ] )
2016-11-02 21:09:14 +00:00
2019-11-14 03:56:30 +00:00
self._text_ctrl.setPlainText( self._text )
2016-11-02 21:09:14 +00:00
#
2019-11-14 03:56:30 +00:00
vbox = QP.VBoxLayout()
2016-11-02 21:09:14 +00:00
2019-11-14 03:56:30 +00:00
QP.AddToLayout( vbox, self._text_ctrl, CC.FLAGS_EXPAND_BOTH_WAYS )
2020-07-29 20:52:44 +00:00
QP.AddToLayout( vbox, self._save_to_file, CC.FLAGS_ON_RIGHT )
QP.AddToLayout( vbox, self._done, CC.FLAGS_ON_RIGHT )
2016-11-02 21:09:14 +00:00
2019-11-14 03:56:30 +00:00
self.setLayout( vbox )
2016-11-02 21:09:14 +00:00
2019-12-05 05:29:32 +00:00
size_hint = self.sizeHint()
2016-11-02 21:09:14 +00:00
2019-12-05 05:29:32 +00:00
size_hint.setWidth( max( size_hint.width(), 500 ) )
size_hint.setHeight( max( size_hint.height(), 200 ) )
2016-11-02 21:09:14 +00:00
2019-12-05 05:29:32 +00:00
QP.SetInitialSize( self, size_hint )
2016-11-02 21:09:14 +00:00
2019-11-14 03:56:30 +00:00
self.show()
2016-11-02 21:09:14 +00:00
2019-11-14 03:56:30 +00:00
def EventSaveToFile( self ):
2016-11-02 21:09:14 +00:00
filename = 'keys.txt'
with QP.FileDialog( self, acceptMode = QW.QFileDialog.AcceptSave, fileMode = QW.QFileDialog.AnyFile, default_filename = filename ) as dlg:
2016-11-02 21:09:14 +00:00
2019-11-14 03:56:30 +00:00
if dlg.exec() == QW.QDialog.Accepted:
2016-11-02 21:09:14 +00:00
2019-01-09 22:59:03 +00:00
path = dlg.GetPath()
2016-11-02 21:09:14 +00:00
2019-01-16 22:40:53 +00:00
with open( path, 'w', encoding = 'utf-8' ) as f:
2018-03-28 21:55:58 +00:00
2019-01-09 22:59:03 +00:00
f.write( self._text )
2018-03-28 21:55:58 +00:00
2016-11-02 21:09:14 +00:00
2017-05-10 21:33:58 +00:00