From 4a1c3e8dec0db95f6a9aac135cf04990a90d3890 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Fri, 5 Dec 2014 19:02:10 +0100 Subject: [PATCH 1/7] [ADD] web_tree_widget --- web_tree_image/__init__.py | 0 web_tree_image/__openerp__.py | 51 +++++++++++++++++++ .../static/src/js/web_tree_image.js | 34 +++++++++++++ web_tree_image/static/src/xml/widget.xml | 6 +++ web_tree_image/view/assets.xml | 10 ++++ 5 files changed, 101 insertions(+) create mode 100644 web_tree_image/__init__.py create mode 100644 web_tree_image/__openerp__.py create mode 100644 web_tree_image/static/src/js/web_tree_image.js create mode 100644 web_tree_image/static/src/xml/widget.xml create mode 100644 web_tree_image/view/assets.xml diff --git a/web_tree_image/__init__.py b/web_tree_image/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/web_tree_image/__openerp__.py b/web_tree_image/__openerp__.py new file mode 100644 index 00000000..b82000eb --- /dev/null +++ b/web_tree_image/__openerp__.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +{ + "name": "Show images in tree views", + "version": "1.0", + "author": "Therp BV", + "description": """\ +This module defines a tree image widget, to be used with function fields of +type character. Use widget='tree_image' in your view definition. Optionally, set +a 'height' tag. Default height is 16px. + +The content of the field can be any of the following: + +* the absolute or relative location of an image. For example, \ +"//static/src/img/youricon.png" + +* a standard icon from the web distribution, without path or extension, For \ +example, 'gtk-open' + +* A dynamic image in a data url base 64 format. To show a regular image field \ +from the model, use a function field wrapper that retrieves the image with \ +bin_size=False in the context, and prefix with 'data:image/png;base64,' + """, + 'depends': [ + 'web', + ], + 'qweb': [ + 'static/src/xml/widget.xml', + ], + 'data': [ + 'view/assets.xml', + ], +} diff --git a/web_tree_image/static/src/js/web_tree_image.js b/web_tree_image/static/src/js/web_tree_image.js new file mode 100644 index 00000000..cd97d39b --- /dev/null +++ b/web_tree_image/static/src/js/web_tree_image.js @@ -0,0 +1,34 @@ +/* + OpenERP, Open Source Management Solution + This module copyright (C) 2014 Therp BV (). + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +openerp.web_tree_image = function (instance) { + instance.web.list.Image = instance.web.list.Column.extend({ + /* Return an image tag */ + format: function (row_data, options) { + if (!row_data[this.id] || !row_data[this.id].value) { return ''; } + var src; + if (!/\//.test(row_data[this.id].value)) { + src = '/web/static/src/img/icons/' + row_data[this.id].value + '.png'; + } else { + src = row_data[this.id].value; + } + return instance.web.qweb.render('ListView.row.image', {widget: this, src: src}); + } + }); + instance.web.list.columns.add('field.tree_image', 'instance.web.list.Image'); +}; diff --git a/web_tree_image/static/src/xml/widget.xml b/web_tree_image/static/src/xml/widget.xml new file mode 100644 index 00000000..cbd1bc0b --- /dev/null +++ b/web_tree_image/static/src/xml/widget.xml @@ -0,0 +1,6 @@ + + + + diff --git a/web_tree_image/view/assets.xml b/web_tree_image/view/assets.xml new file mode 100644 index 00000000..fa79d63e --- /dev/null +++ b/web_tree_image/view/assets.xml @@ -0,0 +1,10 @@ + + + + + + From 794fc63c1c873aededbe6e010dea6fc6c726cc1e Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Fri, 5 Dec 2014 19:27:37 +0100 Subject: [PATCH 2/7] [IMP] Handle binary fields. Courtesy of Marcel van der Boom --- web_tree_image/__openerp__.py | 17 ++++++----- .../static/src/js/web_tree_image.js | 30 ++++++++++++++----- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/web_tree_image/__openerp__.py b/web_tree_image/__openerp__.py index b82000eb..c16c1ebc 100644 --- a/web_tree_image/__openerp__.py +++ b/web_tree_image/__openerp__.py @@ -4,6 +4,9 @@ # OpenERP, Open Source Management Solution # This module copyright (C) 2014 Therp BV (). # +# Snippet from https://github.com/hsd/listview_images +# Copyright (C) 2013 Marcel van der Boom +# # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the @@ -23,11 +26,12 @@ "version": "1.0", "author": "Therp BV", "description": """\ -This module defines a tree image widget, to be used with function fields of -type character. Use widget='tree_image' in your view definition. Optionally, set -a 'height' tag. Default height is 16px. +This module defines a tree image widget, to be used with either binary fields +or function fields of type character. Use widget='tree_image' in your view +definition. Optionally, set a 'height' tag. Default height is 16px. -The content of the field can be any of the following: +If you use the widget with a character field, the content of the field can be +any of the following: * the absolute or relative location of an image. For example, \ "//static/src/img/youricon.png" @@ -35,9 +39,8 @@ The content of the field can be any of the following: * a standard icon from the web distribution, without path or extension, For \ example, 'gtk-open' -* A dynamic image in a data url base 64 format. To show a regular image field \ -from the model, use a function field wrapper that retrieves the image with \ -bin_size=False in the context, and prefix with 'data:image/png;base64,' +* A dynamic image in a data url base 64 format. Prefix with \ +'data:image/png;base64,' """, 'depends': [ 'web', diff --git a/web_tree_image/static/src/js/web_tree_image.js b/web_tree_image/static/src/js/web_tree_image.js index cd97d39b..f161a2d5 100644 --- a/web_tree_image/static/src/js/web_tree_image.js +++ b/web_tree_image/static/src/js/web_tree_image.js @@ -1,6 +1,7 @@ /* OpenERP, Open Source Management Solution - This module copyright (C) 2014 Therp BV (). + This module copyright (C) 2014 Therp BV () + (C) 2013 Marcel van der Boom This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as @@ -18,14 +19,29 @@ openerp.web_tree_image = function (instance) { instance.web.list.Image = instance.web.list.Column.extend({ - /* Return an image tag */ format: function (row_data, options) { - if (!row_data[this.id] || !row_data[this.id].value) { return ''; } - var src; - if (!/\//.test(row_data[this.id].value)) { - src = '/web/static/src/img/icons/' + row_data[this.id].value + '.png'; + /* Return a valid img tag. For image fields, test if the + field's value contains just the binary size and retrieve + the image from the dedicated controller in that case. + Otherwise, assume a character field containing either a + stock Odoo icon name without path or extension or a fully + fledged location or data url */ + if (!row_data[this.id] || !row_data[this.id].value) { + return ''; + } + var value = row_data[this.id].value, src; + if (this.type === 'binary') { + if (value && value.substr(0, 10).indexOf(' ') === -1) { + src = "data:image/png;base64," + value; + } else { + src = instance.session.url('/web/binary/image', {model: options.model, field: this.id, id: options.id}); + } } else { - src = row_data[this.id].value; + if (!/\//.test(row_data[this.id].value)) { + src = '/web/static/src/img/icons/' + row_data[this.id].value + '.png'; + } else { + src = row_data[this.id].value; + } } return instance.web.qweb.render('ListView.row.image', {widget: this, src: src}); } From 72c52003758b93d45689d606e9977604f51015b1 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Fri, 5 Dec 2014 19:30:01 +0100 Subject: [PATCH 3/7] [IMP] manifest --- web_tree_image/__openerp__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/web_tree_image/__openerp__.py b/web_tree_image/__openerp__.py index c16c1ebc..4d77d720 100644 --- a/web_tree_image/__openerp__.py +++ b/web_tree_image/__openerp__.py @@ -42,6 +42,7 @@ example, 'gtk-open' * A dynamic image in a data url base 64 format. Prefix with \ 'data:image/png;base64,' """, + 'url': 'https://github.com/OCA/Web', 'depends': [ 'web', ], From 3109fada7f8f5166ea30cea12b716738a269f641 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Fri, 5 Dec 2014 19:46:11 +0100 Subject: [PATCH 4/7] [IMP] Module description --- web_tree_image/__openerp__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_tree_image/__openerp__.py b/web_tree_image/__openerp__.py index 4d77d720..134f9514 100644 --- a/web_tree_image/__openerp__.py +++ b/web_tree_image/__openerp__.py @@ -27,7 +27,7 @@ "author": "Therp BV", "description": """\ This module defines a tree image widget, to be used with either binary fields -or function fields of type character. Use widget='tree_image' in your view +or (function) fields of type character. Use widget='tree_image' in your view definition. Optionally, set a 'height' tag. Default height is 16px. If you use the widget with a character field, the content of the field can be From e497db67805f6be1ee733c06c171022ec39600fa Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Mon, 8 Dec 2014 10:28:21 +0100 Subject: [PATCH 5/7] [RFR] Move module description to README [FIX] rst line endings [IMP] Comment on arbitrary media subtype --- web_tree_image/README.rst | 18 ++++++++++++++++++ web_tree_image/__openerp__.py | 17 ----------------- web_tree_image/static/src/js/web_tree_image.js | 1 + 3 files changed, 19 insertions(+), 17 deletions(-) create mode 100644 web_tree_image/README.rst diff --git a/web_tree_image/README.rst b/web_tree_image/README.rst new file mode 100644 index 00000000..502be0d8 --- /dev/null +++ b/web_tree_image/README.rst @@ -0,0 +1,18 @@ +Display images and icons in tree view +===================================== + +This module defines a tree image widget, to be used with either binary fields +or (function) fields of type character. Use widget='tree_image' in your view +definition. Optionally, set a 'height' tag. Default height is 16px. + +If you use the widget with a character field, the content of the field can be +any of the following: + +* the absolute or relative location of an image. For example, + "//static/src/img/youricon.png" + +* a standard icon from the web distribution, without path or extension, For + example, 'gtk-open' + +* A dynamic image in a data url base 64 format. Prefix with + 'data:image/png;base64,' diff --git a/web_tree_image/__openerp__.py b/web_tree_image/__openerp__.py index 134f9514..ae8c726d 100644 --- a/web_tree_image/__openerp__.py +++ b/web_tree_image/__openerp__.py @@ -25,23 +25,6 @@ "name": "Show images in tree views", "version": "1.0", "author": "Therp BV", - "description": """\ -This module defines a tree image widget, to be used with either binary fields -or (function) fields of type character. Use widget='tree_image' in your view -definition. Optionally, set a 'height' tag. Default height is 16px. - -If you use the widget with a character field, the content of the field can be -any of the following: - -* the absolute or relative location of an image. For example, \ -"//static/src/img/youricon.png" - -* a standard icon from the web distribution, without path or extension, For \ -example, 'gtk-open' - -* A dynamic image in a data url base 64 format. Prefix with \ -'data:image/png;base64,' - """, 'url': 'https://github.com/OCA/Web', 'depends': [ 'web', diff --git a/web_tree_image/static/src/js/web_tree_image.js b/web_tree_image/static/src/js/web_tree_image.js index f161a2d5..1ecaab6e 100644 --- a/web_tree_image/static/src/js/web_tree_image.js +++ b/web_tree_image/static/src/js/web_tree_image.js @@ -32,6 +32,7 @@ openerp.web_tree_image = function (instance) { var value = row_data[this.id].value, src; if (this.type === 'binary') { if (value && value.substr(0, 10).indexOf(' ') === -1) { + // The media subtype (png) seems to be arbitrary src = "data:image/png;base64," + value; } else { src = instance.session.url('/web/binary/image', {model: options.model, field: this.id, id: options.id}); From f100b33f9404f3f2ba07627c2435af8aebbf9f73 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Mon, 8 Dec 2014 10:34:32 +0100 Subject: [PATCH 6/7] [IMP] Widget name 'image' instead of 'tree_image' --- web_tree_image/README.rst | 2 +- web_tree_image/static/src/js/web_tree_image.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/web_tree_image/README.rst b/web_tree_image/README.rst index 502be0d8..83cde6c5 100644 --- a/web_tree_image/README.rst +++ b/web_tree_image/README.rst @@ -2,7 +2,7 @@ Display images and icons in tree view ===================================== This module defines a tree image widget, to be used with either binary fields -or (function) fields of type character. Use widget='tree_image' in your view +or (function) fields of type character. Use widget='image' in your view definition. Optionally, set a 'height' tag. Default height is 16px. If you use the widget with a character field, the content of the field can be diff --git a/web_tree_image/static/src/js/web_tree_image.js b/web_tree_image/static/src/js/web_tree_image.js index 1ecaab6e..f08bf204 100644 --- a/web_tree_image/static/src/js/web_tree_image.js +++ b/web_tree_image/static/src/js/web_tree_image.js @@ -47,5 +47,5 @@ openerp.web_tree_image = function (instance) { return instance.web.qweb.render('ListView.row.image', {widget: this, src: src}); } }); - instance.web.list.columns.add('field.tree_image', 'instance.web.list.Image'); + instance.web.list.columns.add('field.image', 'instance.web.list.Image'); }; From f7dd09e387b7eec501ee949f8aee949cdc6291b2 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Mon, 8 Dec 2014 11:25:30 +0100 Subject: [PATCH 7/7] [FIX] Capitalization consistency --- web_tree_image/README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_tree_image/README.rst b/web_tree_image/README.rst index 83cde6c5..7dc396ad 100644 --- a/web_tree_image/README.rst +++ b/web_tree_image/README.rst @@ -8,10 +8,10 @@ definition. Optionally, set a 'height' tag. Default height is 16px. If you use the widget with a character field, the content of the field can be any of the following: -* the absolute or relative location of an image. For example, +* The absolute or relative location of an image. For example, "//static/src/img/youricon.png" -* a standard icon from the web distribution, without path or extension, For +* A standard icon from the web distribution, without path or extension, For example, 'gtk-open' * A dynamic image in a data url base 64 format. Prefix with