Passed
Push — master ( c14483...ae10f4 )
by Yoshihiro
56s queued 30s
created

SubfunctionProcessingClass.update_line_numbers()   A

Complexity

Conditions 3

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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