Click here to Skip to main content
15,885,366 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.2K   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;

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

		public BookViewModel(Book bk)
			: base(bk)
		{
            //be sure 7zip is initialized
            CatalogService service = CatalogService.Instance;

            if (BookData != null)
                CurrentPage = Service.LoadBook(BookData) as Page;
		}

		#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 (BookData != null && _currentPage != null)
					return string.Format("Page : {0} ({1}/{2})", CurrentPage.FilePath, CurrentPage.Index, BookData.PageCount);
				else
					return string.Empty;
			}
		}

		new public string CacheInfo
		{
			get
			{
				if (BookData != null)
					return string.Format("Image in cache: {0}/{1}; Size: {2} Mo",
						BookData.Pages.Where(p => p.ImageExist == true).Count(),
                        WorkspaceService.Instance.Settings.ImageCacheCount, CacheSize
				);
				else
					return string.Empty;
			}
		}

        private CBR.Components.Controls.PageControl.PageFitMode _FitMode = (CBR.Components.Controls.PageControl.PageFitMode)WorkspaceService.Instance.Settings.AutoFitMode;
		public CBR.Components.Controls.PageControl.PageFitMode 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 BookData != null && Service.IsDynamic(BookData); });
                return simulateCommand;
            }
        }

        public void Simulate()
        {
            SimulateView Dlg = new SimulateView();
            Dlg.Owner = Application.Current.MainWindow;
            Dlg.BookData = BookData;
            Dlg.ShowDialog();
        }
        #endregion

        #region debug page command
        private ICommand debugPageCommand;
        public ICommand DebugPageCommand
        {
            get
            {
                if (debugPageCommand == null)
                    debugPageCommand = new DelegateCommand(Save, delegate() { return BookData != null && Service.IsDynamic(BookData); });
                return debugPageCommand;
            }
        }

        public void DebugPage()
        {
            if (BookData != null)
            {
            }
        }
        #endregion

        #region save command
        private ICommand saveBookCommand;
        public ICommand SaveBookCommand
        {
            get
            {
                if (saveBookCommand == null)
                    saveBookCommand = new DelegateCommand(Save, delegate() { return BookData != null && Service.IsDynamic(BookData); });
                return saveBookCommand;
            }
        }

        public void Save()
        {
            if (BookData != null)
            {
                BookData = Service.SaveBook(BookData);
                CurrentPage = (Page)Service.LoadBook(BookData);
            }
        }
        #endregion

        #region close 

        override public void Close()
        {
            base.Close();
            CurrentPage = null;
        }

        #endregion

        #region page command
        private ICommand changePageCommand;
		public ICommand ChangePageCommand
		{
			get
			{
				if (changePageCommand == null)
					changePageCommand = new DelegateCommand<int>(GotoPage, CanGotoPage);
				return changePageCommand;
			}
		}

		bool CanGotoPage(int step)
		{
            return Service.CheckPageRange(BookData, _currentPage, step);
		}

		void GotoPage(int step)
		{
            CurrentPage = Service.GotoPage(BookData, _currentPage, step);
			
		}
        #endregion

        #region bookmark command
        private ICommand bookmarkCommand;
		public ICommand BookmarkCommand
		{
			get
			{
				if (bookmarkCommand == null)
					bookmarkCommand = new DelegateCommand(BookmarkPage, delegate() { return _currentPage != null; });
				return bookmarkCommand;
			}
		}

		void BookmarkPage()
		{
            Service.SetMark(BookData, _currentPage);
		}

		private ICommand gotoBookmarkCommand;
		public ICommand GotoBookmarkCommand
		{
			get
			{
				if (gotoBookmarkCommand == null)
                    gotoBookmarkCommand = new DelegateCommand(GotoBookmark, delegate() { return Service.HasMark(BookData); });
				return gotoBookmarkCommand;
			}
		}

        void GotoBookmark()
        {
            CurrentPage = Service.GotoMark(BookData);
        }

        private ICommand clearBookmarkCommand;
        public ICommand ClearBookmarkCommand
        {
            get
            {
                if (clearBookmarkCommand == null)
                    clearBookmarkCommand = new DelegateCommand(ClearBookmark, delegate() { return Service.HasMark(BookData); });
                return clearBookmarkCommand;
            }
        }

        void ClearBookmark()
        {
            Service.ClearMark(BookData);
        }
        #endregion

        #region fit mode command
        private ICommand fitModeCommand;
		public ICommand FitModeCommand
		{
			get
			{
				if (fitModeCommand == null)
					fitModeCommand = new DelegateCommand<string>(ChangeFitMode, delegate(string param) { return _currentPage != null; });
				return fitModeCommand;
			}
		}

		void ChangeFitMode(string param)
		{
			if (param == "Width")
				FitMode = Components.Controls.PageControl.PageFitMode.Width;
			else
				if (param == "Height")
					FitMode = Components.Controls.PageControl.PageFitMode.Height;
			else
				FitMode = Components.Controls.PageControl.PageFitMode.None;

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