Files
OmagPHOTO/OptionPanel/OptionPanelVM.vb
T
Emmanuele Sassi 53c8d6d934 OmagPHOTO :
- Aggiunta lista lastre che scorre e gestisce migliaia di lastre.
- Miglioramenti e correzioni varie.
2018-01-15 10:14:32 +00:00

417 lines
14 KiB
VB.net

Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtUILib
Imports EgtWPFLib5
Public Class OptionPanelVM
Inherits VMBase
#Region "FIELDS & PROPERTIES"
Public ReadOnly Property SelSlab As Slab
Get
If IsNothing(Map.refProjectVM) Then Return Nothing
Return Map.refProjectVM.SelSlab
End Get
End Property
Public ReadOnly Property StateList As List(Of IdNameStruct)
Get
If IsNothing(Map.refProjectVM) Then Return Nothing
Return Map.refProjectVM.StateList
End Get
End Property
Public ReadOnly Property MaterialList As List(Of String)
Get
If IsNothing(Map.refProjectVM) Then Return Nothing
Return Map.refProjectVM.MaterialList
End Get
End Property
Public ReadOnly Property PhotoCommand_Visibility As Visibility
Get
If Not IsNothing(Map.refProjectVM) Then
Select Case Map.refProjectVM.SelProjectMode
Case ProjectVM.ProjectModeOpt.DETAIL
Return Visibility.Collapsed
Case ProjectVM.ProjectModeOpt.NEWSLAB
Return Visibility.Visible
End Select
End If
Return Visibility.Collapsed
End Get
End Property
Public ReadOnly Property Remove_Visibility As Visibility
Get
If Not IsNothing(Map.refProjectVM) Then
Select Case Map.refProjectVM.SelProjectMode
Case ProjectVM.ProjectModeOpt.DETAIL
Return Visibility.Visible
Case ProjectVM.ProjectModeOpt.NEWSLAB
Return Visibility.Collapsed
End Select
End If
Return Visibility.Collapsed
End Get
End Property
Private m_Ok_IsEnabled As Boolean = True
Public Property Ok_IsEnabled As Boolean
Get
Return m_Ok_IsEnabled
End Get
Set(value As Boolean)
m_Ok_IsEnabled = value
NotifyPropertyChanged("Ok_IsEnabled")
End Set
End Property
#Region "Messages"
Public ReadOnly Property IdMsg As String
Get
Return EgtMsg(MSG_SLAB + 1)
End Get
End Property
Public ReadOnly Property ImagePathMsg As String
Get
Return EgtMsg(MSG_SLAB + 2)
End Get
End Property
Public ReadOnly Property StateMsg As String
Get
Return EgtMsg(MSG_SLAB + 3)
End Get
End Property
Public ReadOnly Property ProjectAssignedToMsg As String
Get
Return EgtMsg(MSG_SLAB + 4)
End Get
End Property
Public ReadOnly Property MaterialMsg As String
Get
Return EgtMsg(MSG_RAWPARTPAGEUC + 9)
End Get
End Property
Public ReadOnly Property ThicknessMsg As String
Get
Return EgtMsg(MSG_RAWPARTPAGEUC + 5)
End Get
End Property
Public ReadOnly Property WarehousePositionMsg As String
Get
Return EgtMsg(MSG_SLAB + 5)
End Get
End Property
Public ReadOnly Property OkMsg As String
Get
If Not IsNothing(Map.refProjectVM) Then
Select Case Map.refProjectVM.SelProjectMode
Case ProjectVM.ProjectModeOpt.DETAIL
Return EgtMsg(MSG_OPTIONPANEL + 1)
Case ProjectVM.ProjectModeOpt.NEWSLAB
Return EgtMsg(MSG_OPTIONPANEL + 2)
End Select
End If
Return "OkMsg"
End Get
End Property
Public ReadOnly Property CancelMsg As String
Get
Return EgtMsg(MSG_OPTIONPANEL + 3)
End Get
End Property
Public ReadOnly Property RemoveMsg As String
Get
Return EgtMsg(MSG_OPTIONPANEL + 4)
End Get
End Property
#End Region ' Messages
#Region "ToolTip"
Public ReadOnly Property PhotoToolTip As String
Get
Return EgtMsg(MSG_RAWPARTTAB + 2)
End Get
End Property
#End Region ' ToolTip
' Definizione comandi
Private m_cmdPhoto As ICommand
Private m_cmdOk As ICommand
Private m_cmdCancel As ICommand
Private m_cmdRemove As ICommand
#End Region ' FIELDS & PROPERTIES
#Region "CONSTRUCTOR"
Sub New()
' Creo riferimento a questa classe in Map
Map.SetRefOptionPanelVM(Me)
' Assegno funzione per disabilitare bottone di conferma alla lista
Slab.m_IsEnabledBtn = AddressOf IsEnabledBtn
' Funzioni per creare nuovo file Db con tabella
''CreateDbFile()
''CreateTable()
End Sub
#End Region ' CONSTRUCTOR
#Region "METHODS"
Friend Sub InitOptionPanel()
Select Case Map.refProjectVM.SelProjectMode
Case ProjectVM.ProjectModeOpt.DETAIL
SelSlab.SetOrigValues()
SelSlab.ResetIsModified()
Case ProjectVM.ProjectModeOpt.NEWSLAB
' Creo nuova lastra corrente con valori di default
Map.refProjectVM.SelSlab = New Slab(String.Empty, String.Empty, Slab.StateOpt.AVAILABLE, String.Empty, String.Empty, 0, String.Empty)
' Creo nuovo progetto
EgtNewFile()
EgtZoom(ZM.ALL)
End Select
' Aggiorno visualizzazione bottone foto perchè potrei essere passato da mod detail a newslab
NotifyPropertyChanged("PhotoCommand_Visibility")
NotifyPropertyChanged("Remove_Visibility")
NotifyPropertyChanged("OkMsg")
NotifyPropertyChanged("SelSlab")
' Aggiorno stato bottone di conferma
IsEnabledBtn(SelSlab.IsValid)
End Sub
Private Sub IsEnabledBtn(IsEnabled As Boolean)
Ok_IsEnabled = IsEnabled
End Sub
#End Region ' METHODS
#Region "COMMANDS"
#Region "PhotoCommand"
Public ReadOnly Property PhotoCommand As ICommand
Get
If m_cmdPhoto Is Nothing Then
m_cmdPhoto = New Command(AddressOf Photo)
End If
Return m_cmdPhoto
End Get
End Property
Public Sub Photo(ByVal param As Object)
' Se macchina fotografica collegata, faccio una foto
If Map.refMainWindowVM.m_Camera.GetCameraLink() Then
If Map.refMainWindowVM.m_Camera.CameraClick() Then
SelSlab.ImagePath = "*"
Else
Map.refStatusBarVM.SetOutputMessage(EgtMsg(90313), 5, MSG_TYPE.ERROR_) 'Fotografia non riuscita
End If
' Altrimenti lancio browser di immagini
Else
PhotoFromFile()
End If
End Sub
Friend Sub PostPhoto(sPath As String, sContour As String)
' Carico la foto
Map.refDetailPageVM.LoadPhoto(sPath)
' Se richiesto il riconoscimento del contorno
'If Not String.IsNullOrEmpty(sContour) Then
' If Not m_MainWindow.m_CurrentProjectPageUC.LoadContour(sContour) Then
' m_MainWindow.m_CurrentProjectPageUC.SetWarningMessage(EgtMsg(90324)) 'Riconoscimento contorno non riuscito
' End If
'End If
' Aggiorno visualizzazione
EgtZoom(ZM.ALL)
End Sub
Friend Sub PhotoFromFile()
' Apro dialogo per scelta immagine
Dim PhotoDlg As New Microsoft.Win32.OpenFileDialog
PhotoDlg.Title = "Open"
PhotoDlg.Filter = "Image file(*.jpg;*.png;*.bmp)|*.jpg;*.png;*.bmp"
PhotoDlg.FilterIndex = 1
GetMainPrivateProfileString(S_GENERAL, K_IMAGEDIR, "", PhotoDlg.InitialDirectory)
If Not PhotoDlg.ShowDialog Then Return
Dim sPhoto As String = PhotoDlg.FileName
' Assegno la path dell'immagine selezionata
SelSlab.ImagePath = sPhoto
' Altri dati foto
' Carico immagine
If Not Map.refDetailPageVM.LoadPhoto(sPhoto) Then
' Errore nel caricamento della fotografia
MessageBox.Show(EgtMsg(MSG_RAWPARTTAB + 1), EgtMsg(MSG_MESSAGEBOX + 1), MessageBoxButton.OK, MessageBoxImage.Error)
Return
End If
' Aggiorno visualizzazione
EgtZoom(ZM.ALL)
End Sub
Private Function CopyPhoto(OrigPath As String) As String
Dim OrigTxtPath As String = Path.ChangeExtension(OrigPath, "txt")
Dim ImgName As String = ValidateFileName(SelSlab.Id)
Dim ImgExt As String = Path.GetExtension(OrigPath)
Dim nI As Integer = 0
Dim NewImgPath As String = Path.Combine(Map.refMainWindowVM.MainWindowM.sPhotoDir, ImgName & ImgExt)
While File.Exists(NewImgPath)
nI += 1
NewImgPath = Path.Combine(Map.refMainWindowVM.MainWindowM.sPhotoDir, ImgName & "_" & nI.ToString() & ImgExt)
End While
Dim NewTxtPath As String = Path.ChangeExtension(NewImgPath, "txt")
Try
File.Copy(OrigPath, NewImgPath, True)
File.Copy(OrigTxtPath, NewTxtPath, True)
Catch ex As Exception
Return String.Empty
End Try
Return Path.GetFileName(NewImgPath)
End Function
Private Function ValidateFileName(sFileName As String) As String
Dim sTemp As String = sFileName
For Each badChar As Char In Path.GetInvalidFileNameChars()
sTemp = sTemp.Replace(badChar, "_")
Next
Return sTemp
End Function
#End Region ' PhotoCommand
#Region "Ok"
Public ReadOnly Property Ok_Command As ICommand
Get
If m_cmdOk Is Nothing Then
m_cmdOk = New Command(AddressOf Ok)
End If
Return m_cmdOk
End Get
End Property
Public Sub Ok(ByVal param As Object)
Select Case Map.refProjectVM.SelProjectMode
Case ProjectVM.ProjectModeOpt.DETAIL
If SelSlab.IsModified Then
' Aggiorno la lastra con le modifiche
Dim Query As String = "UPDATE " & Slab.DB_SLABS & " SET " &
If(SelSlab.IsModifiedId, Slab.DB_ID & " = '" & SelSlab.Id & "', ", "") &
If(SelSlab.IsModifiedState, Slab.DB_STATE & " = '" & SelSlab.State & "', ", "") &
If(SelSlab.IsModifiedProjectAssignedTo, Slab.DB_PROJASSIGNEDTO & " = '" & SelSlab.ProjectAssignedTo & "', ", "") &
If(SelSlab.IsModifiedMaterial, Slab.DB_MATERIAL & " = '" & SelSlab.Material & "', ", "") &
If(SelSlab.IsModifiedThickness, Slab.DB_THICKNESS & " = '" & SelSlab.Thickness & "', ", "") &
If(SelSlab.IsModifiedWarehousePosition, Slab.DB_WAREHOUSEPOS & " = '" & SelSlab.WarehousePosition & "'", "")
Query = Query.TrimEnd(","c, " "c)
Query &= "WHERE Id = '" & SelSlab.OrigId & "'"
ManageDb.ExecuteQuery(Query)
End If
Case ProjectVM.ProjectModeOpt.NEWSLAB
' Copio immagine
Dim sImgSource As String
If SelSlab.ImagePath = "*" Then
sImgSource = Map.refMainWindowVM.m_Camera.ImageDir & "\" & Camera.FINAL_IMAGE
Else
sImgSource = SelSlab.ImagePath
End If
Dim sCopyImagePath As String = CopyPhoto(sImgSource)
If Not String.IsNullOrEmpty(sCopyImagePath) Then
SelSlab.ImagePath = sCopyImagePath
Else
Dim sMsg As String = "Error copying image or auxiliary info"
Map.refStatusBarVM.SetOutputMessage(sMsg, 5, MSG_TYPE.ERROR_)
Return
End If
' Aggiungo la nuova lastra al Db
Dim Query As String = "INSERT INTO " & Slab.DB_SLABS & " (" & Slab.DB_ID & ", " & Slab.DB_IMAGEPATH & ", " & Slab.DB_STATE & ", " & Slab.DB_PROJASSIGNEDTO & ", " & Slab.DB_MATERIAL & ", " & Slab.DB_THICKNESS & ", " & Slab.DB_WAREHOUSEPOS & ", " & Slab.DB_ADDEDDATE & ")" &
" VALUES ('" & SelSlab.Id & "', " &
"'" & SelSlab.ImagePath & "', " &
SelSlab.State & ", " &
"'" & SelSlab.ProjectAssignedTo & "', " &
"'" & SelSlab.Material & "', " &
SelSlab.Thickness & ", " &
"'" & SelSlab.WarehousePosition & "', " &
"Date('" & (String.Format("{0:yyyy-MM-dd}", SelSlab.AddedDate)) & "'))"
ManageDb.ExecuteQuery(Query)
End Select
Map.refProjectVM.SelProjectMode = ProjectVM.ProjectModeOpt.LIST
End Sub
#End Region ' Ok
#Region "Cancel"
Public ReadOnly Property Cancel_Command As ICommand
Get
If m_cmdCancel Is Nothing Then
m_cmdCancel = New Command(AddressOf Cancel)
End If
Return m_cmdCancel
End Get
End Property
Public Sub Cancel(ByVal param As Object)
Map.refProjectVM.SelProjectMode = ProjectVM.ProjectModeOpt.LIST
End Sub
#End Region ' Cancel
#Region "Remove"
Public ReadOnly Property Remove_Command As ICommand
Get
If m_cmdRemove Is Nothing Then
m_cmdRemove = New Command(AddressOf Remove)
End If
Return m_cmdRemove
End Get
End Property
Public Sub Remove(ByVal param As Object)
' chiedo conferma
If MessageBox.Show(EgtMsg(MSG_OPTIONPANEL + 5), "", MessageBoxButton.YesNo, MessageBoxImage.Question) <>
MessageBoxResult.Yes Then Return
Dim sImagePath As String = SelSlab.ImagePath
' elimino dal Db
Dim Query As String = "DELETE FROM " & Slab.DB_SLABS & " WHERE " &
Slab.DB_ID & " = '" & SelSlab.Id & "'"
If ManageDb.ExecuteQuery(Query) <> 0 Then
' elimino anche la relativa foto
Try
If Not File.Exists(sImagePath) Then
sImagePath = Map.refMainWindowVM.MainWindowM.sPhotoDir & "\" & sImagePath
End If
File.Delete(sImagePath)
File.Delete(Path.ChangeExtension(sImagePath, "txt"))
Catch ex As FileNotFoundException
' non è un problema
Catch ex As Exception
' non è un problema
End Try
End If
' elimino dalla lista
Map.refProjectVM.SlabList.Remove(SelSlab)
' torno alla lista
Map.refProjectVM.SelProjectMode = ProjectVM.ProjectModeOpt.LIST
End Sub
#End Region ' Remove
#End Region ' COMMANDS
End Class