Skip to content

Commit

Permalink
Add a "Open" action
Browse files Browse the repository at this point in the history
* The file(s) are opened in a new window if there is data in
  current window.
* The files are opened as soon as the new instance is idle
* Import function name and tooltip is simplified
Close #581
  • Loading branch information
kbengs authored and jeromerobert committed Jan 22, 2022
1 parent 0be21f1 commit 4172fa6
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 40 deletions.
6 changes: 6 additions & 0 deletions data/menu.ui
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ along with PDF Arranger. If not, see <http://www.gnu.org/licenses/>.
-->
<interface>
<menu id="main_menu">
<section>
<item>
<attribute name="label" translatable="yes">_Open</attribute>
<attribute name="action">win.open</attribute>
</item>
</section>
<section>
<item>
<attribute name="label" translatable="yes">_Import</attribute>
Expand Down
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</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</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 @@ -33,7 +33,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
103 changes: 68 additions & 35 deletions pdfarranger/pdfarranger.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ def __create_actions(self):
('save', self.on_action_save),
('save-as', self.on_action_save_as),
('new', self.on_action_new),
('import', self.on_action_add_doc_activate),
('open', self.on_action_open),
('import', self.on_action_import),
('zoom', self.zoom_change, 'i'),
('close', self.on_action_close),
('quit', self.on_quit),
Expand Down Expand Up @@ -612,18 +613,22 @@ def do_command_line(self, command_line):
files = [Gio.File.new_for_commandline_arg(i)
for i in options.lookup_value(GLib.OPTION_REMAINING)]

a = PageAdder(self)
for f in files:
try:
a.addpages(f.get_path())
except FileNotFoundError as e:
print(e, file=sys.stderr)
self.error_message_dialog(e)

a.commit(select_added=False, add_to_undomanager=True)
GObject.idle_add(self.add_files, files)

return 0

def add_files(self, files):
"""Add files passed as command line arguments."""
a = PageAdder(self)
for f in files:
try:
a.addpages(f.get_path())
except FileNotFoundError as e:
print(e, file=sys.stderr)
self.error_message_dialog(e)

a.commit(select_added=False, add_to_undomanager=True)

@staticmethod
def set_text_renderer_cell_height(iconview):
"""Update text renderer cell height on style update.
Expand Down Expand Up @@ -1069,22 +1074,66 @@ def active_file_names(self):
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))
if self.import_directory is not None:
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=None):
"""Start a new instance."""
filenames = filenames or []
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)."""
response, chooser = self.open_dialog(_('Open…'))

if response == Gtk.ResponseType.ACCEPT:
if self.is_unsaved or self.export_file:
self.on_action_new(filenames=chooser.get_filenames())
else:
adder = PageAdder(self)
for filename in chooser.get_filenames():
adder.addpages(filename)
adder.commit(select_added=False, add_to_undomanager=True)
chooser.destroy()

def on_action_save(self, _action, _param, _unknown):
self.save_or_choose()
Expand Down Expand Up @@ -1143,26 +1192,10 @@ def choose_export_selection_pdf_name(self, _action, mode, _unknown):
def on_action_export_all(self, _action, _param, _unknown):
self.choose_export_pdf_name(GLib.Variant('i', 1))

def on_action_add_doc_activate(self, _action, _param, _unknown):
def on_action_import(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))
if self.import_directory is not None:
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 4172fa6

Please sign in to comment.