| 1 |
# Filter Widget/Dialog |
| 2 |
# Copyright (C) 2006 Johannes Sasongko <[email protected]> |
| 3 |
# Copyright (C) 2008-2010 Adam Olsen |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 2, or (at your option) |
| 8 |
# any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 |
# |
| 19 |
# |
| 20 |
# The developers of the Exaile media player hereby grant permission |
| 21 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 22 |
# distributed together with GStreamer and Exaile. This permission is |
| 23 |
# above and beyond the permissions granted by the GPL license by which |
| 24 |
# Exaile is covered. If you modify this code, you may extend this |
| 25 |
# exception to your version of the code, but you are not obligated to |
| 26 |
# do so. If you do not wish to do so, delete this exception statement |
| 27 |
# from your version. |
| 28 |
|
| 29 |
"""Generic widget and dialog for filtering items. |
| 30 |
|
| 31 |
They resemble the configuration dialogs of Evolution's mail filters |
| 32 |
and Rhythmbox's automatic playlists. |
| 33 |
""" |
| 34 |
|
| 35 |
import urllib.parse |
| 36 |
from typing import Sequence |
| 37 |
|
| 38 |
from gi.types import GObjectMeta |
| 39 |
from gi.repository import Gtk |
| 40 |
|
| 41 |
from xl.nls import gettext as _ |
| 42 |
from xlgui.guiutil import GtkTemplate |
| 43 |
from xlgui.guiutil import gtk_widget_replace |
| 44 |
|
| 45 |
|
| 46 |
@GtkTemplate('ui', 'widgets', 'filter_dialog.ui') |
| 47 |
class FilterDialog(Gtk.Dialog): |
| 48 |
"""Dialog to filter a list of items. |
| 49 |
|
| 50 |
Consists of a FilterWidget and an Add button. |
| 51 |
""" |
| 52 |
|
| 53 |
__gtype_name__ = 'FilterDialog' |
| 54 |
|
| 55 |
( |
| 56 |
name_entry, |
| 57 |
filter, |
| 58 |
match_any, |
| 59 |
random, |
| 60 |
lim_check, |
| 61 |
lim_spin, |
| 62 |
) = GtkTemplate.Child.widgets(6) |
| 63 |
|
| 64 |
def __init__(self, title, parent, criteria): |
| 65 |
"""Create a filter dialog. |
| 66 |
|
| 67 |
Parameters: |
| 68 |
- title: title of the dialog window |
| 69 |
- criteria: possible criteria; see FilterWindow |
| 70 |
""" |
| 71 |
|
| 72 |
Gtk.Dialog.__init__(self, title=title, transient_for=parent) |
| 73 |
self.init_template() |
| 74 |
|
| 75 |
self.add_buttons( |
| 76 |
Gtk.STOCK_CANCEL, |
| 77 |
Gtk.ResponseType.REJECT, |
| 78 |
Gtk.STOCK_OK, |
| 79 |
Gtk.ResponseType.ACCEPT, |
| 80 |
) |
| 81 |
|
| 82 |
f = FilterWidget(sorted(criteria, key=lambda k: _(k[0]))) |
| 83 |
f.add_criteria_row() |
| 84 |
f.set_border_width(5) |
| 85 |
f.show_all() |
| 86 |
|
| 87 |
self.filter = gtk_widget_replace(self.filter, f) |
| 88 |
|
| 89 |
@GtkTemplate.Callback |
| 90 |
def on_add_button_clicked(self, *args): |
| 91 |
self.filter.add_criteria_row() |
| 92 |
|
| 93 |
@GtkTemplate.Callback |
| 94 |
def on_lim_check_toggled(self, *args): |
| 95 |
self.lim_spin.set_sensitive(self.lim_check.get_active()) |
| 96 |
|
| 97 |
def set_limit(self, limit): |
| 98 |
""" |
| 99 |
Sets the limit for the number of items that should be returned |
| 100 |
""" |
| 101 |
if limit > -1: |
| 102 |
self.lim_check.set_active(True) |
| 103 |
self.lim_spin.set_value(limit) |
| 104 |
else: |
| 105 |
self.lim_check.set_active(False) |
| 106 |
|
| 107 |
def get_limit(self): |
| 108 |
""" |
| 109 |
Get the limit value |
| 110 |
""" |
| 111 |
if self.lim_check.get_active(): |
| 112 |
return int(self.lim_spin.get_value()) |
| 113 |
else: |
| 114 |
return -1 |
| 115 |
|
| 116 |
def get_random(self): |
| 117 |
""" |
| 118 |
Returns if the playlist should be random |
| 119 |
""" |
| 120 |
return self.random.get_active() |
| 121 |
|
| 122 |
def set_random(self, random): |
| 123 |
""" |
| 124 |
Sets if this playlist should be random |
| 125 |
""" |
| 126 |
self.random.set_active(random) |
| 127 |
|
| 128 |
def get_match_any(self): |
| 129 |
""" |
| 130 |
Returns true if this dialog should match any of the criteria |
| 131 |
""" |
| 132 |
return self.match_any.get_active() |
| 133 |
|
| 134 |
def set_match_any(self, any): |
| 135 |
""" |
| 136 |
Sets whether this dialog should match any of the criteria or not |
| 137 |
""" |
| 138 |
self.match_any.set_active(any) |
| 139 |
|
| 140 |
def get_name(self): |
| 141 |
""" |
| 142 |
Returns the text in the name_entry |
| 143 |
""" |
| 144 |
return self.name_entry.get_text() |
| 145 |
|
| 146 |
def set_name(self, name): |
| 147 |
""" |
| 148 |
Sets the text in the name_entry |
| 149 |
""" |
| 150 |
self.name_entry.set_text(name) |
| 151 |
|
| 152 |
def get_result(self): |
| 153 |
"""Return the user's input as a list of filter criteria. |
| 154 |
|
| 155 |
See FilterWidget.get_result. |
| 156 |
""" |
| 157 |
# TODO: display error message |
| 158 |
return self.filter.get_result() |
| 159 |
|
| 160 |
def get_state(self): |
| 161 |
"""Return the filter state. |
| 162 |
|
| 163 |
See FilterWidget.get_state. |
| 164 |
""" |
| 165 |
return self.filter.get_state() |
| 166 |
|
| 167 |
def set_state(self, state): |
| 168 |
"""Set the filter state. |
| 169 |
|
| 170 |
See FilterWidget.set_state. |
| 171 |
""" |
| 172 |
self.filter.set_state(state) |
| 173 |
|
| 174 |
|
| 175 |
class FilterWidget(Gtk.Grid): |
| 176 |
"""Widget to filter a list of items. |
| 177 |
|
| 178 |
This widget only includes the criteria selector (Criterion widgets) |
| 179 |
and their Remove buttons; it does not include an Add button. |
| 180 |
|
| 181 |
Attributes: |
| 182 |
- criteria: see example |
| 183 |
- rows: list of (criterion, remove_btn, remove_btn_handler_id) |
| 184 |
- n: number of rows |
| 185 |
|
| 186 |
Example of criteria: |
| 187 |
|
| 188 |
[ |
| 189 |
# name |
| 190 |
(N_('Year'), [ |
| 191 |
# name - field class/factory |
| 192 |
(N_('is'), EntryField), |
| 193 |
(N_('is between'), |
| 194 |
lambda x: EntryLabelEntryField(x, _('and'))), |
| 195 |
(N_('is this year'), NothingField) |
| 196 |
]), |
| 197 |
] |
| 198 |
|
| 199 |
The field class is a class following this interface: |
| 200 |
|
| 201 |
class Field(Gtk.Widget): |
| 202 |
def __init__(self, result_generator): |
| 203 |
pass |
| 204 |
def get_result(self): |
| 205 |
return object |
| 206 |
def get_state(self): |
| 207 |
return object |
| 208 |
def set_state(self, state): |
| 209 |
pass |
| 210 |
|
| 211 |
Although not required, these methods should use unicode instead of |
| 212 |
str objects when dealing with strings. |
| 213 |
""" |
| 214 |
|
| 215 |
def __init__(self, criteria): |
| 216 |
"""Create a filter widget. |
| 217 |
|
| 218 |
Parameter: |
| 219 |
- criteria: see FilterWidget |
| 220 |
""" |
| 221 |
|
| 222 |
super(FilterWidget, self).__init__() |
| 223 |
self.set_column_spacing(10) |
| 224 |
self.set_row_spacing(2) |
| 225 |
self.criteria = criteria |
| 226 |
self.rows = [] |
| 227 |
|
| 228 |
def add_criteria_row(self): |
| 229 |
"""Add a new criteria row.""" |
| 230 |
criterion = Criterion(self.criteria) |
| 231 |
criterion.show() |
| 232 |
|
| 233 |
n = len(self.rows) |
| 234 |
|
| 235 |
if n != 0: |
| 236 |
criterion.set_state(self.rows[-1][0].get_state()) |
| 237 |
|
| 238 |
remove_btn = Gtk.Button() |
| 239 |
image = Gtk.Image() |
| 240 |
image.set_from_icon_name('list-remove', Gtk.IconSize.BUTTON) |
| 241 |
remove_btn.add(image) |
| 242 |
remove_btn_handler_id = remove_btn.connect('clicked', self.__remove_clicked) |
| 243 |
remove_btn.show_all() |
| 244 |
|
| 245 |
self.attach(criterion, 0, n, 1, 1) |
| 246 |
self.attach(remove_btn, 1, n, 1, 1) |
| 247 |
|
| 248 |
self.rows.append((criterion, remove_btn, remove_btn_handler_id)) |
| 249 |
|
| 250 |
def remove_criteria_row(self, row): |
| 251 |
"""Remove a criteria row.""" |
| 252 |
self.remove_row(row) |
| 253 |
del self.rows[row] |
| 254 |
|
| 255 |
def __remove_clicked(self, widget): |
| 256 |
n = self.child_get_property(widget, 'top-attach') |
| 257 |
self.remove_criteria_row(n) |
| 258 |
|
| 259 |
def get_state(self): |
| 260 |
"""Return the filter state. |
| 261 |
|
| 262 |
See set_state for the state format. |
| 263 |
""" |
| 264 |
state = [] |
| 265 |
for row in self.rows: |
| 266 |
state.append(row[0].get_state()) |
| 267 |
state[-1][0].reverse() # reverse so it reads more nicely |
| 268 |
return state |
| 269 |
|
| 270 |
def set_state(self, state): |
| 271 |
"""Set the filter state. |
| 272 |
|
| 273 |
Format: |
| 274 |
|
| 275 |
[ |
| 276 |
( [criterion1_1, criterion1_2, ...], filter1 ), |
| 277 |
( [criterion2_1, criterion2_2, ...], filter2 ), |
| 278 |
... |
| 279 |
] |
| 280 |
""" |
| 281 |
n_present = len(self.rows) |
| 282 |
n_required = len(state) |
| 283 |
for i in range(n_present, n_required): |
| 284 |
self.add_criteria_row() |
| 285 |
for i in range(n_present, n_required, -1): |
| 286 |
self.remove_criteria_row(i - 1) # i is one less than n |
| 287 |
for i, cstate in enumerate(state): |
| 288 |
cstate[0].reverse() # reverse so it becomes a stack |
| 289 |
self.rows[i][0].set_state(cstate) |
| 290 |
|
| 291 |
|
| 292 |
class Criterion(Gtk.Box): |
| 293 |
"""Widget representing one filter criterion. |
| 294 |
|
| 295 |
It contains either: |
| 296 |
- one combo box and another criterion object, or |
| 297 |
- a field object. |
| 298 |
""" |
| 299 |
|
| 300 |
def __init__(self, subcriteria): |
| 301 |
"""Create a criterion object. |
| 302 |
|
| 303 |
Parameters: |
| 304 |
- subcriteria: a list of possible subcriteria; |
| 305 |
see criteria in FilterWidget |
| 306 |
""" |
| 307 |
super(Criterion, self).__init__(spacing=5) |
| 308 |
if isinstance(subcriteria, (Gtk.Widget, GObjectMeta)): |
| 309 |
field_class = subcriteria |
| 310 |
self.child = field_class() |
| 311 |
else: |
| 312 |
self.combo = combo = Gtk.ComboBoxText() |
| 313 |
if len(subcriteria) > 10: |
| 314 |
self.combo.set_wrap_width(5) |
| 315 |
self.subcriteria = subcriteria |
| 316 |
for subc in subcriteria: |
| 317 |
combo.append_text(_(subc[0])) |
| 318 |
combo.set_active(0) |
| 319 |
combo.connect('changed', self._combo_changed) |
| 320 |
combo.show() |
| 321 |
self.pack_start(combo, False, True, 0) |
| 322 |
self.child = Criterion(subcriteria[0][1]) |
| 323 |
self.child.show() |
| 324 |
self.pack_start(self.child, True, True, 0) |
| 325 |
|
| 326 |
def _combo_changed(self, widget): |
| 327 |
"""Called when the combo box changes its value.""" |
| 328 |
state = self.child.get_state() |
| 329 |
self.remove(self.child) |
| 330 |
self.child = Criterion(self.subcriteria[self.combo.get_active()][1]) |
| 331 |
if state: |
| 332 |
self.child.set_state(state) |
| 333 |
self.pack_start(self.child, True, True, 0) |
| 334 |
self.child.show() |
| 335 |
|
| 336 |
def get_state(self): |
| 337 |
"""Return the criterion state. |
| 338 |
|
| 339 |
See set_state for the state format. |
| 340 |
""" |
| 341 |
state = self.child.get_state() |
| 342 |
if isinstance(self.child, Criterion): |
| 343 |
state[0].append(self.subcriteria[self.combo.get_active()][0]) |
| 344 |
else: |
| 345 |
state = ([], state) |
| 346 |
return state |
| 347 |
|
| 348 |
def set_state(self, state): |
| 349 |
"""Set the criterion state. |
| 350 |
|
| 351 |
Format: |
| 352 |
|
| 353 |
([..., grandchild_state, child_state, self_state], filter) |
| 354 |
|
| 355 |
Note the reverse order of the list. This is to give the |
| 356 |
impression of it being a stack responding to pop(). |
| 357 |
""" |
| 358 |
if isinstance(self.child, Criterion): |
| 359 |
text = state[0].pop() |
| 360 |
for i, subc in enumerate(self.subcriteria): |
| 361 |
if subc[0] == text: |
| 362 |
self.combo.set_active(i) |
| 363 |
break |
| 364 |
self.child.set_state(state) |
| 365 |
else: |
| 366 |
if len(state) > 1: |
| 367 |
self.child.set_state(state[1]) |
| 368 |
|
| 369 |
|
| 370 |
# Sample fields |
| 371 |
|
| 372 |
|
| 373 |
class ComboEntryField(Gtk.Box): |
| 374 |
'''Select from multiple fixed values, but allow the user to enter text''' |
| 375 |
|
| 376 |
def __init__(self, values): |
| 377 |
Gtk.Box.__init__(self) |
| 378 |
|
| 379 |
self.combo = Gtk.ComboBoxText.new_with_entry() |
| 380 |
for value in values: |
| 381 |
self.combo.append_text(value) |
| 382 |
|
| 383 |
self.pack_start(self.combo, True, True, 0) |
| 384 |
self.combo.show() |
| 385 |
|
| 386 |
def get_state(self): |
| 387 |
return self.combo.get_active_text() |
| 388 |
|
| 389 |
def set_state(self, state): |
| 390 |
self.combo.get_child().set_text(state) |
| 391 |
|
| 392 |
|
| 393 |
class NullField(Gtk.Box): |
| 394 |
'''Used as a placeholder for __null__ values''' |
| 395 |
|
| 396 |
def get_state(self): |
| 397 |
return ['__null__'] |
| 398 |
|
| 399 |
def set_state(self, state): |
| 400 |
pass |
| 401 |
|
| 402 |
|
| 403 |
class MultiEntryField(Gtk.Box): |
| 404 |
"""Helper field that can be subclassed to get fields with multiple |
| 405 |
GtkEntry widgets and multiple labels.""" |
| 406 |
|
| 407 |
def __init__(self, labels): |
| 408 |
"""Create a field with the specified labels and widths. |
| 409 |
|
| 410 |
Parameter: |
| 411 |
- labels: sequence of string, integer, or None values; |
| 412 |
string represents label, |
| 413 |
integer represents Entry widget with a specific width, |
| 414 |
None represents Entry widget with default width |
| 415 |
""" |
| 416 |
Gtk.Box.__init__(self, spacing=5) |
| 417 |
self.entries = [] |
| 418 |
for label in labels: |
| 419 |
if label is None: |
| 420 |
widget = Gtk.Entry() |
| 421 |
self.entries.append(widget) |
| 422 |
elif isinstance(label, (int, float)): |
| 423 |
widget = Gtk.Entry() |
| 424 |
widget.set_size_request(label, -1) |
| 425 |
self.entries.append(widget) |
| 426 |
else: |
| 427 |
widget = Gtk.Label(label=str(label)) |
| 428 |
self.pack_start(widget, False, True, 0) |
| 429 |
widget.show() |
| 430 |
|
| 431 |
def get_state(self): |
| 432 |
return [e.get_text() for e in self.entries] |
| 433 |
|
| 434 |
def set_state(self, state): |
| 435 |
entries = self.entries |
| 436 |
if isinstance(state, (list, tuple)): |
| 437 |
for i in range(min(len(entries), len(state))): |
| 438 |
entries[i].set_text(state[i]) |
| 439 |
else: |
| 440 |
entries[0].set_text(state) |
| 441 |
|
| 442 |
|
| 443 |
class EntryField(Gtk.Entry): |
| 444 |
def __init__(self): |
| 445 |
Gtk.Entry.__init__(self) |
| 446 |
|
| 447 |
def get_state(self): |
| 448 |
return self.get_text() |
| 449 |
|
| 450 |
def set_state(self, state): |
| 451 |
if isinstance(state, list) or isinstance(state, tuple): |
| 452 |
state = state[0] |
| 453 |
self.set_text(state) |
| 454 |
|
| 455 |
|
| 456 |
class QuotedEntryField(Gtk.Entry): |
| 457 |
def __init__(self): |
| 458 |
Gtk.Entry.__init__(self) |
| 459 |
|
| 460 |
def get_state(self): |
| 461 |
return urllib.parse.quote(self.get_text()) |
| 462 |
|
| 463 |
def set_state(self, state): |
| 464 |
if isinstance(state, list) or isinstance(state, tuple): |
| 465 |
state = state[0] |
| 466 |
self.set_text(urllib.parse.unquote(state)) |
| 467 |
|
| 468 |
|
| 469 |
class EntryLabelEntryField(MultiEntryField): |
| 470 |
def __init__(self, label): |
| 471 |
MultiEntryField.__init__(self, (50, label, 50)) |
| 472 |
|
| 473 |
|
| 474 |
class SpinLabelField(Gtk.Box): |
| 475 |
def __init__(self, label='', top=999999, lower=-999999): |
| 476 |
Gtk.Box.__init__(self, spacing=5) |
| 477 |
self.spin = Gtk.SpinButton.new_with_range(lower, top, 1) |
| 478 |
self.spin.set_value(0) |
| 479 |
self.pack_start(self.spin, False, True, 0) |
| 480 |
self.pack_start(Gtk.Label.new(label), False, True, 0) |
| 481 |
self.show_all() |
| 482 |
|
| 483 |
def get_state(self): |
| 484 |
return self.spin.get_value() |
| 485 |
|
| 486 |
def set_state(self, state): |
| 487 |
if isinstance(state, list) or isinstance(state, tuple): |
| 488 |
state = state[0] |
| 489 |
try: |
| 490 |
self.spin.set_value(int(state)) |
| 491 |
except ValueError: |
| 492 |
pass |
| 493 |
|
| 494 |
|
| 495 |
class SpinButtonAndComboField(Gtk.Box): |
| 496 |
def __init__(self, items=()): |
| 497 |
Gtk.Box.__init__(self, spacing=5) |
| 498 |
self.items: Sequence[str] = items |
| 499 |
|
| 500 |
self.entry = Gtk.SpinButton.new_with_range(0, 999999, 1) |
| 501 |
self.entry.set_value(0) |
| 502 |
self.pack_start(self.entry, True, True, 0) |
| 503 |
|
| 504 |
self.combo = Gtk.ComboBoxText() |
| 505 |
for item in items: |
| 506 |
self.combo.append_text(_(item)) |
| 507 |
self.combo.set_active(0) |
| 508 |
self.pack_start(self.combo, True, True, 0) |
| 509 |
self.show_all() |
| 510 |
|
| 511 |
def set_state(self, state): |
| 512 |
if not isinstance(state, (tuple, list)) or len(state) < 2: |
| 513 |
return |
| 514 |
|
| 515 |
# TODO: Check length. |
| 516 |
try: |
| 517 |
self.entry.set_value(int(state[0])) |
| 518 |
except ValueError: |
| 519 |
pass |
| 520 |
combo_state = state[1] |
| 521 |
try: |
| 522 |
index = self.items.index(combo_state) |
| 523 |
except ValueError: |
| 524 |
pass |
| 525 |
else: |
| 526 |
self.combo.set_active(index) |
| 527 |
|
| 528 |
def get_state(self): |
| 529 |
active_item = self.items[self.combo.get_active()] |
| 530 |
return [self.entry.get_value(), active_item] |