https://github.com/walter426/PyQtUtilities/
There are many GUI which require to display a group of widget(e.g. button, checkbox...) in a grid format.
Below is an user-defined PyQt object to handle this kind of requirement,
Example:
self.WidgetList_QWG = QWidgetGrid('QCheckBox', '', Tbl_WidgetArgu_set, GridDirection = 0, MaxRowCnt = 15)
self.WidgetListt_scrollArea.setWidget(self.WidgetList_QWG)
Code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import math
class QWidgetGrid(QWidget):
def __init__(self, WidgetType, WidgetTypeName, WidgetArguSet, GridDirection = 0, MaxRowCnt = 0, MaxColCnt = 0):
QWidget.__init__(self)
if GridDirection > 1:
return
if MaxRowCnt == 0 and MaxColCnt == 0:
return
TblCnt = len(WidgetArguSet)
if TblCnt <= 0:
return
if MaxRowCnt > 0 and MaxColCnt == 0:
MaxColCnt = int(math.ceil(float(TblCnt)/float(MaxRowCnt)))
elif MaxColCnt > 0 and MaxRowCnt == 0:
MaxRowCnt = int(math.ceil(float(TblCnt)/float(MaxColCnt)))
self.QGL = QGridLayout()
tbl_idx = 0
#From Top to Bottom, than Left to Right
if GridDirection == 0:
ColCnt = int(math.ceil(float(TblCnt)/float(MaxRowCnt)))
for col_idx in range(0, ColCnt):
RowCnt = MaxRowCnt
if col_idx >= ColCnt - 1 and (TblCnt % MaxRowCnt) > 0:
RowCnt = TblCnt % MaxRowCnt
for row_idx in range(0, RowCnt):
str_Widget_curr = 'self.' + WidgetTypeName + '_' + str(tbl_idx)
str_CreateWidget = str_Widget_curr + ' = ' + WidgetType + '('
for k in range(0, len(WidgetArguSet[tbl_idx])):
str_CreateWidget += WidgetArguSet[tbl_idx][k] + ', '
str_CreateWidget = str_CreateWidget.rstrip(',') + ')'
exec(str_CreateWidget)
exec('self.QGL.addWidget(' + str_Widget_curr + ', ' + str(row_idx) + ', ' + str(col_idx) + ')')
tbl_idx += 1
#From Left to Right, Top to Bottom
elif GridDirection == 1:
RowCnt = int(math.ceil(float(TblCnt)/float(MaxColCnt)))
for row_idx in range(0, RowCnt):
ColCnt = MaxColCnt
if row_idx >= RowCnt - 1 and (TblCnt % MaxColCnt) > 0:
ColCnt = TblCnt % MaxColCnt
for col_idx in range(0, ColCnt):
str_Widget_curr = 'self.' + WidgetTypeName + '_' + str(tbl_idx)
str_CreateWidget = str_Widget_curr + ' = ' + WidgetType + '('
for k in range(0, len(WidgetArguSet[tbl_idx])):
str_CreateWidget += WidgetArguSet[tbl_idx][k] + ', '
str_CreateWidget = str_CreateWidget.rstrip(',') + ')'
exec(str_Widget_curr + ' = ' + WidgetType +'(' + str_CreateWidget + ')')
exec('self.QGL.addWidget(' + str_Widget_curr + ', ' + str(row_idx) + ', ' + str(col_idx) + ')')
tbl_idx += 1
self.setLayout(self.QGL)
def __del__(self):
return
Translate
2013年11月16日 星期六
2013年10月22日 星期二
[VBA] Delete an Array Element
Public Sub DeleteArrayItem(arr As Variant, index As Long)
Dim i As Long
For i = index To UBound(arr) - 1
arr(i) = arr(i + 1)
Next
' VB will convert this to 0 or to an empty string.
arr(UBound(arr)) = Empty
ReDim Preserve arr(LBound(arr) To UBound(arr) - 1)
End Sub
Dim i As Long
For i = index To UBound(arr) - 1
arr(i) = arr(i + 1)
Next
' VB will convert this to 0 or to an empty string.
arr(UBound(arr)) = Empty
ReDim Preserve arr(LBound(arr) To UBound(arr) - 1)
End Sub
2013年10月9日 星期三
[VBA] Concatenate multiple tables of identical structure
https://github.com/walter426/VbaUtilities/blob/master/SqlUtilities.bas
'Create table which is cancatenated from multiple tables of the same structure
Public Function CreateTbl_ConcatTbls(Tbl_src_Set As Variant, Tbl_des_name As String, Optional Type_Set As Variant = "") As String
On Error GoTo Err_CreateTbl_ConcatTbls
Dim FailedReason As String
If UBound(Tbl_src_Set) < 0 Then
FailedReason = "No table in the table set"
GoTo Exit_CreateTbl_ConcatTbls
End If
Dim Tbl_src_name As Variant
For Each Tbl_src_name In Tbl_src_Set
If TableExist(Tbl_src_name & "") = False Then
FailedReason = Tbl_src_name & " does not exist!"
GoTo Exit_CreateTbl_ConcatTbls
End If
Next
'Initialize Tbl_des
DelTable (Tbl_des_name)
Dim SQL_cmd As String
Tbl_src_name = Tbl_src_Set(0)
SQL_cmd = "SELECT " & Chr(34) & "null" & Chr(34) & " AS [Type], " & Tbl_src_name & ".* " & vbCrLf & _
"INTO " & Tbl_des_name & " " & vbCrLf & _
"FROM " & Tbl_src_name & " " & vbCrLf & _
"WHERE 1 = 0 " & vbCrLf & _
";"
RunSQL_CmdWithoutWarning (SQL_cmd)
'Start Append
Dim tbl_idx As Integer
Dim SQL_Seq_Type As String
For tbl_idx = 0 To UBound(Tbl_src_Set)
Tbl_src_name = Tbl_src_Set(tbl_idx)
If VarType(Type_Set) > vbArray And Type_Set(tbl_idx) = "" Then
SQL_Seq_Type = ""
Else
SQL_Seq_Type = Chr(34) & Type_Set(tbl_idx) & Chr(34) & " AS [Type], "
End If
SQL_cmd = "INSERT INTO " & Tbl_des_name & " " & vbCrLf & _
"SELECT " & SQL_Seq_Type & "[" & Tbl_src_name & "].* " & vbCrLf & _
"FROM [" & Tbl_src_name & "] " & vbCrLf & _
";"
RunSQL_CmdWithoutWarning (SQL_cmd)
Next
If UBound(Type_Set) < 0 Then
SQL_cmd = "ALTER TABLE [" & Tbl_des_name & "] " & vbCrLf & _
"DROP COLUMN [Type]" & vbCrLf & _
";"
RunSQL_CmdWithoutWarning (SQL_cmd)
End If
Exit_CreateTbl_ConcatTbls:
CreateTbl_ConcatTbls = FailedReason
Exit Function
Err_CreateTbl_ConcatTbls:
FailedReason = Err.Description
Resume Exit_CreateTbl_ConcatTbls
End Function
'Create table which is cancatenated from multiple tables of the same structure
Public Function CreateTbl_ConcatTbls(Tbl_src_Set As Variant, Tbl_des_name As String, Optional Type_Set As Variant = "") As String
On Error GoTo Err_CreateTbl_ConcatTbls
Dim FailedReason As String
If UBound(Tbl_src_Set) < 0 Then
FailedReason = "No table in the table set"
GoTo Exit_CreateTbl_ConcatTbls
End If
Dim Tbl_src_name As Variant
For Each Tbl_src_name In Tbl_src_Set
If TableExist(Tbl_src_name & "") = False Then
FailedReason = Tbl_src_name & " does not exist!"
GoTo Exit_CreateTbl_ConcatTbls
End If
Next
'Initialize Tbl_des
DelTable (Tbl_des_name)
Dim SQL_cmd As String
Tbl_src_name = Tbl_src_Set(0)
SQL_cmd = "SELECT " & Chr(34) & "null" & Chr(34) & " AS [Type], " & Tbl_src_name & ".* " & vbCrLf & _
"INTO " & Tbl_des_name & " " & vbCrLf & _
"FROM " & Tbl_src_name & " " & vbCrLf & _
"WHERE 1 = 0 " & vbCrLf & _
";"
RunSQL_CmdWithoutWarning (SQL_cmd)
'Start Append
Dim tbl_idx As Integer
Dim SQL_Seq_Type As String
For tbl_idx = 0 To UBound(Tbl_src_Set)
Tbl_src_name = Tbl_src_Set(tbl_idx)
If VarType(Type_Set) > vbArray And Type_Set(tbl_idx) = "" Then
SQL_Seq_Type = ""
Else
SQL_Seq_Type = Chr(34) & Type_Set(tbl_idx) & Chr(34) & " AS [Type], "
End If
SQL_cmd = "INSERT INTO " & Tbl_des_name & " " & vbCrLf & _
"SELECT " & SQL_Seq_Type & "[" & Tbl_src_name & "].* " & vbCrLf & _
"FROM [" & Tbl_src_name & "] " & vbCrLf & _
";"
RunSQL_CmdWithoutWarning (SQL_cmd)
Next
If UBound(Type_Set) < 0 Then
SQL_cmd = "ALTER TABLE [" & Tbl_des_name & "] " & vbCrLf & _
"DROP COLUMN [Type]" & vbCrLf & _
";"
RunSQL_CmdWithoutWarning (SQL_cmd)
End If
Exit_CreateTbl_ConcatTbls:
CreateTbl_ConcatTbls = FailedReason
Exit Function
Err_CreateTbl_ConcatTbls:
FailedReason = Err.Description
Resume Exit_CreateTbl_ConcatTbls
End Function
2013年9月24日 星期二
[VBA] Logarithm of base 10
https://github.com/walter426/VbaUtilities/blob/master/MathUtilities.bas
As there is not built-in function for logarithm of base 10, so it is required to build it from the default natural Logarithm function in VBA
Public Function Log10(X)
Log10 = Log(X) / Log(10#)
End Function
As there is not built-in function for logarithm of base 10, so it is required to build it from the default natural Logarithm function in VBA
Public Function Log10(X)
Log10 = Log(X) / Log(10#)
End Function
2013年9月9日 星期一
[VBA] Get Link Table connection Info
A function to obtain link table connection information such as 'link specification', current path...
'Get Link Table connection Info
Public Function GetLinkTblConnInfo(Tbl_name As String, param As String) As String
On Error GoTo Exit_GetLinkTblConnInfo
Dim LinkTblConnInfo As Variant
LinkTblConnInfo = SplitStrIntoArray(CurrentDb.TableDefs(Tbl_name).Connect, ";")
Dim param_idx As Integer
Dim LinkTblConnParam As String
param = param & "="
For param_idx = 0 To UBound(LinkTblConnInfo)
LinkTblConnParam = LinkTblConnInfo(param_idx)
If Left(LinkTblConnParam, Len(param)) = param Then
GetLinkTblConnInfo = Right(LinkTblConnParam, Len(LinkTblConnParam) - Len(param))
Exit For
End If
Next param_idx
Exit_GetLinkTblConnInfo:
Exit Function
Err_GetLinkTblConnInfo:
ShowMsgBox (Err.Description)
GetLinkTblConnInfo = ""
Resume Exit_GetLinkTblConnInfo
End Function
'Get Link Table connection Info
Public Function GetLinkTblConnInfo(Tbl_name As String, param As String) As String
On Error GoTo Exit_GetLinkTblConnInfo
Dim LinkTblConnInfo As Variant
LinkTblConnInfo = SplitStrIntoArray(CurrentDb.TableDefs(Tbl_name).Connect, ";")
Dim param_idx As Integer
Dim LinkTblConnParam As String
param = param & "="
For param_idx = 0 To UBound(LinkTblConnInfo)
LinkTblConnParam = LinkTblConnInfo(param_idx)
If Left(LinkTblConnParam, Len(param)) = param Then
GetLinkTblConnInfo = Right(LinkTblConnParam, Len(LinkTblConnParam) - Len(param))
Exit For
End If
Next param_idx
Exit_GetLinkTblConnInfo:
Exit Function
Err_GetLinkTblConnInfo:
ShowMsgBox (Err.Description)
GetLinkTblConnInfo = ""
Resume Exit_GetLinkTblConnInfo
End Function
2013年9月6日 星期五
[VBA] Get the current path of a link table
'Get the current path of a link table
Public Function GetLinkTblPath(Tbl_name As String) As String
On Error GoTo Exit_GetLinkTblPath
Dim LinkTblPath As String
LinkTblPath = CurrentDb.TableDefs(Tbl_name).Connect
LinkTblPath = Right(LinkTblPath, Len(LinkTblPath) - (InStr(1, LinkTblPath, "DATABASE=") + 8)) & "\" & CurrentDb.TableDefs(Tbl_name).SourceTableName
GetLinkTblPath = LinkTblPath
Exit_GetLinkTblPath:
Exit Function
Err_GetLinkTblPath:
ShowMsgBox (Err.Description)
GetLinkTblPath = ""
Resume Exit_GetLinkTblPath
End Function
Public Function GetLinkTblPath(Tbl_name As String) As String
On Error GoTo Exit_GetLinkTblPath
Dim LinkTblPath As String
LinkTblPath = CurrentDb.TableDefs(Tbl_name).Connect
LinkTblPath = Right(LinkTblPath, Len(LinkTblPath) - (InStr(1, LinkTblPath, "DATABASE=") + 8)) & "\" & CurrentDb.TableDefs(Tbl_name).SourceTableName
GetLinkTblPath = LinkTblPath
Exit_GetLinkTblPath:
Exit Function
Err_GetLinkTblPath:
ShowMsgBox (Err.Description)
GetLinkTblPath = ""
Resume Exit_GetLinkTblPath
End Function
2013年8月29日 星期四
[QGIS] Draw vector layer features correctly on the mapCanvas
Since the CRS stored in a vector layer may not be the same as the one which the current project using.
It must convert the coordinates of features first for correct drawing.
Below is an example to do it.
from qgis.core import *
from qgis.gui import *
mapCanvas = iface.mapCanvas
RubberBand = QgsRubberBand(mapCanvas, False)
DrawVectorsInCandList(mapCanvas, CandList, VL, color, RubberBand)
self.mapCanvas.refresh()
def DrawVectorsInCandList(mapCanvas, CandList, VL, color, RubberBand_l):
if len(CandList) <= 0: #to cater for -1 or +1 frequency
RubberBand_l.reset(False)
return
str_find = ""
for item in CandList:
str_find = str_find + 'or CandID=\'' + item + '\''
str_find = str_find.lstrip('or ')
VL.setSubsetString(str_find)
VL.invertSelection()
VL.setSubsetString("")
featlist = VL.selectedFeatures()
if VL.selectedFeatureCount == 0:
return
fNIdx_CandID = VL.fieldNameIndex("CandID")
Qgs_MPL = QgsGeometry().asMultiPolyline()
VL_crs = QgsCoordinateReferenceSystem()
VL_crs.createFromEpsg(VL.crs().epsg())
mapCanvas_crs = mapCanvas.mapRenderer().destinationCrs()
for feature in featlist:
QgsPoint_O = feature.geometry().vertexAt(0)
QgsPoint_O = CoorTransform(QgsPoint_O, VL_crs, mapCanvas_crs)
QgsPoint_D = feature.geometry().vertexAt(1)
QgsPoint_D = CoorTransform(QgsPoint_D, VL_crs, mapCanvas_crs)
Qgs_MPL.append([QgsPoint_O, QgsPoint_D])
RubberBand_l.reset(False)
RubberBand_l.setColor(color)
RubberBand_l.setWidth(2)
RubberBand_l.setToGeometry(QgsGeometry.fromMultiPolyline(Qgs_MPL), None)
RubberBand_l.show()
It must convert the coordinates of features first for correct drawing.
Below is an example to do it.
from qgis.core import *
from qgis.gui import *
mapCanvas = iface.mapCanvas
RubberBand = QgsRubberBand(mapCanvas, False)
DrawVectorsInCandList(mapCanvas, CandList, VL, color, RubberBand)
self.mapCanvas.refresh()
def DrawVectorsInCandList(mapCanvas, CandList, VL, color, RubberBand_l):
if len(CandList) <= 0: #to cater for -1 or +1 frequency
RubberBand_l.reset(False)
return
str_find = ""
for item in CandList:
str_find = str_find + 'or CandID=\'' + item + '\''
str_find = str_find.lstrip('or ')
VL.setSubsetString(str_find)
VL.invertSelection()
VL.setSubsetString("")
featlist = VL.selectedFeatures()
if VL.selectedFeatureCount == 0:
return
fNIdx_CandID = VL.fieldNameIndex("CandID")
Qgs_MPL = QgsGeometry().asMultiPolyline()
VL_crs = QgsCoordinateReferenceSystem()
VL_crs.createFromEpsg(VL.crs().epsg())
mapCanvas_crs = mapCanvas.mapRenderer().destinationCrs()
for feature in featlist:
QgsPoint_O = feature.geometry().vertexAt(0)
QgsPoint_O = CoorTransform(QgsPoint_O, VL_crs, mapCanvas_crs)
QgsPoint_D = feature.geometry().vertexAt(1)
QgsPoint_D = CoorTransform(QgsPoint_D, VL_crs, mapCanvas_crs)
Qgs_MPL.append([QgsPoint_O, QgsPoint_D])
RubberBand_l.reset(False)
RubberBand_l.setColor(color)
RubberBand_l.setWidth(2)
RubberBand_l.setToGeometry(QgsGeometry.fromMultiPolyline(Qgs_MPL), None)
RubberBand_l.show()
訂閱:
文章 (Atom)