Skip to content

Commit

Permalink
Use flake8 to fix remaining linting, but sxtwl is not included.
Browse files Browse the repository at this point in the history
  • Loading branch information
cary-rowen committed Apr 17, 2024
1 parent c3eb98e commit d410bee
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 61 deletions.
12 changes: 9 additions & 3 deletions addon/globalPlugins/clipboardEnhancement/NAVScreenshot.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# The following code is borrowed from NVBox
import api, ui, winGDI
import api
import ui
import winGDI
import vision
from contentRecog import RecogImageInfo
from ctypes import windll
from . import bitmap
gdi32 = windll.gdi32
user32 = windll.user32


def navigatorObjectScreenshot():
# 获取导航对象
nav = api.getNavigatorObject()
try: # 得到导航对象的位置和大小
try:
# 得到导航对象的位置和大小
left, top, width, height = nav.location
except TypeError:
ui.message(_("Content is not visible"))
Expand All @@ -26,12 +30,14 @@ def navigatorObjectScreenshot():
gdi32.StretchBlt(memDC, 0, 0, width, height, screenDC, left, top, width, height, winGDI.SRCCOPY)
# 把数据放入剪贴板
bitmap.copyBitmapToClip(memBitmap)
finally: # 清理现场
finally:
# 清理现场
gdi32.SelectObject(memDC, oldBitmap)
gdi32.DeleteObject(memBitmap)
gdi32.DeleteDC(memDC)
user32.ReleaseDC(0, screenDC)


def isScreenCurtainRunning():
from visionEnhancementProviders.screenCurtain import ScreenCurtainProvider
screenCurtainId = ScreenCurtainProvider.getSettings().getId()
Expand Down
99 changes: 62 additions & 37 deletions addon/globalPlugins/clipboardEnhancement/bitmap.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# The following code is borrowed from NVBox
import io, sys
import io
import sys
from ctypes import c_buffer, POINTER, byref, cast, Structure, sizeof, windll, wintypes, WinError
import winGDI, winKernel, winUser
import winGDI
import winKernel
import winUser
from contentRecog import RecogImageInfo
from logHandler import log

user32 = windll.user32
gdi32 = windll.gdi32
shell32 = windll.shell32
Expand All @@ -13,55 +18,64 @@
CF_ENHMETAFILE = 14
CF_HDROP = 15
CF_DSPBITMAP = 130
#定义一个BMP文件信息头结构体


# 定义一个BMP文件信息头结构体
class BITMAPFILEHEADER(Structure):
_fields_=[
_fields_ = [
('bfType', wintypes.WORD),
('bfSize', wintypes.DWORD),
('bfReserved1', wintypes.WORD),
('bfReserved2', wintypes.WORD),
('bfOffBits', wintypes.DWORD)
]
#获取二进制数据的方法

# 获取二进制数据的方法
def getBytes(self):
bytesIo = io.BytesIO()
for field in self.__class__._fields_:
bytesIo.write(eval('self.%s' % field[0]).to_bytes(length = sizeof(field[1]), byteorder='little'))
bytesIo.write(eval('self.%s' % field[0]).to_bytes(length=sizeof(field[1]), byteorder='little'))
bytesIo.flush()
try:
return bytesIo.getvalue()
finally:
bytesIo.close()
#生成位图信息头


# 生成位图信息头
def getBitmapInfo(imgInfo):
width, height = imgInfo.recogWidth, imgInfo.recogHeight
bmInfo=winGDI.BITMAPINFOHEADER()
bmInfo = winGDI.BITMAPINFOHEADER()
bmInfo.biSize = sizeof(bmInfo)
bmInfo.biWidth=width
bmInfo.biHeight=height*-1
bmInfo.biPlanes=1
bmInfo.biBitCount=32
bmInfo.biSizeImage=width * height * bmInfo.biBitCount // 8
bmInfo.biCompression=winGDI.BI_RGB
bmInfo.biXPelsPerMeter=bmInfo.biYPelsPerMeter=2834
bmInfo.biWidth = width
bmInfo.biHeight = height * -1
bmInfo.biPlanes = 1
bmInfo.biBitCount = 32
bmInfo.biSizeImage = width * height * bmInfo.biBitCount // 8
bmInfo.biCompression = winGDI.BI_RGB
bmInfo.biXPelsPerMeter = bmInfo.biYPelsPerMeter = 2834
return bmInfo


# 打开内存图像文件
def openMemoryImageFile(imageInfo, pixels):
#生成位图信息头
bitmapInfo=getBitmapInfo(imageInfo)
#构造BMP文件信息头
fileInfo=BITMAPFILEHEADER()
fileInfo.bfType=0x4d42 #BM文件标志
fileInfo.bfOffBits=14 + bitmapInfo.biSize #保存像素数据的字节位置
fileInfo.bfSize=bitmapInfo.biSizeImage + fileInfo.bfOffBits #文件总大小
fileInfo.bfReserved1=fileInfo.bfReserved2=0 #保留字段
#创建内存文件IO
# 生成位图信息头
bitmapInfo = getBitmapInfo(imageInfo)
# 构造BMP文件信息头
fileInfo = BITMAPFILEHEADER()
fileInfo.bfType = 0x4d42 # BM文件标志
fileInfo.bfOffBits = 14 + bitmapInfo.biSize # 保存像素数据的字节位置
fileInfo.bfSize = bitmapInfo.biSizeImage + fileInfo.bfOffBits # 文件总大小
fileInfo.bfReserved1 = fileInfo.bfReserved2 = 0 # 保留字段
# 创建内存文件IO
imgBytesIo = io.BytesIO()
imgBytesIo.write(fileInfo.getBytes()) #写入文件头部信息
imgBytesIo.write(bitmapInfo) #写入位图信息
imgBytesIo.write(pixels) #写入像素点
imgBytesIo.write(fileInfo.getBytes()) # 写入文件头部信息
imgBytesIo.write(bitmapInfo) # 写入位图信息
imgBytesIo.write(pixels) # 写入像素点
imgBytesIo.flush()
return imgBytesIo


# 复制图片到剪贴板
def copyBitmapToClip(bitmap):
hbitmap = bitmap
Expand All @@ -70,44 +84,51 @@ def copyBitmapToClip(bitmap):
winUser.emptyClipboard()
if not user32.SetClipboardData(CF_BITMAP, hbitmap):
raise WinError()


# 从剪贴板获取图片信息
def getClipBitmapInfo():
with winUser.openClipboard(None):
handle = user32.GetClipboardData(CF_DIB)
if not handle:
raise WinError(-1, 'No bitmap handle.')
# 拿到图片信息
with winKernel.HGLOBAL(handle, autoFree = False).lock() as addr:
with winKernel.HGLOBAL(handle, autoFree=False).lock() as addr:
bmInfo = winGDI.BITMAPINFO()
bmInfo.bmiHeader = cast(addr, POINTER(winGDI.BITMAPINFOHEADER)).contents
return bmInfo


# 从剪贴板获取图像
def getClipBitmap(bmInfo):
with winUser.openClipboard(None):
handle = user32.GetClipboardData(CF_BITMAP)
if not handle:
raise WinError(-1, f'No bitmap handle.')
raise WinError(-1, 'No bitmap handle.')
# 宽度和高度
width, height = bmInfo.bmiHeader.biWidth, bmInfo.bmiHeader.biHeight
# 翻转图像
bmInfo.bmiHeader.biHeight = height * -1
try: # 显示图像到设备
try: # 显示图像到设备
screenDC = user32.GetDC(0)
memDC = gdi32.CreateCompatibleDC(screenDC)
oldBitmap = gdi32.SelectObject(memDC, handle)
# 得到像素点
buffer=(winGDI.RGBQUAD * width * height)()
buffer = (winGDI.RGBQUAD * width * height)()
gdi32.GetDIBits(memDC, handle, 0, height, buffer, byref(bmInfo), winGDI.DIB_RGB_COLORS)
return buffer
finally: # 清理现场
finally: # 清理现场
gdi32.SelectObject(memDC, oldBitmap)
gdi32.DeleteDC(memDC)
user32.ReleaseDC(0, screenDC)


def getClipImageFileList():
files = set()
with winUser.openClipboard(None):
handle = user32.GetClipboardData(CF_HDROP)
if not handle: return
if not handle:
return
FS_ENCODING = sys.getfilesystemencoding()
fileCount = shell32.DragQueryFile(handle, -1, None, 0)
for index in range(fileCount):
Expand All @@ -117,17 +138,21 @@ def getClipImageFileList():
filename = buf.value.decode('gbk')
except UnicodeDecodeError:
filename = buf.value.decode(FS_ENCODING)
except: continue
except Exception as e:
log.warning(f"An unexpected error occurred: {e}")
continue
if filename.endswith('.jpg') or filename.endswith('.png') or filename.endswith('.bmp'):
files.add(filename)
return files


def getClipImage():
files = getClipImageFileList()
if files:
with open(files.pop(), 'rb') as f:
f.seek(16) # 跳到 PNG 文件的第 16 个字节位置
width = int.from_bytes(f.read(4), byteorder='big') # 宽度
height = int.from_bytes(f.read(4), byteorder='big') # 高度
f.seek(16) # 跳到 PNG 文件的第 16 个字节位置
width = int.from_bytes(f.read(4), byteorder='big') # 宽度
height = int.from_bytes(f.read(4), byteorder='big') # 高度
f.seek(0)
imgInfo = RecogImageInfo(0, 0, width, height, 1)
return imgInfo, f.read()
Expand Down
1 change: 0 additions & 1 deletion addon/globalPlugins/clipboardEnhancement/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def get_lunar_date(day):
# 以立春为界的农历
s = "%s%s月%s" % ( # day.getLunarYear(False),
'闰' if day.isLunarLeap() else '', constants.Ymc[day.getLunarMonth()], constants.rmc[day.getLunarDay() - 1])
# s = s + '\n今日 %s %s' % (constants.jqmc[day.getJieQi()], tt)
return s


Expand Down
15 changes: 9 additions & 6 deletions addon/globalPlugins/clipboardEnhancement/clipEditor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import re

import api
Expand All @@ -13,7 +12,8 @@ def __init__(self, *args, **kw):
wx.Frame.__init__(self, size=(600, 460), *args, **kw)
self.is_exit = False
pnl = wx.Panel(self)
self.edit = wx.TextCtrl(pnl, pos=(0, 0), style=wx.TE_PROCESS_TAB | wx.TE_MULTILINE | wx.TE_RICH2 | wx.HSCROLL)
self.edit = wx.TextCtrl(
pnl, pos=(0, 0), style=wx.TE_PROCESS_TAB | wx.TE_MULTILINE | wx.TE_RICH2 | wx.HSCROLL)
menubar = wx.MenuBar()
file_menu = wx.Menu()
file_menu.Append(wx.ID_OPEN, '打开(&O)\tCtrl+O', '打开文件')
Expand Down Expand Up @@ -179,16 +179,17 @@ def on_find_close(self, event):

def on_goto(self, event):
line = self.edit.PositionToXY(self.edit.GetInsertionPoint())[2]
dlg = wx.NumberEntryDialog(self, '', '行号(&L)', '转到指定行', line+1, 1, self.edit.GetNumberOfLines())
dlg = wx.NumberEntryDialog(self, '', '行号(&L)', '转到指定行', line + 1, 1, self.edit.GetNumberOfLines())
if dlg.ShowModal() == wx.ID_OK:
point = self.edit.coordinateToPosition(0, dlg.GetValue()-1)
point = self.edit.coordinateToPosition(0, dlg.GetValue() - 1)
self.edit.SetInsertionPoint(point)

def on_open(self, event):
wildcard = '文本文档 (*.txt)|*.txt|' \
'所有文件 (*.*)|*.*'
filename = ''
dlg = wx.FileDialog(self, message='选择一个文件', defaultDir='', defaultFile="", wildcard=wildcard, style=wx.FD_OPEN | wx.FD_CHANGE_DIR | wx.FD_FILE_MUST_EXIST)
dlg = wx.FileDialog(self, message='选择一个文件', defaultDir='', defaultFile="", wildcard=wildcard,
style=wx.FD_OPEN | wx.FD_CHANGE_DIR | wx.FD_FILE_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
filename = dlg.GetPath()
dlg.Destroy()
Expand All @@ -198,6 +199,7 @@ def on_open(self, event):
with open(filename) as f:
self.edit.SetValue(f.read())
except Exception as e:
log.warning(f"An unexpected error occurred: {e}")
try:
with open(filename, encoding='utf-8') as f:
self.edit.SetValue(f.read())
Expand All @@ -209,7 +211,8 @@ def on_save_as(self, evt):
wildcard = '文本文档 (*.txt)|*.txt|' \
'所有文件 (*.*)|*.*'
filepath = ''
dlg = wx.FileDialog(self, message='文件另存为...', defaultDir='', defaultFile='', wildcard=wildcard, style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
dlg = wx.FileDialog(self, message='文件另存为...', defaultDir='', defaultFile='', wildcard=wildcard,
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
filepath = dlg.GetPath()
dlg.Destroy()
Expand Down
10 changes: 5 additions & 5 deletions addon/globalPlugins/clipboardEnhancement/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
Zhi = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
ShX = ["鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"]
jqmc = ["冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏",
"小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑","白露", "秋分", "寒露", "霜降",
"立冬", "小雪", "大雪"]
"小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降",
"立冬", "小雪", "大雪"]
Ymc = ('正', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '腊')
rmc = ["初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
"十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
"廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十", "卅一"]
rmc = ["初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
"十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
"廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十", "卅一"]
XiZ = ['摩羯', '水瓶', '双鱼', '白羊', '金牛', '双子', '巨蟹', '狮子', '处女', '天秤', '天蝎', '射手']
WeekCn = "一二三四五六日"
5 changes: 5 additions & 0 deletions addon/globalPlugins/clipboardEnhancement/cues.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import os
from nvwave import playWaveFile


def Copy():
play_sound("Copy")


def FileInClipboard():
play_sound("FileInClipboard")


def StartOrEnd():
play_sound("StartOrEnd")


def LineBoundary():
play_sound("LineBoundary")


def play_sound(filename):
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'sounds', filename))
return playWaveFile(path + ".wav")
11 changes: 5 additions & 6 deletions addon/globalPlugins/clipboardEnhancement/reReplace.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import wx
import os


class ReDialog(wx.Dialog):
def __init__(self, parent, title='正则替换'):
super().__init__(parent, title=title, size=(300, 180))
box = wx.BoxSizer(wx.VERTICAL)
v1 = wx.BoxSizer(wx.VERTICAL)
st = wx.StaticText(self, -1, label='查找内容(&N)')
self.cb=wx.ComboBox(self, -1, style=wx.CB_DROPDOWN)
self.cb = wx.ComboBox(self, -1, style=wx.CB_DROPDOWN)
v1.Add(st, 1)
v1.Add(self.cb, 2)
st2 = wx.StaticText(self, -1, label='替换为(&P)')
self.cb2=wx.ComboBox(self, -1, style=wx.CB_DROPDOWN)
self.cb2 = wx.ComboBox(self, -1, style=wx.CB_DROPDOWN)
v1.Add(st2, 1)
v1.Add(self.cb2, 2)
r1=wx.BoxSizer(wx.HORIZONTAL)
b_ok=wx.Button(self, wx.ID_OK, label='替换(&R)')
r1 = wx.BoxSizer(wx.HORIZONTAL)
b_ok = wx.Button(self, wx.ID_OK, label='替换(&R)')
b_cancel = wx.Button(self, wx.ID_CANCEL, label='取消')
r1.Add(b_ok, 1)
r1.Add(b_cancel, 1)
box.Add(v1, 1)
box.Add(r1, 1)
self.SetSizer(box)

Loading

0 comments on commit d410bee

Please sign in to comment.