Passed
Push — master ( 1e0f9e...3731d5 )
by Yoshihiro
02:05
created

SubfunctionProcessingClass.update_line_numbers()   A

Complexity

Conditions 3

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5.3197

Importance

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