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