Files
EgtCAM5/MainWindow/MainWindowViewModel.vb
T
Emmanuele Sassi bca8754b82 EgtCAM5 :
- Migliorie varie.
2016-07-09 10:44:36 +00:00

449 lines
18 KiB
VB.net

Imports System.Collections.ObjectModel
Imports System.Threading
Imports System.Math
Imports EgtUILib
Namespace EgtCAM5
Public Class MainWindowViewModel
Inherits ViewModelBase
#Region "FIELDS"
' EGALTECH ENVIRONMENT FIELDS
Private m_objMutex As New Mutex
'' '' '' '' '' ''Private m_nInstance As Integer = 0
Private m_sDataRoot As String = String.Empty
Private m_sConfigDir As String = String.Empty
Private m_sTempDir As String = String.Empty
Private m_sMachinesRoot As String = String.Empty
Private m_sIniFile As String = String.Empty
Private m_nDebug As Integer = 0
' Opzioni abilitate dalla licenza attiva associata alla chiave
Private m_nKeyOptions As UInteger
Friend Enum KEY_OPT As UInteger
BASE = 1
End Enum
' EGALTECH ENVIRONMENT FIELDS WITH PROPERTY
' GRAPHICAL FIELDS
' Event commands
' MainWindow ContentRendered Event
Private m_cmdMainWindow_ContentRendered As ICommand
' MainWindow Activated Event
Private m_cmdMainWindow_Activated As ICommand
' MainWindow Deactivated Event
Private m_cmdMainWindow_Deactivated As ICommand
' MainWindow Closing Event
Private m_cmdMainWindow_Closing As ICommand
' GRAPHICAL ELEMENTS
Private m_TopCommandBar As TopCommandBarView
Public ReadOnly Property TopCommandBar As TopCommandBarView
Get
If IsNothing(m_TopCommandBar) Then
m_TopCommandBar = New TopCommandBarView
m_TopCommandBar.DataContext = New TopCommandBarViewModel
End If
Return m_TopCommandBar
End Get
End Property
' GRAPHICAL FIELDS WITH PROPERTY
' ObservableCollection that contains all the main TabControl pages
Private m_TabList As New ObservableCollection(Of TabViewModel)
Public Property TabList As ObservableCollection(Of TabViewModel)
Get
Return m_TabList
End Get
Set(value As ObservableCollection(Of TabViewModel))
m_TabList = value
End Set
End Property
' Fields that contains current selected page in the main TabControl
Private m_SelectedTab As TabViewModel
Public Property SelectedTab As TabViewModel
Get
Return m_SelectedTab
End Get
Set(value As TabViewModel)
If value IsNot m_SelectedTab Then
m_SelectedTab = value
OnPropertyChanged("SelectedTab")
If TypeOf value Is ProjectViewModel Then
EgtSetCurrentContext(IniFile.m_ProjectSceneContext)
ProjectPageSelected = True
Else
ProjectPageSelected = False
End If
If TypeOf value Is ToolsDbViewModel Then
' Esce dal gruppo di lavorazione corrente per poter aprire le macchine del database
EgtResetCurrMachGroup()
End If
End If
End Set
End Property
' Fields that know if the ProjectPage is currently selected
Private m_ProjectPageSelected As Boolean
Public Property ProjectPageSelected As Boolean
Get
Return m_ProjectPageSelected
End Get
Set(value As Boolean)
If value <> m_ProjectPageSelected Then
m_ProjectPageSelected = value
If value Then
Application.Msn.NotifyColleagues(Application.PROJECTPAGE_SELECTED)
Else
Application.Msn.NotifyColleagues(Application.PROJECTPAGE_DESELECTED)
End If
End If
End Set
End Property
'
Private m_StatusBar As New StatusBarView
Private m_bfirst As Boolean = True
Public ReadOnly Property StatusBar As StatusBarView
Get
If m_bfirst Then
m_StatusBar.DataContext = New StatusBarViewModel
m_bfirst = False
End If
Return m_StatusBar
End Get
End Property
#End Region
#Region "CONSTRUCTOR"
Sub New()
RegisterMyMessages()
' INITIALIZE EGALTECH ENVIRONMENT
InitializeEgtEnvironment()
' INITIALIZE GRAPHICS
TabList.Add(New ProjectViewModel)
TabList.Add(New ToolsDbViewModel)
TabList.Add(New MachiningsDbViewModel)
SelectedTab = TabList(0)
OnPropertyChanged("SelectedTab")
End Sub
#End Region ' Constructor
#Region "COMMANDS"
#Region "cmdMainWindow_Closing"
''' <summary>
''' Returns a command that manage the MainWindow_Unloaded command
''' </summary>
Public ReadOnly Property cmdMainWindow_Closing() As ICommand
Get
If m_cmdMainWindow_Closing Is Nothing Then
m_cmdMainWindow_Closing = New RelayCommand(AddressOf MainWindow_Closing, AddressOf CanMainWindow_Closing)
End If
Return m_cmdMainWindow_Closing
End Get
End Property
''' <summary>
''' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
''' </summary>
Public Sub MainWindow_Closing(ByVal param As Object)
Application.Msn.NotifyColleagues(Application.CLOSEAPPLICATION)
'' Gestisco eventuale file corrente modificato
'If Not m_Controller.ManageModified() Then
' E.Cancel = True
' Return
'End If
' Salvo posizione Form (se non minimizzato)
If Application.Current.MainWindow.WindowState <> WindowState.Minimized Then
Dim WinPos As New WinPos
WindowToWinPos(Application.Current.MainWindow, WinPos)
WritePrivateProfileWinPos(S_GENERAL, K_WINPLACE, WinPos.nFlag, WinPos.nLeft, WinPos.nTop, WinPos.nWidth, WinPos.nHeight)
End If
' Terminazione generale di EgtInterface
EgtExit()
' Rilascio mutex
m_objMutex.Close()
' Aggiorno istanze usate
Dim nTmp As Integer = GetPrivateProfileInt(S_GENERAL, K_INSTANCES, 0)
nTmp -= (1 << (m_nInstance - 1))
WritePrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString())
'' Disabilito gestore Idle
'RemoveHandler Application.Idle, AddressOf Application_Idle
End Sub
''' <summary>
''' Returns true if the application can be closed
''' </summary>
Private Function CanMainWindow_Closing(ByVal param As Object) As Boolean
'' Impedisco uscita se script in esecuzione
'If m_bScriptRunning Then
' m_bStopScript = True
' E.Cancel = True
' Return
'End If
Return True
End Function
#End Region ' Closing Command
#Region "cmdMainWindow_ContentRendered"
''' <summary>
''' Returns a command that manage the MainWindow_ContentRendered command
''' </summary>
Public ReadOnly Property cmdMainWindow_ContentRendered() As ICommand
Get
If m_cmdMainWindow_ContentRendered Is Nothing Then
m_cmdMainWindow_ContentRendered = New RelayCommand(AddressOf MainWindow_ContentRendered)
End If
Return m_cmdMainWindow_ContentRendered
End Get
End Property
''' <summary>
''' Manage the MainWindow_ContentRendered event. This method is invoked by the cmdMainWindow_ContentRendered.
''' </summary>
Public Sub MainWindow_ContentRendered(ByVal param As Object)
' Notify the ContentRendered event
Application.Msn.NotifyColleagues(Application.MAINWINDOW_CONTENTRENDERED)
End Sub
#End Region ' ContentRendered Command
#Region "cmdMainWindow_Activated"
''' <summary>
''' Returns a command that manage the MainWindow_ContentRendered command
''' </summary>
Public ReadOnly Property cmdMainWindow_Activated() As ICommand
Get
If m_cmdMainWindow_Activated Is Nothing Then
m_cmdMainWindow_Activated = New RelayCommand(AddressOf MainWindow_Activated)
End If
Return m_cmdMainWindow_Activated
End Get
End Property
''' <summary>
''' Manage the MainWindow_ContentRendered event. This method is invoked by the cmdMainWindow_ContentRendered.
''' </summary>
Public Sub MainWindow_Activated(ByVal param As Object)
' Notify the ContentRendered event
Application.Msn.NotifyColleagues(Application.MAINWINDOW_ACTIVATED)
End Sub
#End Region ' Activated Command
#Region "cmdMainWindow_Deactivated"
''' <summary>
''' Returns a command that manage the MainWindow_ContentRendered command
''' </summary>
Public ReadOnly Property cmdMainWindow_Deactivated() As ICommand
Get
If m_cmdMainWindow_Deactivated Is Nothing Then
m_cmdMainWindow_Deactivated = New RelayCommand(AddressOf MainWindow_Deactivated)
End If
Return m_cmdMainWindow_Deactivated
End Get
End Property
''' <summary>
''' Manage the MainWindow_ContentRendered event. This method is invoked by the cmdMainWindow_ContentRendered.
''' </summary>
Public Sub MainWindow_Deactivated(ByVal param As Object)
' Notify the ContentRendered event
Application.Msn.NotifyColleagues(Application.MAINWINDOW_DEACTIVATED)
End Sub
#End Region ' Deactivated Command
#End Region
#Region "METHODS"
Private Sub RegisterMyMessages()
Application.Msn.Register(Application.CLOSEAPPLICATIONCOMMAND, Sub()
Application.Current.MainWindow.Close()
End Sub)
End Sub
''' <summary>
''' Method that initialize EgalTech environment
''' </summary>
Private Sub InitializeEgtEnvironment()
'' Installo aggiornamento interfaccia
'AddHandler System.Windows.Forms.Application.Idle, AddressOf Application_Idle
'' Abilito drag and drop
'Me.AllowDrop = True
'' Title
'EmitTitle()
' Impostazione path radice per i dati
m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory
If EgtUILib.GetPrivateProfileString(S_DATA, K_DATAROOT, "", m_sDataRoot, m_sDataRoot & "\" & DAT_FILE_NAME) = 0 Then
m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory
End If
' Impostazione direttorio di configurazione
m_sConfigDir = m_sDataRoot & "\" & CONF_DIR
' Impostazione direttorio per file temporanei
m_sTempDir = m_sDataRoot & "\" & TEMP_DIR
' Impostazione path Ini file
m_sIniFile = m_sConfigDir & "\" & INI_FILE_NAME
' Lo riporto nel modulo IniFile
IniFile.m_sIniFilePath = m_sIniFile
' Impostazione direttorio per le macchine
If GetPrivateProfileString(S_MACH, K_MACHINESDIR, "", m_sMachinesRoot) = 0 Then
m_sMachinesRoot = m_sDataRoot & "\" & MACHINES_DFL_DIR
End If
IniFile.m_sMachinesRoot = m_sMachinesRoot
' Verifico indice di istanza
ManageIstance()
'Imposto nome file log
Dim sCmdLogFile As String = CMDLOG_FILE_NAME.Replace("#", m_nInstance.ToString())
' Leggo e imposto chiave di protezione
Dim sLicFileName As String = String.Empty
GetPrivateProfileString(S_GENERAL, K_LICENCE, LIC_FILE_NAME, sLicFileName)
Dim sLicFile As String = m_sConfigDir & "\" & sLicFileName
Dim sKey As String = String.Empty
EgtUILib.GetPrivateProfileString(S_LICENCE, K_KEY, "", sKey, sLicFile)
EgtSetKey(sKey)
' Recupero opzioni della chiave
Dim bKey As Boolean = EgtGetKeyOptions(17615, 16, 1, m_nKeyOptions)
EgtOutLog("KeyOptions : " & bKey.ToString() & " " & m_nKeyOptions.ToString())
' Inizializzazione generale di EgtInterface
m_nDebug = GetPrivateProfileInt(S_GENERAL, K_DEBUG, 0)
Dim sLogFile As String = m_sTempDir & "\" & GENLOG_FILE_NAME
Dim sLogMsg As String = My.Application.Info.Description.ToString() & " ver. " & My.Application.Info.Version.ToString()
EgtInit(m_nDebug, sLogFile, sLogMsg)
' Leggo direttorio dei messaggi (se manca uso direttorio di configurazione)
Dim sMsgDir As String = String.Empty
If GetPrivateProfileString(S_GENERAL, K_MESSAGESDIR, "", sMsgDir) = 0 Then
sMsgDir = m_sConfigDir
End If
' Leggo file messaggi
Dim sMsgFile As String = String.Empty
GetPrivateProfileString(S_GENERAL, K_MESSAGES, "", sMsgFile)
Dim sMsgFilePath As String = sMsgDir & "\" & sMsgFile
If Not EgtLoadMessages(sMsgFilePath) Then
EgtOutLog("Error in EgtLoadMessages")
End If
' Leggo e imposto livello utilizzatore
IniFile.m_nUserLevel = GetPrivateProfileInt(S_GENERAL, K_USERLEVEL, 1)
' imposto dir font Nfe e font default
Dim sNfeDir As String = String.Empty
GetPrivateProfileString(S_GEOMDB, K_NFEFONTDIR, "", sNfeDir)
Dim sDefFont As String = String.Empty
GetPrivateProfileString(S_GEOMDB, K_DEFAULTFONT, "", sDefFont)
EgtSetFont(sNfeDir, sDefFont)
' imposto dir di default per libreria Lua e lancio libreria di base
Dim sLuaLibsDir As String = String.Empty
GetPrivateProfileString(S_LUA, K_LIBSDIR, "", sLuaLibsDir)
EgtSetLuaLibs(sLuaLibsDir)
Dim sLuaBaseLib As String = String.Empty
GetPrivateProfileString(S_LUA, K_BASELIB, "EgtBase", sLuaBaseLib)
EgtLuaRequire(sLuaBaseLib)
'' aggiungo voce per about box nel menù di sistema
'Dim hSysMenu As IntPtr = GetSystemMenu(Handle, False)
'If hSysMenu <> IntPtr.Zero Then
' AppendMenu(hSysMenu, MF_SEPARATOR, 0, "")
' AppendMenu(hSysMenu, MF_STRING, IDM_ABOUTBOX, "About TestEIn...")
'End If
' Posizione e dimensioni del Form
Dim WinPos As New WinPos
GetPrivateProfileWinPos(S_GENERAL, K_WINPLACE, WinPos.nFlag, WinPos.nLeft, WinPos.nTop, WinPos.nWidth, WinPos.nHeight)
WinPosToWindow(Application.Current.MainWindow, WinPos)
'If m_Controller.GetCommandLog() Then
' tsStatusReg.BackColor = Color.Lime
'ElseIf bLuaReg Then
' tsStatusReg.BackColor = Color.Red
'End If
'' Impostazioni MruLists
'm_MruFiles.Init(m_sIniFile, S_MRUFILES, 8)
'm_MruScripts.Init(m_sIniFile, S_MRUSCRIPTS, 8)
'' Impostazione Testi e ToolTips
'SetMessages()
'' Installo funzione gestione eventi per lua
'EgtSetProcessEvents(m_ProcEventsCallback)
'' Installo funzione output testo su status per lua
'EgtSetOutText(m_OutTextCallback)
End Sub
''' <summary>
''' Funzione che permette di gestire il numero di istanze del programma attive contemporaneamente
''' </summary>
Private Sub ManageIstance()
Dim bCreated As Boolean
Try
m_objMutex = New Mutex(False, "Global\EgtCAM5", bCreated)
Catch
bCreated = False
End Try
If bCreated Then
' Prima istanza
m_nInstance = 1
' Aggiorno stato istanze attive
WritePrivateProfileString(S_GENERAL, K_INSTANCES, m_nInstance.ToString)
Else
' Leggo il massimo numero di istanze ammesse
Dim nMaxInst As Integer = GetPrivateProfileInt(S_GENERAL, K_MAXINST, 1)
nMaxInst = Max(1, Min(nMaxInst, 32))
' Cerco il primo indice di istanza libero (max 32)
Dim nTmp As Integer = GetPrivateProfileInt(S_GENERAL, K_INSTANCES, 0)
m_nInstance = 1
Dim nMask As Integer = 1
While (nTmp And nMask) <> 0 And m_nInstance <= m_nInstance
m_nInstance += 1
nMask *= 2
End While
' Se l'indice supera il massimo
If m_nInstance > nMaxInst Then
' porto in primo piano la prima istanza
Dim bFound As Boolean = False
' processi del programma a 32 bit
Dim localProc As Process() = Process.GetProcessesByName("EgtCAM5R32")
For Each p As Process In localProc
If p.Id <> Process.GetCurrentProcess().Id Then
bFound = True
ShowWindow(p.MainWindowHandle, SW.SHOWMAXIMIZED)
Exit For
End If
Next
' se non trovati processi a 32 bit provo a 64 bit
If Not bFound Then
localProc = Process.GetProcessesByName("EgtCAM5R64")
For Each p As Process In localProc
If p.Id <> Process.GetCurrentProcess().Id Then
bFound = True
ShowWindow(p.MainWindowHandle, SW.SHOWMAXIMIZED)
Exit For
End If
Next
End If
' esco dal programma
End
End If
' Aggiorno stato istanze attive
nTmp += (1 << (m_nInstance - 1))
WritePrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString())
End If
End Sub
#End Region
End Class
End Namespace