Use cropped regions as images; show gallery in order of media_list, and include snapshot in gallery

svn: r15179
This commit is contained in:
Doug Blank 2010-04-17 12:42:42 +00:00
parent 690daa1783
commit 59f12247f5

View File

@ -257,6 +257,32 @@ def format_date(date):
return val return val
return "" return ""
def copy_thumbnail(report, handle, photo, region=None):
"""
Given a handle (and optional region) make (if needed) an
up-to-date cache of a thumbnail, and call report.copy_file
to copy the cached thumbnail to the website.
Return the new path to the image.
"""
db = report.database
to_dir = report.build_path('thumb', handle)
if region:
to_path = os.path.join(to_dir, handle) + ('%d,%d-%d,%d.png' % region)
else:
to_path = os.path.join(to_dir, handle) + '.png'
if photo.get_mime_type():
from_path = ThumbNails.get_thumbnail_path(Utils.media_path_full(
db,
photo.get_path()),
photo.get_mime_type(),
region)
if not os.path.isfile(from_path):
from_path = os.path.join(const.IMAGE_DIR, "document.png")
else:
from_path = os.path.join(const.IMAGE_DIR, "document.png")
report.copy_file(from_path, to_path)
return to_path
class BasePage(object): class BasePage(object):
""" """
This is the base class to write certain HTML pages. This is the base class to write certain HTML pages.
@ -1091,7 +1117,7 @@ class BasePage(object):
self.report.copy_file(Utils.media_path_full(db, obj.get_path()), newpath) self.report.copy_file(Utils.media_path_full(db, obj.get_path()), newpath)
# get media rectangles # get media rectangles
_region_items = self.media_ref_regions(obj_handle, obj) _region_items = self.media_ref_regions(obj_handle)
if len(_region_items): if len(_region_items):
with Html("div") as mediadisplay: with Html("div") as mediadisplay:
@ -1143,7 +1169,7 @@ class BasePage(object):
# no image to return # no image to return
return None return None
def media_ref_regions(self, handle, media): def media_ref_regions(self, handle):
""" """
************************************* *************************************
@ -1239,7 +1265,19 @@ class BasePage(object):
# return media rectangles to its callers # return media rectangles to its callers
return _region_items return _region_items
def display_first_image_as_thumbnail( self, photolist = None): def media_ref_region_to_object(self, media_handle, obj):
"""
Return a region of this image if it refers to this object.
"""
# get a list of all media refs for this object
for mediaref in obj.get_media_list():
# is this mediaref for this image? do we have a rect?
if (mediaref.ref == media_handle and
mediaref.rect is not None):
return mediaref.rect # (x1, y1, x2, y2)
return None
def display_first_image_as_thumbnail( self, photolist, object):
db = self.report.database db = self.report.database
if not photolist or not self.create_media: if not photolist or not self.create_media:
@ -1253,26 +1291,44 @@ class BasePage(object):
with Html("div", class_ = "snapshot") as snapshot: with Html("div", class_ = "snapshot") as snapshot:
if mime_type: if mime_type:
try:
# get media rectangles region = self.media_ref_region_to_object(photo_handle, object)
_region_items = self.media_ref_regions(photo_handle, photo) if region:
lnkref = (self.report.cur_fname, self.page_title, self.gid)
self.report.add_lnkref_to_photo(photo, lnkref)
# make a thumbnail of this region
newpath = copy_thumbnail(self.report, photo_handle, photo, region)
# TODO. Check if build_url_fname can be used.
newpath = "/".join(['..']*3 + [newpath])
if constfunc.win():
newpath = newpath.replace('\\',"/")
snapshot += self.media_link(photo_handle, newpath, '', up = True)
else:
_region_items = self.media_ref_regions(photo_handle)
if len(_region_items): if len(_region_items):
with Html("div") as mediadisplay: with Html("div", id="GalleryDisplay") as mediadisplay:
snapshot += mediadisplay
# Feature #2634; display the mouse-selectable regions.
# See the large block at the top of this function where
# the various regions are stored in _region_items
ordered = Html("ol", class_ = "RegionBox") ordered = Html("ol", class_ = "RegionBox")
snapshot += mediadisplay
mediadisplay += ordered mediadisplay += ordered
while len(_region_items): while len(_region_items):
(name, x, y, w, h, linkurl) = _region_items.pop() (name, x, y, w, h, linkurl) = _region_items.pop()
ordered += Html("li", style = "left:%d%%; top:%d%%; width:%d%%; height:%d%%;" ordered += Html("li",
% (x, y, w, h)) +( style="left:%d%%; top:%d%%; width:%d%%; height:%d%%;"
Html("a", name, href = linkurl) % (x, y, w, h)) + Html("a", name, href = linkurl)
) lnkref = (self.report.cur_fname, self.page_title, self.gid)
self.report.add_lnkref_to_photo(photo, lnkref)
real_path, newpath = self.report.prepare_copy_media(photo)
# TODO. Check if build_url_fname can be used.
newpath = "/".join(['..']*3 + [newpath])
if constfunc.win():
newpath = newpath.replace('\\',"/")
# Need to add link to mediadisplay to get the links:
mediadisplay += self.media_link(photo_handle, newpath, '', up = True)
else:
try:
lnkref = (self.report.cur_fname, self.page_title, self.gid) lnkref = (self.report.cur_fname, self.page_title, self.gid)
self.report.add_lnkref_to_photo(photo, lnkref) self.report.add_lnkref_to_photo(photo, lnkref)
real_path, newpath = self.report.prepare_copy_media(photo) real_path, newpath = self.report.prepare_copy_media(photo)
@ -1289,7 +1345,6 @@ class BasePage(object):
except (IOError, OSError), msg: except (IOError, OSError), msg:
WarningDialog(_("Could not add photo to page"), str(msg)) WarningDialog(_("Could not add photo to page"), str(msg))
else: else:
# get media description # get media description
descr = photo.get_description() descr = photo.get_description()
@ -1308,18 +1363,31 @@ class BasePage(object):
# return snapshot division to its callers # return snapshot division to its callers
return snapshot return snapshot
def display_additional_images_as_gallery( self, photolist = None): def display_additional_images_as_gallery(self, photolist, object):
if not photolist or not self.create_media: if not photolist or not self.create_media:
return None return None
db = self.report.database db = self.report.database
# make referenced images have the same order as in media list:
photolist_handles = {}
for mediaref in photolist:
photolist_handles[mediaref.get_reference_handle()] = mediaref
photolist_ordered = []
for photoref in object.get_media_list():
if photoref.ref in photolist_handles:
photo = photolist_handles[photoref.ref]
photolist_ordered.append(photo)
photolist.remove(photo)
# and add any that are left (should there be any?)
photolist_ordered += photolist
# begin individualgallery division and section title # begin individualgallery division and section title
with Html("div", class_ = "subsection", id = "indivgallery") as section: with Html("div", class_ = "subsection", id = "indivgallery") as section:
section += Html("h4", _("Gallery"), inline = True) section += Html("h4", _("Gallery"), inline = True)
displayed = [] displayed = []
for mediaref in photolist: for mediaref in photolist_ordered:
photo_handle = mediaref.get_reference_handle() photo_handle = mediaref.get_reference_handle()
photo = db.get_object_from_handle(photo_handle) photo = db.get_object_from_handle(photo_handle)
@ -1335,7 +1403,7 @@ class BasePage(object):
try: try:
# get media rectangles # get media rectangles
_region_items = self.media_ref_regions(photo_handle, photo) _region_items = self.media_ref_regions(photo_handle)
if len(_region_items): if len(_region_items):
with Html("div") as mediadisplay: with Html("div") as mediadisplay:
section += mediadisplay section += mediadisplay
@ -2255,7 +2323,7 @@ class PlacePage(BasePage):
body += placedetail body += placedetail
media_list = place.get_media_list() media_list = place.get_media_list()
thumbnail = self.display_first_image_as_thumbnail(media_list) thumbnail = self.display_first_image_as_thumbnail(media_list, place)
if thumbnail is not None: if thumbnail is not None:
placedetail += thumbnail placedetail += thumbnail
@ -2274,7 +2342,7 @@ class PlacePage(BasePage):
# place gallery # place gallery
if self.create_media: if self.create_media:
placegallery = self.display_additional_images_as_gallery(media_list) placegallery = self.display_additional_images_as_gallery(media_list, place)
if placegallery is not None: if placegallery is not None:
placedetail += placegallery placedetail += placegallery
@ -2603,7 +2671,7 @@ class MediaPage(BasePage):
BasePage.__init__(self, report, title, media.gramps_id) BasePage.__init__(self, report, title, media.gramps_id)
# get media rectangles # get media rectangles
_region_items = self.media_ref_regions(handle, media) _region_items = self.media_ref_regions(handle)
of = self.report.create_file(handle, "img") of = self.report.create_file(handle, "img")
self.up = True self.up = True
@ -3175,7 +3243,7 @@ class SourcePage(BasePage):
body += section body += section
media_list = source.get_media_list() media_list = source.get_media_list()
thumbnail = self.display_first_image_as_thumbnail(media_list) thumbnail = self.display_first_image_as_thumbnail(media_list, source)
if thumbnail is not None: if thumbnail is not None:
section += thumbnail section += thumbnail
@ -3207,7 +3275,7 @@ class SourcePage(BasePage):
tbody += trow tbody += trow
# additional media # additional media
sourcemedia = self.display_additional_images_as_gallery(media_list) sourcemedia = self.display_additional_images_as_gallery(media_list, source)
if sourcemedia is not None: if sourcemedia is not None:
section += sourcemedia section += sourcemedia
@ -3581,10 +3649,8 @@ class IndividualPage(BasePage):
if sect6 is not None: if sect6 is not None:
individualdetail += sect6 individualdetail += sect6
media_list = []
photo_list = self.person.get_media_list() photo_list = self.person.get_media_list()
if len(photo_list) > 1: media_list = photo_list[:]
media_list = photo_list[1:]
for handle in self.person.get_family_handle_list(): for handle in self.person.get_family_handle_list():
family = db.get_family_from_handle(handle) family = db.get_family_from_handle(handle)
media_list += family.get_media_list() media_list += family.get_media_list()
@ -3597,7 +3663,7 @@ class IndividualPage(BasePage):
media_list += event.get_media_list() media_list += event.get_media_list()
# display additional images as gallery # display additional images as gallery
sect7 = self.display_additional_images_as_gallery(media_list) sect7 = self.display_additional_images_as_gallery(media_list, self.person)
if sect7 is not None: if sect7 is not None:
individualdetail += sect7 individualdetail += sect7
@ -3674,6 +3740,18 @@ class IndividualPage(BasePage):
photo = db.get_object_from_handle(photo_handle) photo = db.get_object_from_handle(photo_handle)
mime_type = photo.get_mime_type() mime_type = photo.get_mime_type()
if mime_type: if mime_type:
region = self.media_ref_region_to_object(photo_handle, person)
if region:
# make a thumbnail of this region
newpath = copy_thumbnail(self.report, photo_handle, photo, region)
# TODO. Check if build_url_fname can be used.
newpath = "/".join(['..']*3 + [newpath])
if constfunc.win():
newpath = newpath.replace('\\',"/")
thumbnailUrl = newpath
#snapshot += self.media_link(photo_handle, newpath, '', up = True)
else:
(photoUrl, thumbnailUrl) = self.report.prepare_copy_media(photo) (photoUrl, thumbnailUrl) = self.report.prepare_copy_media(photo)
thumbnailUrl = "/".join(['..']*3 + [thumbnailUrl]) thumbnailUrl = "/".join(['..']*3 + [thumbnailUrl])
if constfunc.win(): if constfunc.win():
@ -3906,7 +3984,7 @@ class IndividualPage(BasePage):
db = self.report.database db = self.report.database
self.page_title = self.sort_name self.page_title = self.sort_name
thumbnail = self.display_first_image_as_thumbnail(self.person.get_media_list() ) thumbnail = self.display_first_image_as_thumbnail(self.person.get_media_list(), self.person)
section_title = Html("h3", self.get_name(self.person), inline = True) section_title = Html("h3", self.get_name(self.person), inline = True)