File: image_figure.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (133 lines) | stat: -rw-r--r-- 3,562 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* 
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * 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; version 2 of the
 * License.
 * 
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "image_figure.h"

using namespace wbfig;
using namespace base;

Image::Image(mdc::Layer *layer, FigureEventHub *hub, const model_ObjectRef &self) 
: BaseFigure(layer, hub, self), _image(layer)
{
  set_cache_toplevel_contents(false);
  set_accepts_focus();
  set_accepts_selection();
  set_allowed_resizing(true, true);

  add(&_image, true, true, false); 
  
  _image.set_auto_sizing(true);
  
  _keep_aspect_ratio= false;
}


static void constrain_aspect_ratio(mdc::ItemHandle *handle, Size &size, double ratio)
{
  if ((handle->get_tag() & (HDL_LEFT|HDL_RIGHT)) != 0)
    size.height= size.width/ratio;
  else
    size.width= size.height*ratio;
}


cairo_surface_t *Image::get_image()
{
  return _image.get_image();
}


void Image::keep_aspect_ratio(bool flag)
{
  _keep_aspect_ratio= flag;

  if (flag)
  {
    cairo_surface_t *surf= _image.get_image();

    if (surf)
    {
      double aspect= get_aspect_ratio();
      double width= _image.get_size().width;
      double new_height= width / aspect;

      if (fabs(_image.get_size().height-new_height) > 1)
        set_fixed_size(Size(width, new_height));

      set_drag_handle_constrainer(boost::bind(constrain_aspect_ratio, _1, _2, aspect));
    }
  }
  else
    set_drag_handle_constrainer(boost::function<void (mdc::ItemHandle*,Size&) >());
}


double Image::get_aspect_ratio()
{
  Size size= _image.get_image_size();

  return size.width / size.height;
}


void Image::set_image(cairo_surface_t *image)
{
  _image.set_image(image);
}


bool Image::set_image(const std::string &filename)
{
  return _image.set_image(filename);
}


void Image::set_allow_manual_resizing(bool flag)
{
  if (!flag && _image.auto_sizing())
  {
    // if auto-sizing is being disabled, set the fixed size to the current size, so that it 
    // has an initial size
    _image.set_fixed_size(_image.get_size());
  }
  _image.set_auto_sizing(!flag);

  if (!flag)
    relayout();
}

//--------------------------------------------------------------------------------------------------

bool Image::on_click(mdc::CanvasItem *target, const Point &point, mdc::MouseButton button, mdc::EventState state)
{
  if (!_hub->figure_click(represented_object(), target, point, button, state))
    return super::on_click(target, point, button, state);
  return false;
}

//--------------------------------------------------------------------------------------------------

bool Image::on_double_click(mdc::CanvasItem *target, const Point &point, mdc::MouseButton button, mdc::EventState state)
{
  if (!_hub->figure_double_click(represented_object(), target, point, button, state))
    return super::on_double_click(target, point, button, state);
  return false;
}

//--------------------------------------------------------------------------------------------------