src/eric7/QScintilla/ShellHistoryDialog.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2008 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the shell history dialog.
8 """
9
10 import os
11
12 from PyQt6.QtCore import pyqtSlot, Qt, QItemSelectionModel
13 from PyQt6.QtWidgets import QListWidgetItem, QDialog
14
15 from .Ui_ShellHistoryDialog import Ui_ShellHistoryDialog
16
17
18 class ShellHistoryDialog(QDialog, Ui_ShellHistoryDialog):
19 """
20 Class implementing the shell history dialog.
21 """
22 def __init__(self, history, vm, shell):
23 """
24 Constructor
25
26 @param history reference to the current shell history
27 @type list of str
28 @param vm reference to the viewmanager object
29 @type ViewManager
30 @param shell reference to the shell object
31 @type Shell
32 """
33 super().__init__(shell)
34 self.setupUi(self)
35
36 self.__vm = vm
37 self.__shell = shell
38
39 self.historyList.addItems(history)
40 index = shell.getHistoryIndex()
41 if index < 0 or index >= len(history):
42 self.historyList.setCurrentRow(
43 self.historyList.count() - 1,
44 QItemSelectionModel.SelectionFlag.Select)
45 else:
46 self.historyList.setCurrentRow(
47 index, QItemSelectionModel.SelectionFlag.Select)
48 self.historyList.scrollToItem(self.historyList.currentItem())
49
50 @pyqtSlot()
51 def on_historyList_itemSelectionChanged(self):
52 """
53 Private slot to handle a change of the selection.
54 """
55 selected = len(self.historyList.selectedItems()) > 0
56 self.copyButton.setEnabled(selected and
57 self.__vm.activeWindow() is not None)
58 self.deleteButton.setEnabled(selected)
59 self.executeButton.setEnabled(selected)
60
61 @pyqtSlot(QListWidgetItem)
62 def on_historyList_itemDoubleClicked(self, item):
63 """
64 Private slot to handle a double click of an item.
65
66 @param item reference to the item that was double clicked
67 (QListWidgetItem)
68 """
69 self.on_executeButton_clicked()
70
71 @pyqtSlot()
72 def on_deleteButton_clicked(self):
73 """
74 Private slot to delete the selected entries from the history.
75 """
76 for itm in self.historyList.selectedItems():
77 ditm = self.historyList.takeItem(self.historyList.row(itm))
78 del ditm
79 self.historyList.scrollToItem(self.historyList.currentItem())
80 self.historyList.setFocus()
81
82 @pyqtSlot()
83 def on_copyButton_clicked(self):
84 """
85 Private slot to copy the selected entries to the current editor.
86 """
87 aw = self.__vm.activeWindow()
88 if aw is not None:
89 lines = []
90 for index in range(self.historyList.count()):
91 # selectedItems() doesn't seem to preserve the order
92 itm = self.historyList.item(index)
93 if itm.isSelected():
94 lines.append(itm.text())
95 eol = aw.getLineSeparator()
96 txt = eol.join(lines) + eol
97 aw.insert(txt)
98 self.historyList.setFocus()
99
100 @pyqtSlot()
101 def on_executeButton_clicked(self):
102 """
103 Private slot to execute the selected entries in the shell.
104 """
105 lines = []
106 for index in range(self.historyList.count()):
107 # selectedItems() doesn't seem to preserve the order
108 itm = self.historyList.item(index)
109 if itm.isSelected():
110 lines.append(itm.text())
111 cmds = os.linesep.join(lines) + os.linesep
112 self.__shell.executeLines(
113 cmds,
114 historyIndex=self.historyList.currentRow())
115
116 # reload the list because shell modified it
117 self.on_reloadButton_clicked()
118
119 @pyqtSlot()
120 def on_reloadButton_clicked(self):
121 """
122 Private slot to reload the history.
123 """
124 history = self.__shell.getHistory(None)
125 index = self.__shell.getHistoryIndex()
126
127 self.historyList.clear()
128 self.historyList.addItems(history)
129 if index < 0 or index >= len(history):
130 self.historyList.setCurrentRow(
131 self.historyList.count() - 1,
132 QItemSelectionModel.SelectionFlag.Select)
133 else:
134 self.historyList.setCurrentRow(
135 index, QItemSelectionModel.SelectionFlag.Select)
136 self.historyList.scrollToItem(self.historyList.currentItem())
137
138 self.historyList.setFocus(Qt.FocusReason.OtherFocusReason)
139
140 def getHistory(self):
141 """
142 Public method to retrieve the history from the dialog.
143
144 @return tuple containing the list of history entries and the
145 current row
146 @rtype tuple of (list of str, int)
147 """
148 history = []
149 for index in range(self.historyList.count()):
150 history.append(self.historyList.item(index).text())
151 return history, self.historyList.currentRow()

eric ide

mercurial