Passed
Push — master ( bd08dd...19e348 )
by Yoshihiro
01:04 queued 10s
created

SubfunctionProcessingClass.mouse_image_scroll()   B

Complexity

Conditions 6

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.0106

Importance

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