⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 todotable.cpp

📁 Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3.7平台上编译为嵌入式图形界面操作系统。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    int w = width() - frameWidth();    if (contentsHeight() >= (height() - horizontalHeader()->height()) )	w -= (style().scrollBarExtent().width());        calcFieldSizes( 0, w );    refresh();}void TodoTable::calcFieldSizes(int oldSize, int size){//    qDebug("resize event called for todotable, which we will process");    constructorDone = FALSE; //don't let QTable mess up our logic    int col = headerKeyFields.count();        int max = 0;    int i;    for (i = 0; i < col; i++) {	max += columnWidth(i);    }    if ( oldSize < max )	oldSize = max;        int accumulated = 0;    for (i = 0; i < col; i++) {	float l = (float) columnWidth( i ) / (float) oldSize;	float l2 = l * size;	int newColLen = (int) l2;		int min = minimumFieldSize( (PimTask::TaskFields) headerKeyFields[i] );	if ( newColLen < min )	    newColLen =  min;	// make sure we fill out the space if there's some integer rounding leftover	if ( i == col - 1 && size - accumulated - 2 > min )	   newColLen = size - accumulated - 2;	else	    accumulated += newColLen;		setColumnWidth( i, newColLen );    }    constructorDone = TRUE;}QString TodoTable::categoryLabel( int id ){    // This is called seldom, so calling a load in here    // should be fine.    mCat.load( categoryFileName() );    if ( id == -1 )	return tr( "Unfiled" );    else if ( id == -2 )	return tr( "All" );    return mCat.label( "Todo List", id ); // No tr}void TodoTable::cornerButtonClicked(){    int row = pos( d->modifiedTask() );    if ( row > -1 ) {	int col = currentColumn();	setCurrentCell( row, col );	ensureCellVisible( row, col );    }}void TodoTable::refresh(){    if ( d->hasModifiedTask() ) {	// we might have an invalid uid at this point.  Check row within bounds	// just to be sure	if ( currentRow() < (int) mTasks->sortedTasks().count() && mTasks->sortedTasks().at(currentRow())->uid() == d->modifiedTask() ) {	    d->clearModifiedTask();	} else  {	    // Our modified task can have been filtered out	    int i = pos( d->modifiedTask() );	    if ( i == -1 )		d->clearModifiedTask();	}    }    setNumRows(mTasks->sortedTasks().count());}void TodoTable::slotDoFind( const QString &findString, int category ){    // we have to iterate through the table, this gives the illusion that    // sorting is actually being used.    static bool wrapAround = false;    if ( currFindString != findString ) {	currFindRow = -1;	wrapAround = false;    }    currFindString = findString;    if ( currFindRow < -1 )	currFindRow = currentRow() - 1;    clearSelection( TRUE );    QRegExp r( findString );    r.setCaseSensitive( FALSE );    int rows = numRows();    int row;    for ( row = currFindRow + 1; row < rows; row++ ) {	if ( taskCompare( *(mTasks->sortedTasks().at(row)), r, category) )	    break;    }    if ( row >= rows ) {	currFindRow = -1;	if ( wrapAround )	    emit findWrapAround();	else	    emit findNotFound();    } else {	currFindRow = row;	setCurrentCell( currFindRow, currentColumn() );	// we should always be able to wrap around and find this again,	// so don't give confusing not found message...	emit findFound();	wrapAround = true;    }}static bool taskCompare( const PimTask &task, const QRegExp &r, int category ){    bool returnMe;    QArray<int> cats;    cats = task.categories();    returnMe = false;    if ( (category == -1 && cats.count() == 0) || category == -2 )	returnMe = task.match( r );    else {	int i;	for ( i = 0; i < int(cats.count()); i++ ) {	    if ( cats[i] == category ) {		returnMe = task.match( r );		break;	    }	}    }    return returnMe;}int TodoTable::rowHeight( int ) const{    return RowHeight;}int TodoTable::rowPos( int row ) const{    return RowHeight*row;}int TodoTable::rowAt( int pos ) const{    return QMIN( pos/RowHeight, numRows()-1 );}void TodoTable::paintFocus(QPainter *p, const QRect &r){    if ( !constructorDone )	return;    QRect fr(0, 0, r.width(), r.height() );    if ( mSel == NoSelection ) {	QTable::paintFocus(p, r);    } else {	p->setPen( QPen( black, 1) );	p->setBrush( NoBrush );	p->drawRect( fr.x(), fr.y(), fr.width()-1, fr.height()-1 );    }}void TodoTable::paintCell(QPainter *p, int row, int col,	const QRect &cr, bool){    if ( !constructorDone )	return;#if defined(Q_WS_WIN)    const QColorGroup &cg = ( style().styleHint( QStyle::SH_ItemView_ChangeHighlightOnFocus ) ? palette().inactive() : colorGroup() );#else    const QColorGroup &cg = colorGroup();#endif    p->save();    PimTask task(*(mTasks->sortedTasks().at(row)));        bool selected = FALSE;    if ( mSel != NoSelection  && mSelected.find(task.uid()) != mSelected.end() )	selected = TRUE;/*        bool current = (row == currentRow() );    bool focusCell = (row == currentRow() && col == currentColumn());*/        int field = headerKeyFields[ col ];        if ( selected /*&& !focusCell */ ) {	p->fillRect( 0, 0, cr.width(), cr.height(), cg.brush( QColorGroup::Highlight ) );	p->setPen(cg.highlightedText());    } else if ( d->hasModifiedTask() && task.uid() == d->modifiedTask() ) {	if ( field == d->editedTaskField() ) {	    p->fillRect( 0, 0, cr.width(), cr.height(), cg.brush( QColorGroup::Base ) );	    p->setPen(cg.text() );	} else {	    p->fillRect( 0, 0, cr.width(), cr.height(), cg.brush( QColorGroup::Mid ) );	    p->setPen(cg.light() );	}    } else {	p->fillRect( 0, 0, cr.width(), cr.height(), cg.brush( QColorGroup::Base ) );	p->setPen(cg.text());    }    p->drawLine( 0, cr.height() - 1, cr.width() - 1, cr.height() - 1 );    p->drawLine( cr.width() - 1, 0, cr.width() - 1, cr.height() - 1 );    QFont f = p->font();    QFontMetrics fm(f);    switch(field) {	case PimTask::CompletedField:	    {		//qDebug("BoxSize, Rowheight, %d, %d", BoxSize, RowHeight);		// completed field		int marg = ( cr.width() - BoxSize ) / 2;		int x = 0;		int y = ( cr.height() - BoxSize ) / 2;//		if ( !selected ) {//		    p->setPen( QPen( cg.highlightedText() ) );//		} else {		    p->setPen( QPen( cg.text() ) );//		}				p->drawRect( x + marg, y, BoxSize, BoxSize );		p->drawRect( x + marg+1, y+1, BoxSize-2, BoxSize-2 );		p->fillRect( x + marg+2, y+2, BoxSize-4, BoxSize-4, cg.brush( QColorGroup::Base ) );				p->setPen( darkGreen );		x += 1;		y += 1;		if ( task.isCompleted() ) {		    int i, xx, yy;		    int sseg = BoxSize / 4;		    int lseg = BoxSize / 2;		    sseg -=2; // to fit in BoxSize.		    lseg -=1;		    xx = x+sseg+marg;		    yy = y + lseg;		    QPointArray a( 6*2 );		    // tripple thickens line.		    for (i=0; i < 3; i++) {			a.setPoint(4*i, xx, yy);			a.setPoint(4*i+1, xx+sseg, yy+sseg);			a.setPoint(4*i+2, xx+sseg, yy+sseg);			a.setPoint(4*i+3, xx+sseg+lseg, yy+sseg-lseg);			yy++;		    }		    p->drawLineSegments( a );		}	    }	    break;	case PimTask::Priority:	    // priority field	    {		QString text = QString::number(task.priority());		p->drawText(2,2 + fm.ascent(), text);	    }	    break;	case PimTask::Description:	    // description field	    {		p->drawText(2,2 + fm.ascent(), task.description() );	    }	    break;	case PimTask::Notes:		//must remove any crlf from the text		p->drawText(2,2 + fm.ascent(), task.notes().simplifyWhiteSpace() ); 		break;	case PimTask::StartedDate:	    {		if ( task.hasStartedDate() )		    p->drawText(2,2 + fm.ascent(), TimeString::localYMD( task.startedDate() ) );		else		    p->drawText(2,2 + fm.ascent(), tr("Not started") );	    }	    break;	case PimTask::CompletedDate:	    {		if ( task.isCompleted() )		    p->drawText(2,2 + fm.ascent(), TimeString::localYMD( task.completedDate() ) );		else		    p->drawText(2,2 + fm.ascent(), tr("Unfinished") );	    }	    break;	case PimTask::PercentCompleted:	    {		p->drawText(2,2 + fm.ascent(), QString::number( task.percentCompleted() ) + "%" );	    }	    break;	case PimTask::Status:	{		p->drawText(2,2 + fm.ascent(), statusToText( task.status() ));	}	break;	/*	case PimTask:::	    {		QString text;		if (task.hasDueDate()) {		    text = "HAS";		} else {		    text = tr("None");		}		p->drawText(2,2 + fm.ascent(), text);	    }	    break;	*/    }    p->restore();}/*  Need to store changes in priority as the user selects them.  Otherwise they    might be lost in a closeevent*/void TodoTable::priorityChanged(int){    QTimer::singleShot(0, this, SLOT(setCellContentFromEditor()));}QWidget *TodoTable::createEditor(int row, int col, bool ) const{    int field = headerKeyFields[ col ];    switch (field) {	case PimTask::Priority:	    {		QComboBox *cb = new QComboBox( viewport() );		cb->insertItem( "1" );		cb->insertItem( "2" );		cb->insertItem( "3" );		cb->insertItem( "4" );		cb->insertItem( "5" );		cb->setCurrentItem( mTasks->sortedTasks().at(row)->priority() - 1 );		connect( cb, SIGNAL(activated(int)), this, SLOT(priorityChanged(int)) );		return cb;	    }	default:	    return 0;    }}void TodoTable::setCellContentFromEditor(){    setCellContentFromEditor( currentRow(), currentColumn() );    clearCellWidget( currentRow(), currentColumn() );}void TodoTable::setCellContentFromEditor(int row, int col){    QWidget *w = cellWidget(row,col);    PimTask task(*(mTasks->sortedTasks().at(row)));    if (w->inherits("QComboBox") ) {	int res = ((QComboBox *)w)->currentItem() + 1;	if (task.priority() != res) {	    int i = ((QComboBox *)w)->currentItem() + 1;	    task.setPriority( (PimTask::PriorityValue) i);	    d->setModifiedTask( task.uid(), PimTask::Priority);	    emit updateTask( task );	    //refresh();	} else {	    setFocus();	    d->clearModifiedTask();	}    }}void TodoTable::clearCellWidget(int row, int col){    QTable::clearCellWidget(row, col);}void TodoTable::readSettings(){    QStringList selectedFields, sizeList;    {#ifdef QTOPIA_DESKTOP	QSettings settings;	settings.insertSearchPath( QSettings::Unix, applicationPath );	settings.insertSearchPath( QSettings::Windows, "/Trolltech" );	selectedFields = settings.readListEntry("/palmtopcenter/todolist/fields" );	sizeList = settings.readListEntry("/palmtopcenter/todolist/colwidths" );	mSortColumn = settings.readNumEntry("/palmtopcenter/todolist/sortcolumn", 0);	ascSort = settings.readBoolEntry("/palmtopcenter/todolist/ascsort", FALSE);#else	Config config( "todo" );	config.setGroup( "View" );	selectedFields = config.readListEntry("fields", ',');	sizeList = config.readListEntry("colwidths",',');	mSortColumn = config.readNumEntry("sortcolumn", 0 );	ascSort = config.readBoolEntry("ascsort", 0 );#endif    }    if ( !selectedFields.count() ) {	setFields( defaultFields() );    } else {	QMap<QCString, int> identifierToKey = PimTask::identifierToKeyMap();	for ( QStringList::Iterator it = selectedFields.begin(); it != selectedFields.end(); ++it) {	    int field = identifierToKey[ (*it).data() ];	    headerKeyFields.append( field );	}	setFields( headerKeyFields, sizeList );    }    if ( mSortColumn > -1 ) {	reload(); #ifndef Q_OS_WIN32	horizontalHeader()->setSortIndicator(mSortColumn,!ascSort);#else	horizontalHeader()->setSortIndicator(mSortColumn, ascSort);#endif    }    constructorDone = TRUE;    fitHeadersToWidth();    refresh();}void TodoTable::saveSettings(){    QMap<int,QCString> keyToIdentifier = PimTask::keyToIdentifierMap();    QHeader *header = horizontalHeader();    QStringList fieldList, sizeList;    for ( int i = 0; i < header->count(); i++) {	fieldList.append( keyToIdentifier[ headerKeyFields[i]  ]  );	sizeList.append( QString::number(header->sectionSize(i)) );    }#ifdef QTOPIA_DESKTOP    QSettings settings;    settings.insertSearchPath( QSettings::Unix, applicationPath );    settings.insertSearchPath( QSettings::Windows, "/Trolltech" );    settings.writeEntry( "/palmtopcenter/todolist/fields", fieldList );    settings.writeEntry( "/palmtopcenter/todolist/colwidths", sizeList );    settings.writeEntry( "/palmtopcenter/todolist/sortcolumn", mSortColumn );    settings.writeEntry( "/palmtopcenter/todolist/ascsort", ascSort );#else    Config config( "todo" );    config.setGroup( "View" );    config.writeEntry("fields", fieldList, ',' );    config.writeEntry("colwidths", sizeList, ',' );    config.writeEntry("sortcolumn", mSortColumn );    config.writeEntry("ascsort", ascSort );#endif}void TodoTable::setFields(QValueList<int> f){    QHeader *header = horizontalHeader();    QStringList sizes;    if ( f.count() == 0 ) {	f = defaultFields();	headerKeyFields.clear();    }    // map old sizes to new pos in header list    for ( int i = 0; i < (int) f.count(); i++) {	int pos = headerKeyFields.findIndex( *f.at(i) );	//qDebug(" f key %d found at %d", *f.at(i), pos );	if ( pos > -1 && pos < header->count() )	    sizes.append( QString::number( header->sectionSize(pos) ) );	else	    sizes.append( QString::number( defaultFieldSize( (PimTask::TaskFields) *f.at(i) ) ));    }        setFields(f, sizes);    if ( isVisible() )	fitHeadersToWidth();}void TodoTable::setFields(QValueList<int> f, QStringList sizes){    QHeader *header = horizontalHeader();    int prevSortKey = -1;    if ( mSortColumn > -1 && headerKeyFields.count() )	prevSortKey = headerKeyFields[mSortColumn];    headerKeyFields = f;    while ( header->count() )	header->removeLabel( header->count() - 1 );        // We have to create the internal list before calling QTable::setNumCols as    // setnNumCols forces a repaint    QValueList<int>::ConstIterator iit;    int i = 0;    for (iit = f.begin(); iit != f.end(); ++iit) {	if ( *iit == prevSortKey ) {	    mSortColumn = i;	    break;	}	i++;    }    setNumCols( f.count() );        QMap<int, QString> trFields = PimTask::trFieldsMap();    i = 0;    for (iit = f.begin(); iit != f.end(); ++iit) {#ifndef QTOPIA_DESKTOP	if ( *iit == PimTask::CompletedField )	    header->setLabel( i++, Resource::loadPixmap("task-completed"), "" );	else	    header->setLabel(i++, trFields[*iit] );#else	header->setLabel(i++, trFields[*iit] );#endif    }    i = 0;    for (QStringList::ConstIterator it = sizes.begin(); it != sizes.end(); ++it) {	if ( i < (int) f.count() )	    setColumnWidth(i, (*it).toInt() );		i++;    }    horizontalHeader()->show();}QValueList<int> TodoTable::fields(){    return headerKeyFields;}QValueList<int> TodoTable::defaultFields(){    QValueList<int> l;    l.append( PimTask::CompletedField );    l.append( PimTask::Priority );    l.append( PimTask::Description );    return l;}int TodoTable::defaultFieldSize(PimTask::TaskFields f){    switch( f ) {	case PimTask::CompletedField: return 31;	case PimTask::Status: return 70;	case PimTask::Description: return 157;	case PimTask::Priority: return 52;	case PimTask::PercentCompleted: return 45;	case PimTask::StartedDate: return 100;	case PimTask::CompletedDate: return 100;	default: return 70;    }}int TodoTable::minimumFieldSize(PimTask::TaskFields f){    switch( f ) {	case PimTask::CompletedField: return 31;	default: return 40;    }}void TodoTable::headerClicked(int h){    if ( h != mSortColumn ) {	mSortColumn = h;	ascSort = FALSE;    } else	ascSort = !ascSort;#ifndef Q_OS_WIN32    horizontalHeader()->setSortIndicator(mSortColumn,!ascSort);#else    horizontalHeader()->setSortIndicator(mSortColumn, ascSort);#endif    reload();}QString TodoTable::statusToText(PimTask::TaskStatus s){    switch( s ) {	default: return tr("Not Started");	case PimTask::InProgress: return tr("In Progress");	case PimTask::Completed: return tr("Completed");	case PimTask::Waiting: return tr("Waiting");	case PimTask::Deferred:  return tr("Deferred");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -