[Cocoapsql-developer] commit: optional autoSizing of tableColumns
Status: Alpha
Brought to you by:
alkirkus
From: Olaf v. O. <ol...@ad...> - 2002-06-04 20:23:36
|
June 6, 2002 - added optional autoSizing of the tableColumns after execution of a query or when checkbox is checked on (again). To do: <> I set a maximum to the number of rows rpcessed to calculate field sizes because I am afraid that it locks the system (for a relatively long time) when there are many rows to process. Alternatives are a max number of cells to process, set in the prefferences PLUS a dialog with a prgress indicator and a cancel butoon. <> To calculate the collumnSize to accomodate the cells, we have to add extra space for intercell spacing and more... <> The cellToCellSpacing = [tableView intercellSpacing] is not enough. I had to add 6 more to make (usually) it just fit. <> Different columns with differend types of values show a different result. Specialy date columns are too tight. PSQLController.m added marked lines //if we have info returned populate the results area if([query hasResults]) { [self populateTable]; add if ( tableWillAutoSizeColumns ) { add [self autoSizeTableColumns]; add [tableView setNeedsDisplay:YES]; add } } IB MainMenu.nib window added a checkBox "Size Columns To Fit" PSQLController added outlet checkAutoSizeTableColumns with connection to the checkBox PSQLController added action setTableShouldAutoSizeColums: with connection from the checkBox => PSQLController.h added BOOL tableWillAutoSizeColumns; - (IBAction)setTableShouldAutoSizeColums:(id)sender; - (BOOL)tableShouldAutoSizeColums; - (void)autoSizeTableColumns; => PSQLController.m -(id)init added tableWillAutoSizeColumns = YES; => PSQLController.m added // Check Box "Size Columns To Fit" action - (IBAction)setTableShouldAutoSizeColums:(id)sender { tableWillAutoSizeColumns = [checkAutoSizeTableColumns state] == NSOnState; if (tableWillAutoSizeColumns) [self autoSizeTableColumns]; } - (BOOL)tableShouldAutoSizeColums { return tableWillAutoSizeColumns; } - (void)autoSizeTableColumns { NSFont *dataCellsFont; NSDictionary *attributes; NSSize cellToCellSpacing, cellsSize; float cellWidthFitsAll; int i, j; NSArray *curColumns = [tableView tableColumns]; cellToCellSpacing = [tableView intercellSpacing]; for(i=0;200>i<[curColumns count];i++){ // process 200 rows max if ( dataCellsFont == nil ) { // dataCellsFont = nil if dataCell is not a text-type cell } else { dataCellsFont = [[[curColumns objectAtIndex:i] dataCell] font]; attributes = [NSDictionary dictionaryWithObject:NSFontAttributeName forKey:[dataCellsFont fontName]]; cellsSize = [[[curColumns objectAtIndex:i] headerCell] cellSize]; cellWidthFitsAll = cellsSize.width; /* ... dataCell] cellSize] recalculates size based on cell with value in last row: cellsSize = [[[curColumns objectAtIndex:2] dataCell] cellSize]; Also scanning all rows does not result in a "max-width": so FOR... */ for(j=0;j<[query rowCount];j++){ cellsSize =[[query valueAtRow:j Column:i] sizeWithAttributes:attributes]; if ( cellsSize.width > cellWidthFitsAll ) cellWidthFitsAll = cellsSize.width; } [[curColumns objectAtIndex:i] setWidth: cellWidthFitsAll+cellToCellSpacing.width+6]; // added 6 for a border } } /* or refer to a column with NSTableView's -(NSTableColumn)tableColumnWithIdentifier:(id)anObject */ } => PSQLController.m -(void)awakeFromNib added (to the end) [checkAutoSizeTableColumns setState:NSOnState]; // to make shure the UI reflects the variable's value To have it work the first time a query gets executed I had to set tableViw's dataSource to an empty dataSource on executing Connect for the first time: => PSQLController.h added - (void)setQueryToEmptyDataSource:(id)con; => PSQLController.m added -(void)setQueryToEmptyDataSource:(id)con { [query release]; query=[[PSQLQuery alloc]initWithEmptyData:con]; //query has a retain count of 1 if (query) { [tableView setDataSource:query]; [tableView reloadData]; } } => PSQLController.m added to -(void) doConnect after if([connection connect]) just before } else { if (![tableView dataSource]) { /* get an empty dataSource for the tableView to have the autoColumnWidht set correctly on executing the first query */ [self setQueryToEmptyDataSource:connection]; } => PSQLQuery.h added -(id)initWithEmptyData:(PSQLConnection *)con; => PSQLQuery.m added -(id)initWithEmptyData:(PSQLConnection *)con { if(self=[self init]) { //PGresult* PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status); ExecStatusType pgStatus = PGRES_EMPTY_QUERY; result=PQmakeEmptyPGresult([con conn],pgStatus); if(!result){ querySuccessful = NO; hasResults=NO; } } return self; } |