Skip to content

Commit

Permalink
Add a 'Open files in a new window' button
Browse files Browse the repository at this point in the history
  • Loading branch information
kbengs committed Dec 5, 2021
1 parent 6412988 commit 62eee19
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 28 deletions.
29 changes: 25 additions & 4 deletions data/pdfarranger.ui
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,28 @@ along with PDF Arranger. If not, see <http://www.gnu.org/licenses/>.
<property name="visible">True</property>
<property name="focus_on_click">False</property>
<property name="receives_default">False</property>
<property name="tooltip_text" translatable="yes">Open a file and append it to the current document</property>
<property name="tooltip_text" translatable="yes">Open file(s) in a new window</property>
<property name="action_name">win.open</property>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="icon_name">document-open-symbolic</property>
<property name="use_fallback">True</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton">
<property name="visible">True</property>
<property name="focus_on_click">False</property>
<property name="receives_default">False</property>
<property name="tooltip_text" translatable="yes">Import file(s) to the current document</property>
<property name="action_name">win.import</property>
<child>
<object class="GtkImage">
Expand All @@ -49,7 +70,7 @@ along with PDF Arranger. If not, see <http://www.gnu.org/licenses/>.
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
<property name="position">1</property>
</packing>
</child>
<child>
Expand All @@ -68,7 +89,7 @@ along with PDF Arranger. If not, see <http://www.gnu.org/licenses/>.
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
<property name="position">2</property>
</packing>
</child>
<child>
Expand All @@ -87,7 +108,7 @@ along with PDF Arranger. If not, see <http://www.gnu.org/licenses/>.
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
<property name="position">3</property>
</packing>
</child>
<style>
Expand Down
3 changes: 2 additions & 1 deletion pdfarranger/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
('close', '<Primary>w'),
('quit', '<Primary>q'),
('new', '<Primary>n'),
('import', '<Primary>o'),
('open', '<Primary>o'),
('import', '<Primary>i'),
('zoom(5)', 'plus KP_Add <Primary>plus <Primary>KP_Add'),
('zoom(-5)', 'minus KP_Subtract <Primary>minus <Primary>KP_Subtract'),
('undo', '<Primary>z'),
Expand Down
69 changes: 46 additions & 23 deletions pdfarranger/pdfarranger.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ def __create_actions(self):
('save', self.on_action_save),
('save-as', self.on_action_save_as),
('new', self.on_action_new),
('open', self.on_action_open),
('import', self.on_action_add_doc_activate),
('zoom', self.zoom_change, 'i'),
('close', self.on_action_close),
Expand Down Expand Up @@ -1019,22 +1020,59 @@ def active_file_names(self, detailed=False):
r.discard("")
return r

def on_action_new(self, _action, _param, _unknown):
def open_dialog(self, title):
chooser = Gtk.FileChooserDialog(title=title,
parent=self.window,
action=Gtk.FileChooserAction.OPEN,
buttons=(Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN,
Gtk.ResponseType.ACCEPT))
chooser.set_current_folder(self.import_directory)
chooser.set_select_multiple(True)
file_type_list = ['all', 'pdf']
if len(img2pdf_supported_img) > 0:
file_type_list = ['all', 'img2pdf', 'pdf']
filter_list = self.__create_filters(file_type_list)
for f in filter_list:
chooser.add_filter(f)

return chooser.run(), chooser

def on_action_new(self, _action=None, _param=None, _unknown=None, filenames=[]):
"""Start a new instance."""
if os.name == 'nt':
if sys.executable.find('python3.exe') == -1:
subprocess.Popen(sys.executable)
else:
subprocess.Popen([sys.executable, '-mpdfarranger'])
args = [str(sys.executable)]
for filename in filenames:
args.append(filename)
if sys.executable.find('python3.exe') != -1:
args.insert(1, '-mpdfarranger')
subprocess.Popen(args)
else:
display = Gdk.Display.get_default()
launch_context = display.get_app_launch_context()
desktop_file = "%s.desktop"%(self.get_application_id())
try:
app_info = Gio.DesktopAppInfo.new(desktop_file)
app_info.launch([], launch_context)
launch_files = []
for filename in filenames:
launch_file = Gio.File.new_for_path(filename)
launch_files.append(launch_file)
app_info.launch(launch_files, launch_context)
except TypeError:
subprocess.Popen([sys.executable, '-mpdfarranger'])
args = [str(sys.executable), '-mpdfarranger']
for filename in filenames:
args.append(filename)
subprocess.Popen(args)

def on_action_open(self, _action, _param, _unknown):
"""Open new file(s) in a new window."""
print('open')
response, chooser = self.open_dialog(_('Open…'))

if response == Gtk.ResponseType.ACCEPT:
self.on_action_new(filenames=chooser.get_filenames())
chooser.destroy()

def on_action_save(self, _action, _param, _unknown):
self.save_or_choose()
Expand Down Expand Up @@ -1096,23 +1134,8 @@ def on_action_export_all(self, _action, _param, _unknown):

def on_action_add_doc_activate(self, _action, _param, _unknown):
"""Import doc"""
chooser = Gtk.FileChooserDialog(title=_('Import…'),
parent=self.window,
action=Gtk.FileChooserAction.OPEN,
buttons=(Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN,
Gtk.ResponseType.ACCEPT))
chooser.set_current_folder(self.import_directory)
chooser.set_select_multiple(True)
file_type_list = ['all', 'pdf']
if len(img2pdf_supported_img) > 0:
file_type_list = ['all', 'img2pdf', 'pdf']
filter_list = self.__create_filters(file_type_list)
for f in filter_list:
chooser.add_filter(f)
response, chooser = self.open_dialog(_('Import…'))

response = chooser.run()
if response == Gtk.ResponseType.ACCEPT:
adder = PageAdder(self)
for filename in chooser.get_filenames():
Expand Down
1 change: 1 addition & 0 deletions setup_win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def addicons():
'actions/media-eject',
'actions/document-save',
'actions/document-save-as',
'actions/document-open',
'actions/insert-image',
'actions/object-rotate-left',
'actions/object-rotate-right',
Expand Down

0 comments on commit 62eee19

Please sign in to comment.