[Gtk-sharp-list] progressbar on a treeview

nast90210 muxaul88 at gmail.com
Fri Dec 18 07:32:48 UTC 2015


Андрей, спасибо за ответ. Вот сам код:

using System;
using Gtk;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Мастер;

public partial class MainWindow: Gtk.Window
{
	List<string> fileNames;
	int counter = 1;
    Gtk.ListStore detalListStore;
    bool isParsing = false;

	public MainWindow () : base (Gtk.WindowType.Toplevel)
	{
		Build ();
		fileNames = new List<string>();
        treeview1.EnableGridLines = TreeViewGridLines.Both;
        treeview1.RulesHint = true;
        treeview1.Selection.Mode = SelectionMode.Multiple;
		
        //Свойство Sensitive делает кнопки активными или неактивными
		parsebutton.Sensitive = false;
		deletebutton.Sensitive = false;
        treeview1.Sensitive = false;
        MakeTreeView();
	}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}

    protected void OnOpenbuttonClicked (object sender, EventArgs e)
	{
		FileChooserDialog ofd = new FileChooserDialog ("Выберите файл", this,
FileChooserAction.Open, "Отмена",ResponseType.Cancel,
			"Открыть",ResponseType.Accept);

		//фильтр какие файлы открывать
		FileFilter filter = new FileFilter(); 
		filter.Name = "Файлы";
		filter.AddMimeType ("Склад/pdf");
		filter.AddPattern("*.pdf");
		filter.AddMimeType ("Склад/html");
		filter.AddPattern("*.html");

		ofd.AddFilter (filter); 

		ofd.SelectMultiple = true;

		if (ofd.Run () == (int)ResponseType.Accept) 
		{
			foreach (string f in ofd.Filenames) 
			{
				if (!fileNames.Contains(f))
				{
					fileNames.Add(f);
                    detalListStore.AppendValues(counter,
System.IO.Path.GetFileNameWithoutExtension(f)); 
                    counter++;
					parsebutton.Sensitive = true;
                    treeview1.Sensitive = true;
				}
				else
				{
					MessageDialog md = new MessageDialog(null,
DialogFlags.DestroyWithParent, MessageType.Warning, ButtonsType.Ok,
						"Файл " + System.IO.Path.GetFileNameWithoutExtension(f) + " уже есть в
списке");
					md.Run ();
					md.Destroy(); 
				}
			}
		}
		ofd.Destroy();
    }
        
    protected void MakeTreeView ()
    {
        Gtk.TreeViewColumn numberColumn = new Gtk.TreeViewColumn ();
        numberColumn.Title = "#";
        Gtk.CellRendererText numberCell = new Gtk.CellRendererText ();
        numberColumn.PackStart(numberCell, true);
        numberColumn.AddAttribute(numberCell, "text", 0);
        numberColumn.Clickable = true;
        numberColumn.Resizable = true;

        Gtk.TreeViewColumn nameColumn = new Gtk.TreeViewColumn ();
        nameColumn.Title = "Название";
        Gtk.CellRendererText nameCell = new Gtk.CellRendererText ();
        nameColumn.PackStart(nameCell, true);
        nameColumn.AddAttribute(nameCell, "text", 1);
        nameColumn.Clickable = true;
        nameColumn.Resizable = true;

        Gtk.TreeViewColumn statusColumn = new Gtk.TreeViewColumn ();
        statusColumn.Title = "Статус";
        Gtk.CellRendererProgress statusCell = new Gtk.CellRendererProgress
();
        statusCell.Visible = false;
        statusColumn.PackStart(statusCell, true);
        statusColumn.AddAttribute(statusCell, "text", 2);
        statusColumn.Resizable = true;


        treeview1.AppendColumn(numberColumn);
        treeview1.AppendColumn(nameColumn);
        treeview1.AppendColumn(statusColumn);

        detalListStore = new Gtk.ListStore (typeof (int), typeof (string),
typeof(object));
        treeview1.Model = detalListStore;
        treeview1.ButtonReleaseEvent+= tree_ButtonReleaseEvent;
    }

    protected void tree_ButtonReleaseEvent (object o, ButtonReleaseEventArgs
args)
    {
        if (isParsing!=true) deletebutton.Sensitive = true;
    }

    protected void OnDeletebuttonClicked(object sender, EventArgs e)
    {
        TreeIter iter;
        TreePath[] treePath = treeview1.Selection.GetSelectedRows();
        for (int i  = treePath.Length; i > 0; i--)
        {
            detalListStore.GetIter(out iter, treePath[(i - 1)]);
            fileNames.RemoveAt(i-1);
            detalListStore.Remove(ref iter);
            NomerRefresh();
        }
        deletebutton.Sensitive = false;
        counter = detalListStore.IterNChildren() + 1;
        if (counter == 1)
        {
            treeview1.Sensitive = false;
            parsebutton.Sensitive = false;
        }
     }
        
    protected void NomerRefresh()
    {
        TreeIter iter;
        TreePath path;

        for (int index = 0; index < detalListStore.IterNChildren(); index++)
        {
            path = new TreePath(index.ToString());
            detalListStore.GetIter(out iter, path);
            detalListStore.SetValue(iter, 0, index + 1);
        }
    }

    protected void OnParsebuttonClicked(object sender, EventArgs e)
    {
        openbutton.Sensitive = false;
        parsebutton.Sensitive = false;
        deletebutton.Sensitive = false;
        initWork();
        isParsing = true;
    }

    protected int currentFileIndex;

    protected void initWork()
    {
        currentFileIndex = 0;
        processCurrentFile();
    }

    protected void processCurrentFile()
    {
        DetalWorker dw = new DetalWorker(fileNames[currentFileIndex]);
        Task dwtask = new Task(new System.Action(dw.DoSomeWork));
        dw.StartLoadDP += dw_StartLoadDP;
        dw.StartDW += dw_StartDW;
        dw.StepDW += dw_StepDW;
        dw.FinishDW += dw_FinishDW;
        dwtask.Start();
    }

    protected void dw_StartLoadDP(object sender, EventArgs e)
    {
        Application.Invoke( delegate 
            {
                statusbar1.Push(1,"Загружается файл...");
            });
    }

    protected void dw_StartDW(object sender, DetalWorker.StarDWEventArg e)
    {
        Application.Invoke(delegate
            {
                if (e.name == "dp") statusbar1.Push(1, "Чистка...");
                else if (e.name == "dm") statusbar1.Push(1, "Парсинг...");
                CellRendererText cellX =
(CellRendererText)treeview1.Columns[0].Cells[currentFileIndex];
                CellRendererProgress cell =
(CellRendererProgress)treeview1.Columns[2].Cells[currentFileIndex];
                if (Convert.ToInt32(cellX.Text) == currentFileIndex + 1)
                {
                    cell.Visible = true;
                    cell.Text = "asfas";
                    cell.Value = 0;
                    treeview1.ShowAll();
                }
            });
    }

    protected void dw_StepDW(object sender, EventArgs e)
    {
//        Application.Invoke( delegate 
//            {
//                statusbar1.Push(1,"Step by step...");
////                CellRendererProgress cell =
(CellRendererProgress)treeview1.Columns[2].Cells[currentFileIndex];
////                cell.Pulse = 1;
//            });
    }

    protected void dw_FinishDW(object sender, DetalWorker.EndOfDWEventArg e)
    {

        Application.Invoke( delegate 
            {
               

//                statusbar1.Push(1,"готова");
//                CellRendererProgress cell =
(CellRendererProgress)treeview1.Columns[2].Cells[currentFileIndex];
                if (e.name == "dm")
                {
                    MessageDialog mdw = new MessageDialog(null,
DialogFlags.DestroyWithParent, MessageType.Warning, ButtonsType.Ok,
                        currentFileIndex.ToString());
                    mdw.Run ();
                    mdw.Destroy(); 

                    MessageDialog mds = new MessageDialog(null,
DialogFlags.DestroyWithParent, MessageType.Warning, ButtonsType.Ok,
                        fileNames[currentFileIndex].ToString());
                    mds.Run ();
                    mds.Destroy(); 

//                    cell.Text = "готова";
                    currentFileIndex += 1;
                    if (currentFileIndex < fileNames.Count)
                    {
                        processCurrentFile();
                    }
                    else
                    {       
                        isParsing = false;
                        openbutton.Sensitive = true;
                        fileNames.Clear();
                        MessageDialog mdf = new MessageDialog(null,
DialogFlags.DestroyWithParent, MessageType.Warning, ButtonsType.Ok,
                            "Обработка списка завершина");
                        mdf.Run ();
                        mdf.Destroy(); 
                    }
                }
            });
    }

}



--
View this message in context: http://mono.1490590.n4.nabble.com/progressbar-on-a-treeview-tp4667178p4667183.html
Sent from the Mono - Gtk# mailing list archive at Nabble.com.


More information about the Gtk-sharp-list mailing list