Skip to content

Commit 2e47fd8

Browse files
committed
Can now copy past urls from GUI
1 parent ba473b9 commit 2e47fd8

File tree

4 files changed

+42
-23
lines changed

4 files changed

+42
-23
lines changed

Modules/download_element.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from PyQt5.QtGui import QCursor
88
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QVBoxLayout, QSizePolicy, QMenu, QTextBrowser
99

10-
from utils.utilities import FONT_CONSOLAS, color_text
10+
from utils.utilities import FONT_CONSOLAS, color_text, to_clipboard
1111

1212

1313
# TODO: Enum with states (because keeping track of strings is messy + possiblity a define a broad "error" states
@@ -247,10 +247,12 @@ def process_output(self):
247247

248248
class ProcessListItem(QWidget):
249249
""" ListItem that shows attached process state"""
250-
def __init__(self, process: Download, slot, debug=False, parent=None, tooltip=''):
250+
251+
def __init__(self, process: Download, slot, debug=False, parent=None, url=None):
251252
super(ProcessListItem, self).__init__(parent=parent)
252253
self.process = process
253254
self.slot = slot
255+
self.url = url
254256
self.process.getOutput.connect(self.stat_update)
255257
self.line = QHBoxLayout()
256258
self.setFocusPolicy(Qt.NoFocus)
@@ -294,7 +296,7 @@ def __init__(self, process: Download, slot, debug=False, parent=None, tooltip=''
294296

295297
self.info_label_in_layout = False
296298
self.info_label = QLabel('', parent=self)
297-
self.info_label.setToolTip(tooltip)
299+
self.info_label.setToolTip(f'URL: {url} (Right-click to copy)')
298300
self.info_label.setWordWrap(True)
299301
self.info_label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
300302
self.info_label.hide()
@@ -332,15 +334,21 @@ def open_file(self):
332334
# Print failed to open file to user!
333335

334336
def open_file_menu(self, event):
335-
if self.process.status in ('ERROR', 'Aborted', 'Filesize Error'):
336-
return
337337
menu = QMenu(self.sender())
338-
menu.addAction('Open folder', self.open_file)
338+
339+
if self.process.status not in ('ERROR', 'Aborted', 'Filesize Error'):
340+
menu.addAction('Open folder', self.open_file)
341+
if self.url is not None:
342+
menu.addAction('Copy URL', lambda: to_clipboard(self.url))
343+
if not menu.actions():
344+
return
339345
menu.exec(QCursor.pos())
340346

341347
def open_info_menu(self, event):
342348
menu = QMenu(self.sender())
343349
menu.addAction('Show complete log', self.open_log)
350+
if self.url is not None:
351+
menu.addAction('Copy URL', lambda: to_clipboard(self.url))
344352
menu.exec(QCursor.pos())
345353

346354
def open_log(self):

core.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
import os
33
import sys
44

5-
from PyQt5.QtCore import QProcess, pyqtSignal, Qt, QMimeData
6-
from PyQt5.QtGui import QIcon, QClipboard, QGuiApplication
5+
from PyQt5.QtCore import QProcess, pyqtSignal, Qt
6+
from PyQt5.QtGui import QIcon
77
from PyQt5.QtWidgets import QFileDialog, QTreeWidgetItem, qApp, QDialog, QApplication, QMessageBox, \
88
QTabWidget, QListWidgetItem
99

1010
from Modules import Dialog, Download, MainTab, ParameterTree, MainWindow, AboutTab, Downloader, ParameterTab, TextTab
1111
from Modules.download_element import ProcessListItem, MockDownload
1212
from utils.filehandler import FileHandler
1313
from utils.utilities import path_shortener, color_text, format_in_list, SettingsError, get_stylesheet, \
14-
get_win_accent_color, ProfileLoadError, LessNiceDict
14+
get_win_accent_color, ProfileLoadError, LessNiceDict, to_clipboard
1515

1616

1717
class GUI(MainWindow):
@@ -546,10 +546,8 @@ def window_focus_event(self):
546546

547547
def copy_to_cliboard(self):
548548
""" Adds text to clipboard. """
549-
mime = QMimeData()
550-
mime.setText(self.tab2.download_lineedit.text())
551-
board = QGuiApplication.clipboard()
552-
board.setMimeData(mime, mode=QClipboard.Clipboard)
549+
text = self.tab2.download_lineedit.text()
550+
to_clipboard(text)
553551

554552
def open_folder(self):
555553
""" Opens a folder at specified location. """
@@ -785,14 +783,15 @@ def queue_download(self):
785783

786784
if self.tab1.checkbox.isChecked():
787785
txt = self.settings.user_options['multidl_txt']
788-
786+
url = None
789787
if not txt:
790788
self.alert_message('Error!', 'No textfile selected!', '')
791789
return
792790

793791
command += ['-a', f'{txt}']
794792
else:
795793
txt = self.tab1.url_input.text()
794+
url = txt
796795
command.append(f'{txt}')
797796

798797
# for i in range(len(command)):
@@ -855,16 +854,16 @@ def queue_download(self):
855854

856855
download = Download(self.program_workdir, self.youtube_dl_path, command, parent=self)
857856

858-
self.add_download_to_gui(download, tooltip=f'URL: {txt}')
857+
self.add_download_to_gui(download, url=url)
859858
self.downloader.queue_dl(download)
860859

861-
def add_download_to_gui(self, download, tooltip=''):
860+
def add_download_to_gui(self, download, url=None):
862861
scrollbar = self.tab1.process_list.verticalScrollBar()
863862
place = scrollbar.sliderPosition()
864863
go_bottom = (place == scrollbar.maximum())
865864

866865
slot = QListWidgetItem(parent=self.tab1.process_list)
867-
gui_progress = ProcessListItem(download, slot, debug=self._debug, tooltip=tooltip)
866+
gui_progress = ProcessListItem(download, slot, debug=self._debug, url=url)
868867

869868
self.tab1.process_list.addItem(slot)
870869
self.tab1.process_list.setItemWidget(slot, gui_progress)

utils/filehandler.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
from PyQt5.QtCore import QThreadPool, QTimer, Qt
77

8-
from .task import Task
9-
from .utilities import get_base_settings, SettingsClass, ProfileLoadError
8+
from utils.task import Task
9+
from utils.utilities import get_base_settings, SettingsClass, ProfileLoadError
1010

1111

1212
def threaded_cooldown(func):
@@ -87,17 +87,20 @@ class FileHandler:
8787
# TODO: Implement logging, since returned values from threaded functions are discarded.
8888
# Need to know if errors hanppen!
8989

90-
def __init__(self, settings='settings.json', profiles='profiles.json'):
90+
def __init__(self, settings_path='settings.json', profiles_path='profiles.json'):
9191

92-
self.profile_path = profiles
93-
self.settings_path = settings
92+
self.profile_path = profiles_path
93+
self.settings_path = settings_path
9494
self.work_dir = os.getcwd().replace('\\', '/')
9595

9696
self.force_save = False
9797

9898
self.threadpool = QThreadPool()
9999
self.threadpool.setMaxThreadCount(1)
100100

101+
def __repr__(self):
102+
return f'{__name__}(settings_path={self.settings_path}, profile_path={self.profile_path})'
103+
101104
@staticmethod
102105
def find_file(relative_path, exist=True):
103106
""" Get absolute path to resource, works for dev and for PyInstaller """

utils/utilities.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from traceback import format_exception
1010
from winreg import ConnectRegistry, OpenKey, QueryValueEx, HKEY_CURRENT_USER
1111

12+
1213
LOG_FILE = 'Grabber_error.log'
1314

1415
log = logging.getLogger('Grabber')
@@ -45,7 +46,8 @@ def except_hook(cls, exception, traceback):
4546
# Override PyQt exception handling
4647
sys.excepthook = except_hook
4748

48-
from PyQt5.QtGui import QFont, QColor
49+
from PyQt5.QtCore import QMimeData
50+
from PyQt5.QtGui import QFont, QColor, QGuiApplication, QClipboard
4951
from PyQt5.QtWidgets import QApplication, QMessageBox
5052

5153
FONT_CONSOLAS = QFont()
@@ -64,6 +66,13 @@ def warn_user(error):
6466
QApplication.exit(1)
6567

6668

69+
def to_clipboard(text):
70+
mime = QMimeData()
71+
mime.setText(text)
72+
board = QGuiApplication.clipboard()
73+
board.setMimeData(mime, mode=QClipboard.Clipboard)
74+
75+
6776
def path_shortener(full_path: str):
6877
""" Formats a path to a shorter version, for cleaner UI."""
6978

0 commit comments

Comments
 (0)