EverythingResultWin.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import sys
  2. import os
  3. import subprocess
  4. import pyperclip
  5. from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTableWidgetItem, QVBoxLayout, QMenu, QAction, \
  6. QLineEdit, QHeaderView
  7. from PyQt5.QtCore import Qt
  8. from win import EverythingApi
  9. class ResultWindow(QWidget):
  10. def __init__(self, data):
  11. super().__init__()
  12. self.title = "搜索结果"
  13. self.left = 200
  14. self.top = 200
  15. self.width = 980
  16. self.height = 800
  17. self.data = data
  18. self.initUI()
  19. def initUI(self):
  20. self.setWindowTitle(self.title)
  21. self.setGeometry(self.left, self.top, self.width, self.height)
  22. # 创建搜索框
  23. self.searchInput = QLineEdit(self)
  24. self.searchInput.setPlaceholderText("Search...")
  25. self.searchInput.returnPressed.connect(self.searchFiles)
  26. # 创建表格
  27. self.createTable()
  28. # 设置窗口的布局
  29. layout = QVBoxLayout()
  30. layout.addWidget(self.searchInput)
  31. layout.addWidget(self.tableWidget)
  32. self.setLayout(layout)
  33. # 显示窗口
  34. # self.show()
  35. def createTable(self):
  36. # 创建一个QTableWidget
  37. self.tableWidget = QTableWidget()
  38. # 设置列数
  39. self.tableWidget.setColumnCount(4)
  40. self.tableWidget.setHorizontalHeaderLabels(["名称", "路径", "大小", "修改时间"])
  41. # 添加数据到表格
  42. self.tableWidget.setRowCount(len(self.data))
  43. for row_index, row_data in enumerate(self.data):
  44. self.tableWidget.setItem(row_index, 0, QTableWidgetItem(row_data['file_name']))
  45. self.tableWidget.setItem(row_index, 1, QTableWidgetItem(row_data['file_path']))
  46. self.tableWidget.setItem(row_index, 2, QTableWidgetItem(row_data['file_size']))
  47. self.tableWidget.setItem(row_index, 3, QTableWidgetItem(row_data['modified_time']))
  48. # 设置列宽比例
  49. total_width = self.width
  50. self.tableWidget.setColumnWidth(0, int(total_width * 0.30)) # 30% for File Name
  51. self.tableWidget.setColumnWidth(1, int(total_width * 0.35)) # 35% for Path
  52. self.tableWidget.setColumnWidth(2, int(total_width * 0.10)) # 15% for Size
  53. self.tableWidget.setColumnWidth(3, int(total_width * 0.25)) # 20% for Modified Time
  54. # 设置列宽为用户可调整
  55. header = self.tableWidget.horizontalHeader()
  56. header.setSectionResizeMode(QHeaderView.Interactive)
  57. header.setSectionResizeMode(3, QHeaderView.Stretch)
  58. # 允许排序并设置默认不排序
  59. self.tableWidget.setSortingEnabled(True)
  60. self.tableWidget.sortByColumn(-1, Qt.AscendingOrder) # 初始化时不排序任何列
  61. # 为表格添加右键菜单支持
  62. self.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu)
  63. self.tableWidget.customContextMenuRequested.connect(self.showContextMenu)
  64. def showContextMenu(self, pos):
  65. contextMenu = QMenu(self)
  66. openFileAction = QAction("打开文件", self)
  67. openPathAction = QAction("打开文件路径", self)
  68. copyPathAction = QAction("复制文件路径和名称", self)
  69. contextMenu.addAction(openFileAction)
  70. contextMenu.addAction(openPathAction)
  71. contextMenu.addAction(copyPathAction)
  72. action = contextMenu.exec_(self.mapToGlobal(pos))
  73. if action == openFileAction:
  74. self.openFile()
  75. elif action == openPathAction:
  76. self.openPath()
  77. elif action == copyPathAction:
  78. self.copyPathAndFilename()
  79. def openFile(self):
  80. row = self.tableWidget.currentRow()
  81. if row >= 0:
  82. path = os.path.join(self.tableWidget.item(row, 1).text(), self.tableWidget.item(row, 0).text())
  83. subprocess.Popen(['explorer', path])
  84. def openPath(self):
  85. row = self.tableWidget.currentRow()
  86. if row >= 0:
  87. path = self.tableWidget.item(row, 1).text()
  88. subprocess.Popen(['explorer', '/select,', path])
  89. def copyPathAndFilename(self):
  90. row = self.tableWidget.currentRow()
  91. if row >= 0:
  92. full_path = os.path.join(self.tableWidget.item(row, 1).text(), self.tableWidget.item(row, 0).text())
  93. pyperclip.copy(full_path)
  94. def searchFiles(self):
  95. search_text = self.searchInput.text().strip()
  96. if search_text: # 确保搜索框不为空
  97. # 使用Everything API进行搜索
  98. e_api = EverythingApi.Everything()
  99. new_data = e_api.searchfile(search_text)
  100. self.updateTable(new_data)
  101. def searchIatFiles(self, search_text):
  102. if search_text: # 确保搜索框不为空
  103. self.searchInput.setText(search_text)
  104. # 使用Everything API进行搜索
  105. e_api = EverythingApi.Everything()
  106. new_data = e_api.searchfile(search_text)
  107. self.updateTable(new_data)
  108. def updateTable(self, data):
  109. self.tableWidget.setRowCount(len(data))
  110. for row_index, row_data in enumerate(data):
  111. self.tableWidget.setItem(row_index, 0, QTableWidgetItem(row_data['file_name']))
  112. self.tableWidget.setItem(row_index, 1, QTableWidgetItem(row_data['file_path']))
  113. self.tableWidget.setItem(row_index, 2, QTableWidgetItem(row_data['file_size']))
  114. self.tableWidget.setItem(row_index, 3, QTableWidgetItem(row_data['modified_time']))
  115. self.tableWidget.sortByColumn(-1, Qt.AscendingOrder)
  116. if __name__ == "__main__":
  117. app = QApplication(sys.argv)
  118. data = [] # Placeholder for data
  119. ex = ResultWindow(data)
  120. ex.show()
  121. sys.exit(app.exec_())