📄 todotable.cpp
字号:
for ( i = 0; i < int(cats.count()); i++ ) { if ( cats[i] == category ) { returnMe = task.match( r ); break; } } } return returnMe;}#endif 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(#ifndef QTOPIA_PHONE QPainter *p, const QRect &r#else QPainter *, const QRect &#endif ){#ifndef QTOPIA_PHONE 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 ); }#endif}static long n1 = -1;static long n2 = -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(); if (mLastRowShown != row) {#ifdef Q_OS_UNIX struct timeval t1, t2; gettimeofday(&t1, 0);#endif mLastTaskShown = mTasks->filteredItem(row);#ifdef Q_OS_UNIX gettimeofday(&t2, 0); if (n2 == -1) { n2 = t1.tv_usec; n1 = t2.tv_usec - t1.tv_usec; } else { //qDebug("one row, %ld, one get %ld, (prev %ld", t1.tv_usec - n2, t2.tv_usec - t1.tv_usec, n1 ); n2 = -1; n1 = -1; }#endif mLastRowShown = row; } PimTask task = mLastTaskShown; bool selected = FALSE; int fromRow = QMIN(selectionBeginRow, selectionEndRow); int toRow = QMAX(selectionBeginRow, selectionEndRow); if ( mSel != NoSelection && fromRow != -1 && toRow != -1 && fromRow <= row && toRow >= row && fromRow != toRow) // don't 'select' one item selected = TRUE;/* bool current = (row == currentRow() ); bool focusCell = (row == currentRow() && col == currentColumn());*/ int field = headerKeyFields[ col ]; QBrush backgrnd;#if defined(Q_WS_QWS) || defined(_WS_QWS_) if (row % 2 && style().extendedBrush(QStyle::AlternateBase).style() != NoBrush) backgrnd = style().extendedBrush(QStyle::AlternateBase); else#endif backgrnd = cg.brush( QColorGroup::Base ); if ( selected #ifdef QTOPIA_PHONE || currentRow() == row && (currentColumn() == col || ro)#endif /*&& !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(), backgrnd ); 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(), backgrnd ); p->setPen(cg.text()); }#ifndef QTOPIA_PHONE p->drawLine( 0, cr.height() - 1, cr.width() - 1, cr.height() - 1 ); p->drawLine( cr.width() - 1, 0, cr.width() - 1, cr.height() - 1 );#endif 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+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();}void TodoTable::priorityChanged(int){}void TodoTable::delayCancelEdit(){ QTimer::singleShot(0, this, SLOT(cancelEdit()));}void TodoTable::delaySetCellContentFromEditor(){ QTimer::singleShot(0, this, SLOT(setCellContentFromEditor()));}QWidget *TodoTable::createEditor(int , int , bool ) const{ return 0;}void TodoTable::cancelEdit(){#ifdef QTOPIA_PHONE setFocus(); d->clearModifiedTask(); le->hide();#endif}void TodoTable::setCellContentFromEditor(){ int res = le->currentItem() + 1; PimTask task = mTasks->filteredItem(currentRow()); if (task.priority() != res) { task.setPriority( (PimTask::PriorityValue) res); d->setModifiedTask( task.uid(), PimTask::Priority); emit updateTask( task ); } else { setFocus(); d->clearModifiedTask(); } le->hide(); /* Old way of working... doesn't seem to be a good idea anymore? setCellContentFromEditor( currentRow(), currentColumn() ); clearCellWidget( currentRow(), currentColumn() ); */}void TodoTable::setCellContentFromEditor(int row, int col){ QWidget *w = cellWidget(row,col); PimTask task = mTasks->filteredItem(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 = gQtopiaDesktopConfig->createQSettings(); selectedFields = settings->readListEntry("/todolist/fields" ); sizeList = settings->readListEntry("/todolist/colwidths" ); mSortColumn = settings->readNumEntry("/todolist/sortcolumn", 0); ascSort = settings->readBoolEntry("/todolist/ascsort", FALSE); gQtopiaDesktopConfig->deleteQSettings();#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 ); } // whether sort changes or not, readConfig is expected to reload the data. reload(); #ifndef QTOPIA_PHONE if ( mSortColumn > -1 ) {#ifndef Q_OS_WIN32 horizontalHeader()->setSortIndicator(mSortColumn,!ascSort);#else horizontalHeader()->setSortIndicator(mSortColumn, ascSort);#endif }#endif constructorDone = TRUE; fitHeadersToWidth(); refresh();}void TodoTable::saveSettings(){ // don't write settings, could break things. if (ro) return; 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 = gQtopiaDesktopConfig->createQSettings(); settings->writeEntry( "/todolist/fields", fieldList ); settings->writeEntry( "/todolist/colwidths", sizeList ); settings->writeEntry( "/todolist/sortcolumn", mSortColumn ); settings->writeEntry( "/todolist/ascsort", ascSort ); gQtopiaDesktopConfig->deleteQSettings();#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) { if ( *iit == PimTask::CompletedField ) header->setLabel( i++, Resource::loadPixmap("task-completed"), "" ); else if ( *iit == PimTask::Priority ) header->setLabel( i++, Resource::loadPixmap("task-priority"), "" ); else header->setLabel(i++, trFields[*iit] ); } 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 BoxSize + 6; case PimTask::Status: return 70; case PimTask::Description: return 157; case PimTask::Priority: { QFont fn = font(); QFontMetrics fm(fn); return fm.width(" 8 "); // no tr.. used for size to print number } case PimTask::PercentCompleted: return 45; case PimTask::StartedDate: return 100; case PimTask::CompletedDate: return 100; default: return 70; }}int TodoTable::minimumFieldSize(PimTask::TaskFields f){ return QMIN(defaultFieldSize(f), 45);}void TodoTable::headerClicked(int #ifndef QTOPIA_PHONE h#endif){#ifndef QTOPIA_PHONE 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();#endif}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"); }}#include "todotable.moc"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -