Skip to content

Commit

Permalink
W add saved search dialog and menu
Browse files Browse the repository at this point in the history
  • Loading branch information
diegogangl committed Jun 18, 2022
1 parent bee51ed commit 2b07afb
Show file tree
Hide file tree
Showing 8 changed files with 484 additions and 9 deletions.
14 changes: 14 additions & 0 deletions GTG/gtk/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from GTG.core.dates import Date
from GTG.gtk.backends import BackendsDialog
from GTG.gtk.browser.tag_editor import TagEditor
from GTG.gtk.browser.search_editor import SearchEditor
from GTG.core.timer import Timer
from GTG.gtk.errorhandler import do_error_dialog

Expand Down Expand Up @@ -513,6 +514,19 @@ def open_tag_editor(self, tag):
self.edit_tag_dialog.set_transient_for(self.browser)
self.edit_tag_dialog.insert_action_group('app', self)


def open_search_editor(self, search):
"""Open Saved search editor dialog."""

self.edit_search_dialog = SearchEditor(self.req, self, search)
self.edit_search_dialog.set_transient_for(self.browser)
self.edit_search_dialog.insert_action_group('app', self)

def close_search_editor(self):
"""Close search editor dialog."""

self.edit_search_dialog = None

def close_tag_editor(self):
"""Close tag editor dialog."""

Expand Down
1 change: 1 addition & 0 deletions GTG/gtk/browser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ class GnomeConfig():
MENUS_UI_FILE = os.path.join(data, "context_menus.ui")
MODIFYTAGS_UI_FILE = os.path.join(data, "modify_tags.ui")
TAG_EDITOR_UI_FILE = os.path.join(data, "tag_editor.ui")
SEARCH_EDITOR_UI_FILE = os.path.join(data, "search_editor.ui")
243 changes: 243 additions & 0 deletions GTG/gtk/browser/search_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
# -----------------------------------------------------------------------------
# Getting Things GNOME! - a personal organizer for the GNOME desktop
# Copyright (c) 2008-2013 - Lionel Dricot & Bertrand Rousseau
#
# 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 (at your option) 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/>.
# -----------------------------------------------------------------------------

"""
This module contains the SearchEditor class which is a window that allows the
user to edit a saved search properties.
"""
from gi.repository import GObject, Gtk, Gdk, GdkPixbuf, GLib

import logging
import random
from gettext import gettext as _
from GTG.gtk.browser import GnomeConfig

log = logging.getLogger(__name__)


@Gtk.Template(filename=GnomeConfig.SEARCH_EDITOR_UI_FILE)
class SearchEditor(Gtk.Dialog):
"""
A window to edit certain properties of a saved search.
"""

__gtype_name__ = 'GTG_SearchEditor'
_emoji_chooser = Gtk.Template.Child('emoji-chooser')
_icon_button = Gtk.Template.Child('icon-button')
_name_entry = Gtk.Template.Child('name-entry')

def __init__(self, req, app, search=None):
super().__init__(use_header_bar=1)

set_icon_shortcut = Gtk.Shortcut.new(
Gtk.ShortcutTrigger.parse_string("<Control>I"),
Gtk.CallbackAction.new(self._set_icon))

self.add_shortcut(set_icon_shortcut)

self.req = req
self.app = app
self.search = search

self.set_transient_for(app.browser)
self._title_format = self.get_title()
self._emoji_chooser.set_parent(self._icon_button)

self.is_valid = True
self._emoji = None
self.use_icon = False
self._search_name = ''

self.set_search(search)
self.show()


@GObject.Property(type=str, default='')
def search_name(self):
"""The (new) name of the search."""

return self._search_name

@search_name.setter
def search_name(self, value: str):
self._search_name = value
self._validate()

@GObject.Property(type=str, default='')
def search_query(self):
"""The (new) name of the search."""

return self._search_query

@search_query.setter
def search_query(self, value: str):
self._search_query = value
self._validate()

@GObject.Property(type=bool, default=True)
def is_valid(self):
"""
Whenever it is valid to apply the changes (like malformed search name).
"""

return self._is_valid

@is_valid.setter
def is_valid(self, value: bool):
self._is_valid = value

@GObject.Property(type=bool, default=False)
def has_icon(self):
"""
Whenever the search will have an icon.
"""

return bool(self._emoji)

def _validate(self):
"""
Validates the current search preferences.
Returns true whenever it passes validation, False otherwise,
and modifies the is_valid property appropriately.
On failure, the widgets are modified accordingly to show the user
why it doesn't accept it.
"""

valid = True
valid &= self._validate_search_name()
self.is_valid = valid
return valid

def _validate_search_name(self):
"""
Validates the current search name.
Returns true whenever it passes validation, False otherwise.
On failure, the widgets are modified accordingly to show the user
why it doesn't accept it.
"""

if self.search_name == '':
self._name_entry.add_css_class("error")
self._name_entry.props.tooltip_text = \
_("search name can not be empty")
return False
else:
self._name_entry.remove_css_class("error")
self._name_entry.props.tooltip_text = ""
return True

def set_search(self, search):
"""
Set the search to edit.
Widgets are updated with the information of the search,
the previous information/state is lost.
"""
self.search = search
if search is None:
return

icon = search.icon
self._set_emoji(self._emoji_chooser, text=icon if icon else '')

self.search_name = search.name
self.search_query = search.query
self.set_title(self._title_format % self.search_name)


def do_destroy(self):

self.app.close_search_editor()
super().destroy()

def _cancel(self):
"""
Cancel button has been clicked, closing the editor window without
applying changes.
"""

self.destroy()

def _apply(self):
"""
Apply button has been clicked, applying the settings and closing the
editor window.
"""
if self.search is None:
log.warning("Trying to apply but no search set, shouldn't happen")
self._cancel()
return

if self.has_icon and self._emoji:
self.search.icon = self._emoji
elif self.has_icon: # Should never happen, but just in case
log.warning("Tried to set icon for %r but no icon given",
self.search.name)
self.search.icon = None
else:
self.search.icon = None

if self.search_name != self.search.name:
log.debug("Renaming %r → %r", self.search.name, self.search_name)
self.search.name = self.search_name

self.app.ds.saved_searches.model.emit('items-changed', 0, 0, 0)
self.destroy()

# CALLBACKS #####
@Gtk.Template.Callback('response')
def _response(self, widget: GObject.Object, response: Gtk.ResponseType):
if response == Gtk.ResponseType.APPLY:
self._apply()
else:
self._cancel()


@Gtk.Template.Callback('set_icon')
def _set_icon(self, widget: GObject.Object, shargs: GLib.Variant = None):
"""
Button to set the icon/emoji has been clicked.
"""
self._emoji_chooser.popup()

@Gtk.Template.Callback('emoji_set')
def _set_emoji(self, widget: GObject.Object, text: str = None):
"""
Callback when an emoji has been inserted.
"""
self._emoji = text if text else None

if text:
self._emoji = text
self._icon_button.set_label(text)
if label := self._icon_button.get_child():
label.set_opacity(1)
else:
self._emoji = None
self._icon_button.set_label('🏷️')
if label := self._icon_button.get_child():
label.set_opacity(0.4)

self.notify('has-icon')

@Gtk.Template.Callback('remove_icon')
def _remove_icon(self, widget: GObject.Object):
"""
Callback to remove the icon.
"""

self._set_emoji(self._emoji_chooser, text='')
38 changes: 31 additions & 7 deletions GTG/gtk/browser/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from GTG.core.tags2 import Tag2
from GTG.core.saved_searches import SavedSearch
from GTG.core.datastore2 import Datastore2
from GTG.gtk.browser.tag_context_menu import TagContextMenu
from GTG.gtk.browser.sidebar_context_menu import TagContextMenu, SearchesContextMenu



Expand All @@ -33,6 +33,12 @@ class TagBox(Gtk.Box):
tag = GObject.Property(type=Tag2)


class SearchBox(Gtk.Box):
"""Box subclass to keep a pointer to the tag object"""

search = GObject.Property(type=SavedSearch)


class Sidebar(Gtk.ScrolledWindow):

def __init__(self, app, ds: Datastore2):
Expand Down Expand Up @@ -159,7 +165,7 @@ def __init__(self, app, ds: Datastore2):



def on_RMB_click(self, gesture, sequence):
def on_tag_RMB_click(self, gesture, sequence):

menu = TagContextMenu(self.ds, self.app, gesture.get_widget().tag)
menu.set_parent(gesture.get_widget())
Expand All @@ -174,6 +180,21 @@ def on_RMB_click(self, gesture, sequence):
menu.popup()


def on_searches_RMB_click(self, gesture, sequence):

menu = SearchesContextMenu(self.ds, self.app, gesture.get_widget().search)
menu.set_parent(gesture.get_widget())
menu.set_halign(Gtk.Align.START)
menu.set_position(Gtk.PositionType.BOTTOM)

point = gesture.get_point(sequence)
rect = Gdk.Rectangle()
rect.x = point.x
rect.y = point.y
menu.set_pointing_to(rect)
menu.popup()


def btn_item(self, icon_name:str, text: str, count: str) -> Gtk.Box:
box = Gtk.Box()

Expand Down Expand Up @@ -241,7 +262,7 @@ def tags_setup_cb(self, factory, listitem, user_data=None) -> None:
listitem.set_child(box)

tags_RMB_controller = Gtk.GestureSingle(button=Gdk.BUTTON_SECONDARY)
tags_RMB_controller.connect('begin', self.on_RMB_click)
tags_RMB_controller.connect('begin', self.on_tag_RMB_click)
box.add_controller(tags_RMB_controller)

self.expanders.add(expander)
Expand Down Expand Up @@ -322,7 +343,7 @@ def on_search_reveal(self, event) -> None:

def searches_setup_cb(self, factory, listitem, user_data=None) -> None:

box = Gtk.Box()
box = SearchBox()
label = Gtk.Label()
icon = Gtk.Label()
count_label = Gtk.Label()
Expand All @@ -342,9 +363,9 @@ def searches_setup_cb(self, factory, listitem, user_data=None) -> None:
box.append(count_label)
listitem.set_child(box)

# tags_RMB_controller = Gtk.GestureSingle(button=Gdk.BUTTON_SECONDARY)
# tags_RMB_controller.connect('begin', self.on_RMB_click)
# box.add_controller(tags_RMB_controller)
searches_RMB_controller = Gtk.GestureSingle(button=Gdk.BUTTON_SECONDARY)
searches_RMB_controller.connect('begin', self.on_searches_RMB_click)
box.add_controller(searches_RMB_controller)


def searches_bind_cb(self, signallistitem, listitem, user_data=None) -> None:
Expand All @@ -354,11 +375,14 @@ def searches_bind_cb(self, signallistitem, listitem, user_data=None) -> None:
count_label = icon.get_next_sibling()
box = listitem.get_child()


# HACK: Ugly! But apparently necessary
item = listitem.get_item()
while type(item) is not SavedSearch:
item = item.get_item()

box.search = item

item.bind_property('name', label, 'label', GObject.BindingFlags.DEFAULT | GObject.BindingFlags.SYNC_CREATE)
item.bind_property('icon', icon, 'label', GObject.BindingFlags.DEFAULT | GObject.BindingFlags.SYNC_CREATE)

Expand Down
Loading

0 comments on commit 2b07afb

Please sign in to comment.