PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/CBR/CBR/ViewModels/Books/ComicViewModel.cs

#
C# | 276 lines | 229 code | 47 blank | 0 comment | 47 complexity | fa8b313697ad04037db2115f38000398 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Windows.Input;
  6using System.Windows.Media;
  7using System.Windows.Media.Imaging;
  8using CBR.Core.Helpers;
  9using CBR.Core.Models;
 10using CBR.Core.Services;
 11using System.Windows;
 12using CBR.Views;
 13using CBR.Components;
 14using CBR.Components.Controls;
 15using System.Collections.ObjectModel;
 16
 17namespace CBR.ViewModels
 18{
 19	public class ComicViewModel : BookViewModelBase
 20	{
 21		#region ----------------CONSTRUCTOR----------------
 22
 23		public ComicViewModel(Book bk)
 24			: base(bk)
 25		{
 26			if (Data != null)
 27                CurrentPage = Service.LoadBook(Data) as Page;
 28		}
 29
 30		public ComicViewModel(Book bk, int currentPageIndex, DisplayFitMode mode, double previous)
 31			: base(bk)
 32		{
 33			if (Data != null)
 34				CurrentPage = Data.Pages[currentPageIndex];
 35
 36			PreviousScale = previous;
 37			FitMode = mode;
 38		}
 39
 40		#endregion
 41
 42		#region -----------------PROPERTIES-----------------
 43
 44		private Page _currentPage = null;
 45		public Page CurrentPage
 46		{
 47			get { return _currentPage; }
 48			set
 49			{
 50				if (_currentPage != value)
 51				{
 52					_currentPage = value;
 53					RaisePropertyChanged("CurrentPage");
 54					RaisePropertyChanged("ImgSource");
 55					RaisePropertyChanged("PageInfo");
 56					RaisePropertyChanged("CacheInfo");
 57                    RaisePropertyChanged("FrameList");
 58				}
 59			}
 60		}
 61
 62		public BitmapImage ImgSource
 63		{
 64			get { if (_currentPage != null) return _currentPage.Image; else return null; }
 65			set
 66			{
 67				if (_currentPage.Image != value)
 68				{
 69					_currentPage.Image = value;
 70                    RaisePropertyChanged("ImgSource");
 71                    RaisePropertyChanged("FrameList");
 72				}
 73			}
 74		}
 75
 76        public List<Zone> FrameList
 77        {
 78            get { if (_currentPage != null) return _currentPage.Frames; else return null; }
 79            set
 80            {
 81                if (_currentPage.Frames != value)
 82                {
 83                    _currentPage.Frames = value;
 84                    RaisePropertyChanged("FrameList");
 85                }
 86            }
 87        }
 88
 89		new public string PageInfo
 90		{
 91			get
 92			{
 93				if (Data != null && _currentPage != null)
 94					return string.Format("Page : {0} ({1}/{2})", CurrentPage.FilePath, CurrentPage.Index, Data.PageCount);
 95				else
 96					return string.Empty;
 97			}
 98		}
 99
100		new public string CacheInfo
101		{
102			get
103			{
104				if (Data != null)
105					return string.Format("Image in cache: {0}/{1}; Size: {2} Mo",
106						Data.Pages.Where(p => p.ImageExist == true).Count(),
107                        WorkspaceService.Instance.Settings.ImageCacheCount, CacheSize
108				);
109				else
110					return string.Empty;
111			}
112		}
113
114		#endregion
115
116        #region -----------------COMMANDS-----------------
117
118        #region simulate command
119        private ICommand simulateCommand;
120        public ICommand SimulateCommand
121        {
122            get
123            {
124                if (simulateCommand == null)
125                    simulateCommand = new DelegateCommand(Simulate, delegate() { return Data != null && Service.IsDynamic(Data); });
126                return simulateCommand;
127            }
128        }
129
130        public void Simulate()
131        {
132            SimulateView Dlg = new SimulateView();
133            Dlg.Owner = Application.Current.MainWindow;
134            Dlg.BookData = Data;
135            Dlg.ShowDialog();
136        }
137        #endregion
138
139        #region debug page command
140        private ICommand debugPageCommand;
141        public ICommand DebugPageCommand
142        {
143            get
144            {
145                if (debugPageCommand == null)
146                    debugPageCommand = new DelegateCommand(SaveCommandExecute, delegate() { return Data != null && Service.IsDynamic(Data); });
147                return debugPageCommand;
148            }
149        }
150
151        public void DebugPage()
152        {
153            if (Data != null)
154            {
155            }
156        }
157        #endregion
158
159        #region save command
160
161        override public bool SaveCommandCanExecute()
162        {
163            return Data != null && Service.IsDynamic(Data);
164        }
165
166        override public void SaveCommandExecute()
167        {
168            if (Data != null)
169            {
170                Data = Service.SaveBook(Data);
171                CurrentPage = (Page)Service.LoadBook(Data);
172            }
173        }
174
175        #endregion
176
177        #region close command
178
179        override public void CloseCommandExecute()
180        {
181            base.CloseCommandExecute();
182            CurrentPage = null;
183        }
184
185        #endregion
186
187        #region bookmark commands
188
189        override public bool BookmarkCommandCanExecute()
190        {
191            return CurrentPage != null;
192        }
193
194        override public void BookmarkCommandExecute()
195        {
196            Service.SetMark(Data, _currentPage);
197        }
198
199        override public bool GotoBookmarkCommandCanExecute()
200        {
201            return Service.HasMark(Data);
202        }
203
204        override public void GotoBookmarkCommandExecute()
205        {
206            CurrentPage = Service.GotoMark(Data);
207        }
208
209        #endregion
210
211        #region fit mode command
212
213        override public bool FitModeCommandCanExecute(string param)
214        {
215            return CurrentPage != null;
216        }
217
218        #endregion
219
220        #region change page command
221
222        override public bool CanGotoPage(string step)
223        {
224            return Service.CheckPageRange(Data, _currentPage, Convert.ToInt32(step));
225        }
226
227        override public void GotoPage(string step)
228        {
229            CurrentPage = Service.GotoPage(Data, _currentPage, Convert.ToInt32(step));
230
231        }
232        #endregion
233
234        #region goto page command
235
236        override public bool GotoPageCommandCanExecute(string param)
237        {
238            int pageNumber = 0;
239            if (Data != null && Int32.TryParse(param, out pageNumber))
240                return Service.CanGotoPage(Data, pageNumber);
241            else
242                return true;
243        }
244
245        override public void GotoPageCommandExecute(string param)
246        {
247            int pageNumber = 0;
248            if (Data != null && Int32.TryParse(param, out pageNumber))
249                CurrentPage = Service.GotoPage(Data, pageNumber);
250        }
251
252        override public void GotoLastPageCommandExecute(string param)
253        {
254            if (Data != null)
255                CurrentPage = Service.GotoPage(Data, Data.PageCount);
256        }
257
258        #endregion
259
260		#region two page view command
261
262		override public bool TwoPageCommandCanExecute()
263		{
264			return true;
265		}
266
267		override public void TwoPageCommandExecute()
268		{
269			Mediator.Instance.NotifyColleagues<BookViewModelBase>(ViewModelMessages.SwapTwoPageView, this); 
270		}
271
272		#endregion
273
274        #endregion
275    }
276}