Esempio n. 1
0
        public InfoOwnerData(InfoOwner item)
        {
            var sb = new StringBuilder();

            using (var writer = XmlWriter.Create(sb, new XmlWriterSettings {
                OmitXmlDeclaration = true
            }))
            {
                item.WriteXml(writer);
            }

            ItemData  = sb.ToString();
            ItemLevel = item is Package ? Level.Package :
                        item is Round ? Level.Round :
                        item is Theme ? Level.Theme : Level.Question;
        }
Esempio n. 2
0
        public InfoOwner GetItem()
        {
            InfoOwner item = ItemLevel switch
            {
                Level.Package => new Package(),
                Level.Round => new Round(),
                Level.Theme => new Theme(),
                _ => new Question(),
            };

            using (var sr = new StringReader(ItemData))
            {
                using var reader = XmlReader.Create(sr);
                reader.Read();
                item.ReadXml(reader);
            }

            return(item);
        }
Esempio n. 3
0
        /// <summary>
        /// Получить полные данные для объекта, включая присоединённые элементы коллекций
        /// </summary>
        /// <returns></returns>
        public void GetFullData(SIDocument document, InfoOwner owner)
        {
            var length = owner.Info.Authors.Count;

            for (int i = 0; i < length; i++)
            {
                var docAuthor = document.GetLink(owner.Info.Authors, i);
                if (docAuthor != null)
                {
                    if (Authors == null)
                    {
                        Authors = new List <AuthorInfo>();
                    }

                    if (!Authors.Contains(docAuthor))
                    {
                        Authors.Add(docAuthor);
                    }
                }
            }

            length = owner.Info.Sources.Count;
            for (int i = 0; i < length; i++)
            {
                var docSource = document.GetLink(owner.Info.Sources, i);
                if (docSource != null)
                {
                    if (Sources == null)
                    {
                        Sources = new List <SourceInfo>();
                    }

                    if (!Sources.Contains(docSource))
                    {
                        Sources.Add(docSource);
                    }
                }
            }
        }
        private void AppendInfo(SIDocument doc, Paragraph paragraph, InfoOwner owner)
        {
            var count = owner.Info.Authors.Count;

            if (count > 0)
            {
                paragraph.AppendLine().Append(string.Format("{0}{1}: ", Resources.BaseAuthors, count > 1 ? "ы" : ""));
                paragraph.Append(string.Join(", ", doc.GetRealAuthors(owner.Info.Authors)).EndWithPoint());
            }

            count = owner.Info.Sources.Count;
            if (count > 0)
            {
                paragraph.AppendLine().Append(string.Format("{0}{1}: ", Resources.BaseSources, count > 1 ? "и" : ""));
                paragraph.Append(string.Join(", ", doc.GetRealSources(owner.Info.Sources)).EndWithPoint());
            }

            if (owner.Info.Comments.Text.Length > 0)
            {
                paragraph.AppendLine().Append(string.Format("{0}: ", Resources.Comments));
                paragraph.Append(owner.Info.Comments.Text);
            }
        }
Esempio n. 5
0
        internal static void DoDrag(FrameworkElement host, QDocument active, InfoOwner item, InfoOwnerData itemData, Action beforeDrag = null, Action afterDrag = null)
        {
            if (host == null)
            {
                throw new ArgumentNullException(nameof(host));
            }

            if (active == null)
            {
                throw new ArgumentNullException(nameof(active));
            }

            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (itemData == null)
            {
                throw new ArgumentNullException(nameof(itemData));
            }

            itemData.GetFullData(active.Document, item);
            var dataObject = new DataObject(itemData);

            if (host.DataContext is RoundViewModel roundViewModel)
            {
                var packageViewModel = roundViewModel.OwnerPackage;
                if (packageViewModel == null)
                {
                    throw new ArgumentException(nameof(packageViewModel));
                }

                int index = packageViewModel.Rounds.IndexOf(roundViewModel);

                active.BeginChange();

                try
                {
                    var sb = new StringBuilder();
                    using (var writer = XmlWriter.Create(sb))
                    {
                        roundViewModel.Model.WriteXml(writer);
                    }

                    dataObject.SetData("siqround", "1");
                    dataObject.SetData(DataFormats.Serializable, sb);

                    var result = DragDrop.DoDragDrop(host, dataObject, DragDropEffects.Move);
                    if (result == DragDropEffects.Move)
                    {
                        if (packageViewModel.Rounds[index] != roundViewModel)
                        {
                            index++;
                        }

                        packageViewModel.Rounds.RemoveAt(index);
                        active.CommitChange();
                    }
                    else
                    {
                        active.RollbackChange();
                    }
                }
                catch (Exception exc)
                {
                    active.RollbackChange();
                    throw exc;
                }
            }
            else
            {
                if (host.DataContext is ThemeViewModel themeViewModel)
                {
                    roundViewModel = themeViewModel.OwnerRound;
                    if (roundViewModel == null)
                    {
                        throw new ArgumentException(nameof(roundViewModel));
                    }

                    int index = roundViewModel.Themes.IndexOf(themeViewModel);

                    active.BeginChange();

                    try
                    {
                        var sb = new StringBuilder();
                        using (var writer = XmlWriter.Create(sb))
                        {
                            themeViewModel.Model.WriteXml(writer);
                        }

                        dataObject.SetData("siqtheme", "1");
                        dataObject.SetData(DataFormats.Serializable, sb);

                        var result = DragDrop.DoDragDrop(host, dataObject, DragDropEffects.Move);
                        if (result == DragDropEffects.Move)
                        {
                            if (roundViewModel.Themes[index] != themeViewModel)
                            {
                                index++;
                            }

                            roundViewModel.Themes.RemoveAt(index);
                        }

                        active.CommitChange();
                    }
                    catch (Exception exc)
                    {
                        active.RollbackChange();
                        throw exc;
                    }
                }
                else
                {
                    var questionViewModel = host.DataContext as QuestionViewModel;
                    themeViewModel = questionViewModel.OwnerTheme;
                    if (themeViewModel == null)
                    {
                        throw new ArgumentException(nameof(themeViewModel));
                    }

                    var index = themeViewModel.Questions.IndexOf(questionViewModel);
                    active.BeginChange();

                    try
                    {
                        var sb = new StringBuilder();
                        using (var writer = XmlWriter.Create(sb))
                        {
                            questionViewModel.Model.WriteXml(writer);
                        }

                        dataObject.SetData("siqquestion", "1");
                        dataObject.SetData(DataFormats.Serializable, sb);

                        beforeDrag?.Invoke();

                        DragDropEffects result;
                        try
                        {
                            result = DragDrop.DoDragDrop(host, dataObject, DragDropEffects.Move);
                        }
                        catch (InvalidOperationException)
                        {
                            result = DragDropEffects.None;
                        }
                        finally
                        {
                            host.Opacity = 1.0;

                            afterDrag?.Invoke();
                        }

                        if (result == DragDropEffects.Move)
                        {
                            if (themeViewModel.Questions[index] != questionViewModel)
                            {
                                index++;
                            }

                            themeViewModel.Questions.RemoveAt(index);

                            if (AppSettings.Default.ChangePriceOnMove)
                            {
                                RecountPrices(themeViewModel);
                            }
                        }

                        active.CommitChange();
                    }
                    catch (Exception exc)
                    {
                        active.RollbackChange();
                        throw exc;
                    }
                }
            }
        }