hydrus/include/ClientGUICommon.py

2956 lines
84 KiB
Python
Raw Normal View History

2015-03-18 21:46:29 +00:00
import ClientCaches
import ClientData
2013-02-19 00:11:43 +00:00
import ClientConstants as CC
2016-11-16 20:21:43 +00:00
import ClientGUIMenus
2017-05-24 20:28:24 +00:00
import ClientGUITopLevelWindows
2015-05-13 20:22:39 +00:00
import ClientRatings
2017-03-08 23:23:12 +00:00
import ClientThreading
2017-03-15 20:13:04 +00:00
import HydrusConstants as HC
import HydrusData
import HydrusExceptions
2017-05-10 21:33:58 +00:00
import HydrusGlobals as HG
2013-02-19 00:11:43 +00:00
import os
2013-08-14 20:21:49 +00:00
import sys
2017-02-01 21:11:17 +00:00
import threading
2013-02-19 00:11:43 +00:00
import time
import traceback
import wx
2013-03-15 02:38:12 +00:00
import wx.combo
2013-02-19 00:11:43 +00:00
import wx.richtext
2016-05-18 20:07:14 +00:00
import wx.lib.newevent
2013-02-19 00:11:43 +00:00
ID_TIMER_ANIMATED = wx.NewId()
ID_TIMER_SLIDESHOW = wx.NewId()
ID_TIMER_MEDIA_INFO_DISPLAY = wx.NewId()
2017-04-19 20:58:30 +00:00
def WindowOrAnyTLPChildHasFocus( window ):
2017-04-05 21:16:40 +00:00
focus = wx.Window.FindFocus()
2017-04-19 20:58:30 +00:00
while focus is not None:
2017-04-05 21:16:40 +00:00
2017-04-19 20:58:30 +00:00
if focus == window:
return True
2017-04-05 21:16:40 +00:00
focus = focus.GetParent()
2017-04-19 20:58:30 +00:00
return False
def WindowOrSameTLPChildHasFocus( window ):
focus = wx.Window.FindFocus()
while focus is not None:
2017-04-05 21:16:40 +00:00
if focus == window:
return True
2017-04-19 20:58:30 +00:00
if isinstance( focus, wx.TopLevelWindow ):
return False
focus = focus.GetParent()
2017-04-05 21:16:40 +00:00
return False
2017-04-19 20:58:30 +00:00
def GetFocusTLP():
2017-04-05 21:16:40 +00:00
focus = wx.Window.FindFocus()
2017-04-19 20:58:30 +00:00
return GetTLP( focus )
2017-04-05 21:16:40 +00:00
2017-04-19 20:58:30 +00:00
def GetTLP( window ):
if window is None:
2017-04-05 21:16:40 +00:00
2017-04-19 20:58:30 +00:00
return None
2017-04-05 21:16:40 +00:00
2017-04-19 20:58:30 +00:00
elif isinstance( window, wx.TopLevelWindow ):
2017-04-05 21:16:40 +00:00
2017-04-19 20:58:30 +00:00
return window
2017-04-05 21:16:40 +00:00
else:
2017-04-19 20:58:30 +00:00
return window.GetTopLevelParent()
2017-04-05 21:16:40 +00:00
2017-04-19 20:58:30 +00:00
def TLPHasFocus( window ):
focus_tlp = GetFocusTLP()
window_tlp = GetTLP( window )
2017-04-05 21:16:40 +00:00
return window_tlp == focus_tlp
def WindowHasFocus( window ):
focus = wx.Window.FindFocus()
if focus is None:
return False
return window == focus
def IsWXAncestor( child, ancestor ):
parent = child
while not isinstance( parent, wx.TopLevelWindow ):
if parent == ancestor:
return True
parent = parent.GetParent()
return False
2016-08-31 19:55:14 +00:00
def WrapInGrid( parent, rows, expand_text = False ):
gridbox = wx.FlexGridSizer( 0, 2 )
if expand_text:
gridbox.AddGrowableCol( 0, 1 )
text_flags = CC.FLAGS_VCENTER_EXPAND_DEPTH_ONLY # Trying to expand both ways nixes the center. This seems to work right.
control_flags = CC.FLAGS_VCENTER
2016-09-07 20:01:05 +00:00
sizer_flags = CC.FLAGS_SIZER_VCENTER
2016-08-31 19:55:14 +00:00
else:
gridbox.AddGrowableCol( 1, 1 )
text_flags = CC.FLAGS_VCENTER
control_flags = CC.FLAGS_EXPAND_BOTH_WAYS
2016-09-07 20:01:05 +00:00
sizer_flags = CC.FLAGS_EXPAND_SIZER_BOTH_WAYS
2016-08-31 19:55:14 +00:00
for ( text, control ) in rows:
2016-09-07 20:01:05 +00:00
if isinstance( control, wx.Sizer ):
cflags = sizer_flags
else:
cflags = control_flags
2017-04-19 20:58:30 +00:00
st = BetterStaticText( parent, text )
gridbox.AddF( st, text_flags )
2016-09-07 20:01:05 +00:00
gridbox.AddF( control, cflags )
2016-08-31 19:55:14 +00:00
return gridbox
2016-08-17 20:07:22 +00:00
def WrapInText( control, parent, text ):
hbox = wx.BoxSizer( wx.HORIZONTAL )
2017-04-19 20:58:30 +00:00
st = BetterStaticText( parent, text )
hbox.AddF( st, CC.FLAGS_VCENTER )
2016-08-17 20:07:22 +00:00
hbox.AddF( control, CC.FLAGS_EXPAND_BOTH_WAYS )
return hbox
2013-03-15 02:38:12 +00:00
class AnimatedStaticTextTimestamp( wx.StaticText ):
2013-02-19 00:11:43 +00:00
2013-03-15 02:38:12 +00:00
def __init__( self, parent, prefix, rendering_function, timestamp, suffix ):
2013-02-19 00:11:43 +00:00
2013-03-15 02:38:12 +00:00
self._prefix = prefix
self._rendering_function = rendering_function
self._timestamp = timestamp
self._suffix = suffix
2013-02-19 00:11:43 +00:00
2015-03-25 22:04:19 +00:00
self._last_tick = HydrusData.GetNow()
2013-02-19 00:11:43 +00:00
2013-03-15 02:38:12 +00:00
wx.StaticText.__init__( self, parent, label = self._prefix + self._rendering_function( self._timestamp ) + self._suffix )
2013-02-19 00:11:43 +00:00
2014-05-28 21:03:24 +00:00
self.Bind( wx.EVT_TIMER, self.TIMEREventAnimated, id = ID_TIMER_ANIMATED )
2015-11-18 22:44:07 +00:00
self._timer_animated = wx.Timer( self, ID_TIMER_ANIMATED )
self._timer_animated.Start( 1000, wx.TIMER_CONTINUOUS )
2013-02-19 00:11:43 +00:00
2013-03-15 02:38:12 +00:00
2014-05-28 21:03:24 +00:00
def TIMEREventAnimated( self ):
2013-02-19 00:11:43 +00:00
2015-11-18 22:44:07 +00:00
try:
2013-03-15 02:38:12 +00:00
2015-11-18 22:44:07 +00:00
update = False
2013-03-15 02:38:12 +00:00
2015-11-18 22:44:07 +00:00
now = HydrusData.GetNow()
difference = abs( now - self._timestamp )
if difference < 3600: update = True
elif difference < 3600 * 24 and now - self._last_tick > 60: update = True
elif now - self._last_tick > 3600: update = True
if update:
2016-03-23 19:42:56 +00:00
self.SetLabelText( self._prefix + self._rendering_function( self._timestamp ) + self._suffix )
2015-11-18 22:44:07 +00:00
wx.PostEvent( self.GetEventHandler(), wx.SizeEvent() )
except wx.PyDeadObjectError:
self._timer_animated.Stop()
except:
self._timer_animated.Stop()
raise
2013-03-15 02:38:12 +00:00
2013-02-19 00:11:43 +00:00
2013-03-15 02:38:12 +00:00
2017-01-18 22:52:39 +00:00
class BetterBitmapButton( wx.BitmapButton ):
2017-01-25 22:56:55 +00:00
def __init__( self, parent, bitmap, func, *args, **kwargs ):
2017-01-18 22:52:39 +00:00
wx.BitmapButton.__init__( self, parent, bitmap = bitmap )
2017-01-25 22:56:55 +00:00
self._func = func
2017-01-18 22:52:39 +00:00
self._args = args
self._kwargs = kwargs
self.Bind( wx.EVT_BUTTON, self.EventButton )
def EventButton( self, event ):
2017-01-25 22:56:55 +00:00
self._func( *self._args, **self._kwargs )
2017-01-18 22:52:39 +00:00
2016-10-19 20:02:56 +00:00
class BetterButton( wx.Button ):
2017-01-25 22:56:55 +00:00
def __init__( self, parent, label, func, *args, **kwargs ):
2016-10-19 20:02:56 +00:00
2017-04-19 20:58:30 +00:00
wx.Button.__init__( self, parent, style = wx.BU_EXACTFIT )
self.SetLabelText( label )
2016-10-19 20:02:56 +00:00
2017-01-25 22:56:55 +00:00
self._func = func
2016-12-07 22:12:52 +00:00
self._args = args
self._kwargs = kwargs
2016-10-19 20:02:56 +00:00
self.Bind( wx.EVT_BUTTON, self.EventButton )
def EventButton( self, event ):
2017-01-25 22:56:55 +00:00
self._func( *self._args, **self._kwargs )
2016-10-19 20:02:56 +00:00
2017-05-31 21:50:53 +00:00
class BetterCheckListBox( wx.CheckListBox ):
def GetChecked( self ):
result = [ self.GetClientData( index ) for index in wx.CheckListBox.GetChecked( self ) ]
return result
2016-10-19 20:02:56 +00:00
class BetterChoice( wx.Choice ):
def GetChoice( self ):
selection = self.GetSelection()
if selection != wx.NOT_FOUND: return self.GetClientData( selection )
2016-11-09 23:13:22 +00:00
elif self.GetCount() > 0: return self.GetClientData( 0 )
else: return None
2016-10-19 20:02:56 +00:00
def SelectClientData( self, client_data ):
for i in range( self.GetCount() ):
if client_data == self.GetClientData( i ):
self.Select( i )
return
2016-11-09 23:13:22 +00:00
if self.GetCount() > 0:
self.Select( 0 )
2016-10-19 20:02:56 +00:00
2017-01-04 22:48:23 +00:00
class BetterRadioBox( wx.RadioBox ):
def __init__( self, *args, **kwargs ):
self._indices_to_data = { i : data for ( i, ( s, data ) ) in enumerate( kwargs[ 'choices' ] ) }
kwargs[ 'choices' ] = [ s for ( s, data ) in kwargs[ 'choices' ] ]
wx.RadioBox.__init__( self, *args, **kwargs )
def GetChoice( self ):
index = self.GetSelection()
return self._indices_to_data[ index ]
2017-04-19 20:58:30 +00:00
class BetterStaticText( wx.StaticText ):
2017-04-26 21:58:12 +00:00
def __init__( self, parent, label = None, **kwargs ):
2017-04-19 20:58:30 +00:00
2017-04-26 21:58:12 +00:00
wx.StaticText.__init__( self, parent, **kwargs )
2017-04-19 20:58:30 +00:00
if label is not None:
# to escape mnemonic '&' swallowing
self.SetLabelText( label )
# at some point, rewrite this to be a control that'll produce a custom geteffectiveminsize and use wx.lib.wordwrap to dc draw the text
# st.Wrap is a pain to deal with here, seems to sometimes/always not be able to increase after an initial non-zero call
2017-07-12 20:03:45 +00:00
def SetLabelText( self, text ):
if text != self.GetLabelText():
wx.StaticText.SetLabelText( self, text )
2013-03-15 02:38:12 +00:00
class BufferedWindow( wx.Window ):
def __init__( self, *args, **kwargs ):
wx.Window.__init__( self, *args, **kwargs )
if 'size' in kwargs:
( x, y ) = kwargs[ 'size' ]
self._canvas_bmp = wx.EmptyBitmap( x, y, 24 )
2015-11-18 22:44:07 +00:00
else:
self._canvas_bmp = wx.EmptyBitmap( 20, 20, 24 )
self._dirty = True
2013-03-15 02:38:12 +00:00
self.Bind( wx.EVT_PAINT, self.EventPaint )
self.Bind( wx.EVT_SIZE, self.EventResize )
2015-02-25 19:34:30 +00:00
self.Bind( wx.EVT_ERASE_BACKGROUND, self.EventEraseBackground )
2013-03-15 02:38:12 +00:00
2015-11-18 22:44:07 +00:00
def _Draw( self, dc ):
raise NotImplementedError()
2013-03-15 02:38:12 +00:00
2015-02-25 19:34:30 +00:00
def EventEraseBackground( self, event ): pass
2015-11-18 22:44:07 +00:00
def EventPaint( self, event ):
dc = wx.BufferedPaintDC( self, self._canvas_bmp )
if self._dirty:
self._Draw( dc )
2013-03-15 02:38:12 +00:00
def EventResize( self, event ):
( my_width, my_height ) = self.GetClientSize()
( current_bmp_width, current_bmp_height ) = self._canvas_bmp.GetSize()
2015-11-18 22:44:07 +00:00
if my_width != current_bmp_width or my_height != current_bmp_height:
self._canvas_bmp = wx.EmptyBitmap( my_width, my_height, 24 )
self._dirty = True
2015-12-02 22:32:18 +00:00
self.Refresh()
2013-03-15 02:38:12 +00:00
2015-05-13 20:22:39 +00:00
class BufferedWindowIcon( BufferedWindow ):
def __init__( self, parent, bmp ):
BufferedWindow.__init__( self, parent, size = bmp.GetSize() )
self._bmp = bmp
2015-11-18 22:44:07 +00:00
def _Draw( self, dc ):
2015-05-13 20:22:39 +00:00
background_colour = self.GetParent().GetBackgroundColour()
dc.SetBackground( wx.Brush( background_colour ) )
dc.Clear()
2015-11-18 22:44:07 +00:00
dc.DrawBitmap( self._bmp, 0, 0 )
self._dirty = False
2015-05-13 20:22:39 +00:00
2013-03-15 02:38:12 +00:00
class CheckboxCollect( wx.combo.ComboCtrl ):
2013-04-03 20:56:07 +00:00
def __init__( self, parent, page_key = None ):
2013-03-15 02:38:12 +00:00
wx.combo.ComboCtrl.__init__( self, parent, style = wx.CB_READONLY )
self._page_key = page_key
2017-05-24 20:28:24 +00:00
popup = self._Popup()
2013-03-15 02:38:12 +00:00
#self.UseAltPopupWindow( True )
self.SetPopupControl( popup )
2017-05-24 20:28:24 +00:00
def GetChoice( self ):
2013-12-04 22:44:16 +00:00
2017-05-24 20:28:24 +00:00
return self._collect_by
2013-12-04 22:44:16 +00:00
2013-03-15 02:38:12 +00:00
2017-05-24 20:28:24 +00:00
def SetCollectTypes( self, collect_by, description ):
2013-03-15 02:38:12 +00:00
2017-05-24 20:28:24 +00:00
self._collect_by = collect_by
self.SetValue( description )
2013-03-15 02:38:12 +00:00
2017-05-10 21:33:58 +00:00
HG.client_controller.pub( 'collect_media', self._page_key, self._collect_by )
2013-03-15 02:38:12 +00:00
2013-03-23 17:57:29 +00:00
class _Popup( wx.combo.ComboPopup ):
2013-03-15 02:38:12 +00:00
2017-05-24 20:28:24 +00:00
def __init__( self ):
2013-03-15 02:38:12 +00:00
2013-03-23 17:57:29 +00:00
wx.combo.ComboPopup.__init__( self )
2013-03-15 02:38:12 +00:00
2013-03-23 17:57:29 +00:00
def Create( self, parent ):
2017-05-24 20:28:24 +00:00
self._control = self._Control( parent, self.GetCombo() )
2013-03-23 17:57:29 +00:00
return True
2013-03-15 02:38:12 +00:00
2013-03-23 17:57:29 +00:00
def GetAdjustedSize( self, preferred_width, preferred_height, max_height ):
return( ( preferred_width, -1 ) )
2013-03-15 02:38:12 +00:00
2017-05-24 20:28:24 +00:00
def GetControl( self ):
return self._control
2013-03-15 02:38:12 +00:00
2013-03-23 17:57:29 +00:00
class _Control( wx.CheckListBox ):
2017-05-24 20:28:24 +00:00
def __init__( self, parent, special_parent ):
text_and_data_tuples = set()
sort_by = HC.options[ 'sort_by' ]
for ( sort_by_type, namespaces ) in sort_by:
text_and_data_tuples.update( namespaces )
text_and_data_tuples = list( [ ( namespace, ( 'namespace', namespace ) ) for namespace in text_and_data_tuples ] )
text_and_data_tuples.sort()
2017-06-28 20:23:21 +00:00
ratings_services = HG.client_controller.services_manager.GetServices( ( HC.LOCAL_RATING_LIKE, HC.LOCAL_RATING_NUMERICAL ) )
2013-04-03 20:56:07 +00:00
2017-05-24 20:28:24 +00:00
for ratings_service in ratings_services:
text_and_data_tuples.append( ( ratings_service.GetName(), ( 'rating', ratings_service.GetServiceKey() ) ) )
texts = [ text for ( text, data ) in text_and_data_tuples ] # we do this so it sizes its height properly on init
2013-04-03 20:56:07 +00:00
wx.CheckListBox.__init__( self, parent, choices = texts )
2013-03-23 17:57:29 +00:00
2013-04-03 20:56:07 +00:00
self.Clear()
2017-05-24 20:28:24 +00:00
for ( text, data ) in text_and_data_tuples:
self.Append( text, data )
2013-03-23 17:57:29 +00:00
self._special_parent = special_parent
2013-08-14 20:21:49 +00:00
default = HC.options[ 'default_collect' ]
2013-03-23 17:57:29 +00:00
2017-05-24 20:28:24 +00:00
self.SetValue( default )
self.Bind( wx.EVT_CHECKLISTBOX, self.EventChanged )
self.Bind( wx.EVT_LEFT_DOWN, self.EventLeftDown )
def _BroadcastCollect( self ):
( collect_by, description ) = self._GetValues()
self._special_parent.SetCollectTypes( collect_by, description )
def _GetValues( self ):
collect_by = []
for index in self.GetChecked():
2013-09-04 16:48:44 +00:00
2017-05-24 20:28:24 +00:00
collect_by.append( self.GetClientData( index ) )
2013-09-04 16:48:44 +00:00
2017-05-24 20:28:24 +00:00
collect_by_strings = self.GetCheckedStrings()
if len( collect_by ) > 0:
2013-09-04 16:48:44 +00:00
2017-05-24 20:28:24 +00:00
description = 'collect by ' + '-'.join( collect_by_strings )
else:
description = 'no collections'
2013-09-04 16:48:44 +00:00
2013-03-23 17:57:29 +00:00
2017-05-24 20:28:24 +00:00
return ( collect_by, description )
2013-03-23 17:57:29 +00:00
# as inspired by http://trac.wxwidgets.org/attachment/ticket/14413/test_clb_workaround.py
# what a clusterfuck
def EventLeftDown( self, event ):
index = self.HitTest( event.GetPosition() )
if index != wx.NOT_FOUND:
self.Check( index, not self.IsChecked( index ) )
self.EventChanged( event )
event.Skip()
def EventChanged( self, event ):
2017-05-24 20:28:24 +00:00
self._BroadcastCollect()
2013-12-04 22:44:16 +00:00
2017-05-24 20:28:24 +00:00
def SetValue( self, collect_by ):
2013-12-04 22:44:16 +00:00
2017-05-24 20:28:24 +00:00
# an old possible value, now collapsed to []
if collect_by is None:
collect_by = []
desired_collect_by_rows = set( collect_by )
2013-04-03 20:56:07 +00:00
2017-05-24 20:28:24 +00:00
indices_to_check = []
2013-04-03 20:56:07 +00:00
2017-05-24 20:28:24 +00:00
for index in range( self.GetCount() ):
if self.GetClientData( index ) in desired_collect_by_rows:
indices_to_check.append( index )
2013-03-23 17:57:29 +00:00
2017-05-24 20:28:24 +00:00
self.SetChecked( indices_to_check )
self._BroadcastCollect()
2013-03-23 17:57:29 +00:00
2013-03-15 02:38:12 +00:00
class ChoiceSort( BetterChoice ):
2016-07-27 21:53:34 +00:00
def __init__( self, parent, page_key = None, add_namespaces_and_ratings = True ):
2013-03-15 02:38:12 +00:00
BetterChoice.__init__( self, parent )
self._page_key = page_key
2017-06-28 20:23:21 +00:00
services_manager = HG.client_controller.services_manager
2017-05-24 20:28:24 +00:00
2016-08-03 22:15:54 +00:00
sort_choices = ClientData.GetSortChoices( add_namespaces_and_ratings = add_namespaces_and_ratings )
2013-03-15 02:38:12 +00:00
2017-05-24 20:28:24 +00:00
for sort_by in sort_choices:
( sort_by_type, sort_by_data ) = sort_by
2013-03-15 02:38:12 +00:00
2017-05-24 20:28:24 +00:00
if sort_by_type == 'system':
label = CC.sort_string_lookup[ sort_by_data ]
elif sort_by_type == 'namespaces':
2014-08-27 22:15:22 +00:00
2017-05-24 20:28:24 +00:00
label = '-'.join( sort_by_data )
2014-08-27 22:15:22 +00:00
2017-05-24 20:28:24 +00:00
elif sort_by_type in ( 'rating_descend', 'rating_ascend' ):
2014-08-27 22:15:22 +00:00
2017-05-24 20:28:24 +00:00
service_key = sort_by_data
2014-08-27 22:15:22 +00:00
2017-05-24 20:28:24 +00:00
service = services_manager.GetService( service_key )
2014-08-27 22:15:22 +00:00
2017-05-24 20:28:24 +00:00
if sort_by_type == 'rating_descend':
label = service.GetName() + ' rating highest first'
elif sort_by_type == 'rating_ascend':
label = service.GetName() + ' rating lowest first'
2014-08-27 22:15:22 +00:00
2013-03-15 02:38:12 +00:00
2017-05-24 20:28:24 +00:00
self.Append( 'sort by ' + label, sort_by )
2013-03-15 02:38:12 +00:00
self.Bind( wx.EVT_CHOICE, self.EventChoice )
2017-05-10 21:33:58 +00:00
HG.client_controller.sub( self, 'ACollectHappened', 'collect_media' )
2013-03-15 02:38:12 +00:00
def _BroadcastSort( self ):
selection = self.GetSelection()
if selection != wx.NOT_FOUND:
sort_by = self.GetClientData( selection )
2017-05-10 21:33:58 +00:00
HG.client_controller.pub( 'sort_media', self._page_key, sort_by )
2013-03-15 02:38:12 +00:00
def ACollectHappened( self, page_key, collect_by ):
2017-05-24 20:28:24 +00:00
if page_key == self._page_key:
self._BroadcastSort()
def BroadcastSort( self ):
self._BroadcastSort()
2013-03-15 02:38:12 +00:00
def EventChoice( self, event ):
2017-05-24 20:28:24 +00:00
if self._page_key is not None:
self._BroadcastSort()
2013-03-15 02:38:12 +00:00
2014-02-26 22:09:54 +00:00
class ExportPatternButton( wx.Button ):
ID_HASH = 0
ID_TAGS = 1
ID_NN_TAGS = 2
ID_NAMESPACE = 3
ID_TAG = 4
def __init__( self, parent ):
wx.Button.__init__( self, parent, label = 'pattern shortcuts' )
self.Bind( wx.EVT_BUTTON, self.EventButton )
self.Bind( wx.EVT_MENU, self.EventMenu )
def EventMenu( self, event ):
id = event.GetId()
phrase = None
2014-03-12 22:08:23 +00:00
if id == self.ID_HASH: phrase = '{hash}'
if id == self.ID_TAGS: phrase = '{tags}'
if id == self.ID_NN_TAGS: phrase = '{nn tags}'
2017-03-08 23:23:12 +00:00
if id == self.ID_NAMESPACE: phrase = u'[\u2026]'
if id == self.ID_TAG: phrase = u'(\u2026)'
2014-02-26 22:09:54 +00:00
else: event.Skip()
2017-05-10 21:33:58 +00:00
if phrase is not None: HG.client_controller.pub( 'clipboard', 'text', phrase )
2014-02-26 22:09:54 +00:00
def EventButton( self, event ):
menu = wx.Menu()
menu.Append( -1, 'click on a phrase to copy to clipboard' )
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendSeparator( menu )
2014-02-26 22:09:54 +00:00
2014-03-12 22:08:23 +00:00
menu.Append( self.ID_HASH, 'the file\'s hash - {hash}' )
menu.Append( self.ID_TAGS, 'all the file\'s tags - {tags}' )
menu.Append( self.ID_NN_TAGS, 'all the file\'s non-namespaced tags - {nn tags}' )
2014-02-26 22:09:54 +00:00
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendSeparator( menu )
2014-02-26 22:09:54 +00:00
2017-03-08 23:23:12 +00:00
menu.Append( self.ID_NAMESPACE, u'all instances of a particular namespace - [\u2026]' )
2014-02-26 22:09:54 +00:00
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendSeparator( menu )
2014-02-26 22:09:54 +00:00
2017-03-08 23:23:12 +00:00
menu.Append( self.ID_TAG, u'a particular tag, if the file has it - (\u2026)' )
2014-02-26 22:09:54 +00:00
2017-05-10 21:33:58 +00:00
HG.client_controller.PopupMenu( self, menu )
2014-02-26 22:09:54 +00:00
2014-04-16 20:31:59 +00:00
class FitResistantStaticText( wx.StaticText ):
2014-04-23 20:56:12 +00:00
# this is a huge damn mess! I think I really need to be doing this inside or before the parent's fit, or something
def __init__( self, *args, **kwargs ):
wx.StaticText.__init__( self, *args, **kwargs )
self._wrap = 380
if 'label' in kwargs: self._last_label = kwargs[ 'label' ]
else: self._last_label = ''
2014-04-16 20:31:59 +00:00
def Wrap( self, width ):
2014-04-23 20:56:12 +00:00
self._wrap = width
wx.StaticText.Wrap( self, self._wrap )
2014-04-16 20:31:59 +00:00
( x, y ) = self.GetSize()
2014-04-23 20:56:12 +00:00
if x > self._wrap: x = self._wrap
if x < 150: x = 150
2014-04-16 20:31:59 +00:00
self.SetMinSize( ( x, y ) )
2014-04-23 20:56:12 +00:00
self.SetMaxSize( ( self._wrap, -1 ) )
2016-03-23 19:42:56 +00:00
def SetLabelText( self, label ):
2014-04-23 20:56:12 +00:00
if label != self._last_label:
self._last_label = label
2016-03-23 19:42:56 +00:00
wx.StaticText.SetLabelText( self, label )
2014-04-23 20:56:12 +00:00
self.Wrap( self._wrap )
2014-04-16 20:31:59 +00:00
2013-03-15 02:38:12 +00:00
class Gauge( wx.Gauge ):
def __init__( self, *args, **kwargs ):
wx.Gauge.__init__( self, *args, **kwargs )
2017-07-12 20:03:45 +00:00
self._actual_range = None
self._is_pulsing = False
2013-03-15 02:38:12 +00:00
2017-07-12 20:03:45 +00:00
def SetRange( self, range ):
2013-03-15 02:38:12 +00:00
2017-07-12 20:03:45 +00:00
if range is None:
2017-01-25 22:56:55 +00:00
self.Pulse()
2017-07-12 20:03:45 +00:00
self._is_pulsing = True
2013-03-15 02:38:12 +00:00
else:
if self._is_pulsing:
self.StopPulsing()
2017-07-12 20:03:45 +00:00
if range > 1000:
self._actual_range = range
range = 1000
else:
self._actual_range = None
if range != self.GetRange():
wx.Gauge.SetRange( self, range )
2013-03-15 02:38:12 +00:00
def SetValue( self, value ):
if not self._is_pulsing:
2017-01-25 22:56:55 +00:00
if value is None:
2017-07-12 20:03:45 +00:00
self.Pulse()
2017-07-12 20:03:45 +00:00
self._is_pulsing = True
else:
if self._actual_range is not None:
value = min( int( 1000 * ( float( value ) / self._actual_range ) ), 1000 )
2017-07-12 20:03:45 +00:00
if value != self.GetValue():
wx.Gauge.SetValue( self, value )
2017-07-12 20:03:45 +00:00
2017-01-25 22:56:55 +00:00
2013-03-15 02:38:12 +00:00
def StopPulsing( self ):
self._is_pulsing = False
self.SetRange( 1 )
self.SetValue( 1 )
self.SetValue( 0 )
2013-03-15 02:38:12 +00:00
class ListBook( wx.Panel ):
def __init__( self, *args, **kwargs ):
wx.Panel.__init__( self, *args, **kwargs )
2016-08-10 19:04:08 +00:00
self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_FRAMEBK ) )
2013-03-15 02:38:12 +00:00
2016-05-18 20:07:14 +00:00
self._keys_to_active_pages = {}
self._keys_to_proto_pages = {}
2015-05-06 20:26:18 +00:00
2016-05-25 21:54:03 +00:00
# Don't use LB_SORT! Linux can't handle clientdata that jumps around!
self._list_box = wx.ListBox( self, style = wx.LB_SINGLE )
2013-03-15 02:38:12 +00:00
self._empty_panel = wx.Panel( self )
2016-08-10 19:04:08 +00:00
self._empty_panel.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_FRAMEBK ) )
2013-03-15 02:38:12 +00:00
2016-05-18 20:07:14 +00:00
self._current_key = None
2013-03-15 02:38:12 +00:00
self._current_panel = self._empty_panel
self._panel_sizer = wx.BoxSizer( wx.VERTICAL )
2015-03-18 21:46:29 +00:00
self._panel_sizer.AddF( self._empty_panel, CC.FLAGS_EXPAND_BOTH_WAYS )
2013-03-15 02:38:12 +00:00
hbox = wx.BoxSizer( wx.HORIZONTAL )
2015-03-18 21:46:29 +00:00
hbox.AddF( self._list_box, CC.FLAGS_EXPAND_PERPENDICULAR )
hbox.AddF( self._panel_sizer, CC.FLAGS_EXPAND_SIZER_BOTH_WAYS )
2013-03-15 02:38:12 +00:00
self._list_box.Bind( wx.EVT_LISTBOX, self.EventSelection )
self.SetSizer( hbox )
self.Bind( wx.EVT_MENU, self.EventMenu )
2016-05-18 20:07:14 +00:00
def _ActivatePage( self, key ):
( classname, args, kwargs ) = self._keys_to_proto_pages[ key ]
page = classname( *args, **kwargs )
page.Hide()
self._panel_sizer.AddF( page, CC.FLAGS_EXPAND_SIZER_BOTH_WAYS )
self._keys_to_active_pages[ key ] = page
del self._keys_to_proto_pages[ key ]
2016-07-06 21:13:15 +00:00
self._panel_sizer.CalcMin()
2016-05-18 20:07:14 +00:00
self._RecalcListBoxWidth()
2013-12-18 22:49:24 +00:00
2016-05-18 20:07:14 +00:00
def _GetIndex( self, key ):
for i in range( self._list_box.GetCount() ):
2013-12-18 22:49:24 +00:00
2016-05-18 20:07:14 +00:00
i_key = self._list_box.GetClientData( i )
if i_key == key:
2013-12-18 22:49:24 +00:00
2016-05-18 20:07:14 +00:00
return i
2013-12-18 22:49:24 +00:00
2016-05-18 20:07:14 +00:00
return wx.NOT_FOUND
2013-12-18 22:49:24 +00:00
2016-07-06 21:13:15 +00:00
def _RecalcListBoxWidth( self ):
self.Layout()
2013-03-15 02:38:12 +00:00
def _Select( self, selection ):
2016-05-18 20:07:14 +00:00
if selection == wx.NOT_FOUND:
self._current_key = None
else:
self._current_key = self._list_box.GetClientData( selection )
2013-03-23 17:57:29 +00:00
self._current_panel.Hide()
self._list_box.SetSelection( selection )
2016-05-18 20:07:14 +00:00
if selection == wx.NOT_FOUND:
self._current_panel = self._empty_panel
2013-03-23 17:57:29 +00:00
else:
2013-03-15 02:38:12 +00:00
2016-05-18 20:07:14 +00:00
if self._current_key in self._keys_to_proto_pages:
2013-03-23 17:57:29 +00:00
2016-05-18 20:07:14 +00:00
self._ActivatePage( self._current_key )
2013-03-15 02:38:12 +00:00
2016-05-18 20:07:14 +00:00
self._current_panel = self._keys_to_active_pages[ self._current_key ]
2013-03-23 17:57:29 +00:00
2013-03-15 02:38:12 +00:00
self._current_panel.Show()
self.Layout()
2013-09-11 21:28:19 +00:00
self.Refresh()
2016-11-30 20:24:17 +00:00
# this tells any parent scrolled panel to update its virtualsize and recalc its scrollbars
event = wx.NotifyEvent( wx.wxEVT_SIZE, self.GetId() )
2016-07-06 21:13:15 +00:00
wx.CallAfter( self.ProcessEvent, event )
2016-11-30 20:24:17 +00:00
# now the virtualsize is updated, we now tell any parent resizing frame/dialog that is interested in resizing that now is the time
2017-05-24 20:28:24 +00:00
ClientGUITopLevelWindows.PostSizeChangedEvent( self )
2016-07-06 21:13:15 +00:00
2013-03-15 02:38:12 +00:00
event = wx.NotifyEvent( wx.wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, -1 )
2016-07-06 21:13:15 +00:00
wx.CallAfter( self.ProcessEvent, event )
2013-03-15 02:38:12 +00:00
2016-05-18 20:07:14 +00:00
def AddPage( self, display_name, key, page, select = False ):
if self._GetIndex( key ) != wx.NOT_FOUND:
raise HydrusExceptions.NameException( 'That entry already exists!' )
2013-03-15 02:38:12 +00:00
2016-01-13 22:08:19 +00:00
if not isinstance( page, tuple ):
2013-03-15 02:38:12 +00:00
page.Hide()
2015-03-18 21:46:29 +00:00
self._panel_sizer.AddF( page, CC.FLAGS_EXPAND_SIZER_BOTH_WAYS )
2013-03-15 02:38:12 +00:00
2013-02-19 00:11:43 +00:00
2016-05-25 21:54:03 +00:00
# Can't do LB_SORT because of Linux not being able to track clientdata, have to do it manually.
current_display_names = self._list_box.GetStrings()
insertion_index = len( current_display_names )
for ( i, current_display_name ) in enumerate( current_display_names ):
2017-02-01 21:11:17 +00:00
if current_display_name > display_name:
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
insertion_index = i
2016-02-17 22:06:47 +00:00
2017-02-01 21:11:17 +00:00
break
2016-01-06 21:17:20 +00:00
2014-08-13 22:18:12 +00:00
2017-02-01 21:11:17 +00:00
self._list_box.Insert( display_name, insertion_index, key )
self._keys_to_active_pages[ key ] = page
self._RecalcListBoxWidth()
if self._list_box.GetCount() == 1:
2016-04-27 19:20:37 +00:00
2017-02-01 21:11:17 +00:00
self._Select( 0 )
elif select:
index = self._GetIndex( key )
self._Select( index )
2016-04-27 19:20:37 +00:00
2014-02-26 22:09:54 +00:00
2017-02-01 21:11:17 +00:00
def AddPageArgs( self, display_name, key, classname, args, kwargs ):
2013-07-24 20:26:00 +00:00
2017-02-01 21:11:17 +00:00
if self._GetIndex( key ) != wx.NOT_FOUND:
raise HydrusExceptions.NameException( 'That entry already exists!' )
2013-08-14 20:21:49 +00:00
2017-02-01 21:11:17 +00:00
# Can't do LB_SORT because of Linux not being able to track clientdata, have to do it manually.
2013-08-14 20:21:49 +00:00
2017-02-01 21:11:17 +00:00
current_display_names = self._list_box.GetStrings()
2013-08-14 20:21:49 +00:00
2017-02-01 21:11:17 +00:00
insertion_index = len( current_display_names )
for ( i, current_display_name ) in enumerate( current_display_names ):
if current_display_name > display_name:
insertion_index = i
break
2013-07-24 20:26:00 +00:00
2017-02-01 21:11:17 +00:00
self._list_box.Insert( display_name, insertion_index, key )
2013-07-24 20:26:00 +00:00
2017-02-01 21:11:17 +00:00
self._keys_to_proto_pages[ key ] = ( classname, args, kwargs )
2014-02-26 22:09:54 +00:00
2017-02-01 21:11:17 +00:00
self._RecalcListBoxWidth()
2014-02-26 22:09:54 +00:00
2017-02-01 21:11:17 +00:00
if self._list_box.GetCount() == 1:
self._Select( 0 )
2014-02-26 22:09:54 +00:00
2013-07-31 21:26:38 +00:00
2017-02-01 21:11:17 +00:00
def DeleteAllPages( self ):
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
self._panel_sizer.Detach( self._empty_panel )
2016-03-16 22:19:14 +00:00
2017-02-01 21:11:17 +00:00
self._panel_sizer.Clear( deleteWindows = True )
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
self._panel_sizer.AddF( self._empty_panel, CC.FLAGS_EXPAND_SIZER_BOTH_WAYS )
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
self._current_key = None
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
self._current_panel = self._empty_panel
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
self._keys_to_active_pages = {}
self._keys_to_proto_pages = {}
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
self._list_box.Clear()
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
def DeleteCurrentPage( self ):
2013-07-31 21:26:38 +00:00
2017-02-01 21:11:17 +00:00
selection = self._list_box.GetSelection()
2013-08-14 20:21:49 +00:00
2017-02-01 21:11:17 +00:00
if selection != wx.NOT_FOUND:
2013-07-31 21:26:38 +00:00
2017-02-01 21:11:17 +00:00
key_to_delete = self._current_key
page_to_delete = self._current_panel
2013-07-31 21:26:38 +00:00
2017-02-01 21:11:17 +00:00
next_selection = selection + 1
previous_selection = selection - 1
2014-12-10 22:02:39 +00:00
2017-02-01 21:11:17 +00:00
if next_selection < self._list_box.GetCount():
self._Select( next_selection )
elif previous_selection >= 0:
self._Select( previous_selection )
else:
self._Select( wx.NOT_FOUND )
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
self._panel_sizer.Detach( page_to_delete )
2013-07-31 21:26:38 +00:00
2017-02-01 21:11:17 +00:00
page_to_delete.Destroy()
2013-07-31 21:26:38 +00:00
2017-02-01 21:11:17 +00:00
del self._keys_to_active_pages[ key_to_delete ]
2015-02-03 20:40:21 +00:00
2017-02-01 21:11:17 +00:00
self._list_box.Delete( selection )
2015-02-03 20:40:21 +00:00
2017-02-01 21:11:17 +00:00
self._RecalcListBoxWidth()
2015-02-03 20:40:21 +00:00
2017-02-01 21:11:17 +00:00
def EventMenu( self, event ):
action = ClientCaches.MENU_EVENT_ID_TO_ACTION_CACHE.GetAction( event.GetId() )
if action is not None:
2015-02-03 20:40:21 +00:00
2017-02-01 21:11:17 +00:00
( command, data ) = action
2013-07-31 21:26:38 +00:00
2017-02-01 21:11:17 +00:00
if command == 'select_down': self.SelectDown()
elif command == 'select_up': self.SelectUp()
else: event.Skip()
def EventSelection( self, event ):
if self._list_box.GetSelection() != self._GetIndex( self._current_key ):
event = wx.NotifyEvent( wx.wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, -1 )
self.GetEventHandler().ProcessEvent( event )
if event.IsAllowed():
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
self._Select( self._list_box.GetSelection() )
2016-01-06 21:17:20 +00:00
else:
2017-02-01 21:11:17 +00:00
self._list_box.SetSelection( self._GetIndex( self._current_key ) )
2016-01-06 21:17:20 +00:00
2013-08-14 20:21:49 +00:00
2013-07-24 20:26:00 +00:00
2017-02-01 21:11:17 +00:00
def GetCurrentKey( self ):
2016-07-13 17:37:44 +00:00
2017-02-01 21:11:17 +00:00
return self._current_key
2016-07-13 17:37:44 +00:00
2017-02-01 21:11:17 +00:00
def GetCurrentPage( self ):
2015-10-28 21:29:05 +00:00
2017-02-01 21:11:17 +00:00
if self._current_panel == self._empty_panel:
return None
else:
return self._current_panel
2015-10-28 21:29:05 +00:00
2017-02-01 21:11:17 +00:00
def GetActivePages( self ):
2015-10-28 21:29:05 +00:00
2017-02-01 21:11:17 +00:00
return self._keys_to_active_pages.values()
2016-12-07 22:12:52 +00:00
2017-02-01 21:11:17 +00:00
def GetPage( self, key ):
2016-12-07 22:12:52 +00:00
2017-02-01 21:11:17 +00:00
if key in self._keys_to_proto_pages:
self._ActivatePage( key )
2015-10-28 21:29:05 +00:00
2017-02-01 21:11:17 +00:00
if key in self._keys_to_active_pages:
return self._keys_to_active_pages[ key ]
2013-07-24 20:26:00 +00:00
2017-02-01 21:11:17 +00:00
raise Exception( 'That page not found!' )
2013-08-14 20:21:49 +00:00
2017-02-01 21:11:17 +00:00
2017-03-22 22:38:15 +00:00
def GetPageCount( self ):
return len( self._keys_to_active_pages ) + len( self._keys_to_proto_pages )
2017-02-01 21:11:17 +00:00
def KeyExists( self, key ):
2015-02-03 20:40:21 +00:00
2017-02-01 21:11:17 +00:00
return key in self._keys_to_active_pages or key in self._keys_to_proto_pages
2014-10-29 21:39:01 +00:00
2017-02-01 21:11:17 +00:00
def RenamePage( self, key, new_name ):
2013-07-24 20:26:00 +00:00
2017-02-01 21:11:17 +00:00
index = self._GetIndex( key )
2015-02-03 20:40:21 +00:00
2017-02-01 21:11:17 +00:00
if index != wx.NOT_FOUND:
2015-10-28 21:29:05 +00:00
2017-02-01 21:11:17 +00:00
self._list_box.SetString( index, new_name )
2015-10-28 21:29:05 +00:00
2013-07-24 20:26:00 +00:00
2017-02-01 21:11:17 +00:00
self._RecalcListBoxWidth()
2013-07-24 20:26:00 +00:00
2017-02-01 21:11:17 +00:00
def Select( self, key ):
2015-12-09 23:16:41 +00:00
2017-02-01 21:11:17 +00:00
index = self._GetIndex( key )
2015-12-09 23:16:41 +00:00
2017-02-01 21:11:17 +00:00
if index != wx.NOT_FOUND and index != self._list_box.GetSelection():
2015-12-09 23:16:41 +00:00
2017-02-01 21:11:17 +00:00
event = wx.NotifyEvent( wx.wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, -1 )
2015-12-09 23:16:41 +00:00
2017-02-01 21:11:17 +00:00
self.GetEventHandler().ProcessEvent( event )
2015-12-09 23:16:41 +00:00
2017-02-01 21:11:17 +00:00
if event.IsAllowed():
self._Select( index )
2015-12-09 23:16:41 +00:00
2017-02-01 21:11:17 +00:00
def SelectDown( self ):
2013-07-24 20:26:00 +00:00
2017-02-01 21:11:17 +00:00
current_selection = self._list_box.GetSelection()
if current_selection != wx.NOT_FOUND:
2016-12-21 22:30:54 +00:00
2017-02-01 21:11:17 +00:00
num_entries = self._list_box.GetCount()
2016-12-21 22:30:54 +00:00
2017-02-01 21:11:17 +00:00
if current_selection == num_entries - 1: selection = 0
else: selection = current_selection + 1
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
if selection != current_selection:
self._Select( selection )
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
def SelectPage( self, page_to_select ):
2013-07-24 20:26:00 +00:00
2017-02-01 21:11:17 +00:00
for ( key, page ) in self._keys_to_active_pages.items():
2016-01-06 21:17:20 +00:00
2017-02-01 21:11:17 +00:00
if page == page_to_select:
self._Select( self._GetIndex( key ) )
return
2016-01-06 21:17:20 +00:00
2013-07-24 20:26:00 +00:00
2015-02-03 20:40:21 +00:00
2017-02-01 21:11:17 +00:00
def SelectUp( self ):
2013-07-24 20:26:00 +00:00
2017-02-01 21:11:17 +00:00
current_selection = self._list_box.GetSelection()
2015-10-28 21:29:05 +00:00
2017-02-01 21:11:17 +00:00
if current_selection != wx.NOT_FOUND:
num_entries = self._list_box.GetCount()
if current_selection == 0: selection = num_entries - 1
else: selection = current_selection - 1
2015-10-28 21:29:05 +00:00
2017-02-01 21:11:17 +00:00
if selection != current_selection:
self._Select( selection )
2015-10-28 21:29:05 +00:00
2015-02-03 20:40:21 +00:00
2017-03-15 20:13:04 +00:00
class CheckboxManager( object ):
2017-01-18 22:52:39 +00:00
2017-03-15 20:13:04 +00:00
def GetCurrentValue( self ):
2017-01-18 22:52:39 +00:00
2017-03-15 20:13:04 +00:00
raise NotImplementedError()
2017-01-18 22:52:39 +00:00
2017-03-15 20:13:04 +00:00
def Invert( self ):
raise NotImplementedError()
class CheckboxManagerCalls( CheckboxManager ):
def __init__( self, invert_call, value_call ):
CheckboxManager.__init__( self )
self._invert_call = invert_call
self._value_call = value_call
def GetCurrentValue( self ):
return self._value_call()
def Invert( self ):
self._invert_call()
class CheckboxManagerOptions( CheckboxManager ):
def __init__( self, boolean_name ):
CheckboxManager.__init__( self )
self._boolean_name = boolean_name
def GetCurrentValue( self ):
2017-05-10 21:33:58 +00:00
new_options = HG.client_controller.GetNewOptions()
2017-03-15 20:13:04 +00:00
return new_options.GetBoolean( self._boolean_name )
2017-01-18 22:52:39 +00:00
2017-03-15 20:13:04 +00:00
def Invert( self ):
2017-01-25 22:56:55 +00:00
2017-05-10 21:33:58 +00:00
new_options = HG.client_controller.GetNewOptions()
2017-01-25 22:56:55 +00:00
2017-03-15 20:13:04 +00:00
new_options.InvertBoolean( self._boolean_name )
class MenuBitmapButton( BetterBitmapButton ):
def __init__( self, parent, bitmap, menu_items ):
BetterBitmapButton.__init__( self, parent, bitmap, self.DoMenu )
self._menu_items = menu_items
2017-01-25 22:56:55 +00:00
2017-01-18 22:52:39 +00:00
def DoMenu( self ):
menu = wx.Menu()
2017-01-25 22:56:55 +00:00
for ( item_type, title, description, data ) in self._menu_items:
2017-01-18 22:52:39 +00:00
2017-01-25 22:56:55 +00:00
if item_type == 'normal':
2017-03-15 20:13:04 +00:00
func = data
2017-01-25 22:56:55 +00:00
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendMenuItem( self, menu, title, description, func )
2017-01-25 22:56:55 +00:00
elif item_type == 'check':
2017-03-15 20:13:04 +00:00
check_manager = data
2017-01-25 22:56:55 +00:00
2017-03-15 20:13:04 +00:00
current_value = check_manager.GetCurrentValue()
func = check_manager.Invert
2017-01-25 22:56:55 +00:00
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendMenuCheckItem( self, menu, title, description, current_value, func )
2017-01-25 22:56:55 +00:00
elif item_type == 'separator':
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendSeparator( menu )
2017-01-25 22:56:55 +00:00
2017-01-18 22:52:39 +00:00
2017-05-10 21:33:58 +00:00
HG.client_controller.PopupMenu( self, menu )
2017-01-18 22:52:39 +00:00
2016-11-16 20:21:43 +00:00
class MenuButton( BetterButton ):
def __init__( self, parent, label, menu_items ):
BetterButton.__init__( self, parent, label, self.DoMenu )
self._menu_items = menu_items
def DoMenu( self ):
menu = wx.Menu()
2017-01-25 22:56:55 +00:00
for ( item_type, title, description, data ) in self._menu_items:
2016-11-16 20:21:43 +00:00
2017-01-25 22:56:55 +00:00
if item_type == 'normal':
callable = data
ClientGUIMenus.AppendMenuItem( self, menu, title, description, callable )
elif item_type == 'check':
2017-03-15 20:13:04 +00:00
check_manager = data
2017-01-25 22:56:55 +00:00
2017-03-15 20:13:04 +00:00
initial_value = check_manager.GetInitialValue()
2017-01-25 22:56:55 +00:00
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendMenuCheckItem( self, menu, title, description, initial_value, check_manager.Invert )
2017-01-25 22:56:55 +00:00
elif item_type == 'separator':
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendSeparator( menu )
2017-01-25 22:56:55 +00:00
2017-03-02 02:14:56 +00:00
elif item_type == 'label':
ClientGUIMenus.AppendMenuLabel( menu, title, description )
2016-11-16 20:21:43 +00:00
2017-05-10 21:33:58 +00:00
HG.client_controller.PopupMenu( self, menu )
2016-11-16 20:21:43 +00:00
2017-03-02 02:14:56 +00:00
def SetMenuItems( self, menu_items ):
self._menu_items = menu_items
2015-02-03 20:40:21 +00:00
class NoneableSpinCtrl( wx.Panel ):
2017-03-02 02:14:56 +00:00
def __init__( self, parent, message = '', none_phrase = 'no limit', min = 0, max = 1000000, unit = None, multiplier = 1, num_dimensions = 1 ):
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
wx.Panel.__init__( self, parent )
2013-02-19 00:11:43 +00:00
2015-12-02 22:32:18 +00:00
self._unit = unit
2015-02-03 20:40:21 +00:00
self._multiplier = multiplier
2015-12-02 22:32:18 +00:00
self._num_dimensions = num_dimensions
2013-03-15 02:38:12 +00:00
2017-04-19 20:58:30 +00:00
self._checkbox = wx.CheckBox( self )
2015-02-03 20:40:21 +00:00
self._checkbox.Bind( wx.EVT_CHECKBOX, self.EventCheckBox )
2017-04-19 20:58:30 +00:00
self._checkbox.SetLabelText( none_phrase )
2013-02-19 00:11:43 +00:00
2015-06-17 20:01:41 +00:00
self._one = wx.SpinCtrl( self, min = min, max = max, size = ( 60, -1 ) )
2013-02-19 00:11:43 +00:00
2015-12-23 22:51:04 +00:00
if num_dimensions == 2:
self._two = wx.SpinCtrl( self, initial = 0, min = min, max = max, size = ( 60, -1 ) )
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
hbox = wx.BoxSizer( wx.HORIZONTAL )
2013-02-19 00:11:43 +00:00
2015-07-15 20:28:26 +00:00
if len( message ) > 0:
2017-04-19 20:58:30 +00:00
hbox.AddF( BetterStaticText( self, message + ': ' ), CC.FLAGS_VCENTER )
2015-07-15 20:28:26 +00:00
2016-08-31 19:55:14 +00:00
hbox.AddF( self._one, CC.FLAGS_VCENTER )
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
if self._num_dimensions == 2:
2017-04-19 20:58:30 +00:00
hbox.AddF( BetterStaticText( self, 'x' ), CC.FLAGS_VCENTER )
2016-08-31 19:55:14 +00:00
hbox.AddF( self._two, CC.FLAGS_VCENTER )
2015-02-03 20:40:21 +00:00
2013-02-19 00:11:43 +00:00
2015-12-02 22:32:18 +00:00
if self._unit is not None:
2017-04-19 20:58:30 +00:00
hbox.AddF( BetterStaticText( self, self._unit ), CC.FLAGS_VCENTER )
2015-12-02 22:32:18 +00:00
2016-08-31 19:55:14 +00:00
hbox.AddF( self._checkbox, CC.FLAGS_VCENTER )
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
self.SetSizer( hbox )
2013-02-19 00:11:43 +00:00
2015-04-01 20:44:54 +00:00
def Bind( self, event_type, callback ):
self._checkbox.Bind( wx.EVT_CHECKBOX, callback )
self._one.Bind( wx.EVT_SPINCTRL, callback )
if self._num_dimensions == 2: self._two.Bind( wx.EVT_SPINCTRL, callback )
2015-02-03 20:40:21 +00:00
def EventCheckBox( self, event ):
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
if self._checkbox.GetValue():
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
self._one.Disable()
if self._num_dimensions == 2: self._two.Disable()
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
else:
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
self._one.Enable()
if self._num_dimensions == 2: self._two.Enable()
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
def GetValue( self ):
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
if self._checkbox.GetValue(): return None
else:
if self._num_dimensions == 2: return ( self._one.GetValue() * self._multiplier, self._two.GetValue() * self._multiplier )
else: return self._one.GetValue() * self._multiplier
2013-02-19 00:11:43 +00:00
2016-06-22 20:59:24 +00:00
def SetToolTipString( self, text ):
wx.Panel.SetToolTipString( self, text )
for c in self.GetChildren():
c.SetToolTipString( text )
2015-02-03 20:40:21 +00:00
def SetValue( self, value ):
2013-03-15 02:38:12 +00:00
2015-02-03 20:40:21 +00:00
if value is None:
self._checkbox.SetValue( True )
self._one.Disable()
if self._num_dimensions == 2: self._two.Disable()
else:
self._checkbox.SetValue( False )
if self._num_dimensions == 2:
self._two.Enable()
( value, y ) = value
self._two.SetValue( y / self._multiplier )
self._one.Enable()
self._one.SetValue( value / self._multiplier )
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
class OnOffButton( wx.Button ):
2013-03-15 02:38:12 +00:00
2015-02-03 20:40:21 +00:00
def __init__( self, parent, page_key, topic, on_label, off_label = None, start_on = True ):
2013-03-15 02:38:12 +00:00
2015-02-03 20:40:21 +00:00
if start_on: label = on_label
else: label = off_label
2013-03-15 02:38:12 +00:00
2015-02-03 20:40:21 +00:00
wx.Button.__init__( self, parent, label = label )
2013-03-15 02:38:12 +00:00
2015-02-03 20:40:21 +00:00
self._page_key = page_key
self._topic = topic
self._on_label = on_label
2013-03-15 02:38:12 +00:00
2015-02-03 20:40:21 +00:00
if off_label is None: self._off_label = on_label
else: self._off_label = off_label
2013-09-18 17:23:30 +00:00
2015-02-03 20:40:21 +00:00
self._on = start_on
2013-09-18 17:23:30 +00:00
2015-02-03 20:40:21 +00:00
if self._on: self.SetForegroundColour( ( 0, 128, 0 ) )
else: self.SetForegroundColour( ( 128, 0, 0 ) )
2013-09-18 17:23:30 +00:00
2015-02-03 20:40:21 +00:00
self.Bind( wx.EVT_BUTTON, self.EventButton )
2013-09-18 17:23:30 +00:00
2017-05-10 21:33:58 +00:00
HG.client_controller.sub( self, 'HitButton', 'hit_on_off_button' )
2013-09-18 17:23:30 +00:00
2015-02-03 20:40:21 +00:00
def EventButton( self, event ):
2013-09-18 17:23:30 +00:00
2015-02-03 20:40:21 +00:00
if self._on:
2013-09-18 17:23:30 +00:00
2015-02-03 20:40:21 +00:00
self._on = False
2013-09-18 17:23:30 +00:00
2016-03-23 19:42:56 +00:00
self.SetLabelText( self._off_label )
2013-09-18 17:23:30 +00:00
2015-02-03 20:40:21 +00:00
self.SetForegroundColour( ( 128, 0, 0 ) )
2013-09-18 17:23:30 +00:00
2017-05-10 21:33:58 +00:00
HG.client_controller.pub( self._topic, self._page_key, False )
2013-09-18 17:23:30 +00:00
2015-02-03 20:40:21 +00:00
else:
2013-09-18 17:23:30 +00:00
2015-02-03 20:40:21 +00:00
self._on = True
2013-09-18 17:23:30 +00:00
2016-03-23 19:42:56 +00:00
self.SetLabelText( self._on_label )
2013-09-18 17:23:30 +00:00
2015-02-03 20:40:21 +00:00
self.SetForegroundColour( ( 0, 128, 0 ) )
2014-02-12 23:09:38 +00:00
2017-05-10 21:33:58 +00:00
HG.client_controller.pub( self._topic, self._page_key, True )
2014-02-12 23:09:38 +00:00
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
def IsOn( self ): return self._on
2014-09-03 20:26:49 +00:00
2017-07-27 00:47:13 +00:00
class RatingLike( wx.Window ):
2014-02-12 23:09:38 +00:00
2017-07-27 00:47:13 +00:00
def __init__( self, parent, service_key ):
2014-09-03 20:26:49 +00:00
2017-07-27 00:47:13 +00:00
wx.Window.__init__( self, parent )
2014-09-03 20:26:49 +00:00
2017-07-27 00:47:13 +00:00
self._service_key = service_key
2014-09-03 20:26:49 +00:00
2017-07-27 00:47:13 +00:00
self._canvas_bmp = wx.EmptyBitmap( 16, 16, 24 )
2016-09-07 20:01:05 +00:00
2017-07-27 00:47:13 +00:00
self.Bind( wx.EVT_PAINT, self.EventPaint )
self.Bind( wx.EVT_ERASE_BACKGROUND, self.EventEraseBackground )
2016-09-07 20:01:05 +00:00
2017-07-27 00:47:13 +00:00
self.Bind( wx.EVT_LEFT_DOWN, self.EventLeftDown )
self.Bind( wx.EVT_LEFT_DCLICK, self.EventLeftDown )
self.Bind( wx.EVT_RIGHT_DOWN, self.EventRightDown )
self.Bind( wx.EVT_RIGHT_DCLICK, self.EventRightDown )
2016-09-07 20:01:05 +00:00
2017-07-27 00:47:13 +00:00
self.SetMinSize( ( 16, 16 ) )
self._dirty = True
2016-09-07 20:01:05 +00:00
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
def _Draw( self, dc ):
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
raise NotImplementedError()
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
def EventEraseBackground( self, event ): pass
def EventLeftDown( self, event ):
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
raise NotImplementedError()
2013-03-15 02:38:12 +00:00
2017-07-27 00:47:13 +00:00
def EventPaint( self, event ):
2013-09-18 17:23:30 +00:00
2017-07-27 00:47:13 +00:00
dc = wx.BufferedPaintDC( self, self._canvas_bmp )
2013-09-18 17:23:30 +00:00
2017-07-27 00:47:13 +00:00
if self._dirty:
self._Draw( dc )
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
def EventRightDown( self, event ):
2016-09-07 20:01:05 +00:00
2017-07-27 00:47:13 +00:00
raise NotImplementedError()
2016-09-07 20:01:05 +00:00
2014-09-03 20:26:49 +00:00
2017-07-27 00:47:13 +00:00
def GetServiceKey( self ):
2016-09-07 20:01:05 +00:00
2017-07-27 00:47:13 +00:00
return self._service_key
2016-09-07 20:01:05 +00:00
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
class RatingLikeDialog( RatingLike ):
def __init__( self, parent, service_key ):
2016-09-07 20:01:05 +00:00
2017-07-27 00:47:13 +00:00
RatingLike.__init__( self, parent, service_key )
2016-09-07 20:01:05 +00:00
2017-07-27 00:47:13 +00:00
self._rating_state = ClientRatings.NULL
2014-02-12 23:09:38 +00:00
2017-07-27 00:47:13 +00:00
def _Draw( self, dc ):
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
dc.SetBackground( wx.Brush( self.GetParent().GetBackgroundColour() ) )
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
dc.Clear()
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
( pen_colour, brush_colour ) = ClientRatings.GetPenAndBrushColours( self._service_key, self._rating_state )
2013-03-15 02:38:12 +00:00
2017-07-27 00:47:13 +00:00
ClientRatings.DrawLike( dc, 0, 0, self._service_key, self._rating_state )
2013-03-15 02:38:12 +00:00
2017-07-27 00:47:13 +00:00
self._dirty = False
2013-03-15 02:38:12 +00:00
2017-07-27 00:47:13 +00:00
def EventLeftDown( self, event ):
2013-03-15 02:38:12 +00:00
2017-07-27 00:47:13 +00:00
if self._rating_state == ClientRatings.LIKE: self._rating_state = ClientRatings.NULL
else: self._rating_state = ClientRatings.LIKE
2013-03-15 02:38:12 +00:00
2017-07-27 00:47:13 +00:00
self._dirty = True
2017-01-25 22:56:55 +00:00
2017-07-27 00:47:13 +00:00
self.Refresh()
2016-05-25 21:54:03 +00:00
2017-07-27 00:47:13 +00:00
def EventRightDown( self, event ):
2013-03-15 02:38:12 +00:00
2017-07-27 00:47:13 +00:00
if self._rating_state == ClientRatings.DISLIKE: self._rating_state = ClientRatings.NULL
else: self._rating_state = ClientRatings.DISLIKE
2013-03-15 02:38:12 +00:00
2017-07-27 00:47:13 +00:00
self._dirty = True
2013-03-15 02:38:12 +00:00
2017-07-27 00:47:13 +00:00
self.Refresh()
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
def GetRatingState( self ):
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
return self._rating_state
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
def SetRatingState( self, rating_state ):
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
self._rating_state = rating_state
2015-03-18 21:46:29 +00:00
2017-07-27 00:47:13 +00:00
self._dirty = True
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
self.Refresh()
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
class RatingLikeCanvas( RatingLike ):
def __init__( self, parent, service_key, canvas_key ):
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
RatingLike.__init__( self, parent, service_key )
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
self._canvas_key = canvas_key
self._current_media = None
self._rating_state = None
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
service = HG.client_controller.services_manager.GetService( service_key )
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
name = service.GetName()
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
self.SetToolTipString( name )
2015-07-15 20:28:26 +00:00
2017-07-27 00:47:13 +00:00
HG.client_controller.sub( self, 'ProcessContentUpdates', 'content_updates_gui' )
HG.client_controller.sub( self, 'SetDisplayMedia', 'canvas_new_display_media' )
2015-07-15 20:28:26 +00:00
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
def _Draw( self, dc ):
2016-05-25 21:54:03 +00:00
2017-07-27 00:47:13 +00:00
dc.SetBackground( wx.Brush( self.GetParent().GetBackgroundColour() ) )
dc.Clear()
2016-05-25 21:54:03 +00:00
2017-07-27 00:47:13 +00:00
if self._current_media is not None:
2016-09-28 18:48:01 +00:00
2017-07-27 00:47:13 +00:00
self._rating_state = ClientRatings.GetLikeStateFromMedia( ( self._current_media, ), self._service_key )
2016-09-28 18:48:01 +00:00
2017-07-27 00:47:13 +00:00
ClientRatings.DrawLike( dc, 0, 0, self._service_key, self._rating_state )
2016-09-28 18:48:01 +00:00
2016-05-25 21:54:03 +00:00
2017-07-27 00:47:13 +00:00
self._dirty = False
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
def EventLeftDown( self, event ):
2015-10-07 21:56:22 +00:00
2017-07-27 00:47:13 +00:00
if self._current_media is not None:
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
if self._rating_state == ClientRatings.LIKE: rating = None
else: rating = 1
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
content_update = HydrusData.ContentUpdate( HC.CONTENT_TYPE_RATINGS, HC.CONTENT_UPDATE_ADD, ( rating, self._hashes ) )
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
HG.client_controller.Write( 'content_updates', { self._service_key : ( content_update, ) } )
2015-02-03 20:40:21 +00:00
2013-09-18 17:23:30 +00:00
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
def EventRightDown( self, event ):
2016-09-28 18:48:01 +00:00
2017-07-27 00:47:13 +00:00
if self._current_media is not None:
if self._rating_state == ClientRatings.DISLIKE: rating = None
else: rating = 0
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
content_update = HydrusData.ContentUpdate( HC.CONTENT_TYPE_RATINGS, HC.CONTENT_UPDATE_ADD, ( rating, self._hashes ) )
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
HG.client_controller.Write( 'content_updates', { self._service_key : ( content_update, ) } )
2015-02-03 20:40:21 +00:00
2013-03-15 02:38:12 +00:00
2013-04-17 21:48:18 +00:00
2017-07-27 00:47:13 +00:00
def ProcessContentUpdates( self, service_keys_to_content_updates ):
2013-04-10 18:10:37 +00:00
2017-07-27 00:47:13 +00:00
if self._current_media is not None:
for ( service_key, content_updates ) in service_keys_to_content_updates.items():
for content_update in content_updates:
( data_type, action, row ) = content_update.ToTuple()
if data_type == HC.CONTENT_TYPE_RATINGS:
hashes = content_update.GetHashes()
if len( self._hashes.intersection( hashes ) ) > 0:
self._dirty = True
self.Refresh()
return
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
def SetDisplayMedia( self, canvas_key, media ):
if canvas_key == self._canvas_key:
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
self._current_media = media
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
if self._current_media is None:
self._hashes = set()
else:
self._hashes = self._current_media.GetHashes()
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
self._dirty = True
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
self.Refresh()
2015-02-03 20:40:21 +00:00
2013-04-10 18:10:37 +00:00
2013-03-15 02:38:12 +00:00
2017-07-27 00:47:13 +00:00
class RatingNumerical( wx.Window ):
def __init__( self, parent, service_key ):
2016-04-14 01:54:29 +00:00
2017-07-27 00:47:13 +00:00
wx.Window.__init__( self, parent )
2016-04-14 01:54:29 +00:00
2017-07-27 00:47:13 +00:00
self._service_key = service_key
2014-02-12 23:09:38 +00:00
2017-07-27 00:47:13 +00:00
self._service = HG.client_controller.services_manager.GetService( self._service_key )
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
self._num_stars = self._service.GetNumStars()
self._allow_zero = self._service.AllowZero()
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
my_width = ClientRatings.GetNumericalWidth( self._service_key )
2016-09-07 20:01:05 +00:00
2017-07-27 00:47:13 +00:00
self._canvas_bmp = wx.EmptyBitmap( my_width, 16, 24 )
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
self.Bind( wx.EVT_PAINT, self.EventPaint )
self.Bind( wx.EVT_ERASE_BACKGROUND, self.EventEraseBackground )
2016-09-28 18:48:01 +00:00
2017-07-27 00:47:13 +00:00
self.Bind( wx.EVT_LEFT_DOWN, self.EventLeftDown )
self.Bind( wx.EVT_LEFT_DCLICK, self.EventLeftDown )
self.Bind( wx.EVT_RIGHT_DOWN, self.EventRightDown )
self.Bind( wx.EVT_RIGHT_DCLICK, self.EventRightDown )
2016-09-28 18:48:01 +00:00
2017-07-27 00:47:13 +00:00
self.SetMinSize( ( my_width, 16 ) )
2016-09-28 18:48:01 +00:00
2017-07-27 00:47:13 +00:00
self._dirty = True
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
def _Draw( self, dc ):
2016-09-28 18:48:01 +00:00
2017-07-27 00:47:13 +00:00
raise NotImplementedError()
2013-02-19 00:11:43 +00:00
2017-07-27 00:47:13 +00:00
def _GetRatingFromClickEvent( self, event ):
x = event.GetX()
y = event.GetY()
( my_width, my_height ) = self.GetClientSize()
# assuming a border of 2 on every side here
2013-03-15 02:38:12 +00:00
2017-07-27 00:47:13 +00:00
my_active_width = my_width - 4
my_active_height = my_height - 4
x_adjusted = x - 2
y_adjusted = y - 2
2016-09-28 18:48:01 +00:00
2017-07-27 00:47:13 +00:00
if 0 <= y and y <= my_active_height:
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
if 0 <= x and x <= my_active_width:
2015-02-03 20:40:21 +00:00
2017-07-27 00:47:13 +00:00
proportion_filled = float( x_adjusted ) / my_active_width
2016-09-28 18:48:01 +00:00
2017-07-27 00:47:13 +00:00
if self._allow_zero:
rating = round( proportion_filled * self._num_stars ) / self._num_stars
else:
rating = float( int( proportion_filled * self._num_stars ) ) / ( self._num_stars - 1 )
return rating
2016-09-28 18:48:01 +00:00
2017-07-27 00:47:13 +00:00
return None
2015-05-20 21:31:40 +00:00
def EventEraseBackground( self, event ): pass
def EventLeftDown( self, event ):
raise NotImplementedError()
def EventPaint( self, event ):
2015-11-18 22:44:07 +00:00
dc = wx.BufferedPaintDC( self, self._canvas_bmp )
2015-05-20 21:31:40 +00:00
if self._dirty:
2015-11-18 22:44:07 +00:00
self._Draw( dc )
2015-05-20 21:31:40 +00:00
def EventRightDown( self, event ):
raise NotImplementedError()
2015-07-01 22:02:07 +00:00
def GetServiceKey( self ):
return self._service_key
2015-05-20 21:31:40 +00:00
class RatingNumericalDialog( RatingNumerical ):
def __init__( self, parent, service_key ):
RatingNumerical.__init__( self, parent, service_key )
self._rating_state = ClientRatings.NULL
2015-10-21 21:53:10 +00:00
self._rating = None
2015-05-20 21:31:40 +00:00
2015-11-18 22:44:07 +00:00
def _Draw( self, dc ):
2015-05-20 21:31:40 +00:00
dc.SetBackground( wx.Brush( self.GetParent().GetBackgroundColour() ) )
dc.Clear()
2015-06-10 19:40:25 +00:00
ClientRatings.DrawNumerical( dc, 0, 0, self._service_key, self._rating_state, self._rating )
2015-05-20 21:31:40 +00:00
self._dirty = False
def EventLeftDown( self, event ):
rating = self._GetRatingFromClickEvent( event )
if rating is not None:
self._rating_state = ClientRatings.SET
self._rating = rating
self._dirty = True
self.Refresh()
def EventRightDown( self, event ):
self._rating_state = ClientRatings.NULL
self._dirty = True
self.Refresh()
def GetRating( self ):
return self._rating
def GetRatingState( self ):
return self._rating_state
def SetRating( self, rating ):
self._rating_state = ClientRatings.SET
self._rating = rating
self._dirty = True
self.Refresh()
def SetRatingState( self, rating_state ):
self._rating_state = rating_state
self._dirty = True
self.Refresh()
class RatingNumericalCanvas( RatingNumerical ):
def __init__( self, parent, service_key, canvas_key ):
RatingNumerical.__init__( self, parent, service_key )
self._canvas_key = canvas_key
self._current_media = None
self._rating_state = None
self._rating = None
name = self._service.GetName()
self.SetToolTipString( name )
2017-05-10 21:33:58 +00:00
HG.client_controller.sub( self, 'ProcessContentUpdates', 'content_updates_gui' )
HG.client_controller.sub( self, 'SetDisplayMedia', 'canvas_new_display_media' )
2015-05-20 21:31:40 +00:00
2015-11-18 22:44:07 +00:00
def _Draw( self, dc ):
2015-05-20 21:31:40 +00:00
dc.SetBackground( wx.Brush( self.GetParent().GetBackgroundColour() ) )
dc.Clear()
if self._current_media is not None:
( self._rating_state, self._rating ) = ClientRatings.GetNumericalStateFromMedia( ( self._current_media, ), self._service_key )
2015-06-10 19:40:25 +00:00
ClientRatings.DrawNumerical( dc, 0, 0, self._service_key, self._rating_state, self._rating )
2015-05-20 21:31:40 +00:00
self._dirty = False
def EventLeftDown( self, event ):
if self._current_media is not None:
rating = self._GetRatingFromClickEvent( event )
if rating is not None:
2015-10-14 21:02:25 +00:00
content_update = HydrusData.ContentUpdate( HC.CONTENT_TYPE_RATINGS, HC.CONTENT_UPDATE_ADD, ( rating, self._hashes ) )
2015-05-20 21:31:40 +00:00
2017-05-10 21:33:58 +00:00
HG.client_controller.Write( 'content_updates', { self._service_key : ( content_update, ) } )
2015-05-20 21:31:40 +00:00
def EventRightDown( self, event ):
if self._current_media is not None:
rating = None
2015-10-14 21:02:25 +00:00
content_update = HydrusData.ContentUpdate( HC.CONTENT_TYPE_RATINGS, HC.CONTENT_UPDATE_ADD, ( rating, self._hashes ) )
2015-05-20 21:31:40 +00:00
2017-05-10 21:33:58 +00:00
HG.client_controller.Write( 'content_updates', { self._service_key : ( content_update, ) } )
2015-05-20 21:31:40 +00:00
def ProcessContentUpdates( self, service_keys_to_content_updates ):
if self._current_media is not None:
for ( service_key, content_updates ) in service_keys_to_content_updates.items():
for content_update in content_updates:
( data_type, action, row ) = content_update.ToTuple()
2015-10-14 21:02:25 +00:00
if data_type == HC.CONTENT_TYPE_RATINGS:
2015-05-20 21:31:40 +00:00
hashes = content_update.GetHashes()
if len( self._hashes.intersection( hashes ) ) > 0:
self._dirty = True
self.Refresh()
return
def SetDisplayMedia( self, canvas_key, media ):
if canvas_key == self._canvas_key:
self._current_media = media
2015-11-18 22:44:07 +00:00
if self._current_media is None:
self._hashes = set()
else:
self._hashes = self._current_media.GetHashes()
2015-05-20 21:31:40 +00:00
self._dirty = True
self.Refresh()
2015-05-13 20:22:39 +00:00
2015-02-03 20:40:21 +00:00
class RegexButton( wx.Button ):
ID_REGEX_WHITESPACE = 0
ID_REGEX_NUMBER = 1
ID_REGEX_ALPHANUMERIC = 2
ID_REGEX_ANY = 3
ID_REGEX_BEGINNING = 4
ID_REGEX_END = 5
ID_REGEX_0_OR_MORE_GREEDY = 6
ID_REGEX_1_OR_MORE_GREEDY = 7
ID_REGEX_0_OR_1_GREEDY = 8
ID_REGEX_0_OR_MORE_MINIMAL = 9
ID_REGEX_1_OR_MORE_MINIMAL = 10
ID_REGEX_0_OR_1_MINIMAL = 11
ID_REGEX_EXACTLY_M = 12
ID_REGEX_M_TO_N_GREEDY = 13
ID_REGEX_M_TO_N_MINIMAL = 14
ID_REGEX_LOOKAHEAD = 15
ID_REGEX_NEGATIVE_LOOKAHEAD = 16
ID_REGEX_LOOKBEHIND = 17
ID_REGEX_NEGATIVE_LOOKBEHIND = 18
ID_REGEX_NUMBER_WITHOUT_ZEROES = 19
ID_REGEX_BACKSPACE = 22
ID_REGEX_SET = 23
ID_REGEX_NOT_SET = 24
ID_REGEX_FILENAME = 25
2015-09-23 21:21:02 +00:00
ID_REGEX_MANAGE_FAVOURITES = 26
ID_REGEX_FAVOURITES = range( 100, 200 )
2015-02-03 20:40:21 +00:00
def __init__( self, parent ):
2013-07-24 20:26:00 +00:00
2015-02-03 20:40:21 +00:00
wx.Button.__init__( self, parent, label = 'regex shortcuts' )
2013-07-24 20:26:00 +00:00
2015-02-03 20:40:21 +00:00
self.Bind( wx.EVT_BUTTON, self.EventButton )
self.Bind( wx.EVT_MENU, self.EventMenu )
2013-07-24 20:26:00 +00:00
2015-02-03 20:40:21 +00:00
def EventButton( self, event ):
2013-11-06 18:22:07 +00:00
2015-02-03 20:40:21 +00:00
menu = wx.Menu()
2013-07-24 20:26:00 +00:00
2015-02-03 20:40:21 +00:00
menu.Append( -1, 'click on a phrase to copy to clipboard' )
2013-07-24 20:26:00 +00:00
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendSeparator( menu )
2014-03-12 22:08:23 +00:00
2015-09-23 21:21:02 +00:00
submenu = wx.Menu()
2015-02-03 20:40:21 +00:00
2015-09-23 21:21:02 +00:00
submenu.Append( self.ID_REGEX_WHITESPACE, r'whitespace character - \s' )
submenu.Append( self.ID_REGEX_NUMBER, r'number character - \d' )
submenu.Append( self.ID_REGEX_ALPHANUMERIC, r'alphanumeric or backspace character - \w' )
submenu.Append( self.ID_REGEX_ANY, r'any character - .' )
submenu.Append( self.ID_REGEX_BACKSPACE, r'backspace character - \\' )
submenu.Append( self.ID_REGEX_BEGINNING, r'beginning of line - ^' )
submenu.Append( self.ID_REGEX_END, r'end of line - $' )
2017-03-08 23:23:12 +00:00
submenu.Append( self.ID_REGEX_SET, u'any of these - [\u2026]' )
submenu.Append( self.ID_REGEX_NOT_SET, u'anything other than these - [^\u2026]' )
2015-02-03 20:40:21 +00:00
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendSeparator( submenu )
2015-02-03 20:40:21 +00:00
2015-09-23 21:21:02 +00:00
submenu.Append( self.ID_REGEX_0_OR_MORE_GREEDY, r'0 or more matches, consuming as many as possible - *' )
submenu.Append( self.ID_REGEX_1_OR_MORE_GREEDY, r'1 or more matches, consuming as many as possible - +' )
submenu.Append( self.ID_REGEX_0_OR_1_GREEDY, r'0 or 1 matches, preferring 1 - ?' )
submenu.Append( self.ID_REGEX_0_OR_MORE_MINIMAL, r'0 or more matches, consuming as few as possible - *?' )
submenu.Append( self.ID_REGEX_1_OR_MORE_MINIMAL, r'1 or more matches, consuming as few as possible - +?' )
submenu.Append( self.ID_REGEX_0_OR_1_MINIMAL, r'0 or 1 matches, preferring 0 - *' )
submenu.Append( self.ID_REGEX_EXACTLY_M, r'exactly m matches - {m}' )
submenu.Append( self.ID_REGEX_M_TO_N_GREEDY, r'm to n matches, consuming as many as possible - {m,n}' )
submenu.Append( self.ID_REGEX_M_TO_N_MINIMAL, r'm to n matches, consuming as few as possible - {m,n}?' )
2015-02-03 20:40:21 +00:00
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendSeparator( submenu )
2015-02-03 20:40:21 +00:00
2017-03-08 23:23:12 +00:00
submenu.Append( self.ID_REGEX_LOOKAHEAD, u'the next characters are: (non-consuming) - (?=\u2026)' )
submenu.Append( self.ID_REGEX_NEGATIVE_LOOKAHEAD, u'the next characters are not: (non-consuming) - (?!\u2026)' )
submenu.Append( self.ID_REGEX_LOOKBEHIND, u'the previous characters are: (non-consuming) - (?<=\u2026)' )
submenu.Append( self.ID_REGEX_NEGATIVE_LOOKBEHIND, u'the previous characters are not: (non-consuming) - (?<!\u2026)' )
2015-02-03 20:40:21 +00:00
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendSeparator( submenu )
2015-02-03 20:40:21 +00:00
2015-09-23 21:21:02 +00:00
submenu.Append( self.ID_REGEX_NUMBER_WITHOUT_ZEROES, r'0074 -> 74 - [1-9]+\d*' )
2017-03-15 20:13:04 +00:00
submenu.Append( self.ID_REGEX_FILENAME, r'filename - (?<=' + os.path.sep.encode( 'string_escape' ) + r')[^' + os.path.sep.encode( 'string_escape' ) + r']*?(?=\..*$)' )
2015-09-23 21:21:02 +00:00
menu.AppendMenu( -1, 'regex components', submenu )
submenu = wx.Menu()
submenu.Append( self.ID_REGEX_MANAGE_FAVOURITES, 'manage favourites' )
2017-03-15 20:13:04 +00:00
ClientGUIMenus.AppendSeparator( submenu )
2015-09-23 21:21:02 +00:00
for ( index, ( regex_phrase, description ) ) in enumerate( HC.options[ 'regex_favourites' ] ):
menu_id = index + 100
submenu.Append( menu_id, description )
2015-02-03 20:40:21 +00:00
2015-09-23 21:21:02 +00:00
menu.AppendMenu( -1, 'favourites', submenu )
2015-02-03 20:40:21 +00:00
2017-05-10 21:33:58 +00:00
HG.client_controller.PopupMenu( self, menu )
2014-03-12 22:08:23 +00:00
2015-02-03 20:40:21 +00:00
def EventMenu( self, event ):
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
id = event.GetId()
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
phrase = None
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
if id == self.ID_REGEX_WHITESPACE: phrase = r'\s'
elif id == self.ID_REGEX_NUMBER: phrase = r'\d'
elif id == self.ID_REGEX_ALPHANUMERIC: phrase = r'\w'
elif id == self.ID_REGEX_ANY: phrase = r'.'
elif id == self.ID_REGEX_BACKSPACE: phrase = r'\\'
elif id == self.ID_REGEX_BEGINNING: phrase = r'^'
elif id == self.ID_REGEX_END: phrase = r'$'
2017-03-08 23:23:12 +00:00
elif id == self.ID_REGEX_SET: phrase = u'[\u2026]'
elif id == self.ID_REGEX_NOT_SET: phrase = u'[^\u2026]'
2015-02-03 20:40:21 +00:00
elif id == self.ID_REGEX_0_OR_MORE_GREEDY: phrase = r'*'
elif id == self.ID_REGEX_1_OR_MORE_GREEDY: phrase = r'+'
elif id == self.ID_REGEX_0_OR_1_GREEDY: phrase = r'?'
elif id == self.ID_REGEX_0_OR_MORE_MINIMAL: phrase = r'*?'
elif id == self.ID_REGEX_1_OR_MORE_MINIMAL: phrase = r'+?'
elif id == self.ID_REGEX_0_OR_1_MINIMAL: phrase = r'*'
elif id == self.ID_REGEX_EXACTLY_M: phrase = r'{m}'
elif id == self.ID_REGEX_M_TO_N_GREEDY: phrase = r'{m,n}'
elif id == self.ID_REGEX_M_TO_N_MINIMAL: phrase = r'{m,n}?'
2017-03-08 23:23:12 +00:00
elif id == self.ID_REGEX_LOOKAHEAD: phrase = u'(?=\u2026)'
elif id == self.ID_REGEX_NEGATIVE_LOOKAHEAD: phrase = u'(?!\u2026)'
elif id == self.ID_REGEX_LOOKBEHIND: phrase = u'(?<=\u2026)'
elif id == self.ID_REGEX_NEGATIVE_LOOKBEHIND: phrase = u'(?<!\u2026)'
2015-02-03 20:40:21 +00:00
elif id == self.ID_REGEX_NUMBER_WITHOUT_ZEROES: phrase = r'[1-9]+\d*'
2017-03-15 20:13:04 +00:00
elif id == self.ID_REGEX_FILENAME: phrase = '(?<=' + os.path.sep.encode( 'string_escape' ) + r')[^' + os.path.sep.encode( 'string_escape' ) + r']*?(?=\..*$)'
2015-09-23 21:21:02 +00:00
elif id == self.ID_REGEX_MANAGE_FAVOURITES:
import ClientGUIDialogsManage
with ClientGUIDialogsManage.DialogManageRegexFavourites( self.GetTopLevelParent() ) as dlg:
dlg.ShowModal()
elif id in self.ID_REGEX_FAVOURITES:
index = id - 100
( phrase, description ) = HC.options[ 'regex_favourites' ][ index ]
2015-02-03 20:40:21 +00:00
else: event.Skip()
2017-05-10 21:33:58 +00:00
if phrase is not None: HG.client_controller.pub( 'clipboard', 'text', phrase )
2013-02-19 00:11:43 +00:00
2016-11-02 21:09:14 +00:00
class SaneMultilineTextCtrl( wx.TextCtrl ):
def __init__( self, parent, style = None ):
if style is None:
style = wx.TE_MULTILINE
else:
style |= wx.TE_MULTILINE
wx.TextCtrl.__init__( self, parent, style = style )
self.Bind( wx.EVT_KEY_DOWN, self.EventKeyDown )
def EventKeyDown( self, event ):
ctrl = event.CmdDown()
key_code = event.GetKeyCode()
if ctrl and key_code in ( ord( 'A' ), ord( 'a' ) ):
self.SelectAll()
else:
event.Skip()
2017-04-12 21:46:46 +00:00
class Shortcut( wx.Panel ):
2013-10-09 18:13:42 +00:00
2017-04-12 21:46:46 +00:00
def __init__( self, parent ):
wx.Panel.__init__( self, parent )
self._mouse_radio = wx.RadioButton( self, style = wx.RB_GROUP, label = 'mouse' )
self._mouse_shortcut = ShortcutMouse( self, self._mouse_radio )
self._keyboard_radio = wx.RadioButton( self, label = 'keyboard' )
self._keyboard_shortcut = ShortcutKeyboard( self, self._keyboard_radio )
#
vbox = wx.BoxSizer( wx.VERTICAL )
2017-05-10 21:33:58 +00:00
vbox.AddF( BetterStaticText( self, 'Mouse events only work for the duplicate and archive/delete filters atm!' ), CC.FLAGS_EXPAND_PERPENDICULAR )
2017-04-12 21:46:46 +00:00
gridbox = wx.FlexGridSizer( 0, 2 )
gridbox.AddGrowableCol( 1, 1 )
gridbox.AddF( self._mouse_radio, CC.FLAGS_VCENTER )
gridbox.AddF( self._mouse_shortcut, CC.FLAGS_EXPAND_BOTH_WAYS )
gridbox.AddF( self._keyboard_radio, CC.FLAGS_VCENTER )
gridbox.AddF( self._keyboard_shortcut, CC.FLAGS_EXPAND_BOTH_WAYS )
vbox.AddF( gridbox, CC.FLAGS_EXPAND_BOTH_WAYS )
self.SetSizer( vbox )
def GetValue( self ):
if self._mouse_radio.GetValue() == True:
return self._mouse_shortcut.GetValue()
else:
return self._keyboard_shortcut.GetValue()
def SetValue( self, shortcut ):
if shortcut.GetShortcutType() == CC.SHORTCUT_TYPE_MOUSE:
self._mouse_radio.SetValue( True )
self._mouse_shortcut.SetValue( shortcut )
else:
self._keyboard_radio.SetValue( True )
self._keyboard_shortcut.SetValue( shortcut )
class ShortcutKeyboard( wx.TextCtrl ):
def __init__( self, parent, related_radio = None ):
self._shortcut = ClientData.Shortcut( CC.SHORTCUT_TYPE_KEYBOARD, wx.WXK_F7, [] )
2015-02-03 20:40:21 +00:00
2017-04-12 21:46:46 +00:00
self._related_radio = related_radio
2015-02-03 20:40:21 +00:00
wx.TextCtrl.__init__( self, parent, style = wx.TE_PROCESS_ENTER )
self.Bind( wx.EVT_KEY_DOWN, self.EventKeyDown )
self._SetShortcutString()
2013-10-09 18:13:42 +00:00
2015-02-03 20:40:21 +00:00
def _SetShortcutString( self ):
2013-10-09 18:13:42 +00:00
2017-04-12 21:46:46 +00:00
display_string = self._shortcut.ToString()
2015-02-03 20:40:21 +00:00
wx.TextCtrl.SetValue( self, display_string )
def EventKeyDown( self, event ):
2017-04-12 21:46:46 +00:00
shortcut = ClientData.ConvertKeyEventToShortcut( event )
if shortcut is not None:
self._shortcut = shortcut
if self._related_radio is not None:
self._related_radio.SetValue( True )
self._SetShortcutString()
def GetValue( self ):
return self._shortcut
def SetValue( self, shortcut ):
self._shortcut = shortcut
self._SetShortcutString()
class ShortcutMouse( wx.Button ):
def __init__( self, parent, related_radio = None ):
self._shortcut = ClientData.Shortcut( CC.SHORTCUT_TYPE_MOUSE, CC.SHORTCUT_MOUSE_LEFT, [] )
2017-04-05 21:16:40 +00:00
2017-04-12 21:46:46 +00:00
self._related_radio = related_radio
wx.Button.__init__( self, parent )
self.Bind( wx.EVT_MOUSE_EVENTS, self.EventMouse )
self._SetShortcutString()
def _SetShortcutString( self ):
display_string = self._shortcut.ToString()
self.SetLabel( display_string )
def EventMouse( self, event ):
shortcut = ClientData.ConvertMouseEventToShortcut( event )
if shortcut is not None:
2013-10-09 18:13:42 +00:00
2017-04-12 21:46:46 +00:00
self._shortcut = shortcut
if self._related_radio is not None:
self._related_radio.SetValue( True )
2013-10-09 18:13:42 +00:00
2015-02-03 20:40:21 +00:00
self._SetShortcutString()
2017-04-12 21:46:46 +00:00
2013-10-09 18:13:42 +00:00
2015-02-03 20:40:21 +00:00
2017-04-12 21:46:46 +00:00
def GetValue( self ):
return self._shortcut
2015-02-03 20:40:21 +00:00
2017-04-12 21:46:46 +00:00
def SetValue( self, shortcut ):
2013-10-09 18:13:42 +00:00
2017-04-12 21:46:46 +00:00
self._shortcut = shortcut
2015-02-03 20:40:21 +00:00
self._SetShortcutString()
2013-10-09 18:13:42 +00:00
2015-02-03 20:40:21 +00:00
class StaticBox( wx.Panel ):
2013-10-09 18:13:42 +00:00
2015-02-03 20:40:21 +00:00
def __init__( self, parent, title ):
2013-10-09 18:13:42 +00:00
2015-02-03 20:40:21 +00:00
wx.Panel.__init__( self, parent, style = wx.BORDER_DOUBLE )
2013-10-09 18:13:42 +00:00
2016-08-10 19:04:08 +00:00
self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_FRAMEBK ) )
2015-02-03 20:40:21 +00:00
self._sizer = wx.BoxSizer( wx.VERTICAL )
normal_font = wx.SystemSettings.GetFont( wx.SYS_DEFAULT_GUI_FONT )
normal_font_size = normal_font.GetPointSize()
normal_font_family = normal_font.GetFamily()
title_font = wx.Font( int( normal_font_size ), normal_font_family, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD )
title_text = wx.StaticText( self, label = title, style = wx.ALIGN_CENTER )
title_text.SetFont( title_font )
2015-03-18 21:46:29 +00:00
self._sizer.AddF( title_text, CC.FLAGS_EXPAND_PERPENDICULAR )
2015-02-03 20:40:21 +00:00
self.SetSizer( self._sizer )
2013-10-09 18:13:42 +00:00
2015-02-03 20:40:21 +00:00
def AddF( self, widget, flags ): self._sizer.AddF( widget, flags )
class StaticBoxSorterForListBoxTags( StaticBox ):
def __init__( self, parent, title ):
2013-10-09 18:13:42 +00:00
2015-02-03 20:40:21 +00:00
StaticBox.__init__( self, parent, title )
self._sorter = wx.Choice( self )
self._sorter.Append( 'lexicographic (a-z)', CC.SORT_BY_LEXICOGRAPHIC_ASC )
self._sorter.Append( 'lexicographic (z-a)', CC.SORT_BY_LEXICOGRAPHIC_DESC )
2016-04-27 19:20:37 +00:00
self._sorter.Append( 'lexicographic (a-z) (grouped by namespace)', CC.SORT_BY_LEXICOGRAPHIC_NAMESPACE_ASC )
self._sorter.Append( 'lexicographic (z-a) (grouped by namespace)', CC.SORT_BY_LEXICOGRAPHIC_NAMESPACE_DESC )
2015-02-03 20:40:21 +00:00
self._sorter.Append( 'incidence (desc)', CC.SORT_BY_INCIDENCE_DESC )
self._sorter.Append( 'incidence (asc)', CC.SORT_BY_INCIDENCE_ASC )
2016-09-28 18:48:01 +00:00
self._sorter.Append( 'incidence (desc) (grouped by namespace)', CC.SORT_BY_INCIDENCE_NAMESPACE_DESC )
self._sorter.Append( 'incidence (asc) (grouped by namespace)', CC.SORT_BY_INCIDENCE_NAMESPACE_ASC )
2015-02-03 20:40:21 +00:00
if HC.options[ 'default_tag_sort' ] == CC.SORT_BY_LEXICOGRAPHIC_ASC: self._sorter.Select( 0 )
elif HC.options[ 'default_tag_sort' ] == CC.SORT_BY_LEXICOGRAPHIC_DESC: self._sorter.Select( 1 )
2016-04-27 19:20:37 +00:00
elif HC.options[ 'default_tag_sort' ] == CC.SORT_BY_LEXICOGRAPHIC_NAMESPACE_ASC: self._sorter.Select( 2 )
elif HC.options[ 'default_tag_sort' ] == CC.SORT_BY_LEXICOGRAPHIC_NAMESPACE_DESC: self._sorter.Select( 3 )
elif HC.options[ 'default_tag_sort' ] == CC.SORT_BY_INCIDENCE_DESC: self._sorter.Select( 4 )
elif HC.options[ 'default_tag_sort' ] == CC.SORT_BY_INCIDENCE_ASC: self._sorter.Select( 5 )
2016-09-28 18:48:01 +00:00
elif HC.options[ 'default_tag_sort' ] == CC.SORT_BY_INCIDENCE_NAMESPACE_DESC: self._sorter.Select( 6 )
elif HC.options[ 'default_tag_sort' ] == CC.SORT_BY_INCIDENCE_NAMESPACE_ASC: self._sorter.Select( 7 )
2015-02-03 20:40:21 +00:00
self._sorter.Bind( wx.EVT_CHOICE, self.EventSort )
2015-03-18 21:46:29 +00:00
self.AddF( self._sorter, CC.FLAGS_EXPAND_PERPENDICULAR )
2015-02-03 20:40:21 +00:00
2016-01-13 22:08:19 +00:00
def ChangeTagService( self, service_key ): self._tags_box.ChangeTagService( service_key )
2015-02-03 20:40:21 +00:00
def EventSort( self, event ):
selection = self._sorter.GetSelection()
if selection != wx.NOT_FOUND:
2013-10-09 18:13:42 +00:00
2015-02-03 20:40:21 +00:00
sort = self._sorter.GetClientData( selection )
2013-10-09 18:13:42 +00:00
2015-02-03 20:40:21 +00:00
self._tags_box.SetSort( sort )
2013-10-09 18:13:42 +00:00
2015-02-03 20:40:21 +00:00
def SetTagsBox( self, tags_box ):
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
self._tags_box = tags_box
2013-02-19 00:11:43 +00:00
2015-03-18 21:46:29 +00:00
self.AddF( self._tags_box, CC.FLAGS_EXPAND_BOTH_WAYS )
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
2015-11-11 21:20:41 +00:00
def SetTagsByMedia( self, media, force_reload = False ):
self._tags_box.SetTagsByMedia( media, force_reload = force_reload )
2015-02-03 20:40:21 +00:00
2017-01-11 22:31:30 +00:00
class TextAndGauge( wx.Panel ):
def __init__( self, parent ):
wx.Panel.__init__( self, parent )
2017-04-19 20:58:30 +00:00
self._st = BetterStaticText( self )
2017-01-11 22:31:30 +00:00
self._gauge = Gauge( self )
vbox = wx.BoxSizer( wx.VERTICAL )
vbox.AddF( self._st, CC.FLAGS_EXPAND_PERPENDICULAR )
vbox.AddF( self._gauge, CC.FLAGS_EXPAND_PERPENDICULAR )
self.SetSizer( vbox )
def SetValue( self, text, value, range ):
2017-07-05 21:09:28 +00:00
if text != self._st.GetLabelText():
self._st.SetLabelText( text )
2017-01-11 22:31:30 +00:00
2017-07-12 20:03:45 +00:00
self._gauge.SetRange( range )
self._gauge.SetValue( value )
2017-01-11 22:31:30 +00:00
2017-02-01 21:11:17 +00:00
( DirtyEvent, EVT_DIRTY ) = wx.lib.newevent.NewEvent()
class ThreadToGUIUpdater( object ):
def __init__( self, event_handler, func ):
self._event_handler = event_handler
self._func = func
self._lock = threading.Lock()
self._dirty_count = 0
self._args = None
self._kwargs = None
self._my_object_alive = True
event_handler.Bind( EVT_DIRTY, self.EventDirty )
def EventDirty( self, event ):
with self._lock:
2017-06-21 21:15:59 +00:00
try:
self._func( *self._args, **self._kwargs )
except HydrusExceptions.ShutdownException:
pass
2017-02-01 21:11:17 +00:00
self._dirty_count = 0
# the point here is that we can spam this a hundred times a second and wx will catch up to it when the single event gets processed
# if wx feels like running fast, it'll update at 60fps
# if not, we won't get bungled up with 10,000+ pubsub events in the event queue
def Update( self, *args, **kwargs ):
with self._lock:
self._args = args
self._kwargs = kwargs
if self._dirty_count == 0 and self._my_object_alive:
def wx_code():
try:
wx.PostEvent( self._event_handler, DirtyEvent() )
except TypeError:
if not bool( self._event_handler ):
# Event Handler is dead (would give PyDeadObjectError if accessed--PostEvent throws TypeError)
self._my_object_alive = False
else:
raise
wx.CallAfter( wx_code )
self._dirty_count += 1
take_a_break = self._dirty_count % 1000 == 0
# just in case we are choking the wx thread, let's give it a break every now and then
if take_a_break:
time.sleep( 0.25 )
2016-05-18 20:07:14 +00:00
( TimeDeltaEvent, EVT_TIME_DELTA ) = wx.lib.newevent.NewCommandEvent()
2016-04-20 20:42:21 +00:00
class TimeDeltaButton( wx.Button ):
2017-03-02 02:14:56 +00:00
def __init__( self, parent, min = 1, days = False, hours = False, minutes = False, seconds = False, monthly_allowed = False ):
2016-04-20 20:42:21 +00:00
wx.Button.__init__( self, parent )
self._min = min
self._show_days = days
self._show_hours = hours
self._show_minutes = minutes
self._show_seconds = seconds
2017-03-02 02:14:56 +00:00
self._monthly_allowed = monthly_allowed
2016-04-20 20:42:21 +00:00
self._value = self._min
self.SetLabelText( 'initialising' )
self.Bind( wx.EVT_BUTTON, self.EventButton )
def _RefreshLabel( self ):
text_components = []
value = self._value
2017-03-02 02:14:56 +00:00
if value is None:
2016-04-20 20:42:21 +00:00
2017-03-02 02:14:56 +00:00
text = 'monthly'
2016-04-20 20:42:21 +00:00
2017-03-02 02:14:56 +00:00
else:
2017-06-21 21:15:59 +00:00
text = HydrusData.ConvertTimeDeltaToPrettyString( value )
2017-03-02 02:14:56 +00:00
2016-04-20 20:42:21 +00:00
self.SetLabelText( text )
def EventButton( self, event ):
import ClientGUIDialogs
2017-03-02 02:14:56 +00:00
with ClientGUIDialogs.DialogInputTimeDelta( self, self._value, min = self._min, days = self._show_days, hours = self._show_hours, minutes = self._show_minutes, seconds = self._show_seconds, monthly_allowed = self._monthly_allowed ) as dlg:
2016-04-20 20:42:21 +00:00
if dlg.ShowModal() == wx.ID_OK:
value = dlg.GetValue()
self.SetValue( value )
2016-05-18 20:07:14 +00:00
new_event = TimeDeltaEvent( 0 )
wx.PostEvent( self, new_event )
2016-04-20 20:42:21 +00:00
def GetValue( self ):
return self._value
def SetValue( self, value ):
self._value = value
self._RefreshLabel()
self.GetParent().Layout()
2015-12-09 23:16:41 +00:00
class TimeDeltaCtrl( wx.Panel ):
2017-03-02 02:14:56 +00:00
def __init__( self, parent, min = 1, days = False, hours = False, minutes = False, seconds = False, monthly_allowed = False ):
2015-12-09 23:16:41 +00:00
wx.Panel.__init__( self, parent )
self._min = min
self._show_days = days
self._show_hours = hours
self._show_minutes = minutes
self._show_seconds = seconds
2017-03-02 02:14:56 +00:00
self._monthly_allowed = monthly_allowed
2015-12-09 23:16:41 +00:00
hbox = wx.BoxSizer( wx.HORIZONTAL )
if self._show_days:
self._days = wx.SpinCtrl( self, min = 0, max = 360, size = ( 50, -1 ) )
2017-03-02 02:14:56 +00:00
self._days.Bind( wx.EVT_SPINCTRL, self.EventChange )
2015-12-09 23:16:41 +00:00
2016-08-31 19:55:14 +00:00
hbox.AddF( self._days, CC.FLAGS_VCENTER )
2017-04-19 20:58:30 +00:00
hbox.AddF( BetterStaticText( self, 'days' ), CC.FLAGS_VCENTER )
2015-12-09 23:16:41 +00:00
if self._show_hours:
self._hours = wx.SpinCtrl( self, min = 0, max = 23, size = ( 45, -1 ) )
2017-03-02 02:14:56 +00:00
self._hours.Bind( wx.EVT_SPINCTRL, self.EventChange )
2015-12-09 23:16:41 +00:00
2016-08-31 19:55:14 +00:00
hbox.AddF( self._hours, CC.FLAGS_VCENTER )
2017-04-19 20:58:30 +00:00
hbox.AddF( BetterStaticText( self, 'hours' ), CC.FLAGS_VCENTER )
2015-12-09 23:16:41 +00:00
if self._show_minutes:
self._minutes = wx.SpinCtrl( self, min = 0, max = 59, size = ( 45, -1 ) )
2017-03-02 02:14:56 +00:00
self._minutes.Bind( wx.EVT_SPINCTRL, self.EventChange )
2015-12-09 23:16:41 +00:00
2016-08-31 19:55:14 +00:00
hbox.AddF( self._minutes, CC.FLAGS_VCENTER )
2017-04-19 20:58:30 +00:00
hbox.AddF( BetterStaticText( self, 'minutes' ), CC.FLAGS_VCENTER )
2015-12-09 23:16:41 +00:00
if self._show_seconds:
self._seconds = wx.SpinCtrl( self, min = 0, max = 59, size = ( 45, -1 ) )
2017-03-02 02:14:56 +00:00
self._seconds.Bind( wx.EVT_SPINCTRL, self.EventChange )
2015-12-09 23:16:41 +00:00
2016-08-31 19:55:14 +00:00
hbox.AddF( self._seconds, CC.FLAGS_VCENTER )
2017-04-19 20:58:30 +00:00
hbox.AddF( BetterStaticText( self, 'seconds' ), CC.FLAGS_VCENTER )
2015-12-09 23:16:41 +00:00
2017-03-02 02:14:56 +00:00
if self._monthly_allowed:
self._monthly = wx.CheckBox( self )
self._monthly.Bind( wx.EVT_CHECKBOX, self.EventChange )
hbox.AddF( self._monthly, CC.FLAGS_VCENTER )
2017-04-19 20:58:30 +00:00
hbox.AddF( BetterStaticText( self, 'monthly' ), CC.FLAGS_VCENTER )
2017-03-02 02:14:56 +00:00
2015-12-09 23:16:41 +00:00
self.SetSizer( hbox )
2017-03-02 02:14:56 +00:00
def _UpdateEnables( self ):
2015-12-09 23:16:41 +00:00
value = self.GetValue()
2017-03-02 02:14:56 +00:00
if value is None:
if self._show_days:
self._days.Disable()
if self._show_hours:
self._hours.Disable()
if self._show_minutes:
self._minutes.Disable()
if self._show_seconds:
self._seconds.Disable()
else:
if self._show_days:
self._days.Enable()
if self._show_hours:
self._hours.Enable()
if self._show_minutes:
self._minutes.Enable()
if self._show_seconds:
self._seconds.Enable()
def EventChange( self, event ):
value = self.GetValue()
if value is not None and value < self._min:
2015-12-09 23:16:41 +00:00
self.SetValue( self._min )
2017-03-02 02:14:56 +00:00
self._UpdateEnables()
2016-05-18 20:07:14 +00:00
new_event = TimeDeltaEvent( 0 )
wx.PostEvent( self, new_event )
2015-12-09 23:16:41 +00:00
def GetValue( self ):
2017-03-02 02:14:56 +00:00
if self._monthly_allowed and self._monthly.GetValue():
return None
2015-12-09 23:16:41 +00:00
value = 0
if self._show_days:
value += self._days.GetValue() * 86400
if self._show_hours:
value += self._hours.GetValue() * 3600
if self._show_minutes:
value += self._minutes.GetValue() * 60
if self._show_seconds:
value += self._seconds.GetValue()
return value
def SetValue( self, value ):
2017-03-02 02:14:56 +00:00
if self._monthly_allowed:
2015-12-09 23:16:41 +00:00
2017-03-02 02:14:56 +00:00
if value is None:
self._monthly.SetValue( True )
else:
self._monthly.SetValue( False )
2015-12-09 23:16:41 +00:00
2017-03-02 02:14:56 +00:00
if value is not None:
2015-12-09 23:16:41 +00:00
2017-03-02 02:14:56 +00:00
if value < self._min:
value = self._min
2015-12-09 23:16:41 +00:00
2017-03-02 02:14:56 +00:00
if self._show_days:
self._days.SetValue( value / 86400 )
value %= 86400
2015-12-09 23:16:41 +00:00
2017-03-02 02:14:56 +00:00
if self._show_hours:
self._hours.SetValue( value / 3600 )
value %= 3600
2015-12-09 23:16:41 +00:00
2017-03-02 02:14:56 +00:00
if self._show_minutes:
self._minutes.SetValue( value / 60 )
value %= 60
2015-12-09 23:16:41 +00:00
2017-03-02 02:14:56 +00:00
if self._show_seconds:
self._seconds.SetValue( value )
2015-12-09 23:16:41 +00:00
2017-03-02 02:14:56 +00:00
self._UpdateEnables()
2015-12-09 23:16:41 +00:00
2015-02-03 20:40:21 +00:00
class RadioBox( StaticBox ):
2013-11-06 18:22:07 +00:00
2015-02-03 20:40:21 +00:00
def __init__( self, parent, title, choice_pairs, initial_index = None ):
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
StaticBox.__init__( self, parent, title )
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
self._indices_to_radio_buttons = {}
self._radio_buttons_to_data = {}
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
first_button = True
for ( index, ( text, data ) ) in enumerate( choice_pairs ):
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
if first_button:
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
style = wx.RB_GROUP
2013-04-10 18:10:37 +00:00
2015-02-03 20:40:21 +00:00
first_button = False
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
else: style = 0
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
radio_button = wx.RadioButton( self, label = text, style = style )
2015-03-18 21:46:29 +00:00
self.AddF( radio_button, CC.FLAGS_EXPAND_PERPENDICULAR )
2015-02-03 20:40:21 +00:00
self._indices_to_radio_buttons[ index ] = radio_button
self._radio_buttons_to_data[ radio_button ] = data
2013-02-19 00:11:43 +00:00
2015-03-18 21:46:29 +00:00
if initial_index is not None and initial_index in self._indices_to_radio_buttons: self._indices_to_radio_buttons[ initial_index ].SetValue( True )
2015-02-03 20:40:21 +00:00
2013-02-19 00:11:43 +00:00
2015-02-03 20:40:21 +00:00
def GetSelectedClientData( self ):
2013-11-06 18:22:07 +00:00
2015-02-03 20:40:21 +00:00
for radio_button in self._radio_buttons_to_data.keys():
if radio_button.GetValue() == True: return self._radio_buttons_to_data[ radio_button ]
2013-11-06 18:22:07 +00:00
2015-02-03 20:40:21 +00:00
def SetSelection( self, index ): self._indices_to_radio_buttons[ index ].SetValue( True )
2016-03-23 19:42:56 +00:00
def SetString( self, index, text ): self._indices_to_radio_buttons[ index ].SetLabelText( text )
2015-02-03 20:40:21 +00:00