Click here to Skip to main content
15,883,896 members
Articles / Desktop Programming / WPF

C.B.R.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (52 votes)
22 Oct 2012GPL329 min read 124.1K   1.8K   132  
Comic and electronic publication reader with library management, extended file conversion, and devices support.
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;

namespace CBR.ViewModels
{
	public class ComicViewModel : BookViewModelBase
	{
		#region ----------------CONSTRUCTOR----------------

		public ComicViewModel(Book bk)
			: base(bk)
		{
            if (Data != null)
                CurrentPage = Service.LoadBook(Data) as Page;

            Mediator.Instance.Register(
                (Object o) =>
                {
                    FitMode = (PageControlFitMode)o;
                },
                ViewModelMessages.FitModeChanged);
		}

		#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;
			}
		}

        private PageControlFitMode _FitMode = (PageControlFitMode)WorkspaceService.Instance.Settings.AutoFitMode;
        public PageControlFitMode FitMode
		{
			get { return _FitMode; }
			set
			{
				if (_FitMode != value)
				{
					_FitMode = value;
                    RaisePropertyChanged("FitMode");
				}
			}
		}

		#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;
        }

        override public void FitModeCommandExecute(string param)
        {
            if (param == "Width")
                FitMode = PageControlFitMode.Width;
            else
                if (param == "Height")
                    FitMode = PageControlFitMode.Height;
                else
                    FitMode = PageControlFitMode.None;
        }
        #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
        
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions