📄 dayview.cpp
字号:
int h1 = (hour > 0) ? hourInfo[hour - 1].hits : 0; return h1;}int TimeList::scale( const int h, const int widgetHeight ) const{ int rv; if ( height24 == 0 || height24 * time_height >= widgetHeight ) rv = h * time_height; else rv = h * widgetHeight / height24; return rv;}void TimeList::resize( int h, int start_of_day ){ if ( infoModified ) { TimeVals tmp = list; clear(); list = tmp; addDone(); } if ( height24 != 0 && height24 * time_height >= h ) return; infoModified = TRUE; int spotsNeeded = h / time_height - height24; int i = 0; // Add extra hours until the day is full int hour = start_of_day; for ( ; i < spotsNeeded; i++ ) { bool found = FALSE; while ( !found ) { if ( hour == 24 ) goto AFTER_ADD; if ( hour > 0 && hourInfo[hour].hits == hourInfo[hour - 1].hits ) { hit( hour ); hourInfo[hour - 1].empty = FALSE; found = TRUE; } hour++; } }AFTER_ADD: // Just in case there weren't enough hours to add... for ( ; i < spotsNeeded; i++ ) { hit( 23 ); } height24 = height(25);}QTime TimeList::time( int hour, int number ) const{ int counter = 0; for ( TimeVals::ConstIterator it = list.begin(); it != list.end(); it++ ) { const QTime &t = *it; if ( t.hour() == hour ) counter++; if ( counter == number ) return t; } return QTime( hour, 0 );}void TimeList::setTimeHeight( int th ){ time_height = th;}// ====================================================================CompressedDayLayout::CompressedDayLayout( int w, int h, const int &sod ) : DayViewLayout(w,h), start_of_day( sod ){}void CompressedDayLayout::clear(){ DayViewLayout::clear(); eventsChanged();}void CompressedDayLayout::eventsChanged(){ times.clear(); for ( int i = 0; i < (int)mItems.count(); i++ ) { LayoutItem *item = mItems.at( i ); times.add( referenceDate, item ); } times.addDone();}int CompressedDayLayout::timeToHeight( const QTime &time ) const{ int r1 = times.height( time ); int r2 = times.scale( r1, height ); return r2;}void CompressedDayLayout::setSize( int w, int h ){ times.resize( h, start_of_day ); LayoutManager::setSize( w, h );}void CompressedDayLayout::setTimeHeight( int th ){ DayViewLayout::setTimeHeight( th ); times.setTimeHeight( time_height );}// ====================================================================DayViewContents::DayViewContents( Type t, QWidget *parent, const char *name ) : QScrollView( parent, name, WRepaintNoErase ), typ(t), start_of_day(8), time_height(short_time_height), dataChanged( TRUE ), clicking( FALSE ){ setResizePolicy(Manual);#ifdef Q_WS_QWS QPEApplication::setStylusOperation( viewport(), QPEApplication::RightOnHold );#endif startSel = endSel = -1; dragging = FALSE;#ifdef QTOPIA_DESKTOP day_height += 2; // difference in font metrics?#endif setHScrollBarMode(AlwaysOff); newItemList(); itemList->setTimeHeight( time_height ); viewport()->setBackgroundColor( white ); visibleTimer = new QTimer( this ); connect( visibleTimer, SIGNAL(timeout()), this, SLOT(makeVisible()) ); TimeString::connectChange(this,SLOT(timeStringChanged()));}DayViewContents::~DayViewContents(){ clearItems(); delete itemList;}void DayViewContents::newItemList(){ if (typ == AllDay) { setMargins(0, 0, 0, 0); setBackgroundMode(PaletteDark); itemList = new AllDayLayout(contentsWidth(), contentsHeight()); } else if ( typ == CompressedDay ) { itemList = new CompressedDayLayout( contentsWidth() - time_width, contentsHeight(), start_of_day ); } else { itemList = new DayViewLayout(contentsWidth() - time_width, contentsHeight()); }}void DayViewContents::addOccurrence(Occurrence &ev, const QDate &cDate){ itemList->addOccurrence(ev, cDate); if (typ == AllDay) { resizeContents( itemList->count() > 4 ? width() - style().scrollBarExtent().width() : width(), day_height * ((itemList->count() + 1) / 2 )); itemList->setSize( contentsWidth(), contentsHeight() ); }}void DayViewContents::clearItems(){ itemList->clear(); itemList->setFocusedItem(0); dataChanged = TRUE;}void DayViewContents::layoutItems(){ itemList->layoutItems(TRUE); updateContents(0, 0, contentsWidth(), contentsHeight());}void DayViewContents::timeStringChanged(){ updateContents(0, 0, contentsWidth(), contentsHeight());}void DayViewContents::clearSelectedTimes(){ int oldSelMin = QMIN(startSel, endSel); int oldSelMax = QMAX(startSel, endSel); startSel = endSel = -1; repaintContents(0, posOfHour(oldSelMin - 1), time_width, posOfHour(oldSelMax + 1), FALSE);}void DayViewContents::startAtTime(int t){ start_of_day = t;}void DayViewContents::selectDate(const QDate &d){ itemList->setDate(d);}DayItem *DayViewContents::firstItem( bool show ){ DayItem *orig = itemList->focusedItem(); DayItem *dim = itemList->firstItem(); if (orig != dim && show) moveSelection( orig, dim ); return dim;}DayItem *DayViewContents::lastItem( bool show ){ DayItem *orig = itemList->focusedItem(); DayItem *dim = itemList->lastItem(); if (orig != dim && show ) moveSelection( orig, dim ); return dim;}DayItem *DayViewContents::nextItem( bool show ){ DayItem *orig = itemList->focusedItem(); DayItem *dim = itemList->nextItem(); if (orig != dim && show ) moveSelection( orig, dim ); return dim;}DayItem *DayViewContents::previousItem( bool show ){ DayItem *orig = itemList->focusedItem(); DayItem *dim = itemList->previousItem(); if (orig != dim && show ) moveSelection( orig, dim ); return dim;}DayItem *DayViewContents::currentItem() const{ return itemList->focusedItem();}void DayViewContents::setCurrentItem(DayItem *dim, bool show){ DayItem *orig = itemList->focusedItem(); if (dim != orig) itemList->setFocusedItem(dim); if (show) moveSelection( orig, dim );}void DayViewContents::moveSelection( DayItem *from, DayItem *to ){ if (to) { QRect r = expandRect(to->geometry()); visRect = to->geometry(); if (typ != AllDay) { r.moveBy(time_width, 0); visRect.moveBy(time_width, 0); } visibleTimer->start(5, TRUE); updateContents(r.x(),r.y(), r.width(), r.height()); } if (from && from != to) { QRect ogr = expandRect(from->geometry()); if (typ != AllDay) ogr.moveBy(time_width, 0); updateContents(ogr.x(),ogr.y(), ogr.width(), ogr.height()); }}void DayViewContents::makeVisible(){ if ( visRect.x() < 0 || visRect.y() < 0 || visRect.width() < 0 || visRect.height() < 0 ) return; int cx = visRect.x() + visRect.width()/2; int cy = visRect.y() + visRect.height()/2; ensureVisible( cx, cy, visRect.width()/2+5, visRect.height()/2+5 );}void DayViewContents::moveUp(){ scrollBy(0, -20);}void DayViewContents::moveDown(){ scrollBy(0, 20);}void DayViewContents::resizeEvent( QResizeEvent * ){ setMetrics( QFontMetrics( qApp->font() ) );}void DayView::fontChange( const QFont & ){ view->setMetrics( QFontMetrics(qApp->font() )); allView->setMetrics( QFontMetrics(qApp->font() ));}void DayViewContents::setMetrics( const QFontMetrics &fm ){ time_width = QFontMetrics(dayViewFont()).width( typ == CompressedDay ? TimeString::localHM(QTime(12,45),TimeString::Short) : TimeString::localH(12) )+day_time_margin*2; time_height = QMIN(fm.height() * 3, QMAX(fm.height(), height() / 10)); itemList->setTimeHeight( time_height ); day_height = fm.height() + 5;#ifdef QTOPIA_DESKTOP day_height += 2; // difference in font metrics?#endif // resize children to fit if that is our type. switch(typ) { case ScrollingDay: resizeContents( width() - style().scrollBarExtent().width(), posOfHour(24)); setContentsPos(0, posOfHour(start_of_day)); break; case CompressedDay: { if ( dataChanged ) dataChanged = FALSE; else { // If the data isn't changed, the vertical space available is // smaller than it was before. Remove any assumptions about the // vertical size to prevent making the day too long. itemList->setSize( width(), height() ); } bool needScrollBar = posOfHour(24) > height(); resizeContents( needScrollBar ? width() - style().scrollBarExtent().width() : width(), posOfHour(24) ); int hour = QTime::currentTime().hour(); setContentsPos(0, posOfHour(hour) ); } break; case AllDay: resizeContents( itemList->count() > 4 ? width() - style().scrollBarExtent().width() : width(), day_height * ((itemList->count() + 1) / 2 )); break; } itemList->setSize( contentsWidth() - (typ == AllDay ? 0 : time_width), contentsHeight()); itemList->layoutItems(); updateContents(0,0, contentsWidth(), contentsHeight());}void DayViewContents::setType( Type viewType ){ typ = viewType; QDate ref = itemList->date(); delete itemList; newItemList(); itemList->setDate( ref );}void DayViewContents::contentsMousePressEvent( QMouseEvent *e ){ dragging = TRUE; SortedLayoutItems list = itemList->items(); for (uint i = 0; i < list.size(); i++) { LayoutItem *itm = list.at(i); QRect geo = itm->geometry(); if (typ != AllDay) geo.moveBy(time_width, 0); if (geo.contains( e->pos() ) ) { Occurrence oc = itm->occurrence(); PimEvent ev = itm->event(); emit setCurrentEvent( ev ); if ( e->button() == RightButton ) { QPopupMenu m; m.insertItem( tr( "Edit" ), 1 ); m.insertItem( tr( "Delete" ), 2 );#ifdef Q_WS_QWS if (Ir::supported()) { m.insertItem( tr( "Beam" ), 3 ); }#endif int r = m.exec( e->globalPos() ); if ( r == 1 ) { emit editOccurrence( oc ); } else if ( r == 2 ) { emit removeOccurrence( oc ); } else if ( r == 3 ) { emit beamEvent( ev ); } } else { clicking = TRUE; mClickedItem = itm; } dragging = FALSE; break; } } if ( dragging ) { int h = hourAtPos(e->pos().y()); int oldSelMin = QMIN(startSel, endSel); int oldSelMax = QMAX(startSel, endSel); startSel = endSel = h; oldSelMin = oldSelMin == -1 ? startSel : QMIN(oldSelMin, startSel); oldSelMax = oldSelMax == -1 ? startSel : QMAX(oldSelMax, startSel); repaintContents(0, posOfHour(oldSelMin - 1), time_width, posOfHour(oldSelMax + 1), FALSE); }}void DayViewContents::contentsMouseMoveEvent( QMouseEvent *e ){ if ( dragging ) { int h = hourAtPos(e->pos().y()); if (h == endSel) return; int oldSelMin = QMIN(startSel, endSel); int oldSelMax = QMAX(startSel, endSel); endSel = h; oldSelMin = QMIN(oldSelMin, endSel); oldSelMax = QMAX(oldSelMax, endSel); repaintContents(0, posOfHour(oldSelMin - 1), time_width, posOfHour(oldSelMax + 1), FALSE); }}void DayViewContents::contentsMouseReleaseEvent( QMouseEvent *e ){ if ( dragging ) { dragging = FALSE; int h = hourAtPos(e->pos().y()); int oldSelMin = QMIN(startSel, endSel); int oldSelMax = QMAX(startSel, endSel); endSel = h; oldSelMin = QMIN(oldSelMin, endSel); oldSelMax = QMAX(oldSelMax, endSel); repaintContents(0, posOfHour(oldSelMin - 1), time_width, posOfHour(oldSelMax + 1), FALSE); // XXX should signal new event. emit keyPressed(QString::null); } if ( clicking ) { QRect geo = mClickedItem->geometry(); if (typ != AllDay) geo.moveBy(time_width, 0); if (!geo.contains( e->pos() ) ) clicking = FALSE; } if ( clicking && e->button() == LeftButton ) { emit showDetails(); } clicking = FALSE;}void DayViewContents::keyPressEvent( QKeyEvent *e ){ QString txt = e->text(); if ( !txt.isNull() && txt[0] > ' ' && e->key() < 0x1000 ) { // we this is some sort of thing we know about... e->accept(); emit keyPressed( txt ); } else { // I don't know what this key is, do you? e->ignore(); }}QFont DayViewContents::dayViewFont() const{#ifdef QTOPIA_PHONE return QFont(font().family(), QMIN(font().pointSize()-2,12));#else return font();#endif}void DayViewContents::drawContents( QPainter *p, int, int y, int, int h){ bool isClipped = p->hasClipping(); QRegion oldClip = p->clipRegion(); QRegion bgr = isClipped ? oldClip : QRect(0, 0, contentsWidth(), contentsHeight() ); QRect clipRect( 0, y, contentsWidth(), h ); bgr &= clipRect; // draw items first and clip away their geometries to reduce flicker. for (int k = 0; k < itemList->count(); k++) { DayItem *di = (DayItem*)itemList->items().at(k); QRect r = di->geometry(); r = expandRect( r ); r.moveBy( typ == AllDay ? 0 : time_width, 0 ); if ( r.intersects( clipRect ) ) { // A temporary hack that will be removed once the pseudo-preview mode is gone if ( typ == CompressedDay ) di->setCompressed(); di->drawItem(p, typ == AllDay ? 0 : time_width, 0, palette().active()); bgr -= r; } } bgr.translate( -contentsX(), -contentsY() ); p->setClipRegion( bgr ); //p->fillRect(time_width, x+w-time_width, y, h, palette().active().color(QColorGroup::Base)); p->fillRect(time_width, 0, contentsWidth() - time_width, contentsHeight(), white); int firstSel = QMIN(startSel, endSel); int lastSel = QMAX(startSel, endSel); // draw each hour. QFont f = dayViewFont(); QFontMetrics fm(f); int base = QMIN(time_height - fm.height(), 2); base = time_height - (base / 2) - (fm.height() - fm.ascent()); base -= 2; QColorGroup cgUp = palette().active(); cgUp.setColor(QColorGroup::Button, //cgUp.color(QColorGroup::Button).light(100)); palette().active().color(QColorGroup::Midlight)); QFont selectFont = f; selectFont.setBold(TRUE); // also of possible use, QColorGroup::BrightText.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -