Coverage for C:\leo.repo\leo-editor\leo\plugins\qt_commands.py: 18%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

147 statements  

1#@+leo-ver=5-thin 

2#@+node:ekr.20110605121601.17996: * @file ../plugins/qt_commands.py 

3"""Leo's Qt-related commands defined by @g.command.""" 

4from typing import List 

5from leo.core import leoGlobals as g 

6from leo.core import leoColor 

7from leo.core import leoConfig 

8from leo.core.leoQt import QtGui, QtWidgets 

9#@+others 

10#@+node:ekr.20110605121601.18000: ** init 

11def init(): 

12 """Top-level init function for qt_commands.py.""" 

13 ok = True 

14 g.plugin_signon(__name__) 

15 g.registerHandler("select2", onSelect) 

16 return ok 

17 

18def onSelect(tag, keywords): 

19 c = keywords.get('c') or keywords.get('new_c') 

20 wdg = c.frame.top.leo_body_frame 

21 wdg.setWindowTitle(c.p.h) 

22#@+node:ekr.20110605121601.18001: ** qt: detach-editor-toggle & helpers 

23@g.command('detach-editor-toggle') 

24def detach_editor_toggle(event): 

25 """ Detach or undetach body editor """ 

26 c = event['c'] 

27 detach = True 

28 try: 

29 if c.frame.detached_body_info is not None: 

30 detach = False 

31 except AttributeError: 

32 pass 

33 if detach: 

34 detach_editor(c) 

35 else: 

36 undetach_editor(c) 

37 

38@g.command('detach-editor-toggle-max') 

39def detach_editor_toggle_max(event): 

40 """ Detach editor, maximize """ 

41 c = event['c'] 

42 detach_editor_toggle(event) 

43 if c.frame.detached_body_info is not None: 

44 wdg = c.frame.top.leo_body_frame 

45 wdg.showMaximized() 

46#@+node:ekr.20170324145714.1: *3* qt: detach_editor 

47def detach_editor(c): 

48 wdg = c.frame.top.leo_body_frame 

49 parent = wdg.parent() 

50 if parent is None: 

51 # just show if already detached 

52 wdg.show() 

53 else: 

54 c.frame.detached_body_info = parent, parent.sizes() 

55 wdg.setParent(None) 

56 sheet = c.config.getData('qt-gui-plugin-style-sheet') 

57 if sheet: 

58 sheet = '\n'.join(sheet) 

59 wdg.setStyleSheet(sheet) 

60 wdg.show() 

61#@+node:ekr.20170324145716.1: *3* qt: undetach_editor 

62def undetach_editor(c): 

63 wdg = c.frame.top.leo_body_frame 

64 parent, sizes = c.frame.detached_body_info 

65 parent.insertWidget(0, wdg) 

66 wdg.show() 

67 parent.setSizes(sizes) 

68 c.frame.detached_body_info = None 

69#@+node:ekr.20170324143944.2: ** qt: show-color-names 

70@g.command('show-color-names') 

71def showColorNames(event=None): 

72 """Put up a dialog showing color names.""" 

73 c = event.get('c') 

74 template = ''' 

75 QComboBox { 

76 background-color: %s; 

77 selection-background-color: %s; 

78 selection-color: black; 

79 }''' 

80 ivar = 'leo_settings_color_picker' 

81 if getattr(c, ivar, None): 

82 g.es('The color picker already exists in the icon bar.') 

83 else: 

84 color_list: List[str] = [] 

85 box = QtWidgets.QComboBox() 

86 

87 def onActivated(n, *args, **keys): 

88 color = color_list[n] 

89 sheet = template % (color, color) 

90 box.setStyleSheet(sheet) 

91 g.es("copied to clipboard:", color) 

92 QtWidgets.QApplication.clipboard().setText(color) 

93 

94 box.activated.connect(onActivated) 

95 color_db = leoColor.leo_color_database 

96 for key in sorted(color_db): 

97 if not key.startswith('grey'): # Use gray, not grey. 

98 val = color_db.get(key) 

99 color = QtGui.QColor(val) 

100 color_list.append(val) 

101 pixmap = QtGui.QPixmap(40, 40) 

102 pixmap.fill(color) 

103 icon = QtGui.QIcon(pixmap) 

104 box.addItem(icon, key) 

105 

106 c.frame.iconBar.addWidget(box) 

107 setattr(c, ivar, True) 

108 g.es('created color picker in icon area') 

109 # Do this last, so errors don't prevent re-execution. 

110#@+node:ekr.20170324142416.1: ** qt: show-color-wheel 

111@g.command('show-color-wheel') 

112def showColorWheel(self, event=None): 

113 """Show a Qt color dialog.""" 

114 c, p = self.c, self.c.p 

115 picker = QtWidgets.QColorDialog() 

116 in_color_setting = p.h.startswith('@color ') 

117 try: 

118 text = QtWidgets.QApplication.clipboard().text() 

119 if in_color_setting: 

120 text = p.h.split('=', 1)[1].strip() 

121 color = QtGui.QColor(text) 

122 picker.setCurrentColor(color) 

123 except(ValueError, IndexError) as e: 

124 g.trace('error caught', e) 

125 

126 result = picker.exec_() 

127 if not result: 

128 g.es("No color selected") 

129 elif in_color_setting: 

130 udata = c.undoer.beforeChangeNodeContents(p) 

131 p.h = f"{p.h.split('=', 1)[0].strip()} = {picker.selectedColor().name()}" 

132 c.undoer.afterChangeNodeContents(p, 'change-color', udata) 

133 else: 

134 text = picker.selectedColor().name() 

135 g.es("copied to clipboard:", text) 

136 QtWidgets.QApplication.clipboard().setText(text) 

137#@+node:ekr.20170324143944.3: ** qt: show-fonts 

138@g.command('show-fonts') 

139def showFonts(self, event=None): 

140 """Open a tab in the log pane showing a font picker.""" 

141 c, p = self.c, self.c.p 

142 picker = QtWidgets.QFontDialog() 

143 if p.h.startswith('@font'): 

144 (name, family, weight, slant, size) = leoConfig.parseFont(p.b) 

145 else: 

146 name, family, weight, slant, size = None, None, False, False, 12 

147 try: 

148 font = QtGui.QFont() 

149 if family: 

150 font.setFamily(family) 

151 font.setBold(weight) 

152 font.setItalic(slant) 

153 font.setPointSize(size) 

154 picker.setCurrentFont(font) 

155 except ValueError: 

156 pass 

157 result = picker.exec_() 

158 if not result: 

159 g.es("No font selected") 

160 else: 

161 font = picker.selectedFont() 

162 udata = c.undoer.beforeChangeNodeContents(p) 

163 comments = [x for x in g.splitLines(p.b) if x.strip().startswith('#')] 

164 defs = [ 

165 '\n' if comments else '', 

166 f"{name}_family = {font.family()}\n", 

167 f"{name}_weight = {'bold' if font.bold() else 'normal'}\n", 

168 f"{name}_slant = {'italic' if font.italic() else 'roman'}\n", 

169 f"{name}_size = {font.pointSizeF()}\n" 

170 ] 

171 p.b = ''.join(comments + defs) 

172 c.undoer.afterChangeNodeContents(p, 'change-font', udata) 

173#@+node:ekr.20140918124632.17893: ** qt: show-style-sheet 

174@g.command('show-style-sheet') 

175def print_style_sheet(event): 

176 """show-style-sheet command.""" 

177 c = event.get('c') 

178 if c: 

179 c.styleSheetManager.print_style_sheet() 

180#@+node:ekr.20140918124632.17891: ** qt: style-reload 

181@g.command('style-reload') 

182@g.command('reload-style-sheets') 

183def style_reload(event): 

184 """reload-styles command. 

185 

186 Find the appropriate style sheet and re-apply it. 

187 

188 This replaces execution of the `stylesheet & source` node in settings files. 

189 """ 

190 c = event.get('c') 

191 if c and c.styleSheetManager: 

192 c.reloadSettings() 

193 # Call ssm.reload_settings after reloading all settings. 

194#@+node:ekr.20140918124632.17892: ** qt: style-set-selected 

195@g.command('style-set-selected') 

196def style_set_selected(event): 

197 """style-set-selected command. Set the global stylesheet to c.p.b. (For testing)""" 

198 c = event.get('c') 

199 if c: 

200 c.styleSheetManager.set_selected_style_sheet() 

201#@-others 

202#@@language python 

203#@@tabwidth -4 

204#@@pagewidth 70 

205#@-leo