Krita Source Code Documentation
Loading...
Searching...
No Matches
assist.py
Go to the documentation of this file.
2# SPDX-License-Identifier: GPL-3.0-or-later
3#
4
5try:
6 from PyQt6.QtCore import QTimer, Qt
7 from PyQt6.QtWidgets import (QApplication, QListWidget, QListWidgetItem, QTextBrowser,
8 QVBoxLayout, QWidget)
9except:
10 from PyQt5.QtCore import QTimer, Qt
11 from PyQt5.QtWidgets import (QApplication, QListWidget, QListWidgetItem, QTextBrowser,
12 QVBoxLayout, QWidget)
13
14
15class PopupWidget(QWidget):
16
17 def __init__(self, textedit):
18 flags = Qt.WindowType.ToolTip
19 flags = Qt.WindowType.Window | Qt.WindowType.FramelessWindowHint | \
20 Qt.WindowType.CustomizeWindowHint | Qt.WindowType.X11BypassWindowManagerHint
21 QWidget.__init__(self, None, flags)
22 self.textedit = textedit
23 self.vlayout = QVBoxLayout(self)
24 self.vlayout.setContentsMargins(0, 0, 0, 0)
25 self.init_popup()
26 self.show()
27 self.hidehide()
28 self.active = False
29
30 def show(self, timeout=0, above=False):
31 self.cursor_start_col = self.textedit.textCursor().columnNumber()
32 desktop = QApplication.instance().desktop()
33 screen = desktop.screen(desktop.screenNumber(self))
34 screen_width = screen.width()
35 screen_height = screen.height()
36 win_width = self.width()
37 win_height = self.height()
38 cursorRect = self.textedit.cursorRect()
39 if above:
40 pos = self.textedit.mapToGlobal(cursorRect.topLeft())
41 pos.setY(pos.y() - win_height)
42 else:
43 pos = self.textedit.mapToGlobal(cursorRect.bottomLeft())
44 if pos.y() < 0:
45 pos = self.textedit.mapToGlobal(cursorRect.bottomLeft())
46 if pos.y() + win_height > screen_height:
47 pos = self.textedit.mapToGlobal(cursorRect.topLeft())
48 pos.setY(pos.y() - win_height)
49 if pos.x() + win_width > screen_width:
50 pos.setX(screen_width - win_width)
51
52 self.move(pos)
53 QWidget.show(self)
54 self.active = True
55 if timeout:
56 QTimer.singleShot(timeout * 1000, self.hidehide)
57
58 def hide(self):
59 self.active = False
60 QWidget.hide(self)
61
62
64
65 def init_popup(self):
66 self.browser = QTextBrowser(self)
67 self.layout().addWidget(self.browser)
68
69
70class AutoCompleteItem(QListWidgetItem):
71
72 def __init__(self, item):
73 QListWidgetItem.__init__(self)
74 value = item.name
75 self.setText(value)
76 self.value = value
77 self.kind = item.kind
78
79
81
82 def init_popup(self):
83 self.list = QListWidget(self)
84 self.list.itemClicked.connect(self.insertIteminsertItem)
85 self.layout().addWidget(self.list)
86 self.items = []
87
88 def insertItem(self, item):
89 self.insert()
90
91 def insert(self):
92 completion = self.items[self.list.currentRow()].value
93 cursor = self.textedit.textCursor()
94 col = cursor.columnNumber()
95 line = unicode(cursor.block().text())
96 i = self.cursor_start_col
97 while i > 0:
98 # print(`line[i:col]`)
99 if completion.startswith(line[i:col]):
100 # print("break")
101 break
102 i -= 1
103 # print(col,i)
104 cursor.insertText(completion[col - i:])
105 self.hidehide()
106
107 def setItems(self, proposals):
108 proposals = sorted(proposals, cmp=lambda p1, p2: cmp(p1.name, p2.name))
109 del self.items[:]
110 self.list.clear()
111 for entry in proposals:
112 i = AutoCompleteItem(entry)
113 self.list.addItem(i)
114 self.items.append(i)
115
116 def keyPressEvent(self, event):
117 self.list.keyPressEvent(event)
118 key = event.key()
119 text = event.text()
120 if key in [Qt.Key.Key_Right, Qt.Key.Key_Enter, Qt.Key.Key_Return]:
121 text = ""
122 cursor = self.textedit.textCursor()
123 line = unicode(cursor.block().text())
124 col = cursor.columnNumber()
125 prefix = line[self.cursor_start_col:col] + unicode(text)
126
127 found = False
128 for row, item in enumerate(self.items):
129 if item.value.startswith(prefix):
130 current = self.items[self.list.currentRow()].value
131 if not current.startswith(prefix):
132 self.list.setCurrentRow(row)
133 found = True
134 break
135 if not found:
136 self.hidehide()
137 return
138
139 if key in [Qt.Key.Key_Up, Qt.Key.Key_Down, Qt.Key.Key_PageUp, Qt.Key.Key_PageDown]:
140 return True
141 elif key in [Qt.Key.Key_Tab, Qt.Key.Key_Right, Qt.Key.Key_Enter, Qt.Key.Key_Return]:
142 self.insert()
143 return True
144 elif not text:
145 self.hidehide()
show(self, timeout=0, above=False)
Definition assist.py:30