Passed
Push — master ( acaf9b...fbbf97 )
by Yoshihiro
44s queued 10s
created

SF   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 99
dl 0
loc 209
rs 10
c 0
b 0
f 0
wmc 22

9 Methods

Rating   Name   Duplication   Size   Complexity  
A SubFunctionClass.mouse_image_scroll() 0 33 4
A SubFunctionClass.__init__() 0 8 1
A SubFunctionClass.update_line_numbers() 0 34 3
A SubFunctionClass.resize_gif() 0 22 4
A SubFunctionClass.change_setting() 0 15 1
A SubFunctionClass.print_gif() 0 17 2
A SubFunctionClass.clear_btn_click() 0 16 2
A SubFunctionClass.mouse_y_scroll() 0 14 3
A SubFunctionClass.btn_click() 0 29 2
1
import os
2
import shutil
3
import tkinter as tk
4
import tkinter.filedialog as filedialog
5
6
from PIL import Image, ImageTk
7
8
9
class SubFunctionClass():
10
    def __init__(self, app, tree_folder):
11
        """
12
        Args:
13
            app (instance): lineframeインスタンス
14
15
        """
16
        self.zoom = 0
17
        self.APP = app
18
19
    def mouse_y_scroll(self, event=None):
20
        """マウスホイール移動の設定
21
22
        ・イメージキャンバスでマウスホイールを回したときにイメージキャンバス
23
        をスクロールする。
24
25
        Args:
26
            event (instance): tkinter.Event のインスタンス
27
28
        """
29
        if event.delta > 0:
30
            self.APP.image_space.yview_scroll(-1, 'units')
31
        elif event.delta < 0:
32
            self.APP.image_space.yview_scroll(1, 'units')
33
34
    def mouse_image_scroll(self, event=None):
35
        """Ctrl+マウスホイールの拡大縮小設定
36
37
        ・イメージキャンバスでCtrl+マウスホイールを回したときに画像を
38
        拡大縮小する。
39
40
        Args:
41
            event (instance): tkinter.Event のインスタンス
42
43
        """
44
        curItem = self.APP.tree.focus()
45
        self.APP.lmc.select_list_item = self.APP.tree.item(curItem)["text"]
46
        title = "./data/image/{0}.txt".format(
47
            self.APP.lmc.select_list_item
48
        )
49
        f = open(title, 'r', encoding='utf-8')
50
        zoom = f.read()
51
        self.zoom = int(zoom)
52
        f.close()
53
        if event.delta > 0:
54
            self.zoom -= 5
55
            if self.zoom < 10:
56
                self.zoom = 10
57
        elif event.delta < 0:
58
            self.zoom += 5
59
60
        f = open(title, 'w', encoding='utf-8')
61
        f.write(str(self.zoom))
62
        f.close()
63
        self.APP.lmc.path_read_image(
64
                    'data/image',
65
                    self.APP.lmc.select_list_item,
66
                    self.zoom
67
                )
68
69
    def btn_click(self, event=None):
70
        """似顔絵ボタンを押したとき
71
72
        ・似顔絵ボタンを押したときに画像イメージを似顔絵フレームに
73
        貼り付ける。
74
75
        Args:
76
            event (instance): tkinter.Event のインスタンス
77
78
        """
79
        fTyp = [(u"gif画像", ".gif")]
80
        iDir = os.path.abspath(os.path.dirname(__file__))
81
        self.APP.filepath = filedialog.askopenfilename(
82
            filetypes=fTyp,
83
            initialdir=iDir
84
        )
85
        if not self.APP.filepath == "":
86
            path, ___ = os.path.splitext(
87
                os.path.basename(self.APP.fmc.now_path)
88
            )
89
            ____, ext = os.path.splitext(os.path.basename(self.APP.filepath))
90
            title = shutil.copyfile(
91
                self.APP.filepath,
92
                "./data/character/{0}{1}".format(
93
                    path,
94
                    ext
95
                )
96
            )
97
            self.print_gif(title)
98
99
    def clear_btn_click(self, event=None):
100
        """消去ボタンをクリックしたとき
101
102
        ・消去ボタンをクリックしたときに画像イメージから画像を
103
        削除する。
104
105
        Args:
106
            event (instance): tkinter.Event のインスタンス
107
108
        """
109
        files = "./data/character/{0}.gif".format(
110
            self.APP.lmc.select_list_item
111
        )
112
        if os.path.isfile(files):
113
            os.remove(files)
114
            self.APP.cv.delete("all")
115
116
    def resize_gif(self, im):
117
        """画像をリサイズする
118
119
        ・イメージファイルを縦が長いときは縦を、横が長いときは横を、
120
        同じときは両方を150pxに設定する。
121
122
        Args:
123
            im (instance): イメージインスタンス
124
125
        Returns:
126
            instance: イメージインスタンス
127
128
        """
129
        if im.size[0] == im.size[1]:
130
            resized_image = im.resize((150, 150))
131
        elif im.size[0] > im.size[1]:
132
            zoom = int(im.size[1] * 150 / im.size[0])
133
            resized_image = im.resize((150, zoom))
134
        elif im.size[0] < im.size[1]:
135
            zoom = int(im.size[0] * 200 / im.size[1])
136
            resized_image = im.resize((zoom, 200))
137
        return resized_image
0 ignored issues
show
introduced by
The variable resized_image does not seem to be defined for all execution paths.
Loading history...
138
139
    def print_gif(self, title):
140
        """gifを表示する
141
142
        ・似顔絵キャンバスに画像を張り付ける。
143
144
        Args:
145
            title (str): タイトル
146
147
        """
148
149
        if not title == "":
150
            giffile = Image.open(title)
151
            self.APP.cv.photo = ImageTk.PhotoImage(self.resize_gif(giffile))
152
            giffile.close()
153
            self.APP.cv.itemconfig(
154
                self.APP.image_on_canvas,
155
                image=self.APP.cv.photo
156
            )
157
158
    def change_setting(self, event=None):
159
        """テキストの変更時
160
161
        ・テキストを変更したときに行番号とハイライトを変更する。
162
163
        Args:
164
            event (instance): tkinter.Event のインスタンス
165
166
        """
167
        self.update_line_numbers()
168
        # その行のハイライトを行う
169
        self.APP.hpc.line_highlight(
170
            self.APP.fpc.replacement_check,
171
            self.APP.fpc.find_text,
172
            self.APP.fpc.next_pos
173
        )
174
175
    def update_line_numbers(self, event=None):
176
        """行番号の描画
177
178
        ・行番号をつけて表示する。
179
180
        Args:
181
            event (instance): tkinter.Event のインスタンス
182
183
        """
184
        # 現在の行番号を全て消す
185
        self.APP.line_numbers.delete(tk.ALL)
186
187
        # Textの0, 0座標、つまり一番左上が何行目にあたるかを取得
188
        i = self.APP.text.index("@0,0")
189
        while True:
190
            # dlineinfoは、その行がどの位置にあり、どんなサイズか、を返す
191
            # (3, 705, 197, 13, 18) のように帰る(x,y,width,height,baseline)
192
            dline = self.APP.text.dlineinfo(i)
193
            # dlineinfoに、存在しない行や、スクロールしないと見えない行を渡すとNoneが帰る
194
            if dline is None:
195
                break
196
            else:
197
                y = dline[1]  # y座標を取得
198
199
            # (x座標, y座標, 方向, 表示テキスト)を渡して行番号のテキストを作成
200
            linenum = str(i).split(".")[0]
201
            self.APP.line_numbers.create_text(
202
                3,
203
                y,
204
                anchor=tk.NW,
205
                text=linenum,
206
                font=("", 12)
207
            )
208
            i = self.APP.text.index("%s+1line" % i)
209