/CBR/CBR/ViewModels/Books/ComicViewModel.cs
C# | 276 lines | 229 code | 47 blank | 0 comment | 47 complexity | fa8b313697ad04037db2115f38000398 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using CBR.Core.Helpers;
- using CBR.Core.Models;
- using CBR.Core.Services;
- using System.Windows;
- using CBR.Views;
- using CBR.Components;
- using CBR.Components.Controls;
- using System.Collections.ObjectModel;
-
- namespace CBR.ViewModels
- {
- public class ComicViewModel : BookViewModelBase
- {
- #region ----------------CONSTRUCTOR----------------
-
- public ComicViewModel(Book bk)
- : base(bk)
- {
- if (Data != null)
- CurrentPage = Service.LoadBook(Data) as Page;
- }
-
- public ComicViewModel(Book bk, int currentPageIndex, DisplayFitMode mode, double previous)
- : base(bk)
- {
- if (Data != null)
- CurrentPage = Data.Pages[currentPageIndex];
-
- PreviousScale = previous;
- FitMode = mode;
- }
-
- #endregion
-
- #region -----------------PROPERTIES-----------------
-
- private Page _currentPage = null;
- public Page CurrentPage
- {
- get { return _currentPage; }
- set
- {
- if (_currentPage != value)
- {
- _currentPage = value;
- RaisePropertyChanged("CurrentPage");
- RaisePropertyChanged("ImgSource");
- RaisePropertyChanged("PageInfo");
- RaisePropertyChanged("CacheInfo");
- RaisePropertyChanged("FrameList");
- }
- }
- }
-
- public BitmapImage ImgSource
- {
- get { if (_currentPage != null) return _currentPage.Image; else return null; }
- set
- {
- if (_currentPage.Image != value)
- {
- _currentPage.Image = value;
- RaisePropertyChanged("ImgSource");
- RaisePropertyChanged("FrameList");
- }
- }
- }
-
- public List<Zone> FrameList
- {
- get { if (_currentPage != null) return _currentPage.Frames; else return null; }
- set
- {
- if (_currentPage.Frames != value)
- {
- _currentPage.Frames = value;
- RaisePropertyChanged("FrameList");
- }
- }
- }
-
- new public string PageInfo
- {
- get
- {
- if (Data != null && _currentPage != null)
- return string.Format("Page : {0} ({1}/{2})", CurrentPage.FilePath, CurrentPage.Index, Data.PageCount);
- else
- return string.Empty;
- }
- }
-
- new public string CacheInfo
- {
- get
- {
- if (Data != null)
- return string.Format("Image in cache: {0}/{1}; Size: {2} Mo",
- Data.Pages.Where(p => p.ImageExist == true).Count(),
- WorkspaceService.Instance.Settings.ImageCacheCount, CacheSize
- );
- else
- return string.Empty;
- }
- }
-
- #endregion
-
- #region -----------------COMMANDS-----------------
-
- #region simulate command
- private ICommand simulateCommand;
- public ICommand SimulateCommand
- {
- get
- {
- if (simulateCommand == null)
- simulateCommand = new DelegateCommand(Simulate, delegate() { return Data != null && Service.IsDynamic(Data); });
- return simulateCommand;
- }
- }
-
- public void Simulate()
- {
- SimulateView Dlg = new SimulateView();
- Dlg.Owner = Application.Current.MainWindow;
- Dlg.BookData = Data;
- Dlg.ShowDialog();
- }
- #endregion
-
- #region debug page command
- private ICommand debugPageCommand;
- public ICommand DebugPageCommand
- {
- get
- {
- if (debugPageCommand == null)
- debugPageCommand = new DelegateCommand(SaveCommandExecute, delegate() { return Data != null && Service.IsDynamic(Data); });
- return debugPageCommand;
- }
- }
-
- public void DebugPage()
- {
- if (Data != null)
- {
- }
- }
- #endregion
-
- #region save command
-
- override public bool SaveCommandCanExecute()
- {
- return Data != null && Service.IsDynamic(Data);
- }
-
- override public void SaveCommandExecute()
- {
- if (Data != null)
- {
- Data = Service.SaveBook(Data);
- CurrentPage = (Page)Service.LoadBook(Data);
- }
- }
-
- #endregion
-
- #region close command
-
- override public void CloseCommandExecute()
- {
- base.CloseCommandExecute();
- CurrentPage = null;
- }
-
- #endregion
-
- #region bookmark commands
-
- override public bool BookmarkCommandCanExecute()
- {
- return CurrentPage != null;
- }
-
- override public void BookmarkCommandExecute()
- {
- Service.SetMark(Data, _currentPage);
- }
-
- override public bool GotoBookmarkCommandCanExecute()
- {
- return Service.HasMark(Data);
- }
-
- override public void GotoBookmarkCommandExecute()
- {
- CurrentPage = Service.GotoMark(Data);
- }
-
- #endregion
-
- #region fit mode command
-
- override public bool FitModeCommandCanExecute(string param)
- {
- return CurrentPage != null;
- }
-
- #endregion
-
- #region change page command
-
- override public bool CanGotoPage(string step)
- {
- return Service.CheckPageRange(Data, _currentPage, Convert.ToInt32(step));
- }
-
- override public void GotoPage(string step)
- {
- CurrentPage = Service.GotoPage(Data, _currentPage, Convert.ToInt32(step));
-
- }
- #endregion
-
- #region goto page command
-
- override public bool GotoPageCommandCanExecute(string param)
- {
- int pageNumber = 0;
- if (Data != null && Int32.TryParse(param, out pageNumber))
- return Service.CanGotoPage(Data, pageNumber);
- else
- return true;
- }
-
- override public void GotoPageCommandExecute(string param)
- {
- int pageNumber = 0;
- if (Data != null && Int32.TryParse(param, out pageNumber))
- CurrentPage = Service.GotoPage(Data, pageNumber);
- }
-
- override public void GotoLastPageCommandExecute(string param)
- {
- if (Data != null)
- CurrentPage = Service.GotoPage(Data, Data.PageCount);
- }
-
- #endregion
-
- #region two page view command
-
- override public bool TwoPageCommandCanExecute()
- {
- return true;
- }
-
- override public void TwoPageCommandExecute()
- {
- Mediator.Instance.NotifyColleagues<BookViewModelBase>(ViewModelMessages.SwapTwoPageView, this);
- }
-
- #endregion
-
- #endregion
- }
- }