diff --git a/harbour/ChangeLog b/harbour/ChangeLog index a9caef589b..396a60f355 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,13 @@ The license applies to all entries newer than 2009-04-28. */ +2012-06-11 12:56 UTC-0800 Pritpal Bedi (bedipritpal@hotmail.com) + + contrib/hbqt/tests/dbfbrowser.prg + + contrib/hbqt/tests/dbfbrowserclass.prg + + Added: Table browser demo code by Bacco, thank you. + This code works absolutely flawless both with current and + __HBQT_REVAMP__ protocols. + 2012-06-11 21:06 UTC+0200 Viktor Szakats (harbour syenar.net) * contrib/hbtpathy/telepath.prg * using hb_default() instead of rolling it manually diff --git a/harbour/contrib/hbqt/tests/dbfbrowser.prg b/harbour/contrib/hbqt/tests/dbfbrowser.prg new file mode 100644 index 0000000000..ffdeb6dce9 --- /dev/null +++ b/harbour/contrib/hbqt/tests/dbfbrowser.prg @@ -0,0 +1,89 @@ +/* + * $Id$ + */ + +/* + * Harbour Project source code: + * + * Copyright 2012 Carlos Bacco + * www - http://harbour-project.org + * + */ + +#include "hbqtgui.ch" +#include "dbfbrowserclass.prg" + + +PROCEDURE Main() + LOCAL oWid + LOCAL oButton1 + LOCAL oButton2 + LOCAL oTable1 + LOCAL oLay1 + LOCAL oLay2 + + Set( _SET_EXACT, .T. ) + + oWid := QWidget() + oWid:setWindowTitle( "DBF Browser" ) + + oTable1 := QDBFBrowser() + + // NOTE: QDBFBrowser inherits QTableView, so we can use the methods below. + oTable1:setAlternatingRowColors( .T. ) + oTable1:setShowGrid( .F. ) + oTable1:setTabKeyNavigation( .F. ) + oTable1:verticalHeader():setSelectionBehavior( QAbstractItemView_SelectRows ) + oTable1:verticalHeader():setSelectionMode( QAbstractItemView_SingleSelection ) + // ENDNOTE + + oButton1 := QPushButton() + oButton1:setText( "Open DBF" ) + oButton1:connect( "clicked()", {|| OpenDBF( oWid, oTable1 ) } ) + + oButton2 := QPushButton() + oButton2:setText( "Close DBF" ) + oButton2:connect( "clicked()", {|| CloseDBF( oTable1 ) } ) + + + oLay1 := QVBoxLayout( oWid ) + oLay1:addWidget( oTable1 ) + olay1:addLayout( oLay2 := QHBoxLayout() ) + oLay2:addWidget( oButton1 ) + oLay2:addWidget( oButton2 ) + + oWid:Show() + QApplication():exec() +RETURN + + +PROCEDURE OpenDBF( oWid, oTable ) + STATIC cDir := "." + LOCAL cFile + + cFile := QFileDialog():getOpenFileName( oWid, "Open file", cDir, "DBF files (*.dbf);;All files (*.*)" ) + + IF cFile == "" + RETURN + END + + cDir := hb_FNameDir( cFile ) // To remember last used dir + + CloseDBF( oTable ) + BEGIN SEQUENCE WITH {||.F.} + DBUseArea( .T., NIL, cFile, NIL, .F., .F. ) + END SEQUENCE + + If Used() + oTable:attach() + End +RETURN + +PROCEDURE CloseDBF( oTable ) + oTable:detach() + DBCloseArea() +RETURN + + +#include "dbfbrowserclass.prg" + diff --git a/harbour/contrib/hbqt/tests/dbfbrowserclass.prg b/harbour/contrib/hbqt/tests/dbfbrowserclass.prg new file mode 100644 index 0000000000..15149ff5be --- /dev/null +++ b/harbour/contrib/hbqt/tests/dbfbrowserclass.prg @@ -0,0 +1,185 @@ +/* + * $Id$ + */ + +/* + * Harbour Project source code: + * + * Copyright 2012 Carlos Bacco + * www - http://harbour-project.org + * + */ + +#ifndef HB_QDBFBROWSER_CH_ +#define HB_QDBFBROWSER_CH_ + +#include "hbclass.ch" + + +FUNCTION QDBFBrowser( ... ) +RETURN B2_QDBFBrowser():new( ... ):init() + +CREATE CLASS B2QDBFBrowser INHERIT HB_QTableView FUNCTION B2_QDBFBrowser + METHOD init() + METHOD attach() + METHOD detach() + + VAR oModel + VAR nArea INIT 0 + VAR aStru + + VAR oHack1 +ENDCLASS + +METHOD init() + ::oModel := HBQAbstractItemModel( {| t, r, x, y| B2_QDBFBrowse( Self, t, r, x, y ) } ) + Self:setModel( ::oModel ) +RETURN Self + +METHOD B2QDBFBrowser:attach() + If ( ::nArea := Select() ) > 0 + ::aStru := DBStruct() + ::oModel:reset() + ::oHack1 := ::itemDelegate() // Intermediate variable needed to avoid destroying the :connect() + ::oHack1:connect( "commitData(QWidget*)", {|oWidget| B2_QDBFCommit( Self, oWidget ) } ) + End +RETURN NIL + +METHOD B2QDBFBrowser:detach() + ::oHack1 := ::itemDelegate() // Intermediate variable needed to avoid destroying the :connect() + ::oHack1:disconnect( "commitData(QWidget*)" ) + ::nArea := 0 + ::oModel:reset() +RETURN NIL + + +PROCEDURE B2_QDBFCommit( o, oWidget ) + LOCAL oIndex := o:currentIndex() + LOCAL nCX := oIndex:column() + LOCAL nCY := oIndex:row() + Local nOldArea := Select() + LOCAL cData := oWidget:property( "text" ):toString() + + DBSelectArea( o:nArea ) + DBGoto( nCY + 1 ) + + SWITCH o:aStru[ nCX + 1, 2 ] + CASE "C" + FieldPut( nCX + 1, AllTrim( cData ) ) + EXIT + CASE "N" + FieldPut( nCX + 1, Val( cData ) ) + EXIT + CASE "L" + FieldPut( nCX + 1, Left( cData, 1 ) $ "YyTt" ) + EXIT + CASE "D" + FieldPut( nCX + 1, CToD( cData ) ) + EXIT + ENDSWITCH + + DBSelectArea( nOldArea ) +RETURN + + +FUNCTION B2_QDBFBrowse( o, nT, nRole, nX, nY ) + LOCAL xRet + LOCAL nOldArea := Select() + + If o:nArea == 0 + RETURN NIL + END + + SWITCH nT + CASE HBQT_QAIM_flags + RETURN Qt_ItemIsEnabled + Qt_ItemIsSelectable + Qt_ItemIsEditable; + + CASE HBQT_QAIM_data + + SWITCH nRole + CASE Qt_DisplayRole + DBSelectArea( o:nArea ) + DBGoto( nY + 1 ) + + xRet := "?" + SWITCH o:aStru[ nX + 1, 2 ] + CASE "C" + xRet := AllTrim( FieldGet( nX + 1 ) ) + EXIT + CASE "N" + xRet := hb_NToS( FieldGet( nX + 1 ) ) + EXIT + CASE "L" + xRet := IIf( FieldGet( nX + 1 ), "Yes", "No" ) + EXIT + CASE "D" + xRet := DToC( FieldGet( nX + 1 ) ) + EXIT + ENDSWITCH + + DBSelectArea( nOldArea ) + RETURN xRet + + CASE Qt_EditRole /* Here we can specify different formats for editing*/ + DBSelectArea( o:nArea ) + DBGoto( nY + 1 ) + + xRet := "" + SWITCH o:aStru[ nX + 1, 2 ] + CASE "C" + xRet := AllTrim( FieldGet( nX + 1 ) ) + EXIT + CASE "N" + xRet := hb_NToS( FieldGet( nX + 1 ) ) + EXIT + CASE "L" + xRet := IIf( FieldGet( nX + 1 ), "Y", "N" ) + EXIT + CASE "D" + xRet := DToC( FieldGet( nX + 1 ) ) + EXIT + ENDSWITCH + + DBSelectArea( nOldArea ) + RETURN xRet + + CASE Qt_TextAlignmentRole + SWITCH o:aStru[ nX + 1, 2 ] + CASE "C" + RETURN Qt_AlignVCenter + Qt_AlignLeft + CASE "N" + RETURN Qt_AlignVCenter + Qt_AlignRight + ENDSWITCH + RETURN Qt_AlignCenter + ENDSWITCH + RETURN NIL + + CASE HBQT_QAIM_headerData + SWITCH nRole + CASE Qt_DisplayRole + IF nX == Qt_Horizontal + RETURN o:aStru[ nY + 1, 1 ] + ELSE + RETURN hb_NToS( nY + 1 ) + ENDIF + + CASE Qt_TextAlignmentRole + IF nX == Qt_Horizontal + RETURN Qt_AlignCenter + ELSE + RETURN Qt_AlignVCenter + Qt_AlignRight + ENDIF + + ENDSWITCH + RETURN NIL + + CASE HBQT_QAIM_rowCount + RETURN LastRec() + + CASE HBQT_QAIM_columnCount + RETURN Len( o:aStru ) + ENDSWITCH + + RETURN NIL + +#endif /* HB_QDBFBROWSER_CH_ */