~ubuntu-branches/ubuntu/utopic/sweethome3d/utopic

« back to all changes in this revision

Viewing changes to src/com/eteks/sweethome3d/viewcontroller/PhotoController.java

  • Committer: Package Import Robot
  • Author(s): Gabriele Giacone
  • Date: 2013-03-22 23:35:34 UTC
  • mfrom: (1.1.12)
  • Revision ID: package-import@ubuntu.com-20130322233534-k9g9mz1vch0dfy4y
Tags: 4.0+dfsg-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import com.eteks.sweethome3d.model.AspectRatio;
28
28
import com.eteks.sweethome3d.model.Camera;
29
29
import com.eteks.sweethome3d.model.Home;
30
 
import com.eteks.sweethome3d.model.HomeEnvironment;
31
30
import com.eteks.sweethome3d.model.UserPreferences;
32
31
 
33
32
/**
34
33
 * The controller of the photo creation view.
35
34
 * @author Emmanuel Puybaret
36
35
 */
37
 
public class PhotoController implements Controller {
 
36
public class PhotoController extends AbstractPhotoController {
38
37
  /**
39
38
   * The properties that may be edited by the view associated to this controller. 
40
39
   */
41
 
  public enum Property {ASPECT_RATIO, WIDTH, HEIGHT, QUALITY, VIEW_3D_ASPECT_RATIO, TIME, LENS, CEILING_LIGHT_COLOR}
 
40
  public enum Property {TIME, LENS}
42
41
  
43
42
  private final Home                  home;
44
43
  private final UserPreferences       preferences;
45
 
  private final View                  view3D;
46
44
  private final ViewFactory           viewFactory;
47
 
  private final ContentManager        contentManager;
48
45
  private final PropertyChangeSupport propertyChangeSupport;
49
46
  private final CameraChangeListener  cameraChangeListener;
50
47
  private DialogView                  photoView;
51
48
  
52
 
  private AspectRatio                 aspectRatio;
53
 
  private int                         width;
54
 
  private int                         height;
55
 
  private int                         quality;
56
 
  private float                       view3DAspectRatio;
57
49
  private long                        time;
58
50
  private Camera.Lens                 lens;  
59
 
  private int                         ceilingLightColor;
60
51
 
61
52
  public PhotoController(Home home,
62
53
                         UserPreferences preferences, 
63
54
                         View view3D, ViewFactory viewFactory,
64
55
                         ContentManager contentManager) {
 
56
    super(home, preferences, view3D, contentManager);
65
57
    this.home = home;
66
58
    this.preferences = preferences;
67
 
    this.view3D = view3D;
68
59
    this.viewFactory = viewFactory;
69
 
    this.contentManager = contentManager;
70
60
    this.propertyChangeSupport = new PropertyChangeSupport(this);
71
 
    this.view3DAspectRatio = 1;
72
61
    
73
62
    this.cameraChangeListener = new CameraChangeListener(this);
74
63
    home.getCamera().addPropertyChangeListener(this.cameraChangeListener);
75
64
    home.addPropertyChangeListener(Home.Property.CAMERA, new HomeCameraChangeListener(this));
76
 
    home.getEnvironment().addPropertyChangeListener(HomeEnvironment.Property.CEILING_LIGHT_COLOR, new HomeEnvironmentChangeListener(this));
77
65
    updateProperties();
78
66
  }
79
67
 
106
94
   * with a weak reference to avoid strong link between home and this controller.  
107
95
   */
108
96
  private static class CameraChangeListener implements PropertyChangeListener {
109
 
    private WeakReference<PhotoController> photoController;
 
97
    private WeakReference<AbstractPhotoController> photoController;
110
98
    
111
 
    public CameraChangeListener(PhotoController photoController) {
112
 
      this.photoController = new WeakReference<PhotoController>(photoController);
 
99
    public CameraChangeListener(AbstractPhotoController photoController) {
 
100
      this.photoController = new WeakReference<AbstractPhotoController>(photoController);
113
101
    }
114
102
    
115
103
    public void propertyChange(PropertyChangeEvent ev) {
116
104
      // If controller was garbage collected, remove this listener from camera
117
 
      final PhotoController controller = this.photoController.get();
 
105
      final AbstractPhotoController controller = this.photoController.get();
118
106
      if (controller == null) {
119
107
        ((Camera)ev.getSource()).removePropertyChangeListener(this);
120
108
      } else {
124
112
  }
125
113
 
126
114
  /**
127
 
   * Home environment listener that updates properties. This listener is bound to this controller 
128
 
   * with a weak reference to avoid strong link between home and this controller.  
129
 
   */
130
 
  private static class HomeEnvironmentChangeListener implements PropertyChangeListener {
131
 
    private WeakReference<PhotoController> photoController;
132
 
    
133
 
    public HomeEnvironmentChangeListener(PhotoController photoController) {
134
 
      this.photoController = new WeakReference<PhotoController>(photoController);
135
 
    }
136
 
    
137
 
    public void propertyChange(PropertyChangeEvent ev) {
138
 
      // If controller was garbage collected, remove this listener from home
139
 
      final PhotoController controller = this.photoController.get();
140
 
      if (controller == null) {
141
 
        ((HomeEnvironment)ev.getSource()).removePropertyChangeListener(HomeEnvironment.Property.CEILING_LIGHT_COLOR, this);
142
 
      } else if (ev.getPropertyName().equals(Camera.Property.LENS.name())
143
 
            || ev.getPropertyName().equals(Camera.Property.TIME.name())) {
144
 
          controller.updateProperties();
145
 
      }
146
 
    }
147
 
  }
148
 
 
149
 
  /**
150
115
   * Returns the view associated with this controller.
151
116
   */
152
117
  public DialogView getView() {
165
130
  }
166
131
 
167
132
  /**
168
 
   * Returns the content manager of this controller.
169
 
   */
170
 
  public ContentManager getContentManager() {
171
 
    return this.contentManager;
172
 
  }
173
 
 
174
 
  /**
175
133
   * Adds the property change <code>listener</code> in parameter to this controller.
176
134
   */
177
135
  public void addPropertyChangeListener(Property property, PropertyChangeListener listener) {
189
147
   * Updates edited properties from the photo creation preferences.
190
148
   */
191
149
  protected void updateProperties() {
192
 
    HomeEnvironment homeEnvironment = this.home.getEnvironment();
193
 
    setAspectRatio(homeEnvironment.getPhotoAspectRatio());
194
 
    setWidth(homeEnvironment.getPhotoWidth(), false);
195
 
    setHeight(homeEnvironment.getPhotoHeight(), false);
196
 
    setQuality(homeEnvironment.getPhotoQuality());
197
 
    setTime(this.home.getCamera().getTime());
198
 
    setLens(this.home.getCamera().getLens());
199
 
    setCeilingLightColor(homeEnvironment.getCeillingLightColor());
200
 
  }
201
 
  
202
 
  /**
203
 
   * Sets the aspect ratio of the photo.
204
 
   */
205
 
  public void setAspectRatio(AspectRatio aspectRatio) {
206
 
    if (this.aspectRatio != aspectRatio) {
207
 
      AspectRatio oldAspectRatio = this.aspectRatio;
208
 
      this.aspectRatio = aspectRatio;
209
 
      this.propertyChangeSupport.firePropertyChange(Property.ASPECT_RATIO.name(), oldAspectRatio, aspectRatio);
210
 
      this.home.getEnvironment().setPhotoAspectRatio(this.aspectRatio);
211
 
      if (this.aspectRatio == AspectRatio.VIEW_3D_RATIO) {
212
 
        if (this.view3DAspectRatio != Float.POSITIVE_INFINITY) {
213
 
          setHeight(Math.round(width / this.view3DAspectRatio), false);
214
 
        }
215
 
      } else if (this.aspectRatio.getValue() != null) {
216
 
        setHeight(Math.round(width / this.aspectRatio.getValue()), false);
217
 
      }
218
 
    }
219
 
  }
220
 
  
221
 
  /**
222
 
   * Returns the aspect ratio of the photo.
223
 
   */
224
 
  public AspectRatio getAspectRatio() {
225
 
    return this.aspectRatio;
226
 
  }
227
 
 
228
 
  /**
229
 
   * Sets the width of the photo.
230
 
   */
231
 
  public void setWidth(int width) {
232
 
    setWidth(width, true);
233
 
  }
234
 
  
235
 
  private void setWidth(int width, boolean updateHeight) {
236
 
    if (this.width != width) {
237
 
      int oldWidth = this.width;
238
 
      this.width = width;
239
 
      this.propertyChangeSupport.firePropertyChange(Property.WIDTH.name(), oldWidth, width);
240
 
      if (updateHeight) {
241
 
        if (this.aspectRatio == AspectRatio.VIEW_3D_RATIO) {
242
 
          if (this.view3DAspectRatio != Float.POSITIVE_INFINITY) {
243
 
            setHeight(Math.round(width / this.view3DAspectRatio), false);
244
 
          }
245
 
        } else if (this.aspectRatio.getValue() != null) {
246
 
          setHeight(Math.round(width / this.aspectRatio.getValue()), false);
247
 
        }
248
 
      }
249
 
      this.home.getEnvironment().setPhotoWidth(this.width);
250
 
    }
251
 
  }
252
 
  
253
 
  /**
254
 
   * Returns the width of the photo.
255
 
   */
256
 
  public int getWidth() {
257
 
    return this.width;
258
 
  }
259
 
 
260
 
  /**
261
 
   * Sets the height of the photo.
262
 
   */
263
 
  public void setHeight(int height) {
264
 
    setHeight(height, true);
265
 
  }
266
 
  
267
 
  private void setHeight(int height, boolean updateWidth) {
268
 
    if (this.height != height) {
269
 
      int oldHeight = this.height;
270
 
      this.height = height;
271
 
      this.propertyChangeSupport.firePropertyChange(Property.HEIGHT.name(), oldHeight, height);
272
 
      if (updateWidth) {
273
 
        if (this.aspectRatio == AspectRatio.VIEW_3D_RATIO) {
274
 
          if (this.view3DAspectRatio != Float.POSITIVE_INFINITY) {
275
 
            setWidth(Math.round(height * this.view3DAspectRatio), false);
276
 
          }
277
 
        } else if (this.aspectRatio.getValue() != null) {
278
 
          setWidth(Math.round(height * this.aspectRatio.getValue()), false);
279
 
        }
280
 
      }
281
 
      this.home.getEnvironment().setPhotoHeight(this.height);
282
 
    }
283
 
  }
284
 
  
285
 
  /**
286
 
   * Returns the height of the photo.
287
 
   */
288
 
  public int getHeight() {
289
 
    return this.height;
290
 
  }
291
 
 
292
 
  /**
293
 
   * Sets the rendering quality of the photo.
294
 
   */
295
 
  public void setQuality(int quality) {
296
 
    if (this.quality != quality) {
297
 
      int oldQuality = this.quality;
298
 
      this.quality = Math.min(quality, getQualityLevelCount() - 1);
299
 
      this.propertyChangeSupport.firePropertyChange(Property.QUALITY.name(), oldQuality, quality);
300
 
      this.home.getEnvironment().setPhotoQuality(this.quality);
301
 
    }
302
 
  }
303
 
  
304
 
  /**
305
 
   * Returns the rendering quality of the photo.
306
 
   */
307
 
  public int getQuality() {
308
 
    return this.quality;
309
 
  }
310
 
 
311
 
  /**
312
 
   * Returns the maximum value for quality.
313
 
   */
314
 
  public int getQualityLevelCount() {
315
 
    return 4;
316
 
  }
317
 
  
 
150
    // Update properties only once this object is initialized
 
151
    if (this.home != null) {
 
152
      super.updateProperties();
 
153
      setTime(this.home.getCamera().getTime());
 
154
      setLens(this.home.getCamera().getLens());
 
155
    }
 
156
  }
318
157
  
319
158
  /**
320
159
   * Sets the edited time in UTC time zone.
324
163
      long oldTime = this.time;
325
164
      this.time = time;
326
165
      this.propertyChangeSupport.firePropertyChange(Property.TIME.name(), oldTime, time);
327
 
      this.home.getCamera().setTime(time);
 
166
      Camera homeCamera = this.home.getCamera();
 
167
      homeCamera.removePropertyChangeListener(this.cameraChangeListener);
 
168
      homeCamera.setTime(time);
 
169
      homeCamera.addPropertyChangeListener(this.cameraChangeListener);
328
170
    }
329
171
  }
330
172
  
358
200
  public Camera.Lens getLens() {    
359
201
    return this.lens;
360
202
  }
361
 
  
362
 
  /**
363
 
   * Sets the edited ceiling light color.
364
 
   */
365
 
  public void setCeilingLightColor(int ceilingLightColor) {
366
 
    if (this.ceilingLightColor != ceilingLightColor) {
367
 
      int oldCeilingLightColor = this.ceilingLightColor;
368
 
      this.ceilingLightColor = ceilingLightColor;
369
 
      this.propertyChangeSupport.firePropertyChange(Property.CEILING_LIGHT_COLOR.name(), oldCeilingLightColor, ceilingLightColor);
370
 
      this.home.getEnvironment().setCeillingLightColor(ceilingLightColor);
371
 
    }
372
 
  }
373
 
  
374
 
  /**
375
 
   * Returns the edited ceiling light color.
376
 
   */
377
 
  public int getCeilingLightColor() {
378
 
    return this.ceilingLightColor;
379
 
  }
380
 
 
381
 
  /**
382
 
   * Sets the aspect ratio of the 3D view.
383
 
   */
384
 
  public void set3DViewAspectRatio(float view3DAspectRatio) {
385
 
    if (this.view3DAspectRatio != view3DAspectRatio) {
386
 
      float oldAspectRatio = this.view3DAspectRatio;
387
 
      this.view3DAspectRatio = view3DAspectRatio;
388
 
      this.propertyChangeSupport.firePropertyChange(Property.ASPECT_RATIO.name(), oldAspectRatio, view3DAspectRatio);
389
 
      if (this.aspectRatio == AspectRatio.VIEW_3D_RATIO
390
 
          && this.view3DAspectRatio != Float.POSITIVE_INFINITY) {
391
 
        setHeight(Math.round(this.width / this.view3DAspectRatio), false);
392
 
      }
393
 
    }
394
 
  }
395
 
  
396
 
  /**
397
 
   * Returns the aspect ratio of the 3D view.
398
 
   */
399
 
  public float get3DViewAspectRatio() {
400
 
    return this.view3DAspectRatio;
401
 
  }
402
 
 
403
 
 
404
 
  /**
405
 
   * Returns the 3D view used to compute aspect ratio bound to it.
406
 
   */
407
 
  public View get3DView() {
408
 
    return this.view3D;
409
 
  }
410
 
 
411
 
  /**
412
 
   * Controls the change of value of a visual property in home.
413
 
   */
414
 
  public void setVisualProperty(String propertyName,
415
 
                                Object propertyValue) {
416
 
    this.home.setVisualProperty(propertyName, propertyValue);
417
 
  }
418
203
}