printers: Port the printer menu to GMenuModel

This commit is contained in:
Alice Mikhaylenko 2023-08-03 18:07:25 +04:00 committed by Felipe Borges
parent a597ba706d
commit 3a94c78aad
2 changed files with 110 additions and 98 deletions

View file

@ -69,12 +69,11 @@ struct _PpPrinterEntry
GtkFrame *supply_frame;
GtkDrawingArea *supply_drawing_area;
GtkWidget *show_jobs_dialog_button;
GtkWidget *clean_heads_menuitem;
GtkCheckButton *printer_default_checkbutton;
GtkWidget *remove_printer_menuitem;
GtkBox *printer_error;
GtkLabel *error_status;
gboolean is_default;
/* Dialogs */
PpJobsDialog *pp_jobs_dialog;
@ -101,6 +100,11 @@ enum {
static guint signals[LAST_SIGNAL] = { 0 };
enum {
PROP_0,
PROP_DEFAULT,
};
static InkLevelData *
ink_level_data_new (void)
{
@ -120,6 +124,8 @@ ink_level_data_free (InkLevelData *data)
static void
pp_printer_entry_init (PpPrinterEntry *self)
{
gtk_widget_action_set_enabled (GTK_WIDGET (self), "printer.clean-heads", FALSE);
gtk_widget_init_template (GTK_WIDGET (self));
self->inklevel = ink_level_data_new ();
}
@ -234,6 +240,8 @@ supply_levels_draw_cb (GtkDrawingArea *drawing_area,
context = gtk_widget_get_style_context (GTK_WIDGET (self->supply_drawing_area));
gtk_render_background (context, cr, 0, 0, width, height);
if (!supply_level_is_empty (self))
{
GSList *markers = NULL;
@ -384,8 +392,7 @@ show_printer_details_response_cb (PpPrinterEntry *self,
}
static void
on_show_printer_details_dialog (GtkButton *button,
PpPrinterEntry *self)
printer_details_cb (PpPrinterEntry *self)
{
PpDetailsDialog *dialog = pp_details_dialog_new (self->printer_name,
self->printer_location,
@ -402,8 +409,7 @@ on_show_printer_details_dialog (GtkButton *button,
}
static void
on_show_printer_options_dialog (GtkButton *button,
PpPrinterEntry *self)
printer_options_cb (PpPrinterEntry *self)
{
PpOptionsDialog *dialog;
@ -416,11 +422,17 @@ on_show_printer_options_dialog (GtkButton *button,
}
static void
set_as_default_printer (GtkCheckButton *button,
PpPrinterEntry *self)
set_as_default_printer (PpPrinterEntry *self)
{
if (self->is_default)
return;
printer_set_default (self->printer_name);
self->is_default = TRUE;
g_object_notify (G_OBJECT (self), "default");
g_signal_emit_by_name (self, "printer-changed");
}
@ -442,7 +454,7 @@ check_clean_heads_maintenance_command_cb (GObject *source_object,
if (is_supported)
{
gtk_widget_set_visible (GTK_WIDGET (self->clean_heads_menuitem), TRUE);
gtk_widget_action_set_enabled (GTK_WIDGET (self), "printer.clean-heads", TRUE);
}
}
@ -474,8 +486,7 @@ clean_heads_maintenance_command_cb (GObject *source_object,
}
static void
clean_heads (GtkButton *button,
PpPrinterEntry *self)
printer_clean_heads_cb (PpPrinterEntry *self)
{
if (self->clean_command == NULL)
return;
@ -488,8 +499,7 @@ clean_heads (GtkButton *button,
}
static void
remove_printer (GtkButton *button,
PpPrinterEntry *self)
printer_remove_cb (PpPrinterEntry *self)
{
g_signal_emit_by_name (self, "printer-delete");
}
@ -890,9 +900,8 @@ pp_printer_entry_update (PpPrinterEntry *self,
gtk_label_set_text (self->printer_status, printer_status);
gtk_label_set_text (self->printer_name_label, instance);
g_signal_handlers_block_by_func (self->printer_default_checkbutton, set_as_default_printer, self);
gtk_check_button_set_active (self->printer_default_checkbutton, printer.is_default);
g_signal_handlers_unblock_by_func (self->printer_default_checkbutton, set_as_default_printer, self);
self->is_default = printer.is_default;
g_object_notify (G_OBJECT (self), "default");
self->printer_make_and_model = sanitize_printer_model (printer_make_and_model);
@ -922,8 +931,8 @@ pp_printer_entry_update (PpPrinterEntry *self,
pp_printer_entry_update_jobs_count (self);
gtk_widget_set_sensitive (GTK_WIDGET (self->printer_default_checkbutton), self->is_authorized);
gtk_widget_set_sensitive (GTK_WIDGET (self->remove_printer_menuitem), self->is_authorized);
gtk_widget_action_set_enabled (GTK_WIDGET (self), "printer.default", self->is_authorized);
gtk_widget_action_set_enabled (GTK_WIDGET (self), "printer.remove", self->is_authorized);
}
static void
@ -946,6 +955,42 @@ pp_printer_entry_dispose (GObject *object)
G_OBJECT_CLASS (pp_printer_entry_parent_class)->dispose (object);
}
static void
pp_printer_entry_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
PpPrinterEntry *self = PP_PRINTER_ENTRY (object);
switch (prop_id)
{
case PROP_DEFAULT:
g_value_set_boolean (value, self->is_default);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
pp_printer_entry_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PpPrinterEntry *self = PP_PRINTER_ENTRY (object);
switch (prop_id)
{
case PROP_DEFAULT:
set_as_default_printer (self);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
pp_printer_entry_class_init (PpPrinterEntryClass *klass)
{
@ -963,22 +1008,24 @@ pp_printer_entry_class_init (PpPrinterEntryClass *klass)
gtk_widget_class_bind_template_child (widget_class, PpPrinterEntry, printer_inklevel_label);
gtk_widget_class_bind_template_child (widget_class, PpPrinterEntry, supply_frame);
gtk_widget_class_bind_template_child (widget_class, PpPrinterEntry, supply_drawing_area);
gtk_widget_class_bind_template_child (widget_class, PpPrinterEntry, printer_default_checkbutton);
gtk_widget_class_bind_template_child (widget_class, PpPrinterEntry, show_jobs_dialog_button);
gtk_widget_class_bind_template_child (widget_class, PpPrinterEntry, clean_heads_menuitem);
gtk_widget_class_bind_template_child (widget_class, PpPrinterEntry, remove_printer_menuitem);
gtk_widget_class_bind_template_child (widget_class, PpPrinterEntry, error_status);
gtk_widget_class_bind_template_child (widget_class, PpPrinterEntry, printer_error);
gtk_widget_class_bind_template_callback (widget_class, on_show_printer_details_dialog);
gtk_widget_class_bind_template_callback (widget_class, on_show_printer_options_dialog);
gtk_widget_class_bind_template_callback (widget_class, set_as_default_printer);
gtk_widget_class_bind_template_callback (widget_class, clean_heads);
gtk_widget_class_bind_template_callback (widget_class, remove_printer);
gtk_widget_class_bind_template_callback (widget_class, show_jobs_dialog);
gtk_widget_class_bind_template_callback (widget_class, restart_printer);
object_class->dispose = pp_printer_entry_dispose;
object_class->get_property = pp_printer_entry_get_property;
object_class->set_property = pp_printer_entry_set_property;
g_object_class_install_property (object_class,
PROP_DEFAULT,
g_param_spec_boolean ("default",
"default",
"default",
FALSE,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY));
signals[IS_DEFAULT_PRINTER] =
g_signal_new ("printer-changed",
@ -1004,4 +1051,14 @@ pp_printer_entry_class_init (PpPrinterEntryClass *klass)
NULL, NULL, NULL,
G_TYPE_NONE, 1,
G_TYPE_STRING);
gtk_widget_class_install_action (widget_class, "printer.options", NULL,
(GtkWidgetActionActivateFunc) printer_options_cb);
gtk_widget_class_install_action (widget_class, "printer.details", NULL,
(GtkWidgetActionActivateFunc) printer_details_cb);
gtk_widget_class_install_property_action (widget_class, "printer.default", "default");
gtk_widget_class_install_action (widget_class, "printer.clean-heads", NULL,
(GtkWidgetActionActivateFunc) printer_clean_heads_cb);
gtk_widget_class_install_action (widget_class, "printer.remove", NULL,
(GtkWidgetActionActivateFunc) printer_remove_cb);
}

View file

@ -1,76 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.8 -->
<object class="GtkPopover" id="printer-menu">
<style>
<class name="menu" />
</style>
<child>
<object class="GtkGrid">
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<child>
<object class="GtkModelButton">
<property name="text" translatable="yes">Printing Options</property>
<signal name="clicked" handler="on_show_printer_options_dialog"/>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkModelButton">
<property name="text" translatable="yes">Printer Details</property>
<signal name="clicked" handler="on_show_printer_details_dialog"/>
<layout>
<property name="column">1</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkCheckButton" id="printer_default_checkbutton">
<property name="valign">center</property>
<property name="label" translatable="yes" comments="Set this printer as default">Use Printer by Default</property>
<signal name="toggled" handler="set_as_default_printer"/>
<layout>
<property name="column">0</property>
<property name="row">2</property>
<property name="column-span">3</property>
</layout>
</object>
</child>
<child>
<object class="GtkModelButton" id="clean_heads_menuitem">
<property name="visible">False</property>
<property name="text" translatable="yes" comments="Translators: This button executes command which cleans print heads of the printer.">Clean Print Heads</property>
<signal name="clicked" handler="clean_heads"/>
<layout>
<property name="column">1</property>
<property name="row">3</property>
</layout>
</object>
</child>
<child>
<object class="GtkModelButton" id="remove_printer_menuitem">
<property name="text" translatable="yes">Remove Printer</property>
<signal name="clicked" handler="remove_printer"/>
<layout>
<property name="column">1</property>
<property name="row">4</property>
</layout>
</object>
</child>
</object>
</child>
</object>
<template class="PpPrinterEntry" parent="GtkListBoxRow">
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
@ -126,7 +56,7 @@
<child>
<object class="GtkMenuButton">
<property name="popover">printer-menu</property>
<property name="menu-model">printer_menu</property>
<property name="icon_name">view-more-symbolic</property>
<style>
<class name="flat"/>
@ -313,5 +243,30 @@
</object>
</child>
</template>
<menu id="printer_menu">
<section>
<item>
<attribute name="label" translatable="yes">Printing Options</attribute>
<attribute name="action">printer.options</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Printer Details</attribute>
<attribute name="action">printer.details</attribute>
</item>
<item>
<attribute name="label" translatable="yes" comments="Set this printer as default">Use Printer by Default</attribute>
<attribute name="action">printer.default</attribute>
</item>
<item>
<attribute name="label" translatable="yes" comments="Translators: This button executes command which cleans print heads of the printer.">Clean Print Heads</attribute>
<attribute name="action">printer.clean-heads</attribute>
<attribute name="hidden-when">action-disabled</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Remove Printer</attribute>
<attribute name="action">printer.remove</attribute>
</item>
</section>
</menu>
</interface>