-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffer.py
109 lines (88 loc) · 4.19 KB
/
buffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Andy Stewart
#
# Author: Andy Stewart <lazycat.manatee@gmail.com>
# Maintainer: Andy Stewart <lazycat.manatee@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import random
from pathlib import Path
from core.utils import *
from core.webengine import BrowserBuffer
from PyQt6.QtCore import QUrl
class AppBuffer(BrowserBuffer):
def __init__(self, buffer_id, url, arguments):
BrowserBuffer.__init__(self, buffer_id, url, arguments, False)
self.index_file = os.path.join(os.path.dirname(__file__), "index.html")
self.url = url
self.parent_dir = os.path.abspath(os.path.join(url, os.pardir))
self.image_name = os.path.basename(url)
with open(self.index_file, "r") as f:
html = f.read().replace("%1", os.path.join(os.path.dirname(__file__))).replace("%2", os.path.join("file://", url)).replace("%3", self.theme_background_color)
self.buffer_widget.setHtml(html, QUrl("file://"))
@interactive
def update_theme(self):
super().update_theme()
self.buffer_widget.eval_js("document.getElementsByTagName('img')[0].style.background = '{}'; document.getElementsByClassName('viewer-container')[0].style.background = '{}'".format(self.theme_background_color, self.theme_background_color))
def load_image(self, url):
self.url = url
self.parent_dir = os.path.abspath(os.path.join(url, os.pardir))
self.image_name = os.path.basename(url)
load_image_js = "document.getElementById('image').setAttribute('src', '{0}?{1}');viewer.update();".format(
os.path.join("file://", self.url).replace("\\", "/"),
random.randint(1, 100000))
self.buffer_widget.eval_js(load_image_js)
def is_image_file(self, f):
return Path(f).suffix[1:].lower() in ["jpg", "jpeg", "png", "bmp", "gif", "svg", "webp"]
def get_same_dir_images(self):
files = [f for f in os.listdir(self.parent_dir) if os.path.isfile(os.path.join(self.parent_dir, f))]
images = list(filter(self.is_image_file, files))
images.sort()
return images
def reload_image(self):
self.load_image(os.path.join(self.parent_dir, self.image_name))
def load_next_image(self):
images = self.get_same_dir_images()
if self.image_name in images:
image_index = images.index(self.image_name)
if image_index == len(images) - 1:
image_index = 0
else:
image_index += 1
self.image_name = images[image_index]
self.load_image(os.path.join(self.parent_dir, self.image_name))
self.change_title(self.image_name)
def load_prev_image(self):
images = self.get_same_dir_images()
if self.image_name in images:
image_index = images.index(self.image_name)
if image_index == 0:
image_index = len(images) - 1
else:
image_index -= 1
self.image_name = images[image_index]
self.load_image(os.path.join(self.parent_dir, self.image_name))
self.change_title(self.image_name)
@interactive
def delete_current_image(self):
self.send_input_message("Are you sure you want to delete {0}?".format(self.image_name), "delete_image", "yes-or-no")
@PostGui()
def handle_input_response(self, callback_tag, result_content):
if callback_tag == "delete_image":
image = self.url
self.load_next_image()
os.remove(image)
message_to_emacs("Delete image {} success.".format(image))