示例#1
0
def save_html(dsobjects, nick, colors, tmp_path):
    ''' Output a series of HTML pages from the title, pictures, and
    descriptions '''

    htmlcode = ''
    if len(dsobjects) == 0:
        return None

    for i, dsobj in enumerate(dsobjects):
        htmlcode += HTML_GLUE['slide'][0] + str(i)
        htmlcode += HTML_GLUE['slide'][1] + \
            HTML_GLUE['div'][0]
        if 'title' in dsobj.metadata:
            htmlcode += HTML_GLUE['h1'][0] + \
                dsobj.metadata['title'] + \
                HTML_GLUE['h1'][1]

        pixbuf = None
        media_object = False
        try:
            pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(
                dsobj.file_path, 800, 600)
            key = 'img2'
        except:
            pixbuf = get_pixbuf_from_journal(dsobj, 300, 225)
            key = 'img'

        if pixbuf is not None:
            tmp = HTML_GLUE[key][0]
            tmp += image_to_base64(pixbuf, tmp_path)
            tmp += HTML_GLUE[key][1]

        if 'description' in dsobj.metadata:
            tmp += '<p class="body">' + dsobj.metadata['description'] + '</p>'

        htmlcode += tmp + \
            HTML_GLUE['div'][1]

    return HTML_GLUE['doctype'] + \
        HTML_GLUE['html'][0] + \
        HTML_GLUE['head'][0] + \
        HTML_GLUE['meta'] + \
        HTML_GLUE['title'][0] + \
        nick + ' ' + _('Portfolio') + \
        HTML_GLUE['title'][1] + \
        '<style type="text/css">\n<!--\n-->\nbody {background-color:' + colors[0] + ';}\np.head {font-size: 18pt; font-weight: bold; font-family: "Sans"; }\np.body  {font-size: 12pt; font-weight: regular; font-family: "Sans"; }\ndiv.box{width:630px; padding:10px; border:5px; margin:7px; background:' + colors[1] + '}\n</style>\n' +\
        HTML_GLUE['head'][1] + \
        HTML_GLUE['body'][0] + \
        htmlcode + \
        HTML_GLUE['body'][1] + \
        HTML_GLUE['html'][1]
示例#2
0
    def _show_thumb(self, x, y, w, h):
        ''' Display a preview image and title as a thumbnail. '''

        if len(self._thumbs) < self.i + 1:
            # Create a Sprite for this thumbnail
            pixbuf = None
            try:
                pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(
                    self._dsobjects[self.i].file_path, int(w), int(h))
            except:
                pixbuf = get_pixbuf_from_journal(self._dsobjects[self.i],
                                                 int(w), int(h))
            pixbuf_thumb = pixbuf.scale_simple(int(w), int(h),
                                               gtk.gdk.INTERP_TILES)

            self._thumbs.append(
                [Sprite(self._sprites, x, y, pixbuf_thumb), x, y, self.i])
            self._thumbs[-1][0].set_label(str(self.i + 1))
        self._thumbs[self.i][0].set_layer(TOP)
示例#3
0
 def _add_new_from_journal(self, dsobj):
     self.reflection_data.append({
         'title': _('Untitled'),
         'obj_id': dsobj.object_id
     })
     if hasattr(dsobj, 'metadata'):
         if 'creation_time' in dsobj.metadata:
             self.reflection_data[-1]['creation_time'] = \
                 dsobj.metadata['creation_time']
         else:
             self.reflection_data[-1]['creation_time'] = \
                 int(time.time())
         if 'timestamp' in dsobj.metadata:
             self.reflection_data[-1]['modification_time'] = \
                 dsobj.metadata['timestamp']
         else:
             self.reflection_data[-1]['modification_time'] = \
                 self.reflection_data[-1]['creation_time']
         if 'activity' in dsobj.metadata:
             self.reflection_data[-1]['activities'] = \
                 [utils.bundle_id_to_icon(dsobj.metadata['activity'])]
         if 'title' in dsobj.metadata:
             self.reflection_data[-1]['title'] = \
                 dsobj.metadata['title']
         if 'description' in dsobj.metadata:
             self.reflection_data[-1]['content'] = \
                 [{'text': dsobj.metadata['description']}]
         else:
             self.reflection_data[-1]['content'] = []
         if 'tags' in dsobj.metadata:
             self.reflection_data[-1]['tags'] = []
             tags = dsobj.metadata['tags'].split()
             for tag in tags:
                 if tag[0] != '#':
                     self.reflection_data[-1]['tags'].append('#' + tag)
                 else:
                     self.reflection_data[-1]['tags'].append(tag)
         if 'comments' in dsobj.metadata:
             try:
                 comments = json.loads(dsobj.metadata['comments'])
             except BaseException:
                 comments = []
             self.reflection_data[-1]['comments'] = []
             for comment in comments:
                 try:
                     data = {
                         'nick': comment['from'],
                         'comment': comment['message']
                     }
                     if 'icon-color' in comment:
                         colors = comment['icon-color'].split(',')
                         darker = 1 - utils.lighter_color(colors)
                         data['color'] = colors[darker]
                     else:
                         data['color'] = '#000000'
                     self.reflection_data[-1]['comments'].append(data)
                 except BaseException:
                     _logger.debug('could not parse comment %s' % comment)
         if 'mime_type' in dsobj.metadata and \
            dsobj.metadata['mime_type'][0:5] == 'image':
             new_path = os.path.join(self.tmp_path, dsobj.object_id)
             try:
                 shutil.copy(dsobj.file_path, new_path)
             except Exception as e:
                 logging.error("Couldn't copy %s to %s: %s" %
                               (dsobj.file_path, new_path, e))
             self.reflection_data[-1]['content'].append({'image': new_path})
         elif 'preview' in dsobj.metadata:
             pixbuf = utils.get_pixbuf_from_journal(dsobj, 300, 225)
             if pixbuf is not None:
                 path = os.path.join(self.tmp_path,
                                     dsobj.object_id + '.png')
                 utils.save_pixbuf_to_file(pixbuf, path)
                 self.reflection_data[-1]['content'].append({'image': path})
         self.reflection_data[-1]['stars'] = 0
示例#4
0
    def _show_slide(self):
        ''' Display a title, preview image, and decription for slide i. '''
        self._clear_screen()

        if self._nobjects == 0:
            self._prev_button.set_icon('go-previous-inactive')
            self._next_button.set_icon('go-next-inactive')
            self._description.set_label(
                _('Do you have any items in your Journal starred?'))
            self._description.set_layer(MIDDLE)
            return

        if self.i == 0:
            self._prev_button.set_icon('go-previous-inactive')
        else:
            self._prev_button.set_icon('go-previous')
        if self.i == self._nobjects - 1:
            self._next_button.set_icon('go-next-inactive')
        else:
            self._next_button.set_icon('go-next')

        pixbuf = None
        media_object = False
        try:
            pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(
                self._dsobjects[self.i].file_path, int(PREVIEWW * self._scale),
                int(PREVIEWH * self._scale))
            media_object = True
        except:
            pixbuf = get_pixbuf_from_journal(self._dsobjects[self.i], 300, 225)

        if pixbuf is not None:
            if not media_object:
                self._preview.images[0] = pixbuf.scale_simple(
                    int(PREVIEWW * self._scale), int(PREVIEWH * self._scale),
                    gtk.gdk.INTERP_TILES)
                self._full_screen.hide()
                self._preview.set_layer(MIDDLE)
            else:
                self._full_screen.images[0] = pixbuf.scale_simple(
                    int(FULLW * self._scale), int(FULLH * self._scale),
                    gtk.gdk.INTERP_TILES)
                self._full_screen.set_layer(MIDDLE)
                self._preview.hide()
        else:
            if self._preview is not None:
                self._preview.hide()
                self._full_screen.hide()

        self._title.set_label(self._dsobjects[self.i].metadata['title'])
        self._title.set_layer(MIDDLE)

        if 'description' in self._dsobjects[self.i].metadata:
            if media_object:
                self._description2.set_label(
                    self._dsobjects[self.i].metadata['description'])
                self._description2.set_layer(MIDDLE)
                self._description.set_label('')
                self._description.hide()
            else:
                self._description.set_label(
                    self._dsobjects[self.i].metadata['description'])
                self._description.set_layer(MIDDLE)
                self._description2.set_label('')
                self._description2.hide()
        else:
            self._description.set_label('')
            self._description.hide()
            self._description2.set_label('')
            self._description2.hide()
        if self._hw == XO175:
            self._bump_id = gobject.timeout_add(int(500), self._bump_test)
def save_pdf(activity, nick, description=None):
    ''' Output a PDF document from the title, pictures, and descriptions '''

    if len(activity.dsobjects) == 0:
        return None

    head = activity.title_size
    body = activity.desc_size / 2

    tmp_file = os.path.join(activity.datapath, 'output.pdf')
    pdf_surface = cairo.PDFSurface(tmp_file, 504, 648)

    fd = Pango.FontDescription('Sans')
    cr = cairo.Context(pdf_surface)
    cr.set_source_rgb(0, 0, 0)

    show_text(cr, fd, nick, head, LEFT_MARGIN, TOP_MARGIN)
    show_text(cr, fd, time.strftime('%x', time.localtime()),
              body, LEFT_MARGIN, TOP_MARGIN + 3 * head)
    if description is not None:
        show_text(cr, fd, description,
                  body, LEFT_MARGIN, TOP_MARGIN + 4 * head)
    cr.show_page()

    for i, dsobj in enumerate(activity.dsobjects):
        if dsobj.metadata['keep'] == '0':
            continue
        if 'title' in dsobj.metadata:
            show_text(cr, fd, dsobj.metadata['title'], head, LEFT_MARGIN,
                      TOP_MARGIN)
        else:
            show_text(cr, fd, _('untitled'), head, LEFT_MARGIN,
                      TOP_MARGIN)

        w = 0
        h = 0
        pixbuf = None
        if os.path.exists(dsobj.file_path):
            print dsobj.file_path
            try:
                w = int(PAGE_WIDTH - LEFT_MARGIN * 2)
                h = int(w * 3 / 4)
                pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
                    dsobj.file_path, w, h)
            except:
                try:
                    w = 300
                    h = 225
                    pixbuf = get_pixbuf_from_journal(dsobj, w, h)
                except:
                    pass

        if pixbuf is not None:
            cr.save()
            Gdk.cairo_set_source_pixbuf(
                cr, pixbuf, LEFT_MARGIN, TOP_MARGIN + 150)
            cr.rectangle(LEFT_MARGIN, TOP_MARGIN + 150, w, h)
            cr.fill()
            cr.restore()

        text = ''
        if 'description' in dsobj.metadata:
            text += dsobj.metadata['description']
        if 'comments' in dsobj.metadata:
            text += '\n'
            text += parse_comments(json.loads(dsobj.metadata['comments']))
        show_text(cr, fd, text, body, LEFT_MARGIN, h + 175)

        cr.show_page()

    return tmp_file
示例#6
0
 def _add_new_from_journal(self, dsobj):
     self.reflection_data.append({
         'title': _('Untitled'), 'obj_id': dsobj.object_id})
     if hasattr(dsobj, 'metadata'):
         if 'creation_time' in dsobj.metadata:
             self.reflection_data[-1]['creation_time'] = \
                 dsobj.metadata['creation_time']
         else:
             self.reflection_data[-1]['creation_time'] = \
                 int(time.time())
         if 'timestamp' in dsobj.metadata:
             self.reflection_data[-1]['modification_time'] = \
                 dsobj.metadata['timestamp']
         else:
             self.reflection_data[-1]['modification_time'] = \
                 self.reflection_data[-1]['creation_time']
         if 'activity' in dsobj.metadata:
             self.reflection_data[-1]['activities'] = \
                 [utils.bundle_id_to_icon(dsobj.metadata['activity'])]
         if 'title' in dsobj.metadata:
             self.reflection_data[-1]['title'] = \
                 dsobj.metadata['title']
         if 'description' in dsobj.metadata:
             self.reflection_data[-1]['content'] = \
                 [{'text': dsobj.metadata['description']}]
         else:
             self.reflection_data[-1]['content'] = []
         if 'tags' in dsobj.metadata:
             self.reflection_data[-1]['tags'] = []
             tags = dsobj.metadata['tags'].split()
             for tag in tags:
                 if tag[0] != '#':
                     self.reflection_data[-1]['tags'].append('#' + tag)
                 else:
                     self.reflection_data[-1]['tags'].append(tag)
         if 'comments' in dsobj.metadata:
             try:
                 comments = json.loads(dsobj.metadata['comments'])
             except:
                 comments = []
             self.reflection_data[-1]['comments'] = []
             for comment in comments:
                 try:
                     data = {'nick': comment['from'],
                             'comment': comment['message']}
                     if 'icon-color' in comment:
                         colors = comment['icon-color'].split(',')
                         darker = 1 - utils.lighter_color(colors)
                         data['color'] = colors[darker]
                     else:
                         data['color'] = '#000000'
                     self.reflection_data[-1]['comments'].append(data)
                 except:
                     _logger.debug('could not parse comment %s'
                                   % comment)
         if 'mime_type' in dsobj.metadata and \
            dsobj.metadata['mime_type'][0:5] == 'image':
             new_path = os.path.join(self.tmp_path,
                                     dsobj.object_id)
             try:
                 shutil.copy(dsobj.file_path, new_path)
             except Exception as e:
                 logging.error("Couldn't copy %s to %s: %s" %
                               (dsobj.file_path, new_path, e))
             self.reflection_data[-1]['content'].append(
                 {'image': new_path})
         elif 'preview' in dsobj.metadata:
             pixbuf = utils.get_pixbuf_from_journal(dsobj, 300, 225)
             if pixbuf is not None:
                 path = os.path.join(self.tmp_path,
                                     dsobj.object_id + '.png')
                 utils.save_pixbuf_to_file(pixbuf, path)
                 self.reflection_data[-1]['content'].append(
                     {'image': path})
         self.reflection_data[-1]['stars'] = 0
    def _setup_workspace(self):
        ''' Prepare to render the datastore entries. '''

        # Use the lighter color for the text background
        if lighter_color(self.colors) == 0:
            tmp = self.colors[0]
            self.colors[0] = self.colors[1]
            self.colors[1] = tmp

        self._width = gtk.gdk.screen_width()
        self._height = gtk.gdk.screen_height()
        self._scale = gtk.gdk.screen_height() / 900.

        if not HAVE_TOOLBOX and self._hw[0:2] == 'xo':
            titlef = 18
            descriptionf = 12
        else:
            titlef = 36
            descriptionf = 24

        self._find_starred()
        for ds in self.dsobjects:
            if 'title' in ds.metadata:
                title = ds.metadata['title']
            else:
                title = None
            pixbuf = None
            media_object = False
            mimetype = None
            if 'mime_type' in ds.metadata:
                mimetype = ds.metadata['mime_type']
            if mimetype[0:5] == 'image':
                pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(
                    ds.file_path, MAXX, MAXY)
                    # ds.file_path, 300, 225)
                media_object = True
            else:
                pixbuf = get_pixbuf_from_journal(ds, MAXX, MAXY)  # 300, 225)
            if 'description' in ds.metadata:
                desc = ds.metadata['description']
            else:
                desc = None
            self.slides.append(Slide(True, ds.object_id, self.colors,
                                     title, pixbuf, desc))

        # Generate the sprites we'll need...
        self._sprites = Sprites(self._canvas)

        self._help = Sprite(
            self._sprites,
            int((self._width - int(PREVIEWW * self._scale)) / 2),
            int(PREVIEWY * self._scale),
            gtk.gdk.pixbuf_new_from_file_at_size(
                os.path.join(activity.get_bundle_path(), 'help.png'),
                int(PREVIEWW * self._scale), int(PREVIEWH * self._scale)))
        self._help.hide()

        self._genblanks(self.colors)

        self._title = Sprite(self._sprites, 0, 0, self._title_pixbuf)
        self._title.set_label_attributes(int(titlef * self._scale),
                                         rescale=False)
        self._preview = Sprite(self._sprites,
            int((self._width - int(PREVIEWW * self._scale)) / 2),
            int(PREVIEWY * self._scale), self._preview_pixbuf)

        self._description = Sprite(self._sprites,
                                   int(DESCRIPTIONX * self._scale),
                                   int(DESCRIPTIONY * self._scale),
                                   self._desc_pixbuf)
        self._description.set_label_attributes(int(descriptionf * self._scale))

        self._my_canvas = Sprite(self._sprites, 0, 0, self._canvas_pixbuf)
        self._my_canvas.set_layer(BOTTOM)

        self._clear_screen()

        self.i = 0
        self._show_slide()

        self._playing = False
        self._rate = 10