|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2008 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the shell history dialog. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 from PyQt5.QtCore import pyqtSlot, Qt, QItemSelectionModel |
|
15 from PyQt5.QtWidgets import QListWidgetItem, QDialog |
|
16 |
|
17 from .Ui_ShellHistoryDialog import Ui_ShellHistoryDialog |
|
18 |
|
19 |
|
20 class ShellHistoryDialog(QDialog, Ui_ShellHistoryDialog): |
|
21 """ |
|
22 Class implementing the shell history dialog. |
|
23 """ |
|
24 def __init__(self, history, vm, shell): |
|
25 """ |
|
26 Constructor |
|
27 |
|
28 @param history reference to the current shell history |
|
29 @type list of str |
|
30 @param vm reference to the viewmanager object |
|
31 @type ViewManager |
|
32 @param shell reference to the shell object |
|
33 @type Shell |
|
34 """ |
|
35 super(ShellHistoryDialog, self).__init__(shell) |
|
36 self.setupUi(self) |
|
37 |
|
38 self.__vm = vm |
|
39 self.__shell = shell |
|
40 |
|
41 self.historyList.addItems(history) |
|
42 index = shell.getHistoryIndex() |
|
43 if index < 0 or index >= len(history): |
|
44 self.historyList.setCurrentRow( |
|
45 self.historyList.count() - 1, QItemSelectionModel.Select) |
|
46 else: |
|
47 self.historyList.setCurrentRow(index, QItemSelectionModel.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, QItemSelectionModel.Select) |
|
132 else: |
|
133 self.historyList.setCurrentRow(index, QItemSelectionModel.Select) |
|
134 self.historyList.scrollToItem(self.historyList.currentItem()) |
|
135 |
|
136 self.historyList.setFocus(Qt.OtherFocusReason) |
|
137 |
|
138 def getHistory(self): |
|
139 """ |
|
140 Public method to retrieve the history from the dialog. |
|
141 |
|
142 @return tuple containing the list of history entries and the |
|
143 current row |
|
144 @rtype tuple of (list of str, int) |
|
145 """ |
|
146 history = [] |
|
147 for index in range(self.historyList.count()): |
|
148 history.append(self.historyList.item(index).text()) |
|
149 return history, self.historyList.currentRow() |